修改入口文件main.go,更新了用户处理器和考试服务的逻辑,同时修改了成绩服务的实现。请注意,config.yaml文件有未暂存的更改,需要先将其添加到暂存区才能提交。
This commit is contained in:
24
cmd/main.go
24
cmd/main.go
@@ -8,6 +8,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"log"
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -27,6 +29,28 @@ func main() {
|
|||||||
// 全局中间件
|
// 全局中间件
|
||||||
r.Use(middleware.Cors())
|
r.Use(middleware.Cors())
|
||||||
|
|
||||||
|
// 静态文件服务和前端路由回退处理
|
||||||
|
distPath := "frontend/dist"
|
||||||
|
if _, err := os.Stat(distPath); err == nil {
|
||||||
|
// dist 目录存在,提供静态文件服务
|
||||||
|
r.Static("/static", distPath)
|
||||||
|
|
||||||
|
// 前端路由回退处理(SPA 支持)
|
||||||
|
r.NoRoute(func(c *gin.Context) {
|
||||||
|
// API 路由未找到,返回 404
|
||||||
|
if len(c.Request.URL.Path) >= 4 && c.Request.URL.Path[:4] == "/api" {
|
||||||
|
c.JSON(http.StatusNotFound, gin.H{"error": "API not found"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 其他路由,返回前端 index.html(由 vue-router 处理)
|
||||||
|
c.File(distPath + "/index.html")
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// dist 目录不存在,提示需要构建前端
|
||||||
|
fmt.Println("⚠️ Frontend dist directory not found. Please build frontend first.")
|
||||||
|
fmt.Println(" Run: cd frontend && npm install && npm run build")
|
||||||
|
}
|
||||||
|
|
||||||
// 设置路由
|
// 设置路由
|
||||||
routes.SetupRouter(r)
|
routes.SetupRouter(r)
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"exam_registration/internal/middleware"
|
|
||||||
"exam_registration/internal/service"
|
"exam_registration/internal/service"
|
||||||
"exam_registration/pkg/response"
|
"exam_registration/pkg/response"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"exam_registration/internal/dao"
|
"exam_registration/internal/dao"
|
||||||
"exam_registration/internal/model"
|
"exam_registration/internal/model"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"gorm.io/gorm"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -57,7 +57,7 @@ func (s *ExamService) DeleteExam(id uint64) error {
|
|||||||
|
|
||||||
func (s *ExamService) UpdateExamStatus(id uint64, status int) error {
|
func (s *ExamService) UpdateExamStatus(id uint64, status int) error {
|
||||||
// 更新考试状态并同步更新相关报名记录的状态
|
// 更新考试状态并同步更新相关报名记录的状态
|
||||||
return dao.DB.Transaction(func(tx *dao.DB) error {
|
return dao.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.Model(&model.Exam{}).Where("id = ?", id).Update("status", status).Error; err != nil {
|
if err := tx.Model(&model.Exam{}).Where("id = ?", id).Update("status", status).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"exam_registration/internal/dao"
|
"exam_registration/internal/dao"
|
||||||
"exam_registration/internal/model"
|
"exam_registration/internal/model"
|
||||||
"errors"
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ScoreService struct{}
|
type ScoreService struct{}
|
||||||
@@ -22,7 +23,7 @@ func (s *ScoreService) CreateScore(score *model.ExamScore) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScoreService) BatchCreateScores(scores []model.ExamScore) error {
|
func (s *ScoreService) BatchCreateScores(scores []model.ExamScore) error {
|
||||||
return dao.DB.Transaction(func(tx *dao.DB) error {
|
return dao.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
for i := range scores {
|
for i := range scores {
|
||||||
scores[i].Pass = scores[i].Score >= 60
|
scores[i].Pass = scores[i].Score >= 60
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user