From 7862701a26c9b21a24338d47bf6336226e599448 Mon Sep 17 00:00:00 2001 From: clz Date: Thu, 11 May 2023 21:00:29 +0800 Subject: [PATCH] to: bill api --- internal/bill/domain.go | 10 ++-- internal/bill/handle.go | 87 ++++++++++++++++++++++++++++++++ internal/bill/repository.go | 2 +- internal/bill/service.go | 10 ++-- internal/infrastructure/fiber.go | 21 ++++---- 5 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 internal/bill/handle.go diff --git a/internal/bill/domain.go b/internal/bill/domain.go index 4d702a2..9dbc858 100644 --- a/internal/bill/domain.go +++ b/internal/bill/domain.go @@ -31,9 +31,9 @@ type BillRepository interface { } type BillService interface { - FetchBills(ctx context.Context, date BDate) (*[]Bill, error) - FetchBillByID(ctx context.Context, id int) (*Bill, error) - BuildBill(ctx context.Context, bill *Bill) error - ModifyBill(ctx context.Context, bill *Bill) error - DestroyBill(ctx context.Context, id int) error + GetBills(ctx context.Context, date BDate) (*[]Bill, error) + GetBillByID(ctx context.Context, id int) (*Bill, error) + CreateBill(ctx context.Context, bill *Bill) error + UpdateBill(ctx context.Context, bill *Bill) error + DeleteBill(ctx context.Context, id int) error } diff --git a/internal/bill/handle.go b/internal/bill/handle.go new file mode 100644 index 0000000..d93d28c --- /dev/null +++ b/internal/bill/handle.go @@ -0,0 +1,87 @@ +package bill + +import ( + "context" + "github.com/gofiber/fiber/v2" +) + +type BillHandler struct { + billService BillService +} + +func NewBillHandler(billRoute fiber.Router, bs BillService) { + handler := &BillHandler{ + billService: bs, + } + + billRoute.Get("/", handler.getBills) + billRoute.Post("/", handler.createBill) + billRoute.Put("/", handler.updateBill) + billRoute.Delete("/", handler.deleteBill) +} + +func (h *BillHandler) getBills(c *fiber.Ctx) error { + customContext, cancel := context.WithCancel(context.Background()) + defer cancel() + // TODO() + bills, err := h.billService.GetBills(customContext, BDate{}) + 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": bills, + }) + +} + +func (h *BillHandler) createBill(c *fiber.Ctx) error { + customContext, cancel := context.WithCancel(context.Background()) + defer cancel() + // TODO(): create bill + err := h.billService.CreateBill(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", + }) +} + +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{}) + 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", + }) +} + +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) + 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", + }) +} diff --git a/internal/bill/repository.go b/internal/bill/repository.go index 6ed5116..5f40391 100644 --- a/internal/bill/repository.go +++ b/internal/bill/repository.go @@ -17,7 +17,7 @@ type mariaDBRepository struct { mariadb *sql.DB } -func newMariaDBRepository(mariadb *sql.DB) BillRepository { +func NewMariaDBRepository(mariadb *sql.DB) BillRepository { return &mariaDBRepository{ mariadb: mariadb, } diff --git a/internal/bill/service.go b/internal/bill/service.go index cdd9b72..ac105cd 100644 --- a/internal/bill/service.go +++ b/internal/bill/service.go @@ -20,7 +20,7 @@ func NewBillService(r BillRepository) BillService { // - date is {year, month} when return a month bills // - date is {year} when return a year bills // - date is {} when return all bills -func (b *billService) FetchBills(ctx context.Context, date BDate) (*[]Bill, error) { +func (b *billService) GetBills(ctx context.Context, date BDate) (*[]Bill, error) { if date.Year == 0 && (date.Month != 0 || date.Day != 0) { date.Year = time.Now().Year() } else if date.Month == 0 && date.Day != 0 { @@ -38,18 +38,18 @@ func (b *billService) FetchBills(ctx context.Context, date BDate) (*[]Bill, erro } } -func (b *billService) FetchBillByID(ctx context.Context, id int) (*Bill, error) { +func (b *billService) GetBillByID(ctx context.Context, id int) (*Bill, error) { return b.billRepository.GetBillByID(ctx, id) } -func (b *billService) BuildBill(ctx context.Context, bill *Bill) error { +func (b *billService) CreateBill(ctx context.Context, bill *Bill) error { return b.billRepository.CreateBill(ctx, bill) } -func (b *billService) ModifyBill(ctx context.Context, bill *Bill) error { +func (b *billService) UpdateBill(ctx context.Context, bill *Bill) error { return b.billRepository.UpdateBill(ctx, bill) } -func (b *billService) DestroyBill(ctx context.Context, id int) error { +func (b *billService) DeleteBill(ctx context.Context, id int) error { return b.billRepository.DeleteBill(ctx, id) } diff --git a/internal/infrastructure/fiber.go b/internal/infrastructure/fiber.go index 39a4fda..39822da 100644 --- a/internal/infrastructure/fiber.go +++ b/internal/infrastructure/fiber.go @@ -1,10 +1,8 @@ package infrastructure import ( - "bill-go-fiber/internal/auth" - "bill-go-fiber/internal/city" + "bill-go-fiber/internal/bill" "bill-go-fiber/internal/misc" - "bill-go-fiber/internal/user" "fmt" "log" @@ -53,18 +51,21 @@ func Run() { app.Use(requestid.New()) // Create repositories. - cityRepository := city.NewCityRepository(mariadb) - userRepository := user.NewUserRepository(mariadb) + //cityRepository := city.NewCityRepository(mariadb) + //userRepository := user.NewUserRepository(mariadb) + billRepository := bill.NewMariaDBRepository(mariadb) // Create all of our services. - cityService := city.NewCityService(cityRepository) - userService := user.NewUserService(userRepository) + //cityService := city.NewCityService(cityRepository) + //userService := user.NewUserService(userRepository) + billService := bill.NewBillService(billRepository) // Prepare our endpoints for the API. misc.NewMiscHandler(app.Group("/api/v1")) - auth.NewAuthHandler(app.Group("/api/v1/auth")) - city.NewCityHandler(app.Group("/api/v1/cities"), cityService) - user.NewUserHandler(app.Group("/api/v1/users"), userService) + //auth.NewAuthHandler(app.Group("/api/v1/auth")) + //city.NewCityHandler(app.Group("/api/v1/cities"), cityService) + //user.NewUserHandler(app.Group("/api/v1/users"), userService) + bill.NewBillHandler(app.Group("/api/v1/bills"), billService) // Prepare an endpoint for 'Not Found'. app.All("*", func(c *fiber.Ctx) error {