to: bill api
This commit is contained in:
parent
2d77947872
commit
7862701a26
|
@ -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
|
||||
}
|
||||
|
|
87
internal/bill/handle.go
Normal file
87
internal/bill/handle.go
Normal file
|
@ -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",
|
||||
})
|
||||
}
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user