74 lines
2.1 KiB
Go
74 lines
2.1 KiB
Go
|
package routers
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
"serverctl/pkg/api/middleware"
|
||
|
"serverctl/pkg/application/projects"
|
||
|
"serverctl/pkg/infrastructure/dependencies"
|
||
|
)
|
||
|
|
||
|
func projectsRouter(router *gin.Engine, d *dependencies.Dependencies) {
|
||
|
projectsApi := router.Group("/projects", middleware.BasicAuthMiddleware(d.Logger, d.UsersService))
|
||
|
projectsApi.POST("/", func(c *gin.Context) {
|
||
|
type CreateProjectRequest struct {
|
||
|
Name string `json:"name" binding:"required"`
|
||
|
}
|
||
|
var createProjectRequest CreateProjectRequest
|
||
|
if err := c.BindJSON(&createProjectRequest); err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
userId, _ := c.Get("userId")
|
||
|
createProjectId, err := d.ProjectsService.CreateProject(c.Request.Context(), userId.(int), createProjectRequest.Name)
|
||
|
if err != nil {
|
||
|
c.JSON(http.StatusBadRequest, gin.H{"message": "you have provided invalid input"})
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.JSON(http.StatusCreated, gin.H{"message": "project has been created", "projectId": createProjectId})
|
||
|
})
|
||
|
projectsApi.GET("/", func(c *gin.Context) {
|
||
|
userId, _ := c.Get("userId")
|
||
|
|
||
|
projectsArr, err := d.ProjectsService.Get(c.Request.Context(), userId.(int))
|
||
|
if err != nil {
|
||
|
d.Logger.Warn(err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
type GetProjectMembers struct {
|
||
|
MemberId int `json:"memberId" binding:"required"`
|
||
|
MemberRole string `json:"memberRole" binding:"required"`
|
||
|
}
|
||
|
type GetProject struct {
|
||
|
Id int `json:"id" binding:"required"`
|
||
|
Name string `json:"name" binding:"required"`
|
||
|
Members []*GetProjectMembers `json:"members" binding:"required"`
|
||
|
}
|
||
|
|
||
|
membersAsGetProjectMembers := func(projectMembers []projects.ProjectMember) []*GetProjectMembers {
|
||
|
gpm := make([]*GetProjectMembers, len(projectMembers))
|
||
|
|
||
|
for i, pm := range projectMembers {
|
||
|
gpm[i] = &GetProjectMembers{
|
||
|
MemberId: pm.MemberId,
|
||
|
MemberRole: pm.Role,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return gpm
|
||
|
}
|
||
|
|
||
|
getProject := make([]GetProject, 0)
|
||
|
for _, p := range projectsArr {
|
||
|
getProject = append(getProject, GetProject{
|
||
|
Id: p.Id,
|
||
|
Name: p.Name,
|
||
|
Members: membersAsGetProjectMembers(p.Members),
|
||
|
})
|
||
|
}
|
||
|
c.JSON(http.StatusOK, getProject)
|
||
|
})
|
||
|
}
|