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