feat: finish bill api
This commit is contained in:
parent
2aec86a6da
commit
c777315687
|
@ -14,10 +14,37 @@ func NewBillHandler(billRoute fiber.Router, bs BillService) {
|
||||||
billService: bs,
|
billService: bs,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
billRoute.Get("/id/:billID", handler.getBillById)
|
||||||
billRoute.Get("/:year?/:month?/:day?", handler.checkGetBillsParamsMiddleware, handler.getBills)
|
billRoute.Get("/:year?/:month?/:day?", handler.checkGetBillsParamsMiddleware, handler.getBills)
|
||||||
billRoute.Post("/", handler.createBill)
|
billRoute.Post("", handler.createBill)
|
||||||
billRoute.Put("/", handler.updateBill)
|
billRoute.Put("/:billID", handler.checkIfBillExistsMiddleware, handler.updateBill)
|
||||||
billRoute.Delete("/", handler.deleteBill)
|
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 {
|
func (h *BillHandler) getBills(c *fiber.Ctx) error {
|
||||||
|
@ -33,6 +60,7 @@ func (h *BillHandler) getBills(c *fiber.Ctx) error {
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
|
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"data": bills,
|
"data": bills,
|
||||||
|
@ -69,31 +97,48 @@ func (h *BillHandler) createBill(c *fiber.Ctx) error {
|
||||||
func (h *BillHandler) updateBill(c *fiber.Ctx) error {
|
func (h *BillHandler) updateBill(c *fiber.Ctx) error {
|
||||||
customContext, cancel := context.WithCancel(context.Background())
|
customContext, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
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 {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
|
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 {
|
func (h *BillHandler) deleteBill(c *fiber.Ctx) error {
|
||||||
customContext, cancel := context.WithCancel(context.Background())
|
customContext, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
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 {
|
if err != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(&fiber.Map{
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"message": err.Error(),
|
"message": err.Error(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
|
return c.Status(fiber.StatusOK).JSON(&fiber.Map{
|
||||||
"status": "success",
|
"status": "success",
|
||||||
|
"message": "Bill has been deleted successfully!",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package bill
|
package bill
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"gopkg.in/go-playground/validator.v9"
|
"gopkg.in/go-playground/validator.v9"
|
||||||
)
|
)
|
||||||
|
@ -24,3 +25,33 @@ func (h *BillHandler) checkGetBillsParamsMiddleware(c *fiber.Ctx) error {
|
||||||
c.Locals("bDate", date)
|
c.Locals("bDate", date)
|
||||||
return c.Next()
|
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()
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user