bill-server-go/internal/bill/handler.go

147 lines
3.6 KiB
Go
Raw Normal View History

2023-05-11 21:00:29 +08:00
package bill
import (
"context"
"github.com/gofiber/fiber/v2"
)
2023-06-12 20:17:08 +08:00
// TODO: 日期传输是使用时间戳,存储时分开年月日
2023-05-11 21:00:29 +08:00
type BillHandler struct {
billService BillService
}
func NewBillHandler(billRoute fiber.Router, bs BillService) {
handler := &BillHandler{
billService: bs,
}
2023-06-12 19:57:57 +08:00
billRoute.Get("/id/:billID", handler.getBillById)
2023-06-10 17:31:16 +08:00
billRoute.Get("/:year?/:month?/:day?", handler.checkGetBillsParamsMiddleware, handler.getBills)
2023-06-12 19:57:57 +08:00
billRoute.Post("", handler.createBill)
billRoute.Put("/:billID", handler.checkIfBillExistsMiddleware, handler.updateBill)
billRoute.Delete("/:billID", handler.checkIfBillExistsMiddleware, handler.deleteBill)
}
func (h *BillHandler) getBillById(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
targetBillId, err := c.ParamsInt("billID")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"status": "fail",
"message": "Invalid bill ID",
})
}
bill, err := h.billService.GetBillByID(customContext, targetBillId)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
"status": "success",
"data": bill,
})
2023-05-11 21:00:29 +08:00
}
func (h *BillHandler) getBills(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
2023-06-10 16:53:19 +08:00
bDate := c.Locals("bDate").(BDate)
bills, err := h.billService.GetBills(customContext, bDate)
2023-05-11 21:00:29 +08:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
2023-06-12 19:57:57 +08:00
2023-05-11 21:00:29 +08:00
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
"status": "success",
"data": bills,
})
}
func (h *BillHandler) createBill(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
2023-06-10 17:31:16 +08:00
bill := &Bill{}
if err := c.BodyParser(bill); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
err := h.billService.CreateBill(customContext, bill)
2023-05-11 21:00:29 +08:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
2023-06-10 17:31:16 +08:00
2023-05-11 21:00:29 +08:00
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
2023-06-10 17:31:16 +08:00
"status": "success",
"message": "Bill has been created successfully!",
2023-05-11 21:00:29 +08:00
})
}
func (h *BillHandler) updateBill(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
2023-06-12 19:57:57 +08:00
targetBillId := c.Locals("billID").(int)
bill := &Bill{}
if err := c.BodyParser(bill); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
bill.ID = uint(targetBillId)
err := h.billService.UpdateBill(customContext, bill)
2023-05-11 21:00:29 +08:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
2023-06-12 19:57:57 +08:00
2023-05-11 21:00:29 +08:00
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
2023-06-12 19:57:57 +08:00
"status": "success",
"message": "Bill has been updated successfully!",
2023-05-11 21:00:29 +08:00
})
}
func (h *BillHandler) deleteBill(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
2023-06-12 19:57:57 +08:00
targetBillId := c.Locals("billID").(int)
err := h.billService.DeleteBill(customContext, targetBillId)
2023-05-11 21:00:29 +08:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
2023-06-12 19:57:57 +08:00
2023-05-11 21:00:29 +08:00
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
2023-06-12 19:57:57 +08:00
"status": "success",
"message": "Bill has been deleted successfully!",
2023-05-11 21:00:29 +08:00
})
}