Skip to content

Commit 948aae2

Browse files
committed
project details endpoint
1 parent 919fcfb commit 948aae2

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

backend/controllers/projects.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"gorm.io/gorm"
1414
"log"
1515
"net/http"
16+
"strconv"
1617
"strings"
1718
"time"
1819
)
@@ -117,6 +118,54 @@ func FindProjectsForOrg(c *gin.Context) {
117118
c.JSON(http.StatusOK, response)
118119
}
119120

121+
func ProjectDetails(c *gin.Context) {
122+
123+
currentOrg, exists := c.Get(middleware.ORGANISATION_ID_KEY)
124+
projectIdStr := c.Param("project_id")
125+
126+
if projectIdStr == "" {
127+
c.String(http.StatusBadRequest, "ProjectId not specified")
128+
return
129+
}
130+
131+
projectId, err := strconv.Atoi(projectIdStr)
132+
if err != nil {
133+
c.String(http.StatusBadRequest, "Invalid ProjectId")
134+
return
135+
}
136+
137+
if !exists {
138+
c.String(http.StatusForbidden, "Not allowed to access this resource")
139+
return
140+
}
141+
142+
var org models.Organisation
143+
err = models.DB.GormDB.Where("id = ?", currentOrg).First(&org).Error
144+
if err != nil {
145+
if errors.Is(err, gorm.ErrRecordNotFound) {
146+
c.String(http.StatusNotFound, fmt.Sprintf("Could not find organisation: %v", currentOrg))
147+
} else {
148+
c.String(http.StatusInternalServerError, "Unknown error occurred while fetching database")
149+
}
150+
return
151+
}
152+
153+
project, err := models.DB.GetProject(uint(projectId))
154+
if err != nil {
155+
log.Printf("could not fetch project: %v", err)
156+
c.String(http.StatusInternalServerError, "Could not fetch project")
157+
return
158+
}
159+
160+
if project.OrganisationID != org.ID {
161+
log.Printf("Forbidden access: not allowed to access projectID: %v logged in org: %v", project.OrganisationID, org.ID)
162+
c.String(http.StatusForbidden, "No access to this project")
163+
return
164+
}
165+
166+
c.JSON(http.StatusOK, project.MapToJsonStruct())
167+
}
168+
120169
type CreateProjectRequest struct {
121170
Name string `json:"name"`
122171
ConfigurationYaml string `json:"configurationYaml"`

backend/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ func main() {
165165
projectsApiGroup := apiGroup.Group("/projects")
166166
projectsApiGroup.Use(middleware.GetWebMiddleware())
167167
projectsApiGroup.GET("/", controllers.FindProjectsForOrg)
168+
projectsApiGroup.GET("/:project_id", controllers.ProjectDetails)
168169
projectsApiGroup.GET("/:project_id/runs", controllers.RunsForProject)
169170

170171
runsApiGroup := apiGroup.Group("/runs")

0 commit comments

Comments
 (0)