Skip to content

Commit

Permalink
增加项目空间成员管理api
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamans committed Dec 22, 2018
1 parent c24ce81 commit 51cf37e
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 14 deletions.
6 changes: 6 additions & 0 deletions model/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func Get(id int) (Project, bool){
return data, ok
}

func GetOne(query model.QueryParam) (Project, bool) {
var data Project
ok := model.GetOne(TableName, &data, query)
return data, ok
}

func Update(id int, data Project) bool {
updateFields := map[string]interface{}{
"name": data.Name,
Expand Down
1 change: 1 addition & 0 deletions model/project/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Project struct {
ID int `gorm:"primary_key"`
Name string `gorm:"type:varchar(100);not null;default:''"`
Description string `gorm:"type:varchar(100);not null;default:''"`
SpaceId int `gorm:"type:int(11);not null;default:0"`
Space string `gorm:"type:varchar(100);not null;default:''"`
Repo string `gorm:"type:varchar(20);not null;default:''"`
RepoUrl string `gorm:"type:varchar(200);not null;default:''"`
Expand Down
62 changes: 54 additions & 8 deletions module/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ func init() {
route.Register(route.API_PROJECT_LIST, listProject)
route.Register(route.API_PROJECT_DETAIL, detailProject)
route.Register(route.API_PROJECT_DELETE, deleteProject)
route.Register(route.API_PROJECT_EXISTS, existsProject)
}

type ProjectParamValid struct {
Name string `valid:"required" errmsg:"required=project name cannot be empty"`
Description string `valid:"require" errmsg:"required=project description cannot be empty"`
SpaceId int `valid:"int_min=1" errmsg:"required=space_id cannot be empty"`
Space string `valid:"require" errmsg:"required=project space cannot be empty"`
Repo string `valid:"require" errmsg:"required=repo type cannot be empty"`
RepoMode int `valid:"require" errmsg:"required=repo mode cannot be empty"`
RepoMode int `valid:"int_min=1" errmsg:"required=repo_mode cannot be empty"`
RepoUrl string `valid:"require" errmsg:"required=repo remote addr cannot be empty"`
DeployServer []string `valid:"require" errmsg:"required=deploy server cannot be empty"`
DeployUser string `valid:"require" errmsg:"required=deploy user cannot be epmty"`
Expand All @@ -37,6 +39,7 @@ func updateProject(c *goweb.Context) error {
params := ProjectParamValid{
Name: c.PostForm("name"),
Description: c.PostForm("description"),
SpaceId: c.PostFormInt("space_id"),
Space: c.PostForm("space"),
Repo: c.PostForm("repo"),
RepoMode: c.PostFormInt("repoMode"),
Expand All @@ -51,12 +54,31 @@ func updateProject(c *goweb.Context) error {
return nil
}

var (
needAudit, status int
exists bool
err error
)

projExists := &projectService.Project{
ID: c.PostFormInt("id"),
SpaceId: params.SpaceId,
Name: params.Name,
}
exists, err = projExists.CheckProjectExists()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
if exists {
syncd.RenderAppError(c, "project update failed, project name have exists")
return nil
}

deployServer := goutil.StrSlice2IntSlice(params.DeployServer)
needAudit := 0
if c.PostFormInt("needAudit") != 0 {
needAudit = 1
}
status := 0
if c.PostFormInt("status") != 0 {
status = 1
}
Expand All @@ -65,6 +87,7 @@ func updateProject(c *goweb.Context) error {
ID: c.PostFormInt("id"),
Name: params.Name,
Description: params.Description,
SpaceId: params.SpaceId,
Space: params.Space,
Repo: params.Repo,
RepoUrl: params.RepoUrl,
Expand All @@ -81,19 +104,20 @@ func updateProject(c *goweb.Context) error {
RepoPass: c.PostForm("repoPass"),
BuildScript: c.PostForm("buildScript"),
}
if err := project.CreateOrUpdate(); err != nil {
syncd.RenderParamError(c, err.Error())
if err = project.CreateOrUpdate(); err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
syncd.RenderJson(c, nil)
return nil
}

func listProject(c *goweb.Context) error {
offset, limit := c.QueryInt("offset"), c.QueryInt("limit")
keyword := c.Query("keyword")
offset, limit, keyword, spaceId := c.QueryInt("offset"), c.QueryInt("limit"), c.Query("keyword"), c.QueryInt("space_id")

project := &projectService.Project{}
project := &projectService.Project{
SpaceId: spaceId,
}
list, total, err := project.List(keyword, offset, limit)
if err != nil {
syncd.RenderAppError(c, err.Error())
Expand Down Expand Up @@ -140,3 +164,25 @@ func deleteProject(c *goweb.Context) error {
syncd.RenderJson(c, nil)
return nil
}

func existsProject(c *goweb.Context) error {
id, spaceId, keyword := c.QueryInt("id"), c.QueryInt("space_id"), c.Query("keyword")
if spaceId == 0 || keyword == "" {
syncd.RenderParamError(c, "params error")
return nil
}
project := &projectService.Project{
ID: id,
SpaceId: spaceId,
Name: keyword,
}
exists, err := project.CheckProjectExists()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
syncd.RenderJson(c, goweb.JSON{
"exists": exists,
})
return nil
}
65 changes: 61 additions & 4 deletions module/project/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ func updateProjectSpace(c *goweb.Context) error {
syncd.RenderParamError(c, "name can not be empty")
return nil
}

spaceExists := &projectService.Space{
ID: id,
Name: name,
}
exists, err := spaceExists.CheckSpaceExists()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
if exists {
syncd.RenderAppError(c, "space data update failed, space name have exists")
return nil
}

projectSpace := &projectService.Space{
ID: id,
Name: name,
Expand All @@ -48,8 +63,28 @@ func listProjectSpace(c *goweb.Context) error {
syncd.RenderAppError(c, err.Error())
return nil
}

//check if project exists in the space
var newList []map[string]interface{}
for _, l := range list {
projService := &projectService.Project{
SpaceId: l.ID,
}
exists, err := projService.CheckSpaceHaveProject()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
newList = append(newList, map[string]interface{}{
"id": l.ID,
"name": l.Name,
"description": l.Description,
"have_project": exists,
"ctime": l.Ctime,
})
}
syncd.RenderJson(c, goweb.JSON{
"list": list,
"list": newList,
"total": total,
})
return nil
Expand All @@ -68,10 +103,29 @@ func detailProjectSpace(c *goweb.Context) error {
}

func deleteProjectSpace(c *goweb.Context) error {
var (
id int
exists bool
err error
)
id = c.PostFormInt("id")
proj := &projectService.Project{
SpaceId: id,
}
exists, err = proj.CheckSpaceHaveProject()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
if exists {
syncd.RenderAppError(c, "space delete failed, project in space is not empty")
return nil
}

projectSpace := &projectService.Space{
ID: c.PostFormInt("id"),
ID: id,
}
if err := projectSpace.Delete(); err != nil {
if err = projectSpace.Delete(); err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
Expand All @@ -82,7 +136,10 @@ func deleteProjectSpace(c *goweb.Context) error {
func existsProjectSpace(c *goweb.Context) error {
keyword := c.Query("keyword")
id := c.QueryInt("id")

if keyword == "" {
syncd.RenderParamError(c, "params error")
return nil
}
projectSpace := &projectService.Space{
ID: id,
Name: keyword,
Expand Down
25 changes: 25 additions & 0 deletions module/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package user
import (
"github.com/tinystack/goweb"
"github.com/tinystack/govalidate"
"github.com/tinystack/goutil"
"github.com/tinystack/syncd"
"github.com/tinystack/syncd/route"
userService "github.com/tinystack/syncd/service/user"
Expand All @@ -18,6 +19,7 @@ func init() {
route.Register(route.API_USER_DETAIL, detailUser)
route.Register(route.API_USER_EXISTS, existsUser)
route.Register(route.API_USER_DELETE, deleteUser)
route.Register(route.API_USER_SEARCH, searchUser)
}

type UserParamValid struct {
Expand Down Expand Up @@ -162,3 +164,26 @@ func deleteUser(c *goweb.Context) error {
syncd.RenderJson(c, nil)
return nil
}
func searchUser(c *goweb.Context) error {
keyword := c.Query("keyword")
if keyword == "" {
syncd.RenderJson(c, nil)
return nil
}

user := &userService.User{}
if goutil.IsEmail(keyword) {
user.Email = keyword
} else {
user.Name = keyword
}
list, err := user.Search()
if err != nil {
syncd.RenderAppError(c, err.Error())
return nil
}
syncd.RenderJson(c, goweb.JSON{
"list": list,
})
return nil
}
8 changes: 8 additions & 0 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const (
CODE_ERR_SYSTEM = 1000
CODE_ERR_APP = 1001
CODE_ERR_PARAM = 1002
CODE_ERR_DATA_REPEAT = 1003
)

func RenderParamError(c *goweb.Context, msg string) {
Expand All @@ -38,3 +39,10 @@ func RenderJson(c *goweb.Context, data interface{}) {
"data": data,
})
}

func RenderCustomerError(c *goweb.Context, code int, msg string) {
c.Json(http.StatusOK, goweb.JSON{
"code": code,
"message": msg,
})
}
4 changes: 4 additions & 0 deletions route/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ const (
API_PROJECT_DELETE = "POST /api/project/delete"
API_PROJECT_LIST = "GET /api/project/list"
API_PROJECT_DETAIL = "GET /api/project/detail"
API_PROJECT_EXISTS = "GET /api/project/exists"
API_PROJECT_SPACE_UPDATE = "POST /api/project/space/update"
API_PROJECT_SPACE_LIST = "GET /api/project/space/list"
API_PROJECT_SPACE_DETAIL = "GET /api/project/space/detail"
API_PROJECT_SPACE_DELETE = "POST /api/project/space/delete"
API_PROJECT_SPACE_EXISTS = "GET /api/project/space/exists"
API_PROJECT_SPACE_USER_ADD = "POST /api/project/space/user/add"
API_PROJECT_SPACE_USER_LIST = "GET /api/project/space/user/list"

API_SERVER_GROUP_UPDATE = "POST /api/server/group/update"
API_SERVER_GROUP_LIST = "GET /api/server/group/list"
Expand All @@ -36,4 +39,5 @@ const (
API_USER_DETAIL = "GET /api/user/detail"
API_USER_EXISTS = "GET /api/user/exists"
API_USER_DELETE = "POST /api/user/delete"
API_USER_SEARCH = "GET /api/user/search"
)
Loading

0 comments on commit 51cf37e

Please sign in to comment.