Skip to content

Commit 3a4141a

Browse files
committed
add routes and handlers for payroll
1 parent 5078a11 commit 3a4141a

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

cmd/api/payroll.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"log/slog"
5+
"net/http"
6+
"strconv"
7+
8+
db "github.com/Hopertz/Hr-Be/internal/db/sqlc"
9+
"github.com/google/uuid"
10+
"github.com/labstack/echo/v4"
11+
)
12+
13+
func (app *application) createPayrollHandler(c echo.Context) error {
14+
15+
var input struct {
16+
EmployeeID uuid.UUID `json:"employee_id" validate:"required"`
17+
BasicSalary float64 `json:"basic_salary" validate:"required,gt=0"`
18+
TIN string `json:"tin" validate:"required"`
19+
BankName string `json:"bank_name" validate:"required"`
20+
BankAccount string `json:"bank_account" validate:"required"`
21+
}
22+
23+
if err := c.Bind(&input); err != nil {
24+
return err
25+
}
26+
27+
if err := app.validator.Struct(input); err != nil {
28+
return c.JSON(http.StatusBadRequest, map[string]string{"error": err.Error()})
29+
}
30+
31+
rounded := float64(int(input.BasicSalary*100)) / 100
32+
33+
s := strconv.FormatFloat(rounded, 'f', 2, 64)
34+
35+
args := db.CreatePayrollParams{
36+
EmployeeID: input.EmployeeID,
37+
BasicSalary: s,
38+
Tin: input.TIN,
39+
BankName: input.BankName,
40+
BankAccount: input.BankAccount,
41+
}
42+
43+
err := app.store.CreatePayroll(c.Request().Context(), args)
44+
45+
if err != nil {
46+
slog.Error("Error inserting payroll ", "Error", err.Error())
47+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
48+
}
49+
50+
return c.JSON(http.StatusCreated, map[string]string{"success": "payroll created successful"})
51+
52+
}
53+
54+
func (app *application) getAllPayroll(c echo.Context) error {
55+
56+
payroll, err := app.store.GetAllPayroll(c.Request().Context())
57+
58+
if err != nil {
59+
slog.Error("Error getting all payroll ", "Error", err.Error())
60+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
61+
}
62+
63+
return c.JSON(http.StatusOK, payroll)
64+
65+
}
66+
67+
func (app *application) getPayroll(c echo.Context) error {
68+
69+
id := c.Param("payroll_id")
70+
71+
payroll_id, err := uuid.Parse(id)
72+
73+
if err != nil {
74+
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid uuid"})
75+
}
76+
77+
payroll, err := app.store.GetPayroll(c.Request().Context(), payroll_id)
78+
79+
if err != nil {
80+
slog.Error("Error getting payroll by id ", "Error", err.Error())
81+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
82+
}
83+
84+
return c.JSON(http.StatusOK, payroll)
85+
86+
}
87+
88+
func (app *application) DeletePayroll(c echo.Context) error {
89+
90+
id := c.Param("id")
91+
payroll_id, err := uuid.Parse(id)
92+
if err != nil {
93+
return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid uuid"})
94+
}
95+
96+
err = app.store.DeletePayroll(c.Request().Context(), payroll_id)
97+
if err != nil {
98+
slog.Error("Error deleting payroll ", "Error", err.Error())
99+
return c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
100+
}
101+
102+
return c.JSON(http.StatusOK, nil)
103+
104+
}

cmd/api/routes.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ func (app *application) routes() *echo.Echo {
6363
g.PUT("/announcements/:announcement_id", app.updateAnnouncementByIdHandler, app.requireAdminOrHr)
6464
g.DELETE("/announcements/:announcement_id", app.deleteAnnouncementHandler, app.requireAdminOrHr)
6565

66+
// payroll
67+
g.GET("/payroll", app.getAllPayroll, app.requireAdminOrHr)
68+
g.POST("/payroll", app.createPayrollHandler, app.requireAdminOrHr)
69+
g.GET("/payroll/:payroll_id", app.getPayroll, app.requireAdminOrHr)
70+
g.DELETE("/payroll", app.DeletePayroll, app.requireAdminOrHr)
6671

6772
// approvers
6873

0 commit comments

Comments
 (0)