feat: add projects/ endpoint for initiating extension of api

This commit is contained in:
eyad-hussein 2024-07-09 22:43:50 +03:00
parent 13015bba5a
commit 20fde317b3
4 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,20 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package structs
// Project represents a project
type Project struct {
ID int64 `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
TemplateType string `json:"template_type"`
CardType string `json:"card_type"`
}
type CreateProjectOption struct {
Title string `json:"title" binding:"Required;MaxSize(100)"`
Content string `json:"content"`
TemplateType uint8 `json:"template_type"`
CardType uint8 `json:"card_type"`
}

View File

@ -864,6 +864,36 @@ func Routes() *web.Router {
})
}
m.Group("/{username}", func() {
m.Group("/projects", func() {
// m.Group("", func() {
// m.Get("", org.Projects)
// m.Get("/{id}", org.ViewProject)
// }, reqUnitAccess(unit.TypeProjects, perm.AccessModeRead, true))
m.Group("", func() { //nolint:dupl
// m.Get("/new", org.RenderNewProject)
m.Post("", bind(api.CreateProjectOption{}), org.CreateProject)
// m.Group("/{id}", func() {
// m.Post("", web.Bind(forms.EditProjectColumnForm{}), org.AddColumnToProjectPost)
// m.Post("/move", project.MoveColumns)
// m.Post("/delete", org.DeleteProject)
// m.Get("/edit", org.RenderEditProject)
// m.Post("/edit", web.Bind(forms.CreateProjectForm{}), org.EditProjectPost)
// m.Post("/{action:open|close}", org.ChangeProjectStatus)
// m.Group("/{columnID}", func() {
// m.Put("", web.Bind(forms.EditProjectColumnForm{}), org.EditProjectColumn)
// m.Delete("", org.DeleteProjectColumn)
// m.Post("/default", org.SetDefaultProjectColumn)
// m.Post("/move", org.MoveIssues)
// })
// })
})
}, repoAssignment())
})
m.Group("", func() {
// Miscellaneous (no scope required)
if setting.API.EnableSwagger {

View File

@ -0,0 +1,39 @@
package org
import (
"log"
"net/http"
project_model "code.gitea.io/gitea/models/project"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/context"
)
func CreateProject(ctx *context.APIContext) {
form := web.GetForm(ctx).(*api.CreateProjectOption)
log.Println(ctx.ContextUser.ID)
project := &project_model.Project{
OwnerID: ctx.ContextUser.ID,
Title: form.Title,
Description: form.Content,
CreatorID: ctx.Doer.ID,
TemplateType: project_model.TemplateType(form.TemplateType),
CardType: project_model.CardType(form.CardType),
}
if ctx.ContextUser.IsOrganization() {
project.Type = project_model.TypeOrganization
} else {
project.Type = project_model.TypeIndividual
}
if err := project_model.NewProject(ctx, project); err != nil {
ctx.Error(http.StatusInternalServerError, "NewProject", err)
return
}
ctx.JSON(http.StatusCreated, map[string]int64{"id": project.ID})
}

View File

@ -0,0 +1,65 @@
package convert
// use this as reference to create the ToProject function:
/*
// ToLabel converts Label to API format
func ToLabel(label *issues_model.Label, repo *repo_model.Repository, org *user_model.User) *api.Label {
result := &api.Label{
ID: label.ID,
Name: label.Name,
Exclusive: label.Exclusive,
Color: strings.TrimLeft(label.Color, "#"),
Description: label.Description,
IsArchived: label.IsArchived(),
}
labelBelongsToRepo := label.BelongsToRepo()
// calculate URL
if labelBelongsToRepo && repo != nil {
result.URL = fmt.Sprintf("%s/labels/%d", repo.APIURL(), label.ID)
} else { // BelongsToOrg
if org != nil {
result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/labels/%d", setting.AppURL, url.PathEscape(org.Name), label.ID)
} else {
log.Error("ToLabel did not get org to calculate url for label with id '%d'", label.ID)
}
}
if labelBelongsToRepo && repo == nil {
log.Error("ToLabel did not get repo to calculate url for label with id '%d'", label.ID)
}
return result
}
*/
// ToProject converts Project to API format
// func ToProject(project *project_model.Project, repo *repo_model.Repository, org *user_model.User) *api.Project {
// result := &api.Project{
// ID: project.ID,
// Title: project.Title,
// Description: project.Description,
// TemplateType: project.TemplateType,
// CardType: project.CardType,
// }
// projectBelongsToRepo := project.BelongsToRepo()
// // calculate URL
// if projectBelongsToRepo && repo != nil {
// result.URL = fmt.Sprintf("%s/projects/%d", repo.APIURL(), project.ID)
// } else { // BelongsToOrg
// if org != nil {
// result.URL = fmt.Sprintf("%sapi/v1/orgs/%s/projects/%d", setting.AppURL, url.PathEscape(org.Name), project.ID)
// } else {
// log.Error("ToProject did not get org to calculate url for project with id '%d'", project.ID)
// }
// }
// if projectBelongsToRepo && repo == nil {
// log.Error("ToProject did not get repo to calculate url for project with id '%d'", project.ID)
// }
// return result
// }