@@ -25,8 +25,50 @@ import (
25
25
"gorm.io/gorm"
26
26
)
27
27
28
- func ListProjects (c * gin.Context ) {
28
+ func ListProjectsApi (c * gin.Context ) {
29
+ // assume all exists as validated in middleware
30
+ organisationId := c .GetString (middleware .ORGANISATION_ID_KEY )
31
+ organisationSource := c .GetString (middleware .ORGANISATION_SOURCE_KEY )
29
32
33
+ var org models.Organisation
34
+ err := models .DB .GormDB .Where ("external_id = ? AND external_source = ?" , organisationId , organisationSource ).First (& org ).Error
35
+ if err != nil {
36
+ if errors .Is (err , gorm .ErrRecordNotFound ) {
37
+ slog .Info ("Organisation not found" , "organisationId" , organisationId , "source" , organisationSource )
38
+ c .String (http .StatusNotFound , "Could not find organisation: " + organisationId )
39
+ } else {
40
+ slog .Error ("Error fetching organisation" , "organisationId" , organisationId , "source" , organisationSource , "error" , err )
41
+ c .String (http .StatusInternalServerError , "Error fetching organisation" )
42
+ }
43
+ return
44
+ }
45
+
46
+ var projects []models.Project
47
+
48
+ err = models .DB .GormDB .Preload ("Organisation" ).
49
+ Where ("projects.organisation_id = ?" , org .ID ).
50
+ Order ("name" ).
51
+ Find (& projects ).Error
52
+
53
+ if err != nil {
54
+ slog .Error ("Error fetching projects" , "organisationId" , organisationId , "orgId" , org .ID , "error" , err )
55
+ c .String (http .StatusInternalServerError , "Unknown error occurred while fetching database" )
56
+ return
57
+ }
58
+
59
+ marshalledRepos := make ([]interface {}, 0 )
60
+
61
+ for _ , p := range projects {
62
+ marshalled := p .MapToJsonStruct ()
63
+ marshalledRepos = append (marshalledRepos , marshalled )
64
+ }
65
+
66
+ slog .Info ("Successfully fetched projects" , "organisationId" , organisationId , "orgId" , org .ID , "projectCount" , len (projects ))
67
+
68
+ response := make (map [string ]interface {})
69
+ response ["result" ] = marshalledRepos
70
+
71
+ c .JSON (http .StatusOK , response )
30
72
}
31
73
32
74
func FindProjectsForRepo (c * gin.Context ) {
@@ -353,11 +395,10 @@ func ReportProjectsForRepo(c *gin.Context) {
353
395
)
354
396
355
397
project := models.Project {
356
- Name : request .Name ,
357
- ConfigurationYaml : request .ConfigurationYaml ,
358
- OrganisationID : org .ID ,
359
- RepoFullName : repo .RepoFullName ,
360
- Organisation : org ,
398
+ Name : request .Name ,
399
+ OrganisationID : org .ID ,
400
+ RepoFullName : repo .RepoFullName ,
401
+ Organisation : org ,
361
402
}
362
403
363
404
err = models .DB .GormDB .Create (& project ).Error
0 commit comments