From 1edad0cf741ed61019150f28175a2b9d664817fe Mon Sep 17 00:00:00 2001 From: Guibeen Date: Sun, 22 Mar 2020 19:19:41 +0800 Subject: [PATCH] =?UTF-8?q?fix=20#309=EF=BC=8C=E6=9C=AA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/projects.go | 35 +++++++++++++++++++++++++++++++++++ models/k_project.go | 27 +++++++++++++++++++++++++++ routers/router.go | 6 ++++++ 3 files changed, 68 insertions(+) create mode 100644 models/k_project.go diff --git a/controller/projects.go b/controller/projects.go index c85a467..2f5fd72 100644 --- a/controller/projects.go +++ b/controller/projects.go @@ -43,3 +43,38 @@ func GetManageProjects(c *gin.Context) { //返回json数组 c.JSON(http.StatusOK, managedProjects) } + +func AddProject(c *gin.Context) { + // TODO: 判断是否是已注册用户 + var project *models.ProjectDetail + // 解析传入的json数据,并绑定到project上。若失败,将返回错误并在http头部写入400状态码 + c.BindJSON(&project) + // TODO: 需要判断Url是否合法 + if urlLegal := true; !urlLegal { + c.JSON(http.StatusOK, gin.H{"result":"项目url不合法,请检查"}) + return + } + // TODO: 需要判断用户是否有权限将项目导入平台 + if authorized := true; !authorized { + c.JSON(http.StatusOK, gin.H{"result":"抱歉,您没有导入项目的权限"}) + return + } + + c.JSON(http.StatusOK, gin.H{ + "url": project.GithubUrl, + "name": project.ProjectName, + }) +/* + code := models.AddProject(project) + var result string + if code == 0 { + // url已经存在,说明项目已经在平台上,进行相应处理 + result = "项目已在平台中" + } else if code == -1 { + result = "项目导入失败" + } else { + result = "项目导入成功" + } + c.JSON(http.StatusOK, gin.H{"result": result}) +*/ +} \ No newline at end of file diff --git a/models/k_project.go b/models/k_project.go new file mode 100644 index 0000000..93d4193 --- /dev/null +++ b/models/k_project.go @@ -0,0 +1,27 @@ +package models + +import( + "fmt" +) + +// 导入项目时的信息 +type ProjectDetail struct { + ProjectName string `gorm:"column:project_name" json:"projectName"` + ProjectCoverUrl string `gorm:"column:project_cover_url" json:"projectCoverUrl"` + Introduction string `gorm:"column:project_description" json:"introdection"` + GithubUrl string `gorm:"column:project_url" json:"githubUrl"` + DecideType int `json:"decideType"` +} + +func AddProject(project *ProjectDetail) int { + tem := -1 + DB.Table("k_project").Select("project_id").Where("project_url = ?", project.GithubUrl).First(&tem) + if tem >= 0 { // 说明有ID,即项目已在平台内 + return 0 + } else if err := DB.Table("k_project").Create(&project).Error; err != nil { + fmt.Println(err.Error()) + return -1 + } else { + return 1 + } +} \ No newline at end of file diff --git a/routers/router.go b/routers/router.go index 0d67e5c..7617f5e 100644 --- a/routers/router.go +++ b/routers/router.go @@ -39,6 +39,12 @@ func RouterInit() *gin.Engine { //管理项目 projects.GET("/manage", controller.GetManageProjects) } + + projectsOperation := apiv1.Group("/projects") + { + //导入项目 + projectsOperation.POST("/add", controller.AddProject) + } } return r