feat: account.
This commit is contained in:
@ -33,6 +33,12 @@ func (tc *StepTrainingController) CreateTrainingRecord(c *gin.Context) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
username, exists := c.Get("username")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息,请重新登录"})
|
||||
return
|
||||
}
|
||||
record.Username = username.(string)
|
||||
|
||||
// 使用事务保存数据[4](@ref)
|
||||
err := tc.DB.Transaction(func(tx *gorm.DB) error {
|
||||
@ -123,6 +129,11 @@ func (tc *StepTrainingController) GetTrainingRecords(c *gin.Context) {
|
||||
PageNum int `form:"pageNum,default=1"` // 页码,默认第一页
|
||||
PageSize int `form:"pageSize,default=10"` // 每页数量,默认10条
|
||||
}
|
||||
username, exists := c.Get("username")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "无法获取用户信息,请重新登录"})
|
||||
return
|
||||
}
|
||||
|
||||
var params PaginationParams
|
||||
if err := c.ShouldBindQuery(¶ms); err != nil {
|
||||
@ -147,13 +158,13 @@ func (tc *StepTrainingController) GetTrainingRecords(c *gin.Context) {
|
||||
)
|
||||
|
||||
// 获取总记录数
|
||||
if err := tc.DB.Model(&models.StepTrainRecord{}).Count(&totalRows).Error; err != nil {
|
||||
if err := tc.DB.Model(&models.StepTrainRecord{}).Where("username = ?", username).Count(&totalRows).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取记录总数失败"})
|
||||
return
|
||||
}
|
||||
|
||||
// 查询分页数据(按开始时间倒序排列)
|
||||
result := tc.DB.
|
||||
result := tc.DB.Where("username = ?", username).
|
||||
Order("start_time DESC"). // 按开始时间倒序
|
||||
Offset(offset).
|
||||
Limit(params.PageSize).
|
||||
|
||||
Reference in New Issue
Block a user