From eeca90d2e52adf9850d9e2536aaabb5f2c70d1f6 Mon Sep 17 00:00:00 2001 From: longtao <1273211023@qq.com> Date: Mon, 13 Mar 2023 15:23:56 +0800 Subject: [PATCH] support team_name query --- internal/app/api/rest/subscribe.go | 18 ++++- internal/app/api/router.go | 1 + internal/app/initialization/core.go | 12 +-- internal/app/model/common.go | 10 ++- .../app/model/entity/mysql_special_awards.go | 16 ++++ internal/app/model/vo/subscribe.go | 5 ++ .../repository/cache/mysql_challenge_score.go | 4 +- .../repository/cache/mysql_special_awards.go | 18 +++++ .../app/repository/mysql_challenge_score.go | 4 +- .../app/repository/mysql_special_awards.go | 27 +++++++ internal/app/service/subscribe.go | 77 ++++++++++++++++--- 11 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 internal/app/model/entity/mysql_special_awards.go create mode 100644 internal/app/repository/cache/mysql_special_awards.go create mode 100644 internal/app/repository/mysql_special_awards.go diff --git a/internal/app/api/rest/subscribe.go b/internal/app/api/rest/subscribe.go index 76a7c56..fca1a2a 100644 --- a/internal/app/api/rest/subscribe.go +++ b/internal/app/api/rest/subscribe.go @@ -44,7 +44,7 @@ func (ctl *SubscribeController) EmailCreate(c *gin.Context) { } func (ctl *SubscribeController) ChallengeScore(c *gin.Context) { - var req vo.PageVO + var req vo.ChallengeScoreReq if err := c.ShouldBind(&req); err != nil { c.JSON(http.StatusOK, response.BuildErr(errors.Wrap(err))) return @@ -55,5 +55,19 @@ func (ctl *SubscribeController) ChallengeScore(c *gin.Context) { c.JSON(http.StatusOK, response.BuildErr(errors.Wrap(err))) return } - c.JSON(http.StatusOK, response.BuildWithPage(res, req, int(total))) + if res == nil { + c.JSON(http.StatusOK, response.BuildErr(errors.WrapDetail(errors.EC40004, "no data find"))) + return + } + c.JSON(http.StatusOK, response.BuildWithPage(res, *req.Page, int(total))) +} + +func (ctl *SubscribeController) SpecialAwards(c *gin.Context) { + res, err := ctl.SubscribeService.SpecialAwards() + if err != nil { + c.JSON(http.StatusOK, response.BuildErr(errors.Wrap(err))) + return + } + + c.JSON(http.StatusOK, response.Build(res)) } diff --git a/internal/app/api/router.go b/internal/app/api/router.go index 530cac9..2a429e9 100644 --- a/internal/app/api/router.go +++ b/internal/app/api/router.go @@ -11,5 +11,6 @@ func InitSubscribeRouter(r *gin.RouterGroup, ctl *rest.SubscribeController) { { subscribeRouter.POST("/email", ctl.EmailCreate) subscribeRouter.GET("/challenge-score", ctl.ChallengeScore) + subscribeRouter.GET("/special-awards", ctl.SpecialAwards) } } diff --git a/internal/app/initialization/core.go b/internal/app/initialization/core.go index 613d65a..c5caf2b 100644 --- a/internal/app/initialization/core.go +++ b/internal/app/initialization/core.go @@ -11,21 +11,23 @@ import ( func NewRepositories(mysqlIcndev *gorm.DB) *model.Repositories { return &model.Repositories{ - SubscribeRepo: repository.NewSubscribeEmailRepo(mysqlIcndev), - ChallengeRepo: repository.NewChallengeScoreRepo(mysqlIcndev), + SubscribeRepo: repository.NewSubscribeEmailRepo(mysqlIcndev), + ChallengeRepo: repository.NewChallengeScoreRepo(mysqlIcndev), + SpecialAwardsRepo: repository.NewSpecialAwardsRepo(mysqlIcndev), } } func NewCacheRepositories(r *model.Repositories) *model.CacheRepositories { return &model.CacheRepositories{ - SubscribeCacheRepo: cache.NewSubscribeEmailCacheRepo(r.SubscribeRepo), - ChallengeCacheRepo: cache.NewChallengeScoreCacheRepo(r.ChallengeRepo), + SubscribeCacheRepo: cache.NewSubscribeEmailCacheRepo(r.SubscribeRepo), + ChallengeCacheRepo: cache.NewChallengeScoreCacheRepo(r.ChallengeRepo), + SpecialAwardsCacheRepo: cache.NewSpecialAwardsCacheRepo(r.SpecialAwardsRepo), } } func NewServices(r *model.CacheRepositories) *model.Services { return &model.Services{ - SubscribeService: service.NewSubscribeService(r.SubscribeCacheRepo, r.ChallengeCacheRepo), + SubscribeService: service.NewSubscribeService(r.SubscribeCacheRepo, r.ChallengeCacheRepo, r.SpecialAwardsCacheRepo), } } diff --git a/internal/app/model/common.go b/internal/app/model/common.go index c39fe01..1172764 100644 --- a/internal/app/model/common.go +++ b/internal/app/model/common.go @@ -8,13 +8,15 @@ import ( ) type Repositories struct { - SubscribeRepo *repository.SubscribeEmailRepo - ChallengeRepo *repository.ChallengeScoreRepo + SubscribeRepo *repository.SubscribeEmailRepo + ChallengeRepo *repository.ChallengeScoreRepo + SpecialAwardsRepo *repository.SpecialAwardsRepo } type CacheRepositories struct { - SubscribeCacheRepo *cache.SubscribeEmailCacheRepo - ChallengeCacheRepo *cache.ChallengeScoreCacheRepo + SubscribeCacheRepo *cache.SubscribeEmailCacheRepo + ChallengeCacheRepo *cache.ChallengeScoreCacheRepo + SpecialAwardsCacheRepo *cache.SpecialAwardsCacheRepo } type Services struct { diff --git a/internal/app/model/entity/mysql_special_awards.go b/internal/app/model/entity/mysql_special_awards.go new file mode 100644 index 0000000..2a93c3e --- /dev/null +++ b/internal/app/model/entity/mysql_special_awards.go @@ -0,0 +1,16 @@ +package entity + +const TableNameSpecialAwards = "special_awards" + +type SpecialAwards struct { + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true;comment:主键" json:"id"` + Rank int `gorm:"column:rank" json:"rank"` + TeamName string `gorm:"column:team_name" json:"team_name"` + TaskCompleted string `gorm:"column:task_completed" json:"task_completed"` + FinalScore int `gorm:"column:final_score" json:"final_score"` + UpdateTime int64 `gorm:"column:update_time" json:"update_time"` +} + +func (*SpecialAwards) TableName() string { + return TableNameSpecialAwards +} diff --git a/internal/app/model/vo/subscribe.go b/internal/app/model/vo/subscribe.go index 4338961..6f2e250 100644 --- a/internal/app/model/vo/subscribe.go +++ b/internal/app/model/vo/subscribe.go @@ -3,3 +3,8 @@ package vo type CreateSubscribeEmailReq struct { Email string `form:"email" json:"email"` } + +type ChallengeScoreReq struct { + TeamName string `form:"team_name" json:"team_name"` + Page *PageVO +} diff --git a/internal/app/repository/cache/mysql_challenge_score.go b/internal/app/repository/cache/mysql_challenge_score.go index 54029a3..afd4035 100644 --- a/internal/app/repository/cache/mysql_challenge_score.go +++ b/internal/app/repository/cache/mysql_challenge_score.go @@ -13,6 +13,6 @@ func NewChallengeScoreCacheRepo(dbr *repository.ChallengeScoreRepo) *ChallengeSc return &ChallengeScoreCacheRepo{dbr: dbr} } -func (repo *ChallengeScoreCacheRepo) FindByLimit(offset, limit int) ([]*entity.ChallengeScore, int64, error) { - return repo.dbr.FindByLimit(offset, limit) +func (repo *ChallengeScoreCacheRepo) FindAll() ([]*entity.ChallengeScore, int64, error) { + return repo.dbr.FindAll() } diff --git a/internal/app/repository/cache/mysql_special_awards.go b/internal/app/repository/cache/mysql_special_awards.go new file mode 100644 index 0000000..02ba4ad --- /dev/null +++ b/internal/app/repository/cache/mysql_special_awards.go @@ -0,0 +1,18 @@ +package cache + +import ( + "github.com/bianjieai/icndev-server/internal/app/model/entity" + "github.com/bianjieai/icndev-server/internal/app/repository" +) + +type SpecialAwardsCacheRepo struct { + dbr *repository.SpecialAwardsRepo +} + +func NewSpecialAwardsCacheRepo(dbr *repository.SpecialAwardsRepo) *SpecialAwardsCacheRepo { + return &SpecialAwardsCacheRepo{dbr: dbr} +} + +func (repo *SpecialAwardsCacheRepo) FindAll() ([]*entity.SpecialAwards, int64, error) { + return repo.dbr.FindAll() +} diff --git a/internal/app/repository/mysql_challenge_score.go b/internal/app/repository/mysql_challenge_score.go index 7a49a35..100f6c0 100644 --- a/internal/app/repository/mysql_challenge_score.go +++ b/internal/app/repository/mysql_challenge_score.go @@ -6,7 +6,7 @@ import ( ) type IChallengeScoreRepo interface { - FindByLimit(offset, limit int) ([]*entity.ChallengeScore, int64, error) + FindAll() ([]*entity.ChallengeScore, int64, error) } type ChallengeScoreRepo struct { @@ -17,7 +17,7 @@ func NewChallengeScoreRepo(db *gorm.DB) *ChallengeScoreRepo { return &ChallengeScoreRepo{db: db} } -func (repo *ChallengeScoreRepo) FindByLimit(offset, limit int) ([]*entity.ChallengeScore, int64, error) { +func (repo *ChallengeScoreRepo) FindAll() ([]*entity.ChallengeScore, int64, error) { var res []*entity.ChallengeScore var total int64 var err error diff --git a/internal/app/repository/mysql_special_awards.go b/internal/app/repository/mysql_special_awards.go new file mode 100644 index 0000000..480a84e --- /dev/null +++ b/internal/app/repository/mysql_special_awards.go @@ -0,0 +1,27 @@ +package repository + +import ( + "github.com/bianjieai/icndev-server/internal/app/model/entity" + "gorm.io/gorm" +) + +type ISpecialAwardsRepo interface { + FindAll() ([]*entity.SpecialAwards, int64, error) +} + +type SpecialAwardsRepo struct { + db *gorm.DB +} + +func NewSpecialAwardsRepo(db *gorm.DB) *SpecialAwardsRepo { + return &SpecialAwardsRepo{db: db} +} + +func (repo *SpecialAwardsRepo) FindAll() ([]*entity.SpecialAwards, int64, error) { + var res []*entity.SpecialAwards + var total int64 + var err error + tx := repo.db.Table(entity.TableNameSpecialAwards) + err = tx.Count(&total).Find(&res).Error + return res, total, err +} diff --git a/internal/app/service/subscribe.go b/internal/app/service/subscribe.go index 1b59c2a..5321a6a 100644 --- a/internal/app/service/subscribe.go +++ b/internal/app/service/subscribe.go @@ -12,12 +12,13 @@ import ( ) type SubscribeService struct { - subscribeRepo *cache.SubscribeEmailCacheRepo - challengeRepo *cache.ChallengeScoreCacheRepo + subscribeRepo *cache.SubscribeEmailCacheRepo + challengeRepo *cache.ChallengeScoreCacheRepo + specialAwardsRepo *cache.SpecialAwardsCacheRepo } -func NewSubscribeService(subscribeRepo *cache.SubscribeEmailCacheRepo, challengeRepo *cache.ChallengeScoreCacheRepo) *SubscribeService { - return &SubscribeService{subscribeRepo: subscribeRepo, challengeRepo: challengeRepo} +func NewSubscribeService(subscribeRepo *cache.SubscribeEmailCacheRepo, challengeRepo *cache.ChallengeScoreCacheRepo, specialAwardsRepo *cache.SpecialAwardsCacheRepo) *SubscribeService { + return &SubscribeService{subscribeRepo: subscribeRepo, challengeRepo: challengeRepo, specialAwardsRepo: specialAwardsRepo} } func (svc *SubscribeService) SubscribeEmail(req *vo.CreateSubscribeEmailReq) error { @@ -28,9 +29,14 @@ func (svc *SubscribeService) SubscribeEmail(req *vo.CreateSubscribeEmailReq) err return err } -func (svc *SubscribeService) ChallengeScore(req vo.PageVO) (*dto.ChallengeScoreDTO, int64, error) { - offset, limit, _ := utils.ParsePage(req.Page, req.Size, req.Total) - res, total, err := svc.challengeRepo.FindByLimit(offset, limit) +func (svc *SubscribeService) ChallengeScore(req vo.ChallengeScoreReq) (*dto.ChallengeScoreDTO, int64, error) { + var ( + offset, limit int + ) + if req.TeamName == "" { + offset, limit, _ = utils.ParsePage(req.Page.Page, req.Page.Size, req.Page.Total) + } + res, total, err := svc.challengeRepo.FindAll() if err != nil { return nil, 0, err } @@ -47,13 +53,62 @@ func (svc *SubscribeService) ChallengeScore(req vo.PageVO) (*dto.ChallengeScoreD scores = append(scores, scoreRank) } sort.Sort(&scores) - if offset+limit >= len(res) { - challengeScore.ScoreRank = scores[offset:len(res)] + + if req.TeamName != "" { + index := -1 + for i, v := range scores { + if v.TeamName == req.TeamName { + index = i + break + } + } + if index == -1 { + return nil, 0, nil + } + + page := index / req.Page.Size + req.Page.Page = page + 1 + offset, limit, _ = utils.ParsePage(page+1, req.Page.Size, true) + if offset+limit >= len(res) { + challengeScore.ScoreRank = scores[offset:len(res)] + } else { + challengeScore.ScoreRank = scores[offset : offset+limit] + } + return &challengeScore, total, nil } else { - challengeScore.ScoreRank = scores[offset : offset+limit] + if offset+limit >= len(res) { + challengeScore.ScoreRank = scores[offset:len(res)] + } else { + challengeScore.ScoreRank = scores[offset : offset+limit] + } + return &challengeScore, total, nil } - return &challengeScore, total, nil } else { return nil, 0, errors.New("no data in db") } } + +func (svc *SubscribeService) SpecialAwards() (*dto.ChallengeScoreDTO, error) { + res, _, err := svc.specialAwardsRepo.FindAll() + if err != nil { + return nil, err + } + var challengeScore dto.ChallengeScoreDTO + if len(res) > 0 { + challengeScore.UpdateTime = res[0].UpdateTime + var scores dto.Scores + for _, v := range res { + var scoreRank dto.ScoreRank + scoreRank.Rank = v.Rank + scoreRank.TeamName = v.TeamName + scoreRank.TaskCompleted = v.TaskCompleted + scoreRank.FinalScore = v.FinalScore + scores = append(scores, scoreRank) + } + sort.Sort(&scores) + challengeScore.ScoreRank = scores + return &challengeScore, nil + } else { + return nil, errors.New("no data in db") + } +}