Compare commits
No commits in common. "6945e6db93240c959b29cf208a1bd72375ab1ab8" and "2d467c710fd0335d8f2814b92b0c4ef8aafe28d8" have entirely different histories.
6945e6db93
...
2d467c710f
2
.env
2
.env
@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
LOG_FILE_ENABLE=1
|
|
||||||
# Nacos配置
|
# Nacos配置
|
||||||
NACOS_HOST=127.0.0.1
|
NACOS_HOST=127.0.0.1
|
||||||
NACOS_PORT=8848
|
NACOS_PORT=8848
|
||||||
|
@ -13,21 +13,11 @@ var MySQL *gorm.DB
|
|||||||
|
|
||||||
func InitMySQLClient() {
|
func InitMySQLClient() {
|
||||||
// 创建连接对象
|
// 创建连接对象
|
||||||
mysqlConfig := mysql.Config{
|
conn, err := gorm.Open(mysql.Open(config.Scd.MySQL.GetDSN()), &gorm.Config{Logger: logger.DefaultGormLogger()})
|
||||||
DSN: config.Scd.MySQL.GetDSN(),
|
|
||||||
DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式
|
|
||||||
DontSupportRenameColumn: true, // 用 `change` 重命名列
|
|
||||||
}
|
|
||||||
conn, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{Logger: logger.DefaultGormLogger()})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("初始化MySQL连接失败, 错误信息: %v", err)
|
log.Panic("初始化MySQL连接失败, 错误信息: %v", err)
|
||||||
} else {
|
} else {
|
||||||
log.Debug("MySQL连接成功")
|
log.Debug("MySQL连接成功")
|
||||||
}
|
}
|
||||||
MySQL = conn
|
MySQL = conn
|
||||||
|
|
||||||
//db, err := conn.DB()
|
|
||||||
//db.SetMaxIdleConns(10) // 用于设置连接池中空闲连接的最大数量
|
|
||||||
//db.SetMaxOpenConns(100) // 用于设置数据库连接的最大打开数量
|
|
||||||
//db.SetConnMaxLifetime(time.Hour) // 设置连接的最大存活时间
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// BaseDbModel 数据库通用字段
|
// BaseDbModel 数据库通用字段
|
||||||
type BaseDbModel struct {
|
type BaseDbModel struct {
|
||||||
ID uint `gorm:"type:int(11);primarykey;" json:"id"`
|
Id string `json:"id" gorm:"type:varchar(50);primarykey"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt DateTime `json:"createdAt"`
|
||||||
UpdatedAt time.Time `json:"updated_at"`
|
UpdatedAt DateTime `json:"updatedAt"`
|
||||||
DeletedAt gorm.DeletedAt `json:"-" gorm:"index:deleted"`
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index:deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
/*// BeforeCreate 创建数据库对象之前生成UUID
|
// BeforeCreate 创建数据库对象之前生成UUID
|
||||||
func (m *BaseDbModel) BeforeCreate(*gorm.DB) (err error) {
|
func (m *BaseDbModel) BeforeCreate(*gorm.DB) (err error) {
|
||||||
m.Id = uuid.New().String()
|
m.Id = uuid.New().String()
|
||||||
return
|
return
|
||||||
}*/
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
api:
|
app:
|
||||||
port: 8083
|
port: 8083
|
||||||
name: online_code
|
name: online_code
|
||||||
version: v1
|
version: v1
|
||||||
@ -15,5 +15,5 @@ mysql:
|
|||||||
redis:
|
redis:
|
||||||
host: 127.0.0.1
|
host: 127.0.0.1
|
||||||
port: 6379
|
port: 6379
|
||||||
password: loser7659
|
password: loser123
|
||||||
db: 0
|
db: 0
|
@ -9,12 +9,12 @@ import (
|
|||||||
// 初始化数据库表
|
// 初始化数据库表
|
||||||
func databaseTable() {
|
func databaseTable() {
|
||||||
dbs := []any{
|
dbs := []any{
|
||||||
new(entity.UserBasic),
|
new(entity.User),
|
||||||
new(entity.TestCase),
|
new(entity.Category),
|
||||||
new(entity.CategoryBasic),
|
new(entity.Problem),
|
||||||
new(entity.ProblemCategory),
|
new(entity.ProblemCategory),
|
||||||
//new(entity.ProblemBasic),
|
new(entity.Submit),
|
||||||
//new(entity.SubmitBasic),
|
new(entity.TestCase),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := client.MySQL.AutoMigrate(dbs...); err != nil {
|
if err := client.MySQL.AutoMigrate(dbs...); err != nil {
|
||||||
|
7
main.go
7
main.go
@ -2,25 +2,18 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.echol.cn/loser/logger"
|
|
||||||
"git.echol.cn/loser/logger/log"
|
"git.echol.cn/loser/logger/log"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"net/http"
|
"net/http"
|
||||||
"online_code/config"
|
"online_code/config"
|
||||||
"online_code/core"
|
"online_code/core"
|
||||||
"online_code/initialize"
|
|
||||||
"online_code/route"
|
"online_code/route"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var g errgroup.Group
|
var g errgroup.Group
|
||||||
|
|
||||||
func init() {
|
|
||||||
logger.InitLogger(logger.LogConfig{Mode: logger.Dev, LokiEnable: false, FileEnable: true})
|
|
||||||
initialize.InitSystem() // 初始化系统配置
|
|
||||||
}
|
|
||||||
|
|
||||||
// 启动入口
|
// 启动入口
|
||||||
func main() {
|
func main() {
|
||||||
// 强制日志颜色化
|
// 强制日志颜色化
|
||||||
|
@ -2,14 +2,14 @@ package entity
|
|||||||
|
|
||||||
import "online_code/common/types"
|
import "online_code/common/types"
|
||||||
|
|
||||||
// CategoryBasic 分类表
|
// Category 分类表
|
||||||
type CategoryBasic struct {
|
type Category struct {
|
||||||
types.BaseDbModel
|
types.BaseDbModel
|
||||||
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'问题表的唯一标识'"`
|
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'分类的唯一标识'"`
|
||||||
Name string `json:"name" gorm:"column:name;type:varchar(100);comment:'分类名称'" `
|
Name string `json:"name" gorm:"column:name;type:varchar(100);comment:'分类名称'" `
|
||||||
ParentId int `json:"parent_id" gorm:"column:parent_id;type:int(11);comment:'父级ID'" `
|
ParentId int `json:"parent_id" gorm:"column:parent_id;type:int(11);comment:'父级ID'" `
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *CategoryBasic) TableName() string {
|
func (table *Category) TableName() string {
|
||||||
return "category_basic"
|
return "category"
|
||||||
}
|
}
|
21
models/entity/problem.go
Normal file
21
models/entity/problem.go
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
import "online_code/common/types"
|
||||||
|
|
||||||
|
// Problem 问题表
|
||||||
|
type Problem struct {
|
||||||
|
types.BaseDbModel
|
||||||
|
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'问题表的唯一标识'" ` // 问题表的唯一标识
|
||||||
|
ProblemCategories []*ProblemCategory `json:"problem_categories" gorm:"foreignKey:problem_id;references:id;comment:'关联问题分类表'" ` // 关联问题分类表
|
||||||
|
Title string `json:"title" gorm:"column:title;type:varchar(255);comment:'文章标题'" ` // 文章标题
|
||||||
|
Content string `json:"content" gorm:"column:content;type:text;comment:'文章正文'" ` // 文章正文
|
||||||
|
MaxRuntime int `json:"max_runtime" gorm:"column:max_runtime;type:int(11);comment:'最大运行时长'" ` // 最大运行时长
|
||||||
|
MaxMem int `json:"max_mem" gorm:"column:max_mem;type:int(11);comment:'最大运行内存'" ` // 最大运行内存
|
||||||
|
TestCases []*TestCase `json:"test_cases" gorm:"foreignKey:problem_identity;references:identity;comment:'关联测试用例表'" ` // 关联测试用例表
|
||||||
|
PassNum int64 `json:"pass_num" gorm:"column:pass_num;type:int(11);comment:'通过次数'" ` // 通过次数
|
||||||
|
SubmitNum int64 `json:"submit_num" gorm:"column:submit_num;type:int(11);comment:'提交次数'" ` // 提交次数
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Problem) TableName() string {
|
||||||
|
return "problem_basic"
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
package entity
|
|
||||||
|
|
||||||
import "online_code/common/types"
|
|
||||||
|
|
||||||
// ProblemBasic 问题基础表
|
|
||||||
type ProblemBasic struct {
|
|
||||||
types.BaseDbModel
|
|
||||||
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'问题表的唯一标识'"` // 问题表的唯一标识
|
|
||||||
ProblemCategories []ProblemCategory `json:"problem_categories" gorm:"ForeignKey:ProblemId;comment:'关联问题分类表'"` // 关联问题分类表
|
|
||||||
Title string `json:"title" gorm:"column:title;type:varchar(255);comment:'文章标题'"` // 文章标题
|
|
||||||
Content string `json:"content" gorm:"column:content;type:text;comment:'文章正文'"` // 文章正文
|
|
||||||
MaxRuntime int `json:"max_runtime" gorm:"column:max_runtime;type:int(11);comment:'最大运行时长'"` // 最大运行时长
|
|
||||||
MaxMem int `json:"max_mem" gorm:"column:max_mem;type:int(11);comment:'最大运行内存'"` // 最大运行内存
|
|
||||||
TestCases []TestCase `json:"test_cases" gorm:"foreignKey:ProblemIdentity;comment:'关联测试用例表'"` // 关联测试用例表
|
|
||||||
PassNum int64 `json:"pass_num" gorm:"column:pass_num;type:int(11);comment:'通过次数'"` // 通过次数
|
|
||||||
SubmitNum int64 `json:"submit_num" gorm:"column:submit_num;type:int(11);comment:'提交次数'"` // 提交次数
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *ProblemBasic) TableName() string {
|
|
||||||
return "problem_basic"
|
|
||||||
}
|
|
@ -9,7 +9,7 @@ type ProblemCategory struct {
|
|||||||
types.BaseDbModel
|
types.BaseDbModel
|
||||||
ProblemId uint `json:"problem_id" gorm:"column:problem_id;type:int(11);comment:'问题的ID'" ` // 问题的ID
|
ProblemId uint `json:"problem_id" gorm:"column:problem_id;type:int(11);comment:'问题的ID'" ` // 问题的ID
|
||||||
CategoryId uint `json:"category_id" gorm:"column:category_id;type:int(11);comment:'分类的ID'" ` // 分类的ID
|
CategoryId uint `json:"category_id" gorm:"column:category_id;type:int(11);comment:'分类的ID'" ` // 分类的ID
|
||||||
CategoryBasic *CategoryBasic `json:"category_basic" gorm:"foreignKey:CategoryId;comment:'关联分类的基础信息表'" ` // 关联分类的基础信息表
|
CategoryBasic *Category `json:"category_basic" gorm:"foreignKey:id;references:category_id;comment:'关联分类的基础信息表'" ` // 关联分类的基础信息表
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *ProblemCategory) TableName() string {
|
func (table *ProblemCategory) TableName() string {
|
||||||
|
16
models/entity/submit.go
Normal file
16
models/entity/submit.go
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package entity
|
||||||
|
|
||||||
|
// Submit 提交表
|
||||||
|
type Submit struct {
|
||||||
|
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'用户唯一标识'" ` // 唯一标识
|
||||||
|
ProblemIdentity string `json:"problem_identity" gorm:"column:problem_identity;type:varchar(36);comment:'问题表的唯一标识'" ` // 问题表的唯一标识
|
||||||
|
ProblemBasic *Problem `json:"problem_basic" gorm:"foreignKey:identity;references:problem_identity;comment:'关联问题基础表'" ` // 关联问题基础表
|
||||||
|
UserIdentity string `json:"user_identity" gorm:"column:user_identity;type:varchar(36);comment:'用户表的唯一标识'" ` // 用户表的唯一标识
|
||||||
|
UserBasic *User `json:"user_basic" gorm:"foreignKey:identity;references:user_identity;comment:'联用户基础表'" ` // 关联用户基础表
|
||||||
|
Path string `json:"path" gorm:"column:path;type:varchar(255);comment:'代码存放路径'" ` // 代码存放路径
|
||||||
|
Status int `json:"status" gorm:"column:status;type:tinyint(1);comment:'状态'" ` // 【-1-待判断,1-答案正确,2-答案错误,3-运行超时,4-运行超内存, 5-编译错误】
|
||||||
|
}
|
||||||
|
|
||||||
|
func (table *Submit) TableName() string {
|
||||||
|
return "submit"
|
||||||
|
}
|
@ -1,19 +0,0 @@
|
|||||||
package entity
|
|
||||||
|
|
||||||
import "online_code/common/types"
|
|
||||||
|
|
||||||
// SubmitBasic 提交表
|
|
||||||
type SubmitBasic struct {
|
|
||||||
types.BaseDbModel
|
|
||||||
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'用户唯一标识'" ` // 唯一标识
|
|
||||||
ProblemIdentity string `json:"problem_identity" gorm:"column:problem_identity;type:varchar(36);comment:'问题表的唯一标识'" ` // 问题表的唯一标识
|
|
||||||
ProblemBasic *ProblemBasic `json:"problem_basic" gorm:"foreignKey:ProblemIdentity;comment:'关联问题基础表'" ` // 关联问题基础表
|
|
||||||
UserIdentity string `json:"user_identity" gorm:"column:user_identity;type:varchar(36);comment:'用户表的唯一标识'" ` // 用户表的唯一标识
|
|
||||||
UserBasic *UserBasic `json:"user_basic" gorm:"foreignKey:UserIdentity;comment:'联用户基础表'" ` // 关联用户基础表
|
|
||||||
Path string `json:"path" gorm:"column:path;type:varchar(255);comment:'代码存放路径'" ` // 代码存放路径
|
|
||||||
Status int `json:"status" gorm:"column:status;type:tinyint(1);comment:'状态'" ` // 【-1-待判断,1-答案正确,2-答案错误,3-运行超时,4-运行超内存, 5-编译错误】
|
|
||||||
}
|
|
||||||
|
|
||||||
func (table *SubmitBasic) TableName() string {
|
|
||||||
return "submit_basic"
|
|
||||||
}
|
|
@ -5,8 +5,8 @@ import "online_code/common/types"
|
|||||||
// TestCase 测试用例
|
// TestCase 测试用例
|
||||||
type TestCase struct {
|
type TestCase struct {
|
||||||
types.BaseDbModel
|
types.BaseDbModel
|
||||||
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'测试用例唯一标识''" `
|
Identity string `json:"identity" gorm:"column:identity;type:varchar(255);comment:'用户唯一标识''" `
|
||||||
ProblemIdentity string `json:"problem_identity" gorm:"column:problem_identity;type:varchar(36);comment:'问题'" `
|
ProblemIdentity string `json:"problem_identity" gorm:"column:problem_identity;type:varchar(255);comment:'问题'" `
|
||||||
Input string `json:"input" gorm:"column:input;type:text;" `
|
Input string `json:"input" gorm:"column:input;type:text;" `
|
||||||
Output string `json:"output" gorm:"column:output;type:text;" `
|
Output string `json:"output" gorm:"column:output;type:text;" `
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ package entity
|
|||||||
|
|
||||||
import "online_code/common/types"
|
import "online_code/common/types"
|
||||||
|
|
||||||
type UserBasic struct {
|
type User struct {
|
||||||
types.BaseDbModel
|
types.BaseDbModel
|
||||||
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'用户唯一标识'"`
|
Identity string `json:"identity" gorm:"column:identity;type:varchar(36);comment:'用户唯一标识'"`
|
||||||
Name string `json:"name" gorm:"column:name;type:varchar(100);not null;comment:'用户名'"`
|
Name string `json:"name" gorm:"column:name;type:varchar(100);not null;comment:'用户名'"`
|
||||||
@ -14,6 +14,6 @@ type UserBasic struct {
|
|||||||
IsAdmin int `json:"is_admin" gorm:"column:is_admin;type:tinyint(1);comment:'是否是管理员【0-否,1-是】'"`
|
IsAdmin int `json:"is_admin" gorm:"column:is_admin;type:tinyint(1);comment:'是否是管理员【0-否,1-是】'"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (table *UserBasic) TableName() string {
|
func (table *User) TableName() string {
|
||||||
return "user_basic"
|
return "user"
|
||||||
}
|
}
|
@ -3,5 +3,4 @@ package param
|
|||||||
type GetProblemList struct {
|
type GetProblemList struct {
|
||||||
page
|
page
|
||||||
Keyword string `json:"keyword" form:"keyword"` // 问题关键字
|
Keyword string `json:"keyword" form:"keyword"` // 问题关键字
|
||||||
CategoryIdentity string `json:"category_identity" form:"category_identity"` // 问题分类标识
|
|
||||||
}
|
}
|
||||||
|
@ -13,23 +13,19 @@ func ProblemService() *problemService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetList 获取题目列表
|
// GetList 获取题目列表
|
||||||
func (problemService) GetList(p param.GetProblemList) (records entity.ProblemBasic, count int64, err error) {
|
func (problemService) GetList(p param.GetProblemList) (records entity.Problem, count int64, err error) {
|
||||||
sel := client.MySQL.Scopes(page(p.Current, p.Size)).Preload("ProblemCategories").Preload("ProblemCategories.CategoryBasic")
|
sel := client.MySQL.Scopes(page(p.Current, p.Size))
|
||||||
|
|
||||||
if p.Keyword != "" {
|
if p.Keyword != "" {
|
||||||
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
|
sel.Where("title LIKE ? OR content like ?", "%"+p.Keyword+"%", "%"+p.Keyword+"%")
|
||||||
}
|
}
|
||||||
if p.CategoryIdentity != "" {
|
|
||||||
sel.Joins("RIGHT JOINN problem_category pc on pc.problem_id = problem.id").
|
|
||||||
Where("pc.category_id= (SELECT cb.id FROM category cb WHERE cb.identity = ?)", p.CategoryIdentity)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
|
err = sel.Order("updated_at DESC").Find(&records).Offset(-1).Limit(-1).Count(&count).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetProblemInfo 获取题目详情
|
// GetProblemInfo 获取题目详情
|
||||||
func (problemService) GetProblemInfo(id int) (problem entity.ProblemBasic, err error) {
|
func (problemService) GetProblemInfo(id int) (problem entity.Problem, err error) {
|
||||||
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
|
err = client.MySQL.Where("id = ?", id).Find(&problem).Error
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -3,5 +3,6 @@ package route
|
|||||||
import "github.com/gin-gonic/gin"
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func InitRoute(g *gin.RouterGroup) {
|
func InitRoute(g *gin.RouterGroup) {
|
||||||
problem(g.Group("/problem"))
|
api := g.Group("/api")
|
||||||
|
problem(api.Group("/problem"))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user