Skip to content

Commit 426b103

Browse files
committed
add route & handler for payroll update
1 parent 1f7d26c commit 426b103

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

cmd/api/payroll.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,70 @@ func (app *application) DeletePayroll(c echo.Context) error {
272272
return c.JSON(http.StatusOK, nil)
273273

274274
}
275+
276+
func (app *application) updatePayrollHandler(c echo.Context) error {
277+
278+
var input struct {
279+
BasicSalary *float64 `json:"basic_salary"`
280+
TIN *string `json:"tin"`
281+
BankName *string `json:"bank_name"`
282+
BankAccount *string `json:"bank_account"`
283+
IsActive *bool `json:"is_active"`
284+
}
285+
286+
if err := c.Bind(&input); err != nil {
287+
return err
288+
}
289+
290+
if err := app.validator.Struct(input); err != nil {
291+
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
292+
}
293+
294+
e, err := app.store.JustGetPayroll(c.Request().Context())
295+
296+
if err != nil {
297+
slog.Error("Error Getting employee for update ", "Error", err.Error())
298+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
299+
}
300+
301+
if input.BasicSalary != nil {
302+
rounded := float64(int(*input.BasicSalary*100)) / 100
303+
bs := strconv.FormatFloat(rounded, 'f', 2, 64)
304+
e.BasicSalary = bs
305+
}
306+
307+
if input.BankAccount != nil {
308+
e.BankAccount = *input.BankAccount
309+
}
310+
311+
if input.BankName != nil {
312+
e.BankName = *input.BankName
313+
}
314+
315+
if input.TIN != nil {
316+
e.Tin = *input.TIN
317+
}
318+
319+
if input.IsActive != nil {
320+
e.IsActive = *input.IsActive
321+
}
322+
323+
args := db.UpdatePayrollParams{
324+
ID: e.ID,
325+
BasicSalary: e.BasicSalary,
326+
Tin: e.Tin,
327+
BankName: e.BankName,
328+
BankAccount: e.BankAccount,
329+
IsActive: e.IsActive,
330+
}
331+
332+
err = app.store.UpdatePayroll(c.Request().Context(), args)
333+
334+
if err != nil {
335+
slog.Error("Error updating payroll ", "Error", err.Error())
336+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
337+
}
338+
339+
return c.JSON(http.StatusOK, map[string]string{"success": "payroll update successful"})
340+
341+
}

cmd/api/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (app *application) routes() *echo.Echo {
6666
// payroll
6767
g.GET("/payroll", app.getAllPayroll, app.requireAdminOrHr)
6868
g.POST("/payroll", app.createPayrollHandler, app.requireAdminOrHr)
69+
g.PUT("/payroll", app.updatePayrollHandler, app.requireAdminOrHr)
6970
g.GET("/payroll/:payroll_id", app.getPayroll, app.requireAdminOrHr)
7071
g.DELETE("/payroll", app.DeletePayroll, app.requireAdminOrHr)
7172

0 commit comments

Comments
 (0)