feat: finish bill api

This commit is contained in:
clz 2023-06-12 19:57:57 +08:00
parent 2aec86a6da
commit c777315687
2 changed files with 85 additions and 9 deletions

View File

@ -14,10 +14,37 @@ func NewBillHandler(billRoute fiber.Router, bs BillService) {
billService: bs,
}
billRoute.Get("/id/:billID", handler.getBillById)
billRoute.Get("/:year?/:month?/:day?", handler.checkGetBillsParamsMiddleware, handler.getBills)
billRoute.Post("/", handler.createBill)
billRoute.Put("/", handler.updateBill)
billRoute.Delete("/", handler.deleteBill)
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,
})
}
func (h *BillHandler) getBills(c *fiber.Ctx) error {
@ -33,6 +60,7 @@ func (h *BillHandler) getBills(c *fiber.Ctx) error {
"message": err.Error(),
})
}
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
"status": "success",
"data": bills,
@ -69,31 +97,48 @@ func (h *BillHandler) createBill(c *fiber.Ctx) error {
func (h *BillHandler) updateBill(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
// TODO(): update data
err := h.billService.UpdateBill(customContext, &Bill{})
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)
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",
"status": "success",
"message": "Bill has been updated successfully!",
})
}
func (h *BillHandler) deleteBill(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
// TODO(): delete data id
err := h.billService.DeleteBill(customContext, 0)
targetBillId := c.Locals("billID").(int)
err := h.billService.DeleteBill(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",
"status": "success",
"message": "Bill has been deleted successfully!",
})
}

View File

@ -1,6 +1,7 @@
package bill
import (
"context"
"github.com/gofiber/fiber/v2"
"gopkg.in/go-playground/validator.v9"
)
@ -24,3 +25,33 @@ func (h *BillHandler) checkGetBillsParamsMiddleware(c *fiber.Ctx) error {
c.Locals("bDate", date)
return c.Next()
}
func (h *BillHandler) checkIfBillExistsMiddleware(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
targetLabelID, err := c.ParamsInt("billID")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"status": "fail",
"message": "Please specify a valid bill ID!",
})
}
searchLabel, err := h.billService.GetBillByID(customContext, targetLabelID)
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"status": "fail",
"message": err.Error(),
})
}
if searchLabel == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"status": "fail",
"message": "These is no bill with this ID!",
})
}
c.Locals("billID", targetLabelID)
return c.Next()
}