mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
api: implement logic for all (user and org)/reponame/projects endpoints and crud operations
create endpoints for each operation in the web router and corresponding handler and tested them manually
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
project_model "code.gitea.io/gitea/models/project"
|
||||
attachment_model "code.gitea.io/gitea/models/repo"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
@@ -68,16 +67,10 @@ func ChangeProjectStatus(ctx *context.APIContext) {
|
||||
|
||||
// Projects renders the home page of projects
|
||||
func GetProjects(ctx *context.APIContext) {
|
||||
ctx.Data["Title"] = ctx.Tr("repo.projects")
|
||||
|
||||
sortType := ctx.FormTrim("sort")
|
||||
|
||||
isShowClosed := strings.ToLower(ctx.FormTrim("state")) == "closed"
|
||||
keyword := ctx.FormTrim("q")
|
||||
page := ctx.FormInt("page")
|
||||
if page <= 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
var projectType project_model.Type
|
||||
if ctx.ContextUser.IsOrganization() {
|
||||
@@ -86,10 +79,6 @@ func GetProjects(ctx *context.APIContext) {
|
||||
projectType = project_model.TypeIndividual
|
||||
}
|
||||
projects, err := db.Find[project_model.Project](ctx, project_model.SearchOptions{
|
||||
ListOptions: db.ListOptions{
|
||||
Page: page,
|
||||
PageSize: setting.UI.IssuePagingNum,
|
||||
},
|
||||
OwnerID: ctx.ContextUser.ID,
|
||||
IsClosed: optional.Some(isShowClosed),
|
||||
OrderBy: project_model.GetSearchOrderByBySortType(sortType),
|
||||
@@ -180,31 +169,31 @@ func GetProject(ctx *context.APIContext) {
|
||||
ctx.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
||||
// AddColumnToProject adds a new column to a project
|
||||
func AddColumnToProject(ctx *context.APIContext) {
|
||||
project, err := project_model.GetProjectByID(ctx, ctx.PathParamInt64(":id"))
|
||||
// EditProject updates a project
|
||||
func EditProject(ctx *context.APIContext) {
|
||||
form := web.GetForm(ctx).(*api.CreateProjectOption)
|
||||
projectID := ctx.PathParamInt64(":id")
|
||||
|
||||
p, err := project_model.GetProjectByID(ctx, projectID)
|
||||
if err != nil {
|
||||
ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err)
|
||||
return
|
||||
}
|
||||
if project.OwnerID != ctx.ContextUser.ID {
|
||||
if p.OwnerID != ctx.ContextUser.ID {
|
||||
ctx.NotFound("", nil)
|
||||
return
|
||||
}
|
||||
|
||||
form := web.GetForm(ctx).(*api.EditProjectColumnOption)
|
||||
column := &project_model.Column{
|
||||
ProjectID: project.ID,
|
||||
Title: form.Title,
|
||||
Sorting: form.Sorting,
|
||||
Color: form.Color,
|
||||
}
|
||||
if err := project_model.NewColumn(ctx, column); err != nil {
|
||||
ctx.ServerError("NewProjectColumn", err)
|
||||
p.Title = form.Title
|
||||
p.Description = form.Content
|
||||
p.CardType = project_model.CardType(form.CardType)
|
||||
|
||||
if err = project_model.UpdateProject(ctx, p); err != nil {
|
||||
ctx.ServerError("UpdateProjects", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusCreated, column)
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
}
|
||||
|
||||
// DeleteProject delete a project
|
||||
@@ -229,71 +218,32 @@ func DeleteProject(ctx *context.APIContext) {
|
||||
ctx.JSON(http.StatusOK, map[string]any{"message": "project deleted successfully"})
|
||||
}
|
||||
|
||||
// EditProject updates a project
|
||||
func EditProject(ctx *context.APIContext) {
|
||||
form := web.GetForm(ctx).(*api.CreateProjectOption)
|
||||
projectID := ctx.PathParamInt64(":id")
|
||||
|
||||
ctx.Data["CancelLink"] = fmt.Sprintf("%s/-/projects/%d", ctx.ContextUser.HomeLink(), projectID)
|
||||
|
||||
p, err := project_model.GetProjectByID(ctx, projectID)
|
||||
if err != nil {
|
||||
ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err)
|
||||
return
|
||||
}
|
||||
if p.OwnerID != ctx.ContextUser.ID {
|
||||
ctx.NotFound("", nil)
|
||||
return
|
||||
}
|
||||
|
||||
p.Title = form.Title
|
||||
p.Description = form.Content
|
||||
p.CardType = project_model.CardType(form.CardType)
|
||||
|
||||
if err = project_model.UpdateProject(ctx, p); err != nil {
|
||||
ctx.ServerError("UpdateProjects", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
}
|
||||
|
||||
// MoveColumns moves or keeps columns in a project and sorts them inside that project
|
||||
func MoveColumns(ctx *context.APIContext) {
|
||||
// AddColumnToProject adds a new column to a project
|
||||
func AddColumnToProject(ctx *context.APIContext) {
|
||||
project, err := project_model.GetProjectByID(ctx, ctx.PathParamInt64(":id"))
|
||||
if err != nil {
|
||||
ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err)
|
||||
return
|
||||
}
|
||||
if !project.CanBeAccessedByOwnerRepo(ctx.ContextUser.ID, ctx.Repo.Repository) {
|
||||
ctx.NotFound("CanBeAccessedByOwnerRepo", nil)
|
||||
if project.OwnerID != ctx.ContextUser.ID {
|
||||
ctx.NotFound("", nil)
|
||||
return
|
||||
}
|
||||
|
||||
type movedColumnsForm struct {
|
||||
Columns []struct {
|
||||
ColumnID int64 `json:"columnID"`
|
||||
Sorting int64 `json:"sorting"`
|
||||
} `json:"columns"`
|
||||
form := web.GetForm(ctx).(*api.EditProjectColumnOption)
|
||||
column := &project_model.Column{
|
||||
ProjectID: project.ID,
|
||||
Title: form.Title,
|
||||
Sorting: form.Sorting,
|
||||
Color: form.Color,
|
||||
CreatorID: ctx.Doer.ID,
|
||||
}
|
||||
|
||||
form := &movedColumnsForm{}
|
||||
if err = json.NewDecoder(ctx.Req.Body).Decode(&form); err != nil {
|
||||
ctx.ServerError("DecodeMovedColumnsForm", err)
|
||||
if err := project_model.NewColumn(ctx, column); err != nil {
|
||||
ctx.ServerError("NewProjectColumn", err)
|
||||
return
|
||||
}
|
||||
|
||||
sortedColumnIDs := make(map[int64]int64)
|
||||
for _, column := range form.Columns {
|
||||
sortedColumnIDs[column.Sorting] = column.ColumnID
|
||||
}
|
||||
|
||||
if err = project_model.MoveColumnsOnProject(ctx, project, sortedColumnIDs); err != nil {
|
||||
ctx.ServerError("MoveColumnsOnProject", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.JSON(http.StatusOK, map[string]string{"message": "columns moved successfully"})
|
||||
ctx.JSON(http.StatusCreated, column)
|
||||
}
|
||||
|
||||
// CheckProjectColumnChangePermissions check permission
|
||||
|
Reference in New Issue
Block a user