@@ -8,12 +8,35 @@ import (
8
8
"net/http"
9
9
"time"
10
10
11
+ "github.com/fossology/LicenseDb/pkg/auth"
12
+ "github.com/fossology/LicenseDb/pkg/db"
11
13
"github.com/fossology/LicenseDb/pkg/models"
12
14
"github.com/gin-gonic/gin"
13
- "gorm.io/gorm"
14
15
)
15
16
16
- var DB * gorm.DB
17
+ func Router () * gin.Engine {
18
+ // r is a default instance of gin engine
19
+ r := gin .Default ()
20
+
21
+ // return error for invalid routes
22
+ r .NoRoute (HandleInvalidUrl )
23
+
24
+ // authorization not required for these routes
25
+ r .GET ("/api/license/:shortname" , GetLicense )
26
+ r .GET ("/api/licenses" , SearchInLicense )
27
+ r .GET ("/api/users" , auth .GetAllUser )
28
+ r .GET ("/api/user/:id" , auth .GetUser )
29
+
30
+ // set up authentication
31
+ authorized := r .Group ("/" )
32
+ authorized .Use (auth .AuthenticationMiddleware ())
33
+
34
+ authorized .POST ("/api/license" , CreateLicense )
35
+ authorized .PATCH ("/api/license/update/:shortname" , UpdateLicense )
36
+ authorized .POST ("/api/user" , auth .CreateUser )
37
+
38
+ return r
39
+ }
17
40
18
41
func HandleInvalidUrl (c * gin.Context ) {
19
42
@@ -27,9 +50,10 @@ func HandleInvalidUrl(c *gin.Context) {
27
50
c .JSON (http .StatusNotFound , er )
28
51
}
29
52
func GetAllLicense (c * gin.Context ) {
53
+
30
54
var licenses []models.LicenseDB
31
55
32
- err := DB .Find (& licenses ).Error
56
+ err := db . DB .Find (& licenses ).Error
33
57
if err != nil {
34
58
er := models.LicenseError {
35
59
Status : http .StatusBadRequest ,
@@ -44,7 +68,7 @@ func GetAllLicense(c *gin.Context) {
44
68
res := models.LicenseResponse {
45
69
Data : licenses ,
46
70
Status : http .StatusOK ,
47
- Meta : models.Meta {
71
+ Meta : models.PaginationMeta {
48
72
ResourceCount : len (licenses ),
49
73
},
50
74
}
@@ -60,7 +84,7 @@ func GetLicense(c *gin.Context) {
60
84
return
61
85
}
62
86
63
- err := DB .Where ("shortname = ?" , queryParam ).First (& license ).Error
87
+ err := db . DB .Where ("shortname = ?" , queryParam ).First (& license ).Error
64
88
65
89
if err != nil {
66
90
er := models.LicenseError {
@@ -77,7 +101,7 @@ func GetLicense(c *gin.Context) {
77
101
res := models.LicenseResponse {
78
102
Data : []models.LicenseDB {license },
79
103
Status : http .StatusOK ,
80
- Meta : models.Meta {
104
+ Meta : models.PaginationMeta {
81
105
ResourceCount : 1 ,
82
106
},
83
107
}
@@ -105,7 +129,7 @@ func CreateLicense(c *gin.Context) {
105
129
}
106
130
license := models .LicenseDB (input )
107
131
108
- result := DB .FirstOrCreate (& license )
132
+ result := db . DB .FirstOrCreate (& license )
109
133
if result .RowsAffected == 0 {
110
134
111
135
er := models.LicenseError {
@@ -132,7 +156,7 @@ func CreateLicense(c *gin.Context) {
132
156
res := models.LicenseResponse {
133
157
Data : []models.LicenseDB {license },
134
158
Status : http .StatusCreated ,
135
- Meta : models.Meta {
159
+ Meta : models.PaginationMeta {
136
160
ResourceCount : 1 ,
137
161
},
138
162
}
@@ -144,7 +168,7 @@ func UpdateLicense(c *gin.Context) {
144
168
var update models.LicenseDB
145
169
var license models.LicenseDB
146
170
shortname := c .Param ("shortname" )
147
- if err := DB .Where ("shortname = ?" , shortname ).First (& license ).Error ; err != nil {
171
+ if err := db . DB .Where ("shortname = ?" , shortname ).First (& license ).Error ; err != nil {
148
172
er := models.LicenseError {
149
173
Status : http .StatusBadRequest ,
150
174
Message : fmt .Sprintf ("license with shortname '%s' not found" , shortname ),
@@ -166,7 +190,7 @@ func UpdateLicense(c *gin.Context) {
166
190
c .JSON (http .StatusBadRequest , er )
167
191
return
168
192
}
169
- if err := DB .Model (& license ).Updates (update ).Error ; err != nil {
193
+ if err := db . DB .Model (& license ).Updates (update ).Error ; err != nil {
170
194
er := models.LicenseError {
171
195
Status : http .StatusInternalServerError ,
172
196
Message : "Failed to update license" ,
@@ -180,7 +204,7 @@ func UpdateLicense(c *gin.Context) {
180
204
res := models.LicenseResponse {
181
205
Data : []models.LicenseDB {license },
182
206
Status : http .StatusOK ,
183
- Meta : models.Meta {
207
+ Meta : models.PaginationMeta {
184
208
ResourceCount : 1 ,
185
209
},
186
210
}
@@ -193,16 +217,62 @@ func SearchInLicense(c *gin.Context) {
193
217
field := c .Query ("field" )
194
218
search_term := c .Query ("search_term" )
195
219
search := c .Query ("search" )
196
- if field == "" && search_term == "" {
220
+ SpdxId := c .Query ("spdxid" )
221
+ DetectorType := c .Query ("detector_type" )
222
+ GPLv2compatible := c .Query ("gplv2compatible" )
223
+ GPLv3compatible := c .Query ("gplv3compatible" )
224
+ marydone := c .Query ("marydone" )
225
+ active := c .Query ("active" )
226
+ OSIapproved := c .Query ("osiapproved" )
227
+ fsffree := c .Query ("fsffree" )
228
+ copyleft := c .Query ("copyleft" )
229
+ var license []models.LicenseDB
230
+ query := db .DB .Model (& license )
231
+
232
+ if field == "" && search_term == "" && SpdxId == "" && GPLv2compatible == "" && GPLv3compatible == "" && DetectorType == "" && marydone == "" && active == "" && fsffree == "" && OSIapproved == "" && copyleft == "" {
197
233
GetAllLicense (c )
198
234
return
199
235
}
200
- var query * gorm.DB
201
- var license []models.LicenseDB
236
+ if active != "" {
237
+ query = query .Where ("active=?" , active )
238
+ }
239
+
240
+ if fsffree != "" {
241
+ query = query .Where ("fs_ffree=?" , fsffree )
242
+ }
243
+
244
+ if OSIapproved != "" {
245
+ query = query .Where ("os_iapproved=?" , OSIapproved )
246
+ }
247
+
248
+ if copyleft != "" {
249
+ query = query .Where ("copyleft=?" , copyleft )
250
+ }
251
+
252
+ if SpdxId != "" {
253
+ query = query .Where ("spdx_id=?" , SpdxId )
254
+ }
255
+
256
+ if DetectorType != "" {
257
+ query = query .Where ("detector_type=?" , DetectorType )
258
+ }
259
+
260
+ if GPLv2compatible != "" {
261
+ query = query .Where ("gp_lv2compatible=?" , GPLv2compatible )
262
+ }
263
+
264
+ if GPLv3compatible != "" {
265
+ query = query .Where ("gp_lv3compatible=?" , GPLv3compatible )
266
+ }
267
+
268
+ if marydone != "" {
269
+ query = query .Where ("marydone=?" , marydone )
270
+ }
271
+
202
272
if search == "fuzzy" {
203
- query = DB .Where (fmt .Sprintf ("%s ILIKE ?" , field ), fmt .Sprintf ("%%%s%%" , search_term )). Find ( & license )
273
+ query = query .Where (fmt .Sprintf ("%s ILIKE ?" , field ), fmt .Sprintf ("%%%s%%" , search_term ))
204
274
} else if search == "" || search == "full_text_search" {
205
- query = DB .Where (field + " @@ plainto_tsquery(?)" , search_term ). Find ( & license )
275
+ query = query .Where (field + " @@ plainto_tsquery(?)" , search_term )
206
276
} else {
207
277
er := models.LicenseError {
208
278
Status : http .StatusBadRequest ,
@@ -226,10 +296,12 @@ func SearchInLicense(c *gin.Context) {
226
296
c .JSON (http .StatusBadRequest , er )
227
297
return
228
298
}
299
+ query .Find (& license )
300
+
229
301
res := models.LicenseResponse {
230
302
Data : license ,
231
303
Status : http .StatusOK ,
232
- Meta : models.Meta {
304
+ Meta : models.PaginationMeta {
233
305
ResourceCount : len (license ),
234
306
},
235
307
}
0 commit comments