|
@@ -0,0 +1,150 @@
|
|
|
+package admin
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+ "server/dao"
|
|
|
+ "server/model/common/request"
|
|
|
+ "server/model/common/response"
|
|
|
+ "strconv"
|
|
|
+)
|
|
|
+
|
|
|
+type ProjectApi struct{}
|
|
|
+
|
|
|
+func (pa *ProjectApi) QueryProjectList(c *gin.Context) {
|
|
|
+ var search request.SearchProject
|
|
|
+ err := c.ShouldBindJSON(&search)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ list, total, err := projectService.QueryProjectList(search)
|
|
|
+
|
|
|
+ response.OkWithDetailed(response.PageResult{
|
|
|
+ List: list,
|
|
|
+ Total: total,
|
|
|
+ Page: search.PageInfo.Page,
|
|
|
+ PageSize: search.PageInfo.PageSize,
|
|
|
+ }, "获取成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) QueryProjectByCode(c *gin.Context) {
|
|
|
+ code := c.Param("code")
|
|
|
+ if code == "" {
|
|
|
+ response.FailWithMessage("编号不能为空", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ project, err := projectService.QueryProjectByCode(code)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithData(project, c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) CreateProject(c *gin.Context) {
|
|
|
+ //var project dao.Project
|
|
|
+ //err := c.ShouldBindJSON(&project)
|
|
|
+ //if err != nil {
|
|
|
+ // response.FailWithMessage(err.Error(), c)
|
|
|
+ // return
|
|
|
+ //}
|
|
|
+
|
|
|
+ form, err := c.MultipartForm()
|
|
|
+ files := form.File["file"]
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ projectJson := c.PostForm("project")
|
|
|
+
|
|
|
+ var project dao.Project
|
|
|
+ err = json.Unmarshal([]byte(projectJson), &project)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage("JSON失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, file := range files {
|
|
|
+ dst := file.Filename
|
|
|
+
|
|
|
+ projectFile := dao.ProjectFile{
|
|
|
+ Name: dst,
|
|
|
+ Path: "./uploads/file/" + dst,
|
|
|
+ }
|
|
|
+
|
|
|
+ project.Files = append(project.Files, projectFile)
|
|
|
+
|
|
|
+ // gin 简单做了封装,拷贝了文件流
|
|
|
+ if err := c.SaveUploadedFile(file, "./uploads/file/"+dst); err != nil {
|
|
|
+ response.FailWithMessage("上传失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err = projectService.CreateProject(project)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.OkWithMessage("创建成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) CreateProjectFile(c *gin.Context) {
|
|
|
+ var projectFile dao.ProjectFile
|
|
|
+ err := c.ShouldBindJSON(&projectFile)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.CreateProjectFile(projectFile)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("创建成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) UpdateProject(c *gin.Context) {
|
|
|
+ var project dao.Project
|
|
|
+ err := c.ShouldBindJSON(&project)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.UpdateProject(project)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("更新成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) DeleteProject(c *gin.Context) {
|
|
|
+ code := c.Param("code")
|
|
|
+ if code == "" {
|
|
|
+ response.FailWithMessage("编号不能为空", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err := projectService.DeleteProject(code)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("删除成功", c)
|
|
|
+}
|
|
|
+
|
|
|
+func (pa *ProjectApi) DeleteProjectFile(c *gin.Context) {
|
|
|
+ id := c.Param("id")
|
|
|
+ i, err := strconv.Atoi(id)
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ err = projectService.DeleteProjectFile(uint(i))
|
|
|
+ if err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.OkWithMessage("删除成功", c)
|
|
|
+}
|