@@ -3,6 +3,7 @@ package api
3
3
import (
4
4
"encoding/json"
5
5
"github.com/gin-gonic/gin"
6
+ "github.com/konveyor/tackle2-hub/assessment"
6
7
"github.com/konveyor/tackle2-hub/model"
7
8
"gorm.io/gorm/clause"
8
9
"net/http"
@@ -21,6 +22,8 @@ const (
21
22
AppBucketRoot = ApplicationRoot + "/bucket"
22
23
AppBucketContentRoot = AppBucketRoot + "/*" + Wildcard
23
24
AppStakeholdersRoot = ApplicationRoot + "/stakeholders"
25
+ AppAssessmentsRoot = ApplicationRoot + "/assessments"
26
+ AppAssessmentRoot = AppAssessmentsRoot + "/:" + ID2
24
27
)
25
28
26
29
//
@@ -77,6 +80,11 @@ func (h ApplicationHandler) AddRoutes(e *gin.Engine) {
77
80
routeGroup = e .Group ("/" )
78
81
routeGroup .Use (Required ("applications.stakeholders" ))
79
82
routeGroup .PUT (AppStakeholdersRoot , h .StakeholdersUpdate )
83
+ // Assessments
84
+ routeGroup = e .Group ("/" )
85
+ routeGroup .Use (Required ("applications.assessments" ))
86
+ routeGroup .GET (AppAssessmentsRoot , h .AssessmentList )
87
+ routeGroup .POST (AppAssessmentsRoot , h .AssessmentCreate )
80
88
}
81
89
82
90
// Get godoc
@@ -106,8 +114,39 @@ func (h ApplicationHandler) Get(ctx *gin.Context) {
106
114
return
107
115
}
108
116
117
+ questionnaire , err := assessment .NewQuestionnaireResolver (h .DB (ctx ))
118
+ if err != nil {
119
+ _ = ctx .Error (err )
120
+ return
121
+ }
122
+ membership := assessment .NewMembershipResolver (h .DB (ctx ))
123
+ tagsResolver , err := assessment .NewTagResolver (h .DB (ctx ))
124
+ if err != nil {
125
+ _ = ctx .Error (err )
126
+ return
127
+ }
128
+ resolver := assessment .NewApplicationResolver (m , tagsResolver , membership , questionnaire )
129
+ archetypes , err := resolver .Archetypes ()
130
+ if err != nil {
131
+ _ = ctx .Error (err )
132
+ return
133
+ }
134
+ archetypeTags , err := resolver .ArchetypeTags ()
135
+ if err != nil {
136
+ _ = ctx .Error (err )
137
+ return
138
+ }
139
+
109
140
r := Application {}
110
141
r .With (m , tags )
142
+ r .WithArchetypes (archetypes )
143
+ r .WithSourcedTags (archetypeTags , "archetype" )
144
+ r .WithSourcedTags (resolver .AssessmentTags (), "assessment" )
145
+ r .Assessed , err = resolver .Assessed ()
146
+ if err != nil {
147
+ _ = ctx .Error (err )
148
+ return
149
+ }
111
150
112
151
h .Respond (ctx , http .StatusOK , r )
113
152
}
@@ -128,6 +167,19 @@ func (h ApplicationHandler) List(ctx *gin.Context) {
128
167
_ = ctx .Error (result .Error )
129
168
return
130
169
}
170
+
171
+ questionnaire , err := assessment .NewQuestionnaireResolver (h .DB (ctx ))
172
+ if err != nil {
173
+ _ = ctx .Error (err )
174
+ return
175
+ }
176
+ membership := assessment .NewMembershipResolver (h .DB (ctx ))
177
+ tagsResolver , err := assessment .NewTagResolver (h .DB (ctx ))
178
+ if err != nil {
179
+ _ = ctx .Error (err )
180
+ return
181
+ }
182
+
131
183
resources := []Application {}
132
184
for i := range list {
133
185
tags := []model.ApplicationTag {}
@@ -137,9 +189,27 @@ func (h ApplicationHandler) List(ctx *gin.Context) {
137
189
_ = ctx .Error (result .Error )
138
190
return
139
191
}
140
-
192
+ resolver := assessment .NewApplicationResolver (& list [i ], tagsResolver , membership , questionnaire )
193
+ archetypes , aErr := resolver .Archetypes ()
194
+ if aErr != nil {
195
+ _ = ctx .Error (aErr )
196
+ return
197
+ }
198
+ archetypeTags , aErr := resolver .ArchetypeTags ()
199
+ if aErr != nil {
200
+ _ = ctx .Error (aErr )
201
+ return
202
+ }
141
203
r := Application {}
142
204
r .With (& list [i ], tags )
205
+ r .WithArchetypes (archetypes )
206
+ r .WithSourcedTags (archetypeTags , "archetype" )
207
+ r .WithSourcedTags (resolver .AssessmentTags (), "assessment" )
208
+ r .Assessed , err = resolver .Assessed ()
209
+ if err != nil {
210
+ _ = ctx .Error (err )
211
+ return
212
+ }
143
213
resources = append (resources , r )
144
214
}
145
215
@@ -182,7 +252,38 @@ func (h ApplicationHandler) Create(ctx *gin.Context) {
182
252
}
183
253
}
184
254
255
+ questionnaire , err := assessment .NewQuestionnaireResolver (h .DB (ctx ))
256
+ if err != nil {
257
+ _ = ctx .Error (err )
258
+ return
259
+ }
260
+ membership := assessment .NewMembershipResolver (h .DB (ctx ))
261
+ tagsResolver , err := assessment .NewTagResolver (h .DB (ctx ))
262
+ if err != nil {
263
+ _ = ctx .Error (err )
264
+ return
265
+ }
266
+ resolver := assessment .NewApplicationResolver (m , tagsResolver , membership , questionnaire )
267
+ archetypes , err := resolver .Archetypes ()
268
+ if err != nil {
269
+ _ = ctx .Error (err )
270
+ return
271
+ }
272
+ archetypeTags , err := resolver .ArchetypeTags ()
273
+ if err != nil {
274
+ _ = ctx .Error (err )
275
+ return
276
+ }
277
+
185
278
r .With (m , tags )
279
+ r .WithArchetypes (archetypes )
280
+ r .WithSourcedTags (archetypeTags , "archetype" )
281
+ r .WithSourcedTags (resolver .AssessmentTags (), "assessment" )
282
+ r .Assessed , err = resolver .Assessed ()
283
+ if err != nil {
284
+ _ = ctx .Error (err )
285
+ return
286
+ }
186
287
187
288
h .Respond (ctx , http .StatusCreated , r )
188
289
}
@@ -202,12 +303,6 @@ func (h ApplicationHandler) Delete(ctx *gin.Context) {
202
303
_ = ctx .Error (result .Error )
203
304
return
204
305
}
205
- p := Pathfinder {}
206
- err := p .DeleteAssessment ([]uint {id }, ctx )
207
- if err != nil {
208
- _ = ctx .Error (err )
209
- return
210
- }
211
306
result = h .DB (ctx ).Delete (m )
212
307
if result .Error != nil {
213
308
_ = ctx .Error (result .Error )
@@ -231,12 +326,6 @@ func (h ApplicationHandler) DeleteList(ctx *gin.Context) {
231
326
_ = ctx .Error (err )
232
327
return
233
328
}
234
- p := Pathfinder {}
235
- err = p .DeleteAssessment (ids , ctx )
236
- if err != nil {
237
- _ = ctx .Error (err )
238
- return
239
- }
240
329
err = h .DB (ctx ).Delete (
241
330
& model.Application {},
242
331
"id IN ?" ,
@@ -843,6 +932,115 @@ func (h ApplicationHandler) StakeholdersUpdate(ctx *gin.Context) {
843
932
h .Status (ctx , http .StatusNoContent )
844
933
}
845
934
935
+ // AssessmentList godoc
936
+ // @summary List the assessments of an Application and any it inherits from its archetypes.
937
+ // @description List the assessments of an Application and any it inherits from its archetypes.
938
+ // @tags applications
939
+ // @success 200 {object} []api.Assessment
940
+ // @router /applications/{id}/assessments [get]
941
+ // @param id path int true "Application ID"
942
+ func (h ApplicationHandler ) AssessmentList (ctx * gin.Context ) {
943
+ m := & model.Application {}
944
+ id := h .pk (ctx )
945
+ db := h .preLoad (h .DB (ctx ), clause .Associations )
946
+ db = db .Omit ("Analyses" )
947
+ result := db .First (m , id )
948
+ if result .Error != nil {
949
+ _ = ctx .Error (result .Error )
950
+ return
951
+ }
952
+
953
+ questionnaire , err := assessment .NewQuestionnaireResolver (h .DB (ctx ))
954
+ if err != nil {
955
+ _ = ctx .Error (err )
956
+ return
957
+ }
958
+ membership := assessment .NewMembershipResolver (h .DB (ctx ))
959
+ tagsResolver , err := assessment .NewTagResolver (h .DB (ctx ))
960
+ if err != nil {
961
+ _ = ctx .Error (err )
962
+ return
963
+ }
964
+ resolver := assessment .NewApplicationResolver (m , tagsResolver , membership , questionnaire )
965
+ archetypes , err := resolver .Archetypes ()
966
+ if err != nil {
967
+ _ = ctx .Error (err )
968
+ return
969
+ }
970
+
971
+ assessments := m .Assessments
972
+ for _ , a := range archetypes {
973
+ assessments = append (assessments , a .Assessments ... )
974
+ }
975
+
976
+ resources := []Assessment {}
977
+ for i := range assessments {
978
+ r := Assessment {}
979
+ r .With (& assessments [i ])
980
+ resources = append (resources , r )
981
+ }
982
+
983
+ h .Respond (ctx , http .StatusOK , resources )
984
+ }
985
+
986
+ // AssessmentCreate godoc
987
+ // @summary Create an application assessment.
988
+ // @description Create an application assessment.
989
+ // @tags applications
990
+ // @accept json
991
+ // @produce json
992
+ // @success 201 {object} api.Assessment
993
+ // @router /applications/{id}/assessments [post]
994
+ // @param assessment body api.Assessment true "Assessment data"
995
+ func (h ApplicationHandler ) AssessmentCreate (ctx * gin.Context ) {
996
+ application := & model.Application {}
997
+ id := h .pk (ctx )
998
+ db := h .preLoad (h .DB (ctx ), clause .Associations )
999
+ db = db .Omit ("Analyses" )
1000
+ result := db .First (application , id )
1001
+ if result .Error != nil {
1002
+ _ = ctx .Error (result .Error )
1003
+ return
1004
+ }
1005
+
1006
+ r := & Assessment {}
1007
+ err := h .Bind (ctx , r )
1008
+ if err != nil {
1009
+ _ = ctx .Error (err )
1010
+ return
1011
+ }
1012
+ r .Application = & Ref {ID : id }
1013
+ r .Archetype = nil
1014
+ q := & model.Questionnaire {}
1015
+ db = h .preLoad (h .DB (ctx ))
1016
+ result = db .First (q , r .Questionnaire .ID )
1017
+ if result .Error != nil {
1018
+ _ = ctx .Error (result .Error )
1019
+ return
1020
+ }
1021
+ m := r .Model ()
1022
+ m .Sections = q .Sections
1023
+ m .Thresholds = q .Thresholds
1024
+ m .RiskMessages = q .RiskMessages
1025
+ m .CreateUser = h .CurrentUser (ctx )
1026
+
1027
+ resolver , err := assessment .NewTagResolver (h .DB (ctx ))
1028
+ if err != nil {
1029
+ _ = ctx .Error (err )
1030
+ return
1031
+ }
1032
+ assessment .PrepareForApplication (resolver , application , m )
1033
+
1034
+ result = h .DB (ctx ).Create (m )
1035
+ if result .Error != nil {
1036
+ _ = ctx .Error (result .Error )
1037
+ return
1038
+ }
1039
+
1040
+ r .With (m )
1041
+ h .Respond (ctx , http .StatusCreated , r )
1042
+ }
1043
+
846
1044
//
847
1045
// Application REST resource.
848
1046
type Application struct {
@@ -860,6 +1058,9 @@ type Application struct {
860
1058
Owner * Ref `json:"owner"`
861
1059
Contributors []Ref `json:"contributors"`
862
1060
MigrationWave * Ref `json:"migrationWave"`
1061
+ Archetypes []Ref `json:"archetypes"`
1062
+ Assessments []Ref `json:"assessments"`
1063
+ Assessed bool `json:"assessed"`
863
1064
}
864
1065
865
1066
//
@@ -901,6 +1102,32 @@ func (r *Application) With(m *model.Application, tags []model.ApplicationTag) {
901
1102
ref )
902
1103
}
903
1104
r .MigrationWave = r .refPtr (m .MigrationWaveID , m .MigrationWave )
1105
+ r .Assessments = []Ref {}
1106
+ for _ , a := range m .Assessments {
1107
+ ref := Ref {}
1108
+ ref .With (a .ID , "" )
1109
+ r .Assessments = append (r .Assessments , ref )
1110
+ }
1111
+ }
1112
+
1113
+ //
1114
+ // WithArchetypes updates the resource with archetypes.
1115
+ func (r * Application ) WithArchetypes (archetypes []model.Archetype ) {
1116
+ for _ , a := range archetypes {
1117
+ ref := Ref {}
1118
+ ref .With (a .ID , a .Name )
1119
+ r .Archetypes = append (r .Archetypes , ref )
1120
+ }
1121
+ }
1122
+
1123
+ //
1124
+ // WithSourcedTags updates the resource with tags derived from assessments.
1125
+ func (r * Application ) WithSourcedTags (tags []model.Tag , source string ) {
1126
+ for _ , t := range tags {
1127
+ ref := TagRef {}
1128
+ ref .With (t .ID , t .Name , source )
1129
+ r .Tags = append (r .Tags , ref )
1130
+ }
904
1131
}
905
1132
906
1133
//
0 commit comments