You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.4 KiB
50 lines
1.4 KiB
package api
|
|
|
|
import (
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/auth"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/database"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/models"
|
|
"git.aiterp.net/stufflog3/stufflog3-api/internal/slerrors"
|
|
"github.com/gin-gonic/gin"
|
|
"time"
|
|
)
|
|
|
|
func Projects(g *gin.RouterGroup, db database.Database) {
|
|
g.Use(scopeIDMiddleware(db))
|
|
|
|
g.GET("/", handler("projects", func(c *gin.Context) (interface{}, error) {
|
|
return db.Projects(getScope(c).ID).List(c.Request.Context())
|
|
}))
|
|
|
|
g.GET("/:id", handler("project", func(c *gin.Context) (interface{}, error) {
|
|
id, err := reqInt(c, "id")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return db.Projects(getScope(c).ID).Find(c.Request.Context(), id)
|
|
}))
|
|
|
|
g.POST("/", handler("project", func(c *gin.Context) (interface{}, error) {
|
|
project := &models.Project{}
|
|
err := c.BindJSON(project)
|
|
if err != nil {
|
|
return nil, slerrors.BadRequest("Invalid JSON input: " + err.Error())
|
|
}
|
|
|
|
if !project.Status.Valid() {
|
|
return nil, slerrors.BadRequest("Unknown/unsupported project status")
|
|
}
|
|
if project.Name == "" {
|
|
return nil, slerrors.BadRequest("Project name cannot be blank")
|
|
}
|
|
if len(project.Requirements) > 0 {
|
|
return nil, slerrors.BadRequest("You cannot submit requirements this way")
|
|
}
|
|
|
|
project.OwnerID = auth.UserID(c)
|
|
project.CreatedTime = time.Now()
|
|
|
|
return db.Projects(getScope(c).ID).Create(c.Request.Context(), *project)
|
|
}))
|
|
}
|