to: label api

This commit is contained in:
Liangzhao Che 2023-06-01 19:40:48 +08:00
parent 12650c9965
commit 27588753c6
5 changed files with 124 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package infrastructure
import (
"bill-go-fiber/internal/bill"
"bill-go-fiber/internal/label"
"bill-go-fiber/internal/misc"
"fmt"
"log"
@ -55,11 +56,13 @@ func Run() {
//cityRepository := city.NewCityRepository(mariadb)
//userRepository := user.NewUserRepository(mariadb)
billRepository := bill.NewBillRepository(sqlite)
labelRepository := label.NewLabelRepository(sqlite)
// Create all of our services.
//cityService := city.NewCityService(cityRepository)
//userService := user.NewUserService(userRepository)
billService := bill.NewBillService(billRepository)
labelService := label.NewLabelService(labelRepository)
// Prepare our endpoints for the API.
misc.NewMiscHandler(app.Group("/api/v1"))
@ -67,6 +70,7 @@ func Run() {
//city.NewCityHandler(app.Group("/api/v1/cities"), cityService)
//user.NewUserHandler(app.Group("/api/v1/users"), userService)
bill.NewBillHandler(app.Group("/api/v1/bills"), billService)
label.NewLabelHandler(app.Group("/api/v1/labels"), labelService)
// Prepare an endpoint for 'Not Found'.
app.All("*", func(c *fiber.Ctx) error {

View File

@ -1,8 +1,12 @@
package label
import "context"
import (
"context"
"gorm.io/gorm"
)
type Label struct {
gorm.Model
ID int `json:"id"`
Type string `json:"type"`
Name string `json:"name"`
@ -11,7 +15,7 @@ type Label struct {
}
type LabelRepository interface {
GetLabels(ctx context.Context) (*[]Label, error)
GetLabels(ctx context.Context) ([]Label, error)
GetLabelById(ctx context.Context, id int) (*Label, error)
CreateLabel(ctx context.Context, label *Label) error
UpdateLabel(ctx context.Context, label *Label) error
@ -19,7 +23,7 @@ type LabelRepository interface {
}
type LabelService interface {
GetLabels(ctx context.Context) (*[]Label, error)
GetLabels(ctx context.Context) ([]Label, error)
GetLabelById(ctx context.Context, id int) (*Label, error)
CreateLabel(ctx context.Context, label *Label) error
UpdateLabel(ctx context.Context, label *Label) error

View File

@ -1 +1,34 @@
package label
import (
"context"
"github.com/gofiber/fiber/v2"
)
type LabelHandler struct {
labelService LabelService
}
func NewLabelHandler(labelRoute fiber.Router, ls LabelService) {
handler := &LabelHandler{
labelService: ls,
}
labelRoute.Get("", handler.getLabels)
}
func (h *LabelHandler) getLabels(c *fiber.Ctx) error {
customContext, cancel := context.WithCancel(context.Background())
defer cancel()
labels, err := h.labelService.GetLabels(customContext)
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": labels,
})
}

View File

@ -1 +1,45 @@
package label
import (
"context"
"gorm.io/gorm"
)
type sqliteRepository struct {
*gorm.DB
}
func NewLabelRepository(db *gorm.DB) LabelRepository {
return &sqliteRepository{
db,
}
}
func (s *sqliteRepository) GetLabels(ctx context.Context) ([]Label, error) {
var labels []Label
res := s.Find(&labels)
if res.Error != nil {
return nil, res.Error
}
return labels, nil
}
func (s *sqliteRepository) GetLabelById(ctx context.Context, id int) (*Label, error) {
//TODO implement me
panic("implement me")
}
func (s *sqliteRepository) CreateLabel(ctx context.Context, label *Label) error {
//TODO implement me
panic("implement me")
}
func (s *sqliteRepository) UpdateLabel(ctx context.Context, label *Label) error {
//TODO implement me
panic("implement me")
}
func (s *sqliteRepository) DeleteLabel(ctx context.Context, id int) error {
//TODO implement me
panic("implement me")
}

View File

@ -1 +1,37 @@
package label
import (
"context"
)
type labelService struct {
labelRepository LabelRepository
}
func NewLabelService(labelRepository LabelRepository) LabelService {
return &labelService{labelRepository: labelRepository}
}
func (l *labelService) GetLabels(ctx context.Context) ([]Label, error) {
return l.labelRepository.GetLabels(ctx)
}
func (l *labelService) GetLabelById(ctx context.Context, id int) (*Label, error) {
//TODO implement me
panic("implement me")
}
func (l *labelService) CreateLabel(ctx context.Context, label *Label) error {
//TODO implement me
panic("implement me")
}
func (l *labelService) UpdateLabel(ctx context.Context, label *Label) error {
//TODO implement me
panic("implement me")
}
func (l *labelService) DeleteLabel(ctx context.Context, id int) error {
//TODO implement me
panic("implement me")
}