✨ 完善基础架构,新增部分问题接口
parent
761c24efbf
commit
326118eefb
@ -0,0 +1,33 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"online_code/core"
|
||||||
|
"online_code/models/param"
|
||||||
|
"online_code/repository"
|
||||||
|
"online_code/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
type problemApi struct{}
|
||||||
|
|
||||||
|
func ProblemApi() *problemApi {
|
||||||
|
return &problemApi{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (problemApi) GetProbleList(ctx *gin.Context) {
|
||||||
|
var p param.GetProblemList
|
||||||
|
if err := ctx.ShouldBind(&p); err != nil {
|
||||||
|
core.R(ctx).FailWithMessage("参数错误: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
records, count, err := repository.ProblemService().GetList(p)
|
||||||
|
if err != nil {
|
||||||
|
core.R(ctx).FailWithMessage("获取题目列表失败: " + err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 计算总页码
|
||||||
|
totalPage := utils.GenTotalPage(count, p.Size)
|
||||||
|
// 返回结果
|
||||||
|
core.R(ctx).OkWithData(core.PageData{Current: p.Current, Size: p.Size, Total: count, TotalPage: totalPage, Records: records})
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
app:
|
||||||
|
port: 8083
|
||||||
|
name: online_code
|
||||||
|
version: v1
|
||||||
|
prefix: /api/v1
|
||||||
|
|
||||||
|
|
||||||
|
mysql:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 3306
|
||||||
|
user: root
|
||||||
|
password: root
|
||||||
|
db: online_code
|
||||||
|
|
||||||
|
redis:
|
||||||
|
host: 127.0.0.1
|
||||||
|
port: 6379
|
||||||
|
password: loser123
|
||||||
|
db: 0
|
@ -0,0 +1,6 @@
|
|||||||
|
package define
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultCurrent = "1"
|
||||||
|
DefaultSize = "10"
|
||||||
|
)
|
@ -0,0 +1,50 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"git.echol.cn/loser/logger/log"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"golang.org/x/sync/errgroup"
|
||||||
|
"net/http"
|
||||||
|
"online_code/config"
|
||||||
|
"online_code/core"
|
||||||
|
"online_code/route"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var g errgroup.Group
|
||||||
|
|
||||||
|
// 启动入口
|
||||||
|
func main() {
|
||||||
|
// 强制日志颜色化
|
||||||
|
gin.ForceConsoleColor()
|
||||||
|
app := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", config.Scd.Api.Port),
|
||||||
|
Handler: api(),
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
WriteTimeout: 1 * time.Minute,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 启动项目
|
||||||
|
g.Go(func() error {
|
||||||
|
return app.ListenAndServe()
|
||||||
|
})
|
||||||
|
if err := g.Wait(); err != nil {
|
||||||
|
log.Panicf("启动失败,错误信息:%s", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成接口服务
|
||||||
|
func api() http.Handler {
|
||||||
|
app := gin.New()
|
||||||
|
app.Use(gin.Recovery())
|
||||||
|
// 开启自定义请求方式不允许处理函数
|
||||||
|
app.HandleMethodNotAllowed = true
|
||||||
|
// 处理请求方式不对
|
||||||
|
app.NoMethod(core.NoMethodHandler())
|
||||||
|
// 404返回数据
|
||||||
|
app.NoRoute(core.NoRouteHandler())
|
||||||
|
// 初始化路由
|
||||||
|
route.InitRoute(app.Group(config.Scd.Api.Prefix))
|
||||||
|
return app
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package cache
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
// UserInfo 登录用的用户信息结构体
|
||||||
|
type UserInfo struct {
|
||||||
|
UserType string `json:"userType"` // 用户类型
|
||||||
|
UserId string `json:"userId"` // 用户Id
|
||||||
|
}
|
||||||
|
|
||||||
|
// String 实现Stringer接口
|
||||||
|
func (i UserInfo) String() (string, error) {
|
||||||
|
b, err := json.Marshal(i)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(b), nil
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package param
|
||||||
|
|
||||||
|
// 分页通用参数
|
||||||
|
type page struct {
|
||||||
|
Current int `json:"current" form:"current" binding:"required"` // 页码
|
||||||
|
Size int `json:"size" form:"size" binding:"required"` // 每页数量
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package param
|
||||||
|
|
||||||
|
type GetProblemList struct {
|
||||||
|
page
|
||||||
|
Keyword string `json:"keyword" form:"keyword"` // 问题关键字
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import "gorm.io/gorm"
|
||||||
|
|
||||||
|
// 分页组件
|
||||||
|
func page(current, size int) func(db *gorm.DB) *gorm.DB {
|
||||||
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
if current == 0 {
|
||||||
|
current = 1
|
||||||
|
}
|
||||||
|
if size < 1 {
|
||||||
|
size = 10
|
||||||
|
}
|
||||||
|
// 计算偏移量
|
||||||
|
offset := (current - 1) * size
|
||||||
|
// 返回组装结果
|
||||||
|
return db.Offset(offset).Limit(size)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"online_code/client"
|
||||||
|
"online_code/models/entity"
|
||||||
|
"online_code/models/param"
|
||||||
|
)
|
||||||
|
|
||||||
|
type problemService struct{}
|
||||||
|
|
||||||
|
func ProblemService() *problemService {
|
||||||
|
return &problemService{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetList 获取题目列表
|
||||||
|
func (problemService) GetList(p param.GetProblemList) (records entity.Problem, count int64, err error) {
|
||||||
|
sel := client.MySQL.Scopes(page(p.Current, p.Size))
|
||||||
|
|
||||||
|
if p.Keyword != "" {
|
||||||
|
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetProblemInfo 获取题目详情
|
||||||
|
func (problemService) GetProblemInfo(id int) (problem entity.Problem, err error) {
|
||||||
|
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
|
||||||
|
return
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package route
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"online_code/api"
|
||||||
|
)
|
||||||
|
|
||||||
|
func problem(g *gin.RouterGroup) {
|
||||||
|
g.GET("/list", api.ProblemApi().GetProbleList)
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package route
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func InitRoute(g *gin.RouterGroup) {
|
||||||
|
api := g.Group("/api")
|
||||||
|
problem(api.Group("/problem"))
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
// GenTotalPage 计算总页数
|
||||||
|
func GenTotalPage(count int64, size int) int {
|
||||||
|
totalPage := 0
|
||||||
|
if count > 0 {
|
||||||
|
upPage := 0
|
||||||
|
if int(count)%size > 0 {
|
||||||
|
upPage = 1
|
||||||
|
}
|
||||||
|
totalPage = (int(count) / size) + upPage
|
||||||
|
}
|
||||||
|
return totalPage
|
||||||
|
}
|
Loading…
Reference in New Issue