From 004207342fa7da85f77c4f4d08c3f90ebce99eb0 Mon Sep 17 00:00:00 2001 From: Linn3a Date: Mon, 25 Sep 2023 21:32:56 +0800 Subject: [PATCH 1/5] feat: complete division --- cmd/wire/wire_gen.go | 5 +- go.mod | 2 +- internal/docs/swagger.yaml | 174 +++++++++++++++--------------- internal/handler/division.go | 95 +++++++++++++--- internal/repository/division.go | 55 ++++++++-- internal/repository/repository.go | 7 +- internal/schema/division.go | 10 ++ internal/server/middlewares.go | 3 +- internal/server/server.go | 4 + internal/service/division.go | 68 +++++++++--- 10 files changed, 294 insertions(+), 129 deletions(-) diff --git a/cmd/wire/wire_gen.go b/cmd/wire/wire_gen.go index ae013e7..985208d 100644 --- a/cmd/wire/wire_gen.go +++ b/cmd/wire/wire_gen.go @@ -26,13 +26,16 @@ func NewApp() (*server.Server, func(), error) { validate := handler.NewValidater() handlerHandler := handler.NewHandler(logger, validate) serviceService := service.NewService(logger, pointer) + db := repository.NewDB(pointer, logger) cacheCache := cache.NewCache(pointer, logger) repositoryRepository := repository.NewRepository(db, cacheCache, logger, pointer) accountRepository := repository.NewAccountRepository(repositoryRepository) accountService := service.NewAccountService(serviceService, accountRepository) accountHandler := handler.NewAccountHandler(handlerHandler, accountService) - divisionHandler := handler.NewDivisionHandler(handlerHandler) + divisionRepository := repository.NewDivisionRepository(repositoryRepository) + divisionService := service.NewDivisionService(serviceService, divisionRepository) + divisionHandler := handler.NewDivisionHandler(handlerHandler, divisionService, accountRepository) courseGroupRepository := repository.NewCourseGroupRepository(repositoryRepository) courseGroupService := service.NewCourseGroupService(serviceService, courseGroupRepository) courseGroupHandler := handler.NewCourseGroupHandler(handlerHandler, courseGroupService, accountRepository) diff --git a/go.mod b/go.mod index 2f82c38..b8ce7fd 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/internal/docs/swagger.yaml b/internal/docs/swagger.yaml index 6cb0465..ef3a7b9 100644 --- a/internal/docs/swagger.yaml +++ b/internal/docs/swagger.yaml @@ -195,7 +195,7 @@ definitions: type: string rank: allOf: - - $ref: '#/definitions/schema.ReviewRankV1' + - $ref: '#/definitions/schema.ReviewRankV1' description: 评价 remark: description: Remark = 点赞数 - 点踩数 @@ -220,7 +220,7 @@ definitions: type: integer original: allOf: - - $ref: '#/definitions/schema.ReviewHistoryV1' + - $ref: '#/definitions/schema.ReviewHistoryV1' description: 修改前的评教 time: description: 创建时间 @@ -248,7 +248,7 @@ definitions: type: string extra: allOf: - - $ref: '#/definitions/schema.UserExtraV1' + - $ref: '#/definitions/schema.UserExtraV1' description: 额外信息 history: description: 修改历史,slices 必须非空 @@ -263,7 +263,7 @@ definitions: type: boolean rank: allOf: - - $ref: '#/definitions/schema.ReviewRankV1' + - $ref: '#/definitions/schema.ReviewRankV1' description: 评价 remark: description: Remark = 点赞数 - 点踩数 @@ -340,10 +340,10 @@ paths: /divisions: get: consumes: - - application/json + - application/json description: list all divisions produces: - - application/json + - application/json responses: "200": description: OK @@ -361,20 +361,20 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: list all divisions tags: - - Division + - Division post: consumes: - - application/json + - application/json description: create a division, only admin can create parameters: - - description: json - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.DivisionCreateRequest' + - description: json + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.DivisionCreateRequest' produces: - - application/json + - application/json responses: "201": description: Created @@ -390,26 +390,26 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: create a division tags: - - Division + - Division /divisions/{id}: delete: consumes: - - application/json + - application/json description: delete a division, only admin can delete parameters: - - description: division id - in: path - name: id - required: true - type: string - - description: json - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.DivisionDeleteRequest' + - description: division id + in: path + name: id + required: true + type: string + - description: json + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.DivisionDeleteRequest' produces: - - application/json + - application/json responses: "204": description: No Content @@ -425,19 +425,19 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: delete a division tags: - - Division + - Division get: consumes: - - application/json + - application/json description: get a division parameters: - - description: division id - in: path - name: id - required: true - type: string + - description: division id + in: path + name: id + required: true + type: string produces: - - application/json + - application/json responses: "200": description: OK @@ -453,25 +453,25 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: get a division tags: - - Division + - Division put: consumes: - - application/json + - application/json description: modify a division, only admin can modify parameters: - - description: division id - in: path - name: id - required: true - type: string - - description: json - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.DivisionModifyRequest' + - description: division id + in: path + name: id + required: true + type: string + - description: json + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.DivisionModifyRequest' produces: - - application/json + - application/json responses: "200": description: OK @@ -487,21 +487,21 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: modify a division tags: - - Division + - Division /group/{id}: get: consumes: - - application/json + - application/json deprecated: true description: get a course group, old version or v1 version parameters: - - description: course group id - in: path - name: id - required: true - type: string + - description: course group id + in: path + name: id + required: true + type: string produces: - - application/json + - application/json responses: "200": description: OK @@ -521,21 +521,21 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: /group/{group_id} tags: - - CourseGroup + - CourseGroup /login: post: consumes: - - application/json + - application/json description: login with email and password parameters: - - description: LoginRequest - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.LoginRequest' + - description: LoginRequest + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.LoginRequest' produces: - - application/json + - application/json responses: "200": description: OK @@ -551,22 +551,22 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: login tags: - - Account + - Account /register: post: consumes: - - application/json + - application/json description: register with email, password and optional verification code if enabled parameters: - - description: RegisterRequest - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.RegisterRequest' + - description: RegisterRequest + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.RegisterRequest' produces: - - application/json + - application/json responses: "201": description: Created @@ -582,21 +582,21 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: register tags: - - Account + - Account put: consumes: - - application/json + - application/json description: reset password with email, password and optional verification code if enabled parameters: - - description: ResetPasswordRequest - in: body - name: json - required: true - schema: - $ref: '#/definitions/schema.ResetPasswordRequest' + - description: ResetPasswordRequest + in: body + name: json + required: true + schema: + $ref: '#/definitions/schema.ResetPasswordRequest' produces: - - application/json + - application/json responses: "201": description: Created @@ -612,5 +612,5 @@ paths: $ref: '#/definitions/schema.HttpBaseError' summary: reset password tags: - - Account + - Account swagger: "2.0" diff --git a/internal/handler/division.go b/internal/handler/division.go index 10a73ad..6f944ac 100644 --- a/internal/handler/division.go +++ b/internal/handler/division.go @@ -1,9 +1,11 @@ package handler import ( + "fmt" "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 { @@ -12,10 +14,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) { @@ -37,8 +41,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 @@ -53,8 +61,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 @@ -75,9 +93,24 @@ 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()) + h.logger.Info(fmt.Sprintf("user: %+v", user)) + if err != nil { + h.logger.Info(fmt.Sprintf("err: %v", err)) + return err + } + if !user.IsAdmin { + h.logger.Info(fmt.Sprintf("user: %+v", user)) + 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 @@ -93,15 +126,34 @@ 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()) + } + + h.logger.Info(fmt.Sprintf("user_id: %d", user.ID)) + + 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 @@ -123,7 +175,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) } diff --git a/internal/repository/division.go b/internal/repository/division.go index e914d51..c76aff3 100644 --- a/internal/repository/division.go +++ b/internal/repository/division.go @@ -2,7 +2,6 @@ package repository import ( "context" - "github.com/opentreehole/backend/internal/model" ) @@ -28,28 +27,62 @@ 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 + + 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 + } + + response = request + + return response, err } func (d *divisionRepository) DeleteDivision(ctx context.Context, id int) (err error) { - //TODO implement me - panic("implement me") + + return d.GetDB(ctx).Delete(&model.Division{}, id).Error } func NewDivisionRepository(repository Repository) DivisionRepository { diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 9cadc78..ef32e9c 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -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 @@ -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 { return &repository{db: db, cache: cache, logger: logger, conf: conf} } diff --git a/internal/schema/division.go b/internal/schema/division.go index 0613c45..bbd153c 100644 --- a/internal/schema/division.go +++ b/internal/schema/division.go @@ -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 +} diff --git a/internal/server/middlewares.go b/internal/server/middlewares.go index b649571..d6d5d3c 100644 --- a/internal/server/middlewares.go +++ b/internal/server/middlewares.go @@ -7,7 +7,6 @@ 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" @@ -95,7 +94,7 @@ func StackTraceHandler(_ *fiber.Ctx, e any) { func RegisterMiddlewares(conf *config.AtomicAllConfig) func(app *fiber.App) { return func(app *fiber.App) { - app.Use(fiberrecover.New(fiberrecover.Config{EnableStackTrace: true, StackTraceHandler: StackTraceHandler})) + //app.Use(fiberrecover.New(fiberrecover.Config{EnableStackTrace: true, StackTraceHandler: StackTraceHandler})) app.Use(MiddlewareGetUserID) if conf.Load().Mode != "bench" { app.Use(MiddlewareCustomLogger) diff --git a/internal/server/server.go b/internal/server/server.go index 8a80288..fb1e51b 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -1,6 +1,7 @@ package server import ( + "fmt" "os" "os/signal" "strconv" @@ -81,11 +82,14 @@ func (s *Server) Run() { DisableStartupMessage: disableStartupMessage, ErrorHandler: schema.ErrorHandler, }) + s.logger.Info(fmt.Sprintf("register: %v", s.rootRegister)) RegisterMiddlewares(s.config)(app) for _, h := range s.rootRegister { h.RegisterRoute(app) } + s.logger.Info(fmt.Sprintf("handlers: %v", s.handlers)) + for _, h := range s.handlers { h.RegisterRoute(app.Group("/api")) } diff --git a/internal/service/division.go b/internal/service/division.go index f22db76..5f4b843 100644 --- a/internal/service/division.go +++ b/internal/service/division.go @@ -2,7 +2,8 @@ package service import ( "context" - + "github.com/opentreehole/backend/internal/model" + "github.com/opentreehole/backend/internal/repository" "github.com/opentreehole/backend/internal/schema" ) @@ -26,33 +27,74 @@ type DivisionService interface { type divisionService struct { Service + Repository repository.DivisionRepository } func (d *divisionService) ListDivisions(ctx context.Context) (response []*schema.DivisionResponse, err error) { - //TODO implement me - panic("implement me") + + divisionsModel, err := d.Repository.ListDivisions(ctx) + if err != nil { + return nil, err + } + + response = make([]*schema.DivisionResponse, len(divisionsModel)) + for i, model := range divisionsModel { + response[i] = new(schema.DivisionResponse).FromModel(model, nil) + } + + return response, nil } func (d *divisionService) GetDivision(ctx context.Context, id int) (response *schema.DivisionResponse, err error) { - //TODO implement me - panic("implement me") + + divisionModel, err := d.Repository.GetDivisionByID(ctx, id) + if err != nil { + return nil, err + } + + response = new(schema.DivisionResponse).FromModel(divisionModel, nil) + + return response, nil } func (d *divisionService) CreateDivision(ctx context.Context, request *schema.DivisionCreateRequest) (response *schema.DivisionResponse, err error) { - //TODO implement me - panic("implement me") + + divisionModel, err := d.Repository.CreateDivision(ctx, &model.Division{ + Name: request.Name, + Description: request.Description, + }) + + response = new(schema.DivisionResponse).FromModel(divisionModel, nil) + return response, nil } func (d *divisionService) ModifyDivision(ctx context.Context, id int, request *schema.DivisionModifyRequest) (response *schema.DivisionResponse, err error) { - //TODO implement me - panic("implement me") + + divisionModel, err := d.Repository.ModifyDivision(ctx, id, &model.Division{ + Name: request.Name, + Description: request.Description, + Pinned: request.Pinned, + }) + + // TODO get Pinned holes + + response = new(schema.DivisionResponse).FromModel(divisionModel, []struct{}{}) + return response, nil } func (d *divisionService) DeleteDivision(ctx context.Context, id int, request *schema.DivisionDeleteRequest) (response *schema.DivisionResponse, err error) { - //TODO implement me - panic("implement me") + + // TODO move all holes to the target division + + err = d.Repository.DeleteDivision(ctx, id) + if err != nil { + return nil, err + } + + response = &schema.DivisionResponse{} + return response, nil } -func NewDivisionService(service Service) DivisionService { - return &divisionService{Service: service} +func NewDivisionService(service Service, repository repository.DivisionRepository) DivisionService { + return &divisionService{Service: service, Repository: repository} } From fcbb72689586aa826037c10ecc9c5402e745dcb5 Mon Sep 17 00:00:00 2001 From: Linn3a Date: Thu, 5 Oct 2023 19:17:44 +0800 Subject: [PATCH 2/5] fix: fix cache --- internal/repository/division.go | 16 ++++++++++++++++ internal/service/division.go | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/internal/repository/division.go b/internal/repository/division.go index c76aff3..0dfcc5e 100644 --- a/internal/repository/division.go +++ b/internal/repository/division.go @@ -2,6 +2,8 @@ package repository import ( "context" + "errors" + "fmt" "github.com/opentreehole/backend/internal/model" ) @@ -64,6 +66,20 @@ func (d *divisionRepository) CreateDivision(ctx context.Context, request *model. } response = request + var divisions []*model.Division + _, err = d.GetCache(ctx).Get(ctx, "divisions", &divisions) + if err != nil { + if errors.Is(err, fmt.Errorf("entry not found")) { + return response, err + } + } + divisions = append(divisions, response) + + err = d.GetCache(ctx).Set(ctx, "divisions", divisions) + if err != nil { + return nil, err + } + return response, err } diff --git a/internal/service/division.go b/internal/service/division.go index 5f4b843..f3c1983 100644 --- a/internal/service/division.go +++ b/internal/service/division.go @@ -38,8 +38,8 @@ func (d *divisionService) ListDivisions(ctx context.Context) (response []*schema } response = make([]*schema.DivisionResponse, len(divisionsModel)) - for i, model := range divisionsModel { - response[i] = new(schema.DivisionResponse).FromModel(model, nil) + for i, divisionModel := range divisionsModel { + response[i] = new(schema.DivisionResponse).FromModel(divisionModel, nil) } return response, nil @@ -63,6 +63,9 @@ func (d *divisionService) CreateDivision(ctx context.Context, request *schema.Di Name: request.Name, Description: request.Description, }) + if err != nil { + return nil, err + } response = new(schema.DivisionResponse).FromModel(divisionModel, nil) return response, nil From 344597b8f98c140377795a3dc33a4868b9fcc221 Mon Sep 17 00:00:00 2001 From: Linn3a Date: Thu, 5 Oct 2023 19:17:53 +0800 Subject: [PATCH 3/5] fix: fix cache --- internal/repository/division.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/repository/division.go b/internal/repository/division.go index 0dfcc5e..04940e2 100644 --- a/internal/repository/division.go +++ b/internal/repository/division.go @@ -93,12 +93,18 @@ func (d *divisionRepository) ModifyDivision(ctx context.Context, id int, request response = request + //err = d.GetCache(ctx).Set(ctx, "divisions", nil) + //if err != nil { + // return nil, err + //} + return response, err } func (d *divisionRepository) DeleteDivision(ctx context.Context, id int) (err error) { return d.GetDB(ctx).Delete(&model.Division{}, id).Error + } func NewDivisionRepository(repository Repository) DivisionRepository { From 01621ba39944041459b5981deecc8f6e5ac13ae9 Mon Sep 17 00:00:00 2001 From: Linn3a Date: Fri, 13 Oct 2023 10:15:25 +0800 Subject: [PATCH 4/5] fix: delete cache after modify db --- go.mod | 1 + go.sum | 2 ++ internal/repository/division.go | 36 ++++++++++++++++----------------- 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 3ff9615..0b99585 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 519e400..bc231b0 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/repository/division.go b/internal/repository/division.go index 04940e2..a4884a6 100644 --- a/internal/repository/division.go +++ b/internal/repository/division.go @@ -66,18 +66,9 @@ func (d *divisionRepository) CreateDivision(ctx context.Context, request *model. } response = request - var divisions []*model.Division - _, err = d.GetCache(ctx).Get(ctx, "divisions", &divisions) - if err != nil { - if errors.Is(err, fmt.Errorf("entry not found")) { - return response, err - } - } - divisions = append(divisions, response) - - err = d.GetCache(ctx).Set(ctx, "divisions", divisions) - 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 } return response, err @@ -91,20 +82,29 @@ func (d *divisionRepository) ModifyDivision(ctx context.Context, id int, request 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 + } - //err = d.GetCache(ctx).Set(ctx, "divisions", nil) - //if err != nil { - // return nil, err - //} + response = request return response, err } func (d *divisionRepository) DeleteDivision(ctx context.Context, id int) (err error) { - return d.GetDB(ctx).Delete(&model.Division{}, id).Error + 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 { From 6bb80bdad5e07840d395c89489b04789814744f5 Mon Sep 17 00:00:00 2001 From: Linn3a Date: Sun, 15 Oct 2023 20:11:37 +0800 Subject: [PATCH 5/5] fix: delete log --- internal/handler/division.go | 7 +------ internal/server/middlewares.go | 6 +++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/internal/handler/division.go b/internal/handler/division.go index 6f944ac..3296c83 100644 --- a/internal/handler/division.go +++ b/internal/handler/division.go @@ -1,7 +1,6 @@ package handler import ( - "fmt" "github.com/gofiber/fiber/v2" "github.com/opentreehole/backend/internal/repository" "github.com/opentreehole/backend/internal/schema" @@ -95,13 +94,11 @@ func (h *divisionHandler) CreateDivision(c *fiber.Ctx) (err error) { c.Context().SetUserValue("FiberCtx", c) user, err := h.account.GetCurrentUser(c.Context()) - h.logger.Info(fmt.Sprintf("user: %+v", user)) + if err != nil { - h.logger.Info(fmt.Sprintf("err: %v", err)) return err } if !user.IsAdmin { - h.logger.Info(fmt.Sprintf("user: %+v", user)) return c.JSON(schema.Forbidden()) } @@ -146,8 +143,6 @@ func (h *divisionHandler) ModifyDivision(c *fiber.Ctx) (err error) { return c.JSON(schema.Forbidden()) } - h.logger.Info(fmt.Sprintf("user_id: %d", user.ID)) - response, err := h.service.ModifyDivision(c.Context(), id, &body) if err != nil { return err diff --git a/internal/server/middlewares.go b/internal/server/middlewares.go index d6d5d3c..6a058cf 100644 --- a/internal/server/middlewares.go +++ b/internal/server/middlewares.go @@ -7,11 +7,11 @@ import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" - "github.com/rs/zerolog/log" - + fiberrecover "github.com/gofiber/fiber/v2/middleware/recover" "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) { @@ -94,7 +94,7 @@ func StackTraceHandler(_ *fiber.Ctx, e any) { func RegisterMiddlewares(conf *config.AtomicAllConfig) func(app *fiber.App) { return func(app *fiber.App) { - //app.Use(fiberrecover.New(fiberrecover.Config{EnableStackTrace: true, StackTraceHandler: StackTraceHandler})) + app.Use(fiberrecover.New(fiberrecover.Config{EnableStackTrace: true, StackTraceHandler: StackTraceHandler})) app.Use(MiddlewareGetUserID) if conf.Load().Mode != "bench" { app.Use(MiddlewareCustomLogger)