fix: return error when converting project to api project

This commit is contained in:
dineshsalunke 2024-01-17 21:45:36 +05:30
parent 852c0df382
commit dbfecced25
2 changed files with 44 additions and 14 deletions

View File

@ -17,7 +17,7 @@ import (
func innerCreateProject( func innerCreateProject(
ctx *context.APIContext, ctx *context.APIContext,
project_type project_model.Type, projectType project_model.Type,
) { ) {
form := web.GetForm(ctx).(*api.NewProjectPayload) form := web.GetForm(ctx).(*api.NewProjectPayload)
project := &project_model.Project{ project := &project_model.Project{
@ -27,14 +27,14 @@ func innerCreateProject(
Description: form.Description, Description: form.Description,
CreatorID: ctx.Doer.ID, CreatorID: ctx.Doer.ID,
BoardType: project_model.BoardType(form.BoardType), BoardType: project_model.BoardType(form.BoardType),
Type: project_type, Type: projectType,
} }
if ctx.ContextUser != nil { if ctx.ContextUser != nil {
project.OwnerID = ctx.ContextUser.ID project.OwnerID = ctx.ContextUser.ID
} }
if project_type == project_model.TypeRepository { if projectType == project_model.TypeRepository {
project.RepoID = ctx.Repo.Repository.ID project.RepoID = ctx.Repo.Repository.ID
} }
@ -49,7 +49,13 @@ func innerCreateProject(
return return
} }
ctx.JSON(http.StatusCreated, convert.ToAPIProject(ctx, project)) projectResponse, err := convert.ToAPIProject(ctx, project)
if err != nil {
ctx.Error(http.StatusInternalServerError, "NewProject", err)
return
}
ctx.JSON(http.StatusCreated, projectResponse)
} }
func CreateUserProject(ctx *context.APIContext) { func CreateUserProject(ctx *context.APIContext) {
@ -165,7 +171,12 @@ func GetProject(ctx *context.APIContext) {
return return
} }
ctx.JSON(http.StatusOK, convert.ToAPIProject(ctx, project)) projectResponse, err := convert.ToAPIProject(ctx, project)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetProjectByID", err)
return
}
ctx.JSON(http.StatusOK, projectResponse)
} }
func UpdateProject(ctx *context.APIContext) { func UpdateProject(ctx *context.APIContext) {
@ -215,7 +226,12 @@ func UpdateProject(ctx *context.APIContext) {
ctx.Error(http.StatusInternalServerError, "UpdateProject", err) ctx.Error(http.StatusInternalServerError, "UpdateProject", err)
return return
} }
ctx.JSON(http.StatusOK, convert.ToAPIProject(ctx, project)) projectResponse, err := convert.ToAPIProject(ctx, project)
if err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateProject", err)
return
}
ctx.JSON(http.StatusOK, projectResponse)
} }
func DeleteProject(ctx *context.APIContext) { func DeleteProject(ctx *context.APIContext) {
@ -242,7 +258,6 @@ func DeleteProject(ctx *context.APIContext) {
} }
ctx.Status(http.StatusNoContent) ctx.Status(http.StatusNoContent)
} }
func ListUserProjects(ctx *context.APIContext) { func ListUserProjects(ctx *context.APIContext) {

View File

@ -10,8 +10,7 @@ import (
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
) )
func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Project { func ToAPIProject(ctx context.Context, project *project_model.Project) (*api.Project, error) {
apiProject := &api.Project{ apiProject := &api.Project{
Title: project.Title, Title: project.Title,
Description: project.Description, Description: project.Description,
@ -23,7 +22,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
} }
// try to laod the repo // try to laod the repo
project.LoadRepo(ctx) err := project.LoadRepo(ctx)
if err != nil {
return nil, err
}
if project.Repo != nil { if project.Repo != nil {
apiProject.Repo = &api.RepositoryMeta{ apiProject.Repo = &api.RepositoryMeta{
ID: project.RepoID, ID: project.RepoID,
@ -33,7 +35,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
} }
} }
project.LoadCreator(ctx) err = project.LoadCreator(ctx)
if err != nil {
return nil, err
}
if project.Creator != nil { if project.Creator != nil {
apiProject.Creator = &api.User{ apiProject.Creator = &api.User{
ID: project.Creator.ID, ID: project.Creator.ID,
@ -42,7 +47,10 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
} }
} }
project.LoadOwner(ctx) err = project.LoadOwner(ctx)
if err != nil {
return nil, err
}
if project.Owner != nil { if project.Owner != nil {
apiProject.Owner = &api.User{ apiProject.Owner = &api.User{
ID: project.Owner.ID, ID: project.Owner.ID,
@ -51,13 +59,20 @@ func ToAPIProject(ctx context.Context, project *project_model.Project) *api.Proj
} }
} }
return apiProject return apiProject, nil
} }
func ToAPIProjectList(ctx context.Context, projects []*project_model.Project) ([]*api.Project, error) { func ToAPIProjectList(ctx context.Context, projects []*project_model.Project) ([]*api.Project, error) {
result := make([]*api.Project, len(projects)) result := make([]*api.Project, len(projects))
var err error
for i := range projects { for i := range projects {
result[i] = ToAPIProject(ctx, projects[i]) result[i], err = ToAPIProject(ctx, projects[i])
if err != nil {
break
}
}
if err != nil {
return nil, err
} }
return result, nil return result, nil
} }