修改课程活动
This commit is contained in:
@@ -6,6 +6,8 @@ package course_activity
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_activity/internal/logic/course_activity"
|
||||
@@ -18,10 +20,19 @@ import (
|
||||
func DeleteCourseActivityHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteCourseActivityReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
pathParts := strings.Split(r.URL.Path, "/")
|
||||
if len(pathParts) < 3 { // 确保路径格式正确
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid path format"))
|
||||
return
|
||||
}
|
||||
idStr := pathParts[3]
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid meeting ID"))
|
||||
return
|
||||
}
|
||||
fmt.Println("idStr:", idStr)
|
||||
req.Id = int(id)
|
||||
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
|
||||
@@ -6,6 +6,8 @@ package course_activity
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/course_activity/internal/logic/course_activity"
|
||||
@@ -18,10 +20,19 @@ import (
|
||||
func GetCourseActivityHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.GetCourseActivityReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
pathParts := strings.Split(r.URL.Path, "/")
|
||||
if len(pathParts) < 3 { // 确保路径格式正确
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid path format"))
|
||||
return
|
||||
}
|
||||
idStr := pathParts[3]
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid meeting ID"))
|
||||
return
|
||||
}
|
||||
fmt.Println("idStr:", idStr)
|
||||
req.Id = int(id)
|
||||
mysqlCfg := cfg.MySQL
|
||||
dsn := fmt.Sprintf(
|
||||
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
|
||||
|
||||
@@ -33,6 +33,8 @@ func NewListCourseActivityLogic(ctx context.Context, cfg *config.Config, model m
|
||||
|
||||
func (l *ListCourseActivityLogic) ListCourseActivity(req *types.ListCourseActivityReq) (resp *types.ListCourseActivityResp, err error) {
|
||||
// 1. 处理分页参数:计算偏移量(页码从1开始)
|
||||
offset := (req.Page - 1) * req.PageSize // 新增:计算偏移量(关键修复)
|
||||
|
||||
// 2. 构建查询条件(根据可选参数拼接WHERE子句)
|
||||
var whereConditions []string
|
||||
var queryArgs []interface{}
|
||||
@@ -43,11 +45,9 @@ func (l *ListCourseActivityLogic) ListCourseActivity(req *types.ListCourseActivi
|
||||
queryArgs = append(queryArgs, req.CourseId)
|
||||
}
|
||||
|
||||
// 按活动类型筛选(若传入有效ActivityType,1-4)
|
||||
if req.ActivityType >= 1 && req.ActivityType <= 4 {
|
||||
whereConditions = append(whereConditions, "activity_type = ?")
|
||||
queryArgs = append(queryArgs, req.ActivityType)
|
||||
}
|
||||
// 新增:将分页参数(page_size和offset)添加到查询参数中
|
||||
queryArgs = append(queryArgs, req.PageSize) // LIMIT ?
|
||||
queryArgs = append(queryArgs, offset) // OFFSET ?
|
||||
|
||||
// 拼接WHERE子句(若有条件则添加where,多个条件用and连接)
|
||||
whereClause := ""
|
||||
@@ -56,16 +56,16 @@ func (l *ListCourseActivityLogic) ListCourseActivity(req *types.ListCourseActivi
|
||||
}
|
||||
|
||||
// 3. 查询符合条件的总条数(用于分页计算)
|
||||
total, err := l.model.Count(l.ctx, whereClause, queryArgs...)
|
||||
total, err := l.model.Count(l.ctx, whereClause, queryArgs[:len(queryArgs)-2]...) // 注意:Count不需要分页参数,截取前n-2个
|
||||
if err != nil {
|
||||
return nil, err // 传递数据库查询错误
|
||||
}
|
||||
|
||||
// 4. 查询当前页的活动列表数据
|
||||
// 4. 查询当前页的活动列表数据(此时queryArgs包含筛选+分页参数)
|
||||
activities, err := l.model.FindAll(
|
||||
l.ctx,
|
||||
whereClause,
|
||||
queryArgs..., // 这里是最后一个参数,符合语法
|
||||
queryArgs..., // 现在参数包含:筛选条件+page_size+offset,数量匹配
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user