package controllers import ( "encoding/json" "server/models" beego "github.com/beego/beego/v2/server/web" ) // ProgramCategoryController 处理程序分类相关请求 type ProgramCategoryController struct { beego.Controller } // GetProgramCategoriesPublic 不需要认证的获取所有程序分类接口 func (c *ProgramCategoryController) GetProgramCategoriesPublic() { // fmt.Println("调用了不需要认证的GetProgramCategoriesPublic接口") categories, err := models.GetAllProgramCategories() if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "获取程序分类失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序分类成功", "data": categories, } } c.ServeJSON() } // ProgramInfoController 处理程序信息相关请求 type ProgramInfoController struct { beego.Controller } // GetProgramInfosPublic 不需要认证的获取所有程序信息接口 func (c *ProgramInfoController) GetProgramInfosPublic() { // fmt.Println("调用了不需要认证的GetProgramInfosPublic接口") programs, err := models.GetAllProgramInfos() if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "获取程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序信息成功", "data": programs, } } c.ServeJSON() } // ==================== 程序分类相关接口 ==================== // GetAllProgramCategories 获取所有程序分类 func (c *ProgramCategoryController) GetAllProgramCategories() { categories, err := models.GetAllProgramCategories() if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "获取程序分类失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序分类成功", "data": categories, } } c.ServeJSON() } // GetProgramCategoryById 根据ID获取程序分类 func (c *ProgramCategoryController) GetProgramCategoryById() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } category, err := models.GetProgramCategoryById(id) if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "程序分类不存在", } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序分类成功", "data": category, } } c.ServeJSON() } // CreateProgramCategory 创建新程序分类 func (c *ProgramCategoryController) CreateProgramCategory() { var category models.ProgramCategory // 解析请求体 if err := json.Unmarshal(c.Ctx.Input.RequestBody, &category); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "请求参数错误", "error": err.Error(), } c.ServeJSON() return } // 添加程序分类 if _, err := models.AddProgramCategory(&category); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "创建程序分类失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "创建程序分类成功", "data": category, } } c.ServeJSON() } // UpdateProgramCategory 更新程序分类 func (c *ProgramCategoryController) UpdateProgramCategory() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } var category models.ProgramCategory category.CategoryId = id // 解析请求体 if err := json.Unmarshal(c.Ctx.Input.RequestBody, &category); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "请求参数错误", "error": err.Error(), } c.ServeJSON() return } // 更新程序分类 if err := models.UpdateProgramCategory(&category); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "更新程序分类失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "更新程序分类成功", "data": category, } } c.ServeJSON() } // DeleteProgramCategory 删除程序分类 func (c *ProgramCategoryController) DeleteProgramCategory() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } // 删除程序分类 if err := models.DeleteProgramCategory(id); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "删除程序分类失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "删除程序分类成功", } } c.ServeJSON() } // ==================== 程序信息相关接口 ==================== // GetAllProgramInfos 获取所有程序信息 func (c *ProgramInfoController) GetAllProgramInfos() { programs, err := models.GetAllProgramInfos() if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "获取程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序信息成功", "data": programs, } } c.ServeJSON() } // GetProgramInfoById 根据ID获取程序信息 func (c *ProgramInfoController) GetProgramInfoById() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } program, err := models.GetProgramInfoById(id) if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "程序信息不存在", } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序信息成功", "data": program, } } c.ServeJSON() } // CreateProgramInfo 创建新程序信息 func (c *ProgramInfoController) CreateProgramInfo() { var program models.ProgramInfo // 解析请求体 if err := json.Unmarshal(c.Ctx.Input.RequestBody, &program); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "请求参数错误", "error": err.Error(), } c.ServeJSON() return } // 添加程序信息 if _, err := models.AddProgramInfo(&program); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "创建程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "创建程序信息成功", "data": program, } } c.ServeJSON() } // UpdateProgramInfo 更新程序信息 func (c *ProgramInfoController) UpdateProgramInfo() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } var program models.ProgramInfo program.ProgramId = id // 解析请求体 if err := json.Unmarshal(c.Ctx.Input.RequestBody, &program); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "请求参数错误", "error": err.Error(), } c.ServeJSON() return } // 更新程序信息 if err := models.UpdateProgramInfo(&program); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "更新程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "更新程序信息成功", "data": program, } } c.ServeJSON() } // DeleteProgramInfo 删除程序信息 func (c *ProgramInfoController) DeleteProgramInfo() { id, err := c.GetInt(":id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } // 删除程序信息 if err := models.DeleteProgramInfo(id); err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "删除程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "删除程序信息成功", } } c.ServeJSON() } // GetProgramInfosByCategoryId 根据分类ID获取程序信息 func (c *ProgramInfoController) GetProgramInfosByCategoryId() { categoryId, err := c.GetInt(":category_id") if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "参数错误", } c.ServeJSON() return } programs, err := models.GetProgramInfosByCategoryId(categoryId) if err != nil { c.Data["json"] = map[string]interface{}{ "success": false, "message": "获取程序信息失败", "error": err.Error(), } } else { c.Data["json"] = map[string]interface{}{ "success": true, "message": "获取程序信息成功", "data": programs, } } c.ServeJSON() }