Skip to content

Commit

Permalink
chore: simplify function declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
ElaBosak233 committed Jun 8, 2024
1 parent 98c2002 commit 9fee47c
Show file tree
Hide file tree
Showing 43 changed files with 314 additions and 424 deletions.
18 changes: 0 additions & 18 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,24 +323,6 @@ const docTemplate = `{
}
},
"/challenges/{id}/attachment": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "查找附件",
"responses": {}
},
"post": {
"security": [
{
Expand Down
18 changes: 0 additions & 18 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,6 @@
}
},
"/challenges/{id}/attachment": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Challenge"
],
"summary": "查找附件",
"responses": {}
},
"post": {
"security": [
{
Expand Down
11 changes: 0 additions & 11 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1005,17 +1005,6 @@ paths:
summary: 删除附件
tags:
- Challenge
get:
consumes:
- application/json
produces:
- application/json
responses: {}
security:
- ApiKeyAuth: []
summary: 查找附件
tags:
- Challenge
post:
consumes:
- application/json
Expand Down
6 changes: 1 addition & 5 deletions internal/app/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"time"
)

var db *gorm.DB
Expand Down Expand Up @@ -105,11 +104,8 @@ func migrate() {
}

// selfCheck performs a self-check.
// It updates the removed_at field of the Pod table.
// If the removed_at field is greater than the current time, it is forcibly assigned the current time.
// This is to prevent subsequent program errors in judgment.
func selfCheck() {
db.Model(&model.Pod{}).Where("removed_at > ?", time.Now().UnixMilli()).Update("removed_at", time.Now().UnixMilli())
db.Exec("DELETE FROM pods")
}

func initAdmin() {
Expand Down
26 changes: 0 additions & 26 deletions internal/controller/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ type IChallengeController interface {
CreateFlag(ctx *gin.Context)
UpdateFlag(ctx *gin.Context)
DeleteFlag(ctx *gin.Context)
FindAttachment(ctx *gin.Context)
SaveAttachment(ctx *gin.Context)
DeleteAttachment(ctx *gin.Context)
}
Expand Down Expand Up @@ -262,31 +261,6 @@ func (c *ChallengeController) DeleteFlag(ctx *gin.Context) {
})
}

// FindAttachment
// @Summary 查找附件
// @Description
// @Tags Challenge
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Router /challenges/{id}/attachment [get]
func (c *ChallengeController) FindAttachment(ctx *gin.Context) {
id := convertor.ToUintD(ctx.Param("id"), 0)
filename, size, err := c.mediaService.FindChallengeAttachment(id)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": err.Error(),
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"filename": filename,
"size": size,
})
}

// SaveAttachment
// @Summary 保存附件
// @Description
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (g *GameController) Find(ctx *gin.Context) {
return
}
if ok && isEnabled.(bool) {
gameFindRequest.IsEnabled = convertor.TrueP()
gameFindRequest.IsEnabled = &utils.True
}
value, exist := cache.C().Get(fmt.Sprintf("games:%s", utils.HashStruct(gameFindRequest)))
if !exist {
Expand Down
28 changes: 7 additions & 21 deletions internal/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package controller
import (
"fmt"
"github.com/elabosak233/cloudsdale/internal/extension/cache"
"github.com/elabosak233/cloudsdale/internal/model"
"github.com/elabosak233/cloudsdale/internal/model/request"
"github.com/elabosak233/cloudsdale/internal/service"
"github.com/elabosak233/cloudsdale/internal/utils"
"github.com/elabosak233/cloudsdale/internal/utils/convertor"
"github.com/elabosak233/cloudsdale/internal/utils/validator"
ginI18n "github.com/gin-contrib/i18n"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"net/http"
"strings"
"time"
)

Expand Down Expand Up @@ -57,31 +56,18 @@ func (c *UserController) Login(ctx *gin.Context) {
})
return
}
var user model.User
users, total, err := c.userService.Find(request.UserFindRequest{
Username: strings.ToLower(userLoginRequest.Username),
})
if err == nil && total > 0 {
user = users[0]
} else {
ctx.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
})
return
}
if !c.userService.VerifyPasswordByUsername(userLoginRequest.Username, userLoginRequest.Password) {
user, token, err := c.userService.Login(userLoginRequest)
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
"msg": "用户名或密码错误",
"msg": ginI18n.MustGetMessage(ctx, err.Error()),
})
zap.L().Warn(fmt.Sprintf("User %s login failed", user.Username), zap.Uint("user_id", user.ID))
return
}
_ = c.userService.Update(request.UserUpdateRequest{
err = c.userService.Update(request.UserUpdateRequest{
ID: user.ID,
RemoteIP: ctx.RemoteIP(),
})
tokenString, err := c.userService.GetJwtTokenByID(user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"code": http.StatusInternalServerError,
Expand All @@ -92,7 +78,7 @@ func (c *UserController) Login(ctx *gin.Context) {
ctx.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"data": user,
"token": tokenString,
"token": token,
})
zap.L().Info(fmt.Sprintf("User %s login successful", user.Username), zap.Uint("user_id", user.ID))
}
Expand All @@ -106,7 +92,7 @@ func (c *UserController) Login(ctx *gin.Context) {
// @Security ApiKeyAuth
// @Router /users/logout [post]
func (c *UserController) Logout(ctx *gin.Context) {
id, err := c.userService.GetIDByJwtToken(ctx.GetHeader("Authorization"))
id, err := c.userService.Logout(ctx.GetHeader("Authorization"))
if err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"code": http.StatusBadRequest,
Expand Down
10 changes: 9 additions & 1 deletion internal/files/i18n/en.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
welcome: welcome
welcome: "welcome"

user:
not_found: "user not found"
login:
password_incorrect: "password incorrect"

team:
not_found: "team not found"
10 changes: 9 additions & 1 deletion internal/files/i18n/zh-Hans.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
welcome: 欢迎
welcome: "欢迎"

user:
not_found: "用户不存在"
login:
password_incorrect: "密码错误"

team:
not_found: "团队不存在"
11 changes: 6 additions & 5 deletions internal/repository/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
)

type ICategoryRepository interface {
Create(category model.Category) (err error)
Update(category model.Category) (err error)
Find(req request.CategoryFindRequest) (categories []model.Category, err error)
Delete(id uint) (err error)
Create(category model.Category) error
Update(category model.Category) error
Find(req request.CategoryFindRequest) ([]model.Category, error)
Delete(id uint) error
}

type CategoryRepository struct {
Expand All @@ -31,7 +31,8 @@ func (t *CategoryRepository) Update(category model.Category) (err error) {
return result.Error
}

func (t *CategoryRepository) Find(req request.CategoryFindRequest) (categories []model.Category, err error) {
func (t *CategoryRepository) Find(req request.CategoryFindRequest) ([]model.Category, error) {
var categories []model.Category
applyFilters := func(db *gorm.DB) *gorm.DB {
if req.ID != 0 {
db = db.Where("id = ?", req.ID)
Expand Down
9 changes: 5 additions & 4 deletions internal/repository/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewChallengeRepository(db *gorm.DB) IChallengeRepository {
return &ChallengeRepository{db: db}
}

func (t *ChallengeRepository) Create(challenge model.Challenge) (c model.Challenge, err error) {
func (t *ChallengeRepository) Create(challenge model.Challenge) (model.Challenge, error) {
result := t.db.Table("challenges").Create(&challenge)
return challenge, result.Error
}
Expand All @@ -31,12 +31,13 @@ func (t *ChallengeRepository) Delete(id uint) (err error) {
return result.Error
}

func (t *ChallengeRepository) Update(challenge model.Challenge) (c model.Challenge, err error) {
func (t *ChallengeRepository) Update(challenge model.Challenge) (model.Challenge, error) {
result := t.db.Table("challenges").Model(&challenge).Updates(&challenge)
return challenge, result.Error
}

func (t *ChallengeRepository) Find(req request.ChallengeFindRequest) (challenges []model.Challenge, total int64, err error) {
func (t *ChallengeRepository) Find(req request.ChallengeFindRequest) ([]model.Challenge, int64, error) {
var challenges []model.Challenge
applyFilter := func(q *gorm.DB) *gorm.DB {
if req.CategoryID != nil {
q = q.Where("category_id = ?", *(req.CategoryID))
Expand All @@ -59,7 +60,7 @@ func (t *ChallengeRepository) Find(req request.ChallengeFindRequest) (challenges
return q
}
db := applyFilter(t.db.Table("challenges"))

var total int64 = 0
result := db.Model(&model.Challenge{}).Count(&total)
if req.SortOrder != "" && req.SortKey != "" {
db = db.Order(req.SortKey + " " + req.SortOrder)
Expand Down
4 changes: 2 additions & 2 deletions internal/repository/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type IEnvRepository interface {
Create(env model.Env) (e model.Env, err error)
Create(env model.Env) (model.Env, error)
}

type EnvRepository struct {
Expand All @@ -17,7 +17,7 @@ func NewEnvRepository(db *gorm.DB) IEnvRepository {
return &EnvRepository{db: db}
}

func (t *EnvRepository) Create(env model.Env) (e model.Env, err error) {
func (t *EnvRepository) Create(env model.Env) (model.Env, error) {
result := t.db.Table("envs").
Create(&env)
return env, result.Error
Expand Down
12 changes: 6 additions & 6 deletions internal/repository/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

type IFlagRepository interface {
Create(flag model.Flag) (f model.Flag, err error)
Update(flag model.Flag) (f model.Flag, err error)
Delete(flag model.Flag) (err error)
Create(flag model.Flag) (model.Flag, error)
Update(flag model.Flag) (model.Flag, error)
Delete(flag model.Flag) error
}

type FlagRepository struct {
Expand All @@ -19,17 +19,17 @@ func NewFlagRepository(db *gorm.DB) IFlagRepository {
return &FlagRepository{db: db}
}

func (t *FlagRepository) Create(flag model.Flag) (f model.Flag, err error) {
func (t *FlagRepository) Create(flag model.Flag) (model.Flag, error) {
result := t.db.Table("flags").Create(&flag)
return flag, result.Error
}

func (t *FlagRepository) Update(flag model.Flag) (f model.Flag, err error) {
func (t *FlagRepository) Update(flag model.Flag) (model.Flag, error) {
result := t.db.Table("flags").Model(&flag).Updates(&flag)
return flag, result.Error
}

func (t *FlagRepository) Delete(flag model.Flag) (err error) {
func (t *FlagRepository) Delete(flag model.Flag) error {
result := t.db.Table("flags").Delete(&flag)
return result.Error
}
9 changes: 5 additions & 4 deletions internal/repository/flag_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

type IFlagGenRepository interface {
Create(flag model.FlagGen) (f model.FlagGen, err error)
FindByPodID(podIDs []uint) (flags []model.FlagGen, err error)
Create(flag model.FlagGen) (model.FlagGen, error)
FindByPodID(podIDs []uint) ([]model.FlagGen, error)
}

type FlagGenRepository struct {
Expand All @@ -18,12 +18,13 @@ func NewFlagGenRepository(db *gorm.DB) IFlagGenRepository {
return &FlagGenRepository{db: db}
}

func (t *FlagGenRepository) Create(flag model.FlagGen) (f model.FlagGen, err error) {
func (t *FlagGenRepository) Create(flag model.FlagGen) (model.FlagGen, error) {
result := t.db.Table("flag_gens").Create(&flag)
return flag, result.Error
}

func (t *FlagGenRepository) FindByPodID(podIDs []uint) (flags []model.FlagGen, err error) {
func (t *FlagGenRepository) FindByPodID(podIDs []uint) ([]model.FlagGen, error) {
var flags []model.FlagGen
result := t.db.Table("flag_gens").
Where("pod_id IN ?", podIDs).
Find(&flags)
Expand Down
Loading

0 comments on commit 9fee47c

Please sign in to comment.