package controllers import ( "github.com/kataras/iris/v12/mvc" "xps/datamodels" "xps/service" ) type RepoController struct { Base Service service.RepoService } func (c *RepoController) BeforeActivation(b mvc.BeforeActivation) { b.Handle("GET", "/", "GetList" ) b.Handle("GET", "/page", "GetPage" ) b.Handle("GET", "/{repoTypeId:int64}/{repoId:int64}", "GetById" ) b.Handle("GET", "/{repoTypeId:int64}", "GetListById" ) b.Handle("POST", "/add", "Create" ) b.Handle("PUT", "/update", "Update" ) b.Handle("DELETE", "/{repoId:int64}", "DeleteByID" ) } func (c *RepoController) GetList() *JsonResult { params := c.buildParams() data, err := c.Service.GetList(params) if err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("获取成功", data) } } func (c *RepoController) GetPage() *JsonResult { params := c.buildParams() data, err := c.Service.GetPage(params) if err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("获取成功", data) } } func (c *RepoController) GetListById(repoTypeId int64) *JsonResult { data, err := c.Service.GetListById(repoTypeId) if err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("获取成功", data) } } func (c *RepoController) GetById(repoTypeId, repoId int64) *JsonResult { data, err := c.Service.GetById(repoTypeId, repoId) if err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("获取成功", data) } } func (c *RepoController) Create() *JsonResult { userId := c.GetCurUserId() repo := datamodels.Repo{} repo.CreatedBy = userId if err := c.Ctx.ReadJSON(&repo); err != nil { return ResultErr("读取数据失败", nil) } if err := c.Service.Create(&repo); err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("新增成功", nil) } } func (c *RepoController) Update() *JsonResult { userId := c.GetCurUserId() repo := datamodels.Repo{} if err := c.Ctx.ReadJSON(&repo); err != nil { return ResultErr("读取数据失败", nil) } repo.UpdatedBy = userId if err := c.Service.Update(&repo); err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("更新成功", nil) } } func (c *RepoController) DeleteByID(repoId int64) *JsonResult { userId := c.GetCurUserId() m := make(map[string]interface{}) m["repoId"] = repoId m["deletedBy"] = userId if err := c.Service.Delete(m); err != nil { return ResultErr(err.Error(), nil) } else { return ResultOk("删除成功", nil) } }