Skip to content

Commit

Permalink
Merge pull request #126 from OpenTreeHole/division
Browse files Browse the repository at this point in the history
finish Division
  • Loading branch information
JingYiJun authored Oct 15, 2023
2 parents 5ec032b + 6bb80bd commit 3f23f0b
Show file tree
Hide file tree
Showing 10 changed files with 230 additions and 42 deletions.
5 changes: 4 additions & 1 deletion cmd/wire/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/opentreehole/backend

go 1.21

toolchain go1.21.0
//toolchain go1.21.0

require (
github.com/allegro/bigcache/v3 v3.1.0
Expand Down Expand Up @@ -83,6 +83,7 @@ require (
github.com/valyala/tcplisten v1.0.0 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/dl v0.0.0-20231002235324-248fa84a4fc2 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/dl v0.0.0-20231002235324-248fa84a4fc2 h1:RZqlrtSuXDreuzoec3c1KaCotwXXQ9zVqUtVjuivkZQ=
golang.org/dl v0.0.0-20231002235324-248fa84a4fc2/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down
90 changes: 77 additions & 13 deletions internal/handler/division.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package handler

import (
"github.com/gofiber/fiber/v2"

"github.com/opentreehole/backend/internal/repository"
"github.com/opentreehole/backend/internal/schema"
"github.com/opentreehole/backend/internal/service"
)

type DivisionHandler interface {
Expand All @@ -12,10 +13,12 @@ type DivisionHandler interface {

type divisionHandler struct {
*Handler
service service.DivisionService
account repository.AccountRepository
}

func NewDivisionHandler(handler *Handler) DivisionHandler {
return &divisionHandler{Handler: handler}
func NewDivisionHandler(handler *Handler, service service.DivisionService, account repository.AccountRepository) DivisionHandler {
return &divisionHandler{Handler: handler, service: service, account: account}
}

func (h *divisionHandler) RegisterRoute(router fiber.Router) {
Expand All @@ -37,8 +40,12 @@ func (h *divisionHandler) RegisterRoute(router fiber.Router) {
// @Failure 400 {object} schema.HttpError
// @Failure 500 {object} schema.HttpBaseError
func (h *divisionHandler) ListDivisions(c *fiber.Ctx) (err error) {
// TODO:
return c.JSON([]schema.DivisionResponse{})
divisions, err := h.service.ListDivisions(c.Context())
if err != nil {
return err
}

return c.JSON(divisions)
}

// GetDivision godoc
Expand All @@ -53,8 +60,18 @@ func (h *divisionHandler) ListDivisions(c *fiber.Ctx) (err error) {
// @Failure 400 {object} schema.HttpError
// @Failure 500 {object} schema.HttpBaseError
func (h *divisionHandler) GetDivision(c *fiber.Ctx) (err error) {
// TODO:
return c.JSON(schema.DivisionResponse{})

id, err := c.ParamsInt("id")
if err != nil {
return err
}

division, err := h.service.GetDivision(c.Context(), id)
if err != nil {
return err
}

return c.JSON(division)
}

// CreateDivision godoc
Expand All @@ -75,9 +92,22 @@ func (h *divisionHandler) CreateDivision(c *fiber.Ctx) (err error) {
return err
}

// TODO: add service.CreateDivision
c.Context().SetUserValue("FiberCtx", c)
user, err := h.account.GetCurrentUser(c.Context())

if err != nil {
return err
}
if !user.IsAdmin {
return c.JSON(schema.Forbidden())
}

response, err := h.service.CreateDivision(c.Context(), &body)
if err != nil {
return err
}

return c.JSON(schema.DivisionResponse{})
return c.JSON(response)
}

// ModifyDivision godoc
Expand All @@ -93,15 +123,32 @@ func (h *divisionHandler) CreateDivision(c *fiber.Ctx) (err error) {
// @Failure 400 {object} schema.HttpError
// @Failure 500 {object} schema.HttpBaseError
func (h *divisionHandler) ModifyDivision(c *fiber.Ctx) (err error) {
id, err := c.ParamsInt("id")
if err != nil {
return err
}

var body schema.DivisionModifyRequest
err = h.ValidateBody(c, &body)
if err != nil {
return err
}

// TODO: add service.ModifyDivision
c.Context().SetUserValue("FiberCtx", c)
user, err := h.account.GetCurrentUser(c.Context())
if err != nil {
return err
}
if !user.IsAdmin {
return c.JSON(schema.Forbidden())
}

response, err := h.service.ModifyDivision(c.Context(), id, &body)
if err != nil {
return err
}

return c.JSON(schema.DivisionResponse{})
return c.JSON(response)
}

// DeleteDivision godoc
Expand All @@ -123,7 +170,24 @@ func (h *divisionHandler) DeleteDivision(c *fiber.Ctx) (err error) {
return err
}

// TODO: add service.DeleteDivision
id, err := c.ParamsInt("id")
if err != nil {
return err
}

c.Context().SetUserValue("FiberCtx", c)
user, err := h.account.GetCurrentUser(c.Context())
if err != nil {
return err
}
if !user.IsAdmin {
return c.JSON(schema.Forbidden())
}

_, err = h.service.DeleteDivision(c.Context(), id, &body)
if err != nil {
return err
}

return c.JSON(schema.DivisionResponse{})
return c.JSON(nil)
}
77 changes: 66 additions & 11 deletions internal/repository/division.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package repository

import (
"context"

"errors"
"fmt"
"github.com/opentreehole/backend/internal/model"
)

Expand All @@ -28,28 +29,82 @@ type divisionRepository struct {
}

func (d *divisionRepository) ListDivisions(ctx context.Context) (response []*model.Division, err error) {
//TODO implement me
panic("implement me")

_, err = d.GetCache(ctx).Get(ctx, "divisions", &response)
if err == nil {
return response, nil
}

err = d.GetDB(ctx).Find(&response).Error
if err != nil {
return nil, err
}

err = d.GetCache(ctx).Set(ctx, "divisions", response)
if err != nil {
return nil, err
}

return response, err
}

func (d *divisionRepository) GetDivisionByID(ctx context.Context, id int) (response *model.Division, err error) {
//TODO implement me
panic("implement me")

err = d.GetDB(ctx).First(&response, id).Error
if err != nil {
return nil, err
}

return response, err
}

func (d *divisionRepository) CreateDivision(ctx context.Context, request *model.Division) (response *model.Division, err error) {
//TODO implement me
panic("implement me")

err = d.GetDB(ctx).FirstOrCreate(&request, model.Division{Name: request.Name}).Error
if err != nil {
return nil, err
}
response = request

err = d.GetCache(ctx).Delete(ctx, "divisions")
if err != nil && errors.Is(err, fmt.Errorf("entry not found")) {
return response, err
}

return response, err
}

func (d *divisionRepository) ModifyDivision(ctx context.Context, id int, request *model.Division) (response *model.Division, err error) {
//TODO implement me
panic("implement me")

request.ID = id
err = d.GetDB(ctx).Model(&request).Updates(request).Error
if err != nil {
return nil, err
}

err = d.GetCache(ctx).Delete(ctx, "divisions")
if err != nil && errors.Is(err, fmt.Errorf("entry not found")) {
return response, err
}

response = request

return response, err
}

func (d *divisionRepository) DeleteDivision(ctx context.Context, id int) (err error) {
//TODO implement me
panic("implement me")

err = d.GetDB(ctx).Delete(&model.Division{}, id).Error
if err != nil {
return err
}

err = d.GetCache(ctx).Delete(ctx, "divisions")
if err != nil && errors.Is(err, fmt.Errorf("entry not found")) {
return err
}

return nil
}

func NewDivisionRepository(repository Repository) DivisionRepository {
Expand Down
7 changes: 6 additions & 1 deletion internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Repository interface {
GetDB(ctx context.Context) *gorm.DB
GetCache(ctx context.Context) *cache.Cache
GetConfig(ctx context.Context) *config.AllConfig

GetLogger(ctx context.Context) *log.Logger
// GetFiberCtx 获取 fiber.Ctx
// 必须在 ctx 中设置 "FiberCtx" 为 fiber.Ctx,否则会 panic
GetFiberCtx(ctx context.Context) *fiber.Ctx
Expand All @@ -39,6 +39,11 @@ type repository struct {
conf *config.AtomicAllConfig
}

func (r *repository) GetLogger(ctx context.Context) *log.Logger {
//TODO implement me
return r.logger
}

func NewRepository(db *gorm.DB, cache *cache.Cache, logger *log.Logger, conf *config.AtomicAllConfig) Repository {
dbMigration(db, conf, logger)
return &repository{db: db, cache: cache, logger: logger, conf: conf}
Expand Down
10 changes: 10 additions & 0 deletions internal/schema/division.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func (d *DivisionResponse) FromModel(m *model.Division, pinned []struct{}) *Divi
d.Pinned = pinned
return d
}

func (d *DivisionCreateRequest) ToModel() (m *model.Division) {

m = &model.Division{
Name: d.Name,
Description: d.Description,
}

return m
}
3 changes: 1 addition & 2 deletions internal/server/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
fiberrecover "github.com/gofiber/fiber/v2/middleware/recover"
"github.com/rs/zerolog/log"

"github.com/opentreehole/backend/internal/config"
"github.com/opentreehole/backend/internal/schema"
"github.com/opentreehole/backend/pkg/utils"
"github.com/rs/zerolog/log"
)

func GetUserID(c *fiber.Ctx) (int, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/server/server.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"fmt"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -90,11 +91,14 @@ func (s *Server) GetFiberApp() *fiber.App {
DisableStartupMessage: disableStartupMessage,
ErrorHandler: schema.ErrorHandler,
})
s.logger.Info(fmt.Sprintf("register: %v", s.rootRegister))

RegisterMiddlewares(s.config)(s.app)
for _, h := range s.rootRegister {
h.RegisterRoute(s.app)
}
s.logger.Info(fmt.Sprintf("handlers: %v", s.handlers))

for _, h := range s.handlers {
h.RegisterRoute(s.app.Group("/api"))
}
Expand Down
Loading

0 comments on commit 3f23f0b

Please sign in to comment.