change code for statistic and project repo_count, role_id api

This commit is contained in:
wemeya 2016-05-13 22:45:30 +08:00
parent f43d6dab18
commit 8c35e6ffa2
4 changed files with 36 additions and 25 deletions

View File

@ -126,12 +126,11 @@ func (p *ProjectAPI) Get() {
log.Errorf("Error occurred in QueryProject, error: %v", err)
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
sa := &StatisticAPI{userID: p.userID, username: ""}
for i := 0; i < len(projectList); i++ {
if projectList[i].Role == 1 {
if projectList[i].Role == models.PROJECTADMIN {
projectList[i].Togglable = true
}
projectList[i].Repos = sa.GetReposByProject(projectList[i].Name, false)
projectList[i].RepoCount = getRepoCountByProject(projectList[i].Name)
}
p.Data["json"] = projectList
p.ServeJSON()

View File

@ -64,34 +64,29 @@ func (s *StatisticAPI) Get() {
s.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
if isAdmin {
proMap["total_project"] = len(projectList)
proMap["total_project_count"] = len(projectList)
proMap["total_repo_count"] = getTotalRepoCount()
}
for i := 0; i < len(projectList); i++ {
if projectList[i].Role == 1 || projectList[i].Role == 2 {
proMap["my_project"]++
proMap["my_repos"] += s.GetReposByProject(projectList[i].Name, false)
if projectList[i].Role == models.PROJECTADMIN || projectList[i].Role == models.DEVELOPER {
proMap["my_project_count"]++
proMap["my_repo_count"] += getRepoCountByProject(projectList[i].Name)
}
if projectList[i].Public == 1 {
proMap["public_project"]++
proMap["public_repos"] += s.GetReposByProject(projectList[i].Name, false)
}
if isAdmin {
proMap["total_repos"] = s.GetReposByProject(projectList[i].Name, true)
proMap["public_project_count"]++
proMap["public_repo_count"] += getRepoCountByProject(projectList[i].Name)
}
}
s.Data["json"] = proMap
s.ServeJSON()
}
//GetReposByProject returns repo numbers of specified project
func (s *StatisticAPI) GetReposByProject(projectName string, isAdmin bool) int {
//getReposByProject returns repo numbers of specified project
func getRepoCountByProject(projectName string) int {
repoList, err := svc_utils.GetRepoFromCache()
if err != nil {
log.Errorf("Failed to get repo from cache, error: %v", err)
s.RenderError(http.StatusInternalServerError, "internal sever error")
}
if isAdmin {
return len(repoList)
return 0
}
var resp []string
if len(projectName) > 0 {
@ -104,3 +99,14 @@ func (s *StatisticAPI) GetReposByProject(projectName string, isAdmin bool) int {
}
return 0
}
//getTotalRepoCount returns total repo count
func getTotalRepoCount() int {
repoList, err := svc_utils.GetRepoFromCache()
if err != nil {
log.Errorf("Failed to get repo from cache, error: %v", err)
return 0
}
return len(repoList)
}

View File

@ -86,19 +86,25 @@ func QueryProject(query models.Project) ([]models.Project, error) {
sql := `select distinct
p.project_id, p.owner_id, p.name,p.creation_time, p.update_time, p.public`
queryParam := make([]interface{}, 1)
isAdmin, _ := IsAdminRole(query.UserID)
isAdmin, err := IsAdminRole(query.UserID)
if err != nil {
return nil, err
}
if query.Public == 1 {
//if the project is public
sql += ` from project p where p.deleted = 0 and p.public = ?`
queryParam = append(queryParam, query.Public)
} else if !isAdmin {
//if the user is not admin, should join the project_member table to query his/her projects and role id
sql += `, pm.role role from project p
left join project_member pm on p.project_id = pm.project_id
where p.deleted = 0 and (pm.user_id = ?) `
queryParam = append(queryParam, query.UserID)
} else if isAdmin {
//if the user is admin, return all projects
sql += ` from project p where p.deleted = 0 `
}
if query.Name != "" {
sql += " and p.name like ? "
queryParam = append(queryParam, query.Name)
@ -107,14 +113,14 @@ func QueryProject(query models.Project) ([]models.Project, error) {
sql += " order by p.name "
var r []models.Project
_, err := o.Raw(sql, queryParam).QueryRows(&r)
_, err0 := o.Raw(sql, queryParam).QueryRows(&r)
if err != nil {
if err0 != nil {
return nil, err
}
if isAdmin {
for i := 0; i < len(r); i++ {
r[i].Role = 1
r[i].Role = models.PROJECTADMIN
}
}
return r, nil

View File

@ -34,6 +34,6 @@ type Project struct {
Togglable bool
UpdateTime time.Time `orm:"update_time" json:"update_time"`
Role int `json:"RoleId"`
Repos int
Role int `json:"role_id"`
RepoCount int `json:"repo_count"`
}