Skip to content

Commit

Permalink
fix OS-ABC#309,未测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Guibeen committed Mar 22, 2020
1 parent f161912 commit 1edad0c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
35 changes: 35 additions & 0 deletions controller/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
*/
}
27 changes: 27 additions & 0 deletions models/k_project.go
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 6 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func RouterInit() *gin.Engine {
//管理项目
projects.GET("/manage", controller.GetManageProjects)
}

projectsOperation := apiv1.Group("/projects")
{
//导入项目
projectsOperation.POST("/add", controller.AddProject)
}
}

return r
Expand Down

0 comments on commit 1edad0c

Please sign in to comment.