修改入口文件main.go,更新了用户处理器和考试服务的逻辑,同时修改了成绩服务的实现。请注意,config.yaml文件有未暂存的更改,需要先将其添加到暂存区才能提交。

This commit is contained in:
2026-03-20 22:02:30 +08:00
parent 94d22e7cf0
commit c0fc1ea13a
4 changed files with 28 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
)
func main() {
@@ -27,6 +29,28 @@ func main() {
// 全局中间件
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)

View File

@@ -1,7 +1,6 @@
package handler
import (
"exam_registration/internal/middleware"
"exam_registration/internal/service"
"exam_registration/pkg/response"
"github.com/gin-gonic/gin"

View File

@@ -4,7 +4,7 @@ import (
"exam_registration/internal/dao"
"exam_registration/internal/model"
"errors"
"fmt"
"gorm.io/gorm"
"time"
)
@@ -57,7 +57,7 @@ func (s *ExamService) DeleteExam(id uint64) 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 {
return err
}

View File

@@ -4,6 +4,7 @@ import (
"exam_registration/internal/dao"
"exam_registration/internal/model"
"errors"
"gorm.io/gorm"
)
type ScoreService struct{}
@@ -22,7 +23,7 @@ func (s *ScoreService) CreateScore(score *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 {
scores[i].Pass = scores[i].Score >= 60