|
| 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 | +} |
0 commit comments