Skip to content

Commit

Permalink
Merge pull request #14 from coticom/main
Browse files Browse the repository at this point in the history
support team_name query
  • Loading branch information
coticom authored Mar 13, 2023
2 parents d243914 + eeca90d commit 54a5c3d
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 26 deletions.
18 changes: 16 additions & 2 deletions internal/app/api/rest/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
1 change: 1 addition & 0 deletions internal/app/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
12 changes: 7 additions & 5 deletions internal/app/initialization/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down
10 changes: 6 additions & 4 deletions internal/app/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions internal/app/model/entity/mysql_special_awards.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions internal/app/model/vo/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions internal/app/repository/cache/mysql_challenge_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
18 changes: 18 additions & 0 deletions internal/app/repository/cache/mysql_special_awards.go
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 2 additions & 2 deletions internal/app/repository/mysql_challenge_score.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type IChallengeScoreRepo interface {
FindByLimit(offset, limit int) ([]*entity.ChallengeScore, int64, error)
FindAll() ([]*entity.ChallengeScore, int64, error)
}

type ChallengeScoreRepo struct {
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions internal/app/repository/mysql_special_awards.go
Original file line number Diff line number Diff line change
@@ -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
}
77 changes: 66 additions & 11 deletions internal/app/service/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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")
}
}

0 comments on commit 54a5c3d

Please sign in to comment.