all tests works...
This commit is contained in:
@@ -108,19 +108,51 @@ func (r *BudgetRepo) Create(ctx context.Context, req model.CreateBudgetRequest)
|
||||
}
|
||||
|
||||
func (r *BudgetRepo) Update(ctx context.Context, req model.UpdateBudgetRequest) (*model.Budget, error) {
|
||||
_, err := r.db.ExecContext(ctx, `
|
||||
UPDATE budgets
|
||||
SET version=?, amount=?, notes=?,
|
||||
updated_at=strftime('%Y-%m-%dT%H:%M:%SZ','now')
|
||||
WHERE id=?`,
|
||||
req.Version, req.Amount, req.Notes, req.ID,
|
||||
)
|
||||
if err != nil {
|
||||
q := `UPDATE budgets SET updated_at = strftime('%Y-%m-%dT%H:%M:%SZ','now')`
|
||||
args := []any{}
|
||||
|
||||
if req.Amount != nil {
|
||||
q += `, amount = ?`
|
||||
args = append(args, *req.Amount)
|
||||
}
|
||||
if req.Notes != nil {
|
||||
q += `, notes = ?`
|
||||
args = append(args, *req.Notes)
|
||||
}
|
||||
if req.Version != nil {
|
||||
q += `, version = ?`
|
||||
args = append(args, *req.Version)
|
||||
}
|
||||
if req.Currency != nil {
|
||||
q += `, currency = ?`
|
||||
args = append(args, *req.Currency)
|
||||
}
|
||||
if req.FiscalYear != nil {
|
||||
q += `, fiscal_year = ?`
|
||||
args = append(args, *req.FiscalYear)
|
||||
}
|
||||
if req.FiscalPeriod != nil {
|
||||
q += `, fiscal_period = ?`
|
||||
args = append(args, *req.FiscalPeriod)
|
||||
}
|
||||
if req.DepartmentID != nil {
|
||||
q += `, department_id = ?`
|
||||
args = append(args, *req.DepartmentID)
|
||||
}
|
||||
if req.GLAccountID != nil {
|
||||
q += `, gl_account_id = ?`
|
||||
args = append(args, *req.GLAccountID)
|
||||
}
|
||||
|
||||
q += ` WHERE id = ?`
|
||||
args = append(args, req.ID)
|
||||
|
||||
if _, err := r.db.ExecContext(ctx, q, args...); err != nil {
|
||||
return nil, fmt.Errorf("update budget: %w", err)
|
||||
}
|
||||
|
||||
row := r.db.QueryRowContext(ctx,
|
||||
`SELECT`+budgetSelectCols+`FROM budgets WHERE id = ?`, req.ID)
|
||||
`SELECT `+budgetSelectCols+` FROM budgets WHERE id = ?`, req.ID)
|
||||
b, err := scanBudget(row)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch updated budget: %w", err)
|
||||
|
||||
@@ -123,8 +123,8 @@ func (r *ReferenceRepo) ListDepartments(ctx context.Context) ([]model.Department
|
||||
return depts, rows.Err()
|
||||
}
|
||||
|
||||
func (r *ReferenceRepo) DeleteDepartment(ctx context.Context, id int) error {
|
||||
_, err := r.db.ExecContext(ctx, `DELETE FROM departments WHERE id = ?`, id)
|
||||
func (r *ReferenceRepo) DeleteDepartment(ctx context.Context, req model.DeleteDepartmentRequest) error {
|
||||
_, err := r.db.ExecContext(ctx, `DELETE FROM departments WHERE id = ?`, req.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ func (r *ReferenceRepo) getGLAccountByCode(ctx context.Context, code string) (*m
|
||||
func (r *ReferenceRepo) ListGLAccounts(ctx context.Context) ([]model.GLAccount, error) {
|
||||
rows, err := r.db.QueryContext(ctx,
|
||||
`SELECT id, code, description, type, favour_high, active
|
||||
FROM gl_accounts ORDER BY code`)
|
||||
FROM gl_accounts ORDER BY code`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -179,10 +179,12 @@ func (r *ReferenceRepo) ListGLAccounts(ctx context.Context) ([]model.GLAccount,
|
||||
var accts []model.GLAccount
|
||||
for rows.Next() {
|
||||
var a model.GLAccount
|
||||
var glType string
|
||||
var favourHigh, active int
|
||||
if err := rows.Scan(&a.ID, &a.Code, &a.Description, &a.Type, &favourHigh, &active); err != nil {
|
||||
if err := rows.Scan(&a.ID, &a.Code, &a.Description, &glType, &favourHigh, &active); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.Type = glType
|
||||
a.FavourHigh = favourHigh == 1
|
||||
a.Active = active == 1
|
||||
accts = append(accts, a)
|
||||
|
||||
@@ -158,20 +158,16 @@ func (h *ReferenceHandler) SetActivityDepartment(w http.ResponseWriter, r *http.
|
||||
|
||||
// DELETE /api/v1/department/delete
|
||||
func (h *ReferenceHandler) DeleteDepartment(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
var req model.DeleteDepartmentRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("invalid body: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
id, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
if req.ID == 0 {
|
||||
writeError(w, http.StatusBadRequest, "id is required")
|
||||
return
|
||||
}
|
||||
if err := h.repo.DeleteDepartment(r.Context(), id); err != nil {
|
||||
if err := h.repo.DeleteDepartment(r.Context(), req); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Sprintf("delete department: %v", err))
|
||||
return
|
||||
}
|
||||
@@ -227,14 +223,18 @@ func (h *ReferenceHandler) ListGLAccounts(w http.ResponseWriter, r *http.Request
|
||||
writeJSON(w, http.StatusOK, accts)
|
||||
}
|
||||
|
||||
// DELETE /api/v1/gl-accounts/{id}
|
||||
// DELETE /api/v1/gl-accounts/delete
|
||||
func (h *ReferenceHandler) DeleteGLAccount(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := pathID(r, "id")
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid id")
|
||||
var req model.DeleteGLAccountRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, fmt.Sprintf("invalid body: %v", err))
|
||||
return
|
||||
}
|
||||
if err := h.repo.DeleteGLAccount(r.Context(), id); err != nil {
|
||||
if req.ID == 0 {
|
||||
writeError(w, http.StatusBadRequest, "id is required")
|
||||
return
|
||||
}
|
||||
if err := h.repo.DeleteGLAccount(r.Context(), req.ID); err != nil {
|
||||
writeError(w, http.StatusInternalServerError, fmt.Sprintf("delete gl_account: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -49,6 +49,14 @@ type SetDepartmentActivity struct {
|
||||
CostCenter *string `json:"cost_center"`
|
||||
}
|
||||
|
||||
type DeleteDepartmentRequest struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
type DeleteGLAccountRequest struct {
|
||||
ID int `json:"id"`
|
||||
}
|
||||
|
||||
type GLAccount struct {
|
||||
ID int `json:"id"`
|
||||
Code string `json:"code"`
|
||||
|
||||
Reference in New Issue
Block a user