Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 6b1925a

Browse files
Merge pull request #61 from bancodobrasil/feat/swagger-authentication
Feat/swagger authentication
2 parents a5d8298 + b09fb81 commit 6b1925a

File tree

7 files changed

+92
-29
lines changed

7 files changed

+92
-29
lines changed

controllers/v1/rulesheets.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func NewRulesheets(service services.Rulesheets) Rulesheets {
4848
// @Failure 500 {object} responses.Error "Internal Server Error"
4949
// @Failure default {object} responses.Error
5050
// @Response 404 "Not Found"
51-
// @Security Authentication
51+
// @Security Authentication Api Key
52+
// @Security Authentication Bearer Token
5253
// @Router /rulesheets [post]
5354
func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {
5455

@@ -95,8 +96,10 @@ func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {
9596
}
9697

9798
var response = responses.NewRulesheet(&dto)
99+
//id := c.Query("id")
98100
c.JSON(http.StatusCreated, response)
99101
}
102+
100103
}
101104

102105
// GetRulesheets godoc
@@ -114,7 +117,8 @@ func (rc *rulesheets) CreateRulesheet() gin.HandlerFunc {
114117
// @Failure 500 {object} responses.Error "Internal Server Error"
115118
// @Failure default {object} responses.Error
116119
// @Response 404 "Not Found"
117-
// @Security Authentication
120+
// @Security Authentication Api Key
121+
// @Security Authentication Bearer Token
118122
// @Router /rulesheets/ [get]
119123
func (rc *rulesheets) GetRulesheets() gin.HandlerFunc {
120124

@@ -217,7 +221,8 @@ func (rc *rulesheets) GetRulesheets() gin.HandlerFunc {
217221
// @Failure 500 {object} responses.Error "Internal Server Error"
218222
// @Failure default {object} responses.Error
219223
// @Response 404 "Not Found"
220-
// @Security Authentication
224+
// @Security Authentication Api Key
225+
// @Security Authentication Bearer Token
221226
// @Router /rulesheets/{id} [get]
222227
func (rc *rulesheets) GetRulesheet() gin.HandlerFunc {
223228

@@ -269,7 +274,8 @@ func (rc *rulesheets) GetRulesheet() gin.HandlerFunc {
269274
// @Failure 500 {object} responses.Error "Internal Server Error"
270275
// @Failure default {object} responses.Error
271276
// @Response 404 "Not Found"
272-
// @Security Authentication
277+
// @Security Authentication Api Key
278+
// @Security Authentication Bearer Token
273279
// @Router /rulesheets/{id} [put]
274280
func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {
275281

@@ -366,7 +372,8 @@ func (rc *rulesheets) UpdateRulesheet() gin.HandlerFunc {
366372
// @Failure 500 {object} responses.Error "Internal Server Error"
367373
// @Failure default {object} responses.Error
368374
// @Response 404 "Not Found"
369-
// @Security Authentication
375+
// @Security Authentication Api Key
376+
// @Security Authentication Bearer Token
370377
// @Router /rulesheets/{id} [delete]
371378
func (rc *rulesheets) DeleteRulesheet() gin.HandlerFunc {
372379

docs/docs.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ const docTemplate = `{
2929
"post": {
3030
"security": [
3131
{
32-
"Authentication": []
32+
"Authentication Api Key": []
33+
},
34+
{
35+
"Authentication Bearer Token": []
3336
}
3437
],
3538
"description": "Create Rulesheet description",
@@ -95,7 +98,10 @@ const docTemplate = `{
9598
"get": {
9699
"security": [
97100
{
98-
"Authentication": []
101+
"Authentication Api Key": []
102+
},
103+
{
104+
"Authentication Bearer Token": []
99105
}
100106
],
101107
"description": "List Rulesheet description",
@@ -173,7 +179,10 @@ const docTemplate = `{
173179
"get": {
174180
"security": [
175181
{
176-
"Authentication": []
182+
"Authentication Api Key": []
183+
},
184+
{
185+
"Authentication Bearer Token": []
177186
}
178187
],
179188
"description": "Get Rulesheet by ID description",
@@ -238,7 +247,10 @@ const docTemplate = `{
238247
"put": {
239248
"security": [
240249
{
241-
"Authentication": []
250+
"Authentication Api Key": []
251+
},
252+
{
253+
"Authentication Bearer Token": []
242254
}
243255
],
244256
"description": "Update Rulesheet by ID description",
@@ -312,7 +324,10 @@ const docTemplate = `{
312324
"delete": {
313325
"security": [
314326
{
315-
"Authentication": []
327+
"Authentication Api Key": []
328+
},
329+
{
330+
"Authentication Bearer Token": []
316331
}
317332
],
318333
"description": "Delete Rulesheet by ID description",
@@ -446,7 +461,12 @@ const docTemplate = `{
446461
}
447462
},
448463
"securityDefinitions": {
449-
"Authentication": {
464+
"Authentication Api Key": {
465+
"type": "apiKey",
466+
"name": "X-API-Key",
467+
"in": "header"
468+
},
469+
"Authentication Bearer Token": {
450470
"type": "apiKey",
451471
"name": "Authorization",
452472
"in": "header"

docs/swagger.json

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
"post": {
2323
"security": [
2424
{
25-
"Authentication": []
25+
"Authentication Api Key": []
26+
},
27+
{
28+
"Authentication Bearer Token": []
2629
}
2730
],
2831
"description": "Create Rulesheet description",
@@ -88,7 +91,10 @@
8891
"get": {
8992
"security": [
9093
{
91-
"Authentication": []
94+
"Authentication Api Key": []
95+
},
96+
{
97+
"Authentication Bearer Token": []
9298
}
9399
],
94100
"description": "List Rulesheet description",
@@ -166,7 +172,10 @@
166172
"get": {
167173
"security": [
168174
{
169-
"Authentication": []
175+
"Authentication Api Key": []
176+
},
177+
{
178+
"Authentication Bearer Token": []
170179
}
171180
],
172181
"description": "Get Rulesheet by ID description",
@@ -231,7 +240,10 @@
231240
"put": {
232241
"security": [
233242
{
234-
"Authentication": []
243+
"Authentication Api Key": []
244+
},
245+
{
246+
"Authentication Bearer Token": []
235247
}
236248
],
237249
"description": "Update Rulesheet by ID description",
@@ -305,7 +317,10 @@
305317
"delete": {
306318
"security": [
307319
{
308-
"Authentication": []
320+
"Authentication Api Key": []
321+
},
322+
{
323+
"Authentication Bearer Token": []
309324
}
310325
],
311326
"description": "Delete Rulesheet by ID description",
@@ -439,7 +454,12 @@
439454
}
440455
},
441456
"securityDefinitions": {
442-
"Authentication": {
457+
"Authentication Api Key": {
458+
"type": "apiKey",
459+
"name": "X-API-Key",
460+
"in": "header"
461+
},
462+
"Authentication Bearer Token": {
443463
"type": "apiKey",
444464
"name": "Authorization",
445465
"in": "header"

docs/swagger.yaml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ paths:
100100
schema:
101101
$ref: '#/definitions/v1.Error'
102102
security:
103-
- Authentication: []
103+
- Authentication Api Key: []
104+
- Authentication Bearer Token: []
104105
summary: Create Rulesheet
105106
tags:
106107
- Rulesheet
@@ -150,7 +151,8 @@ paths:
150151
schema:
151152
$ref: '#/definitions/v1.Error'
152153
security:
153-
- Authentication: []
154+
- Authentication Api Key: []
155+
- Authentication Bearer Token: []
154156
summary: List Rulesheets
155157
tags:
156158
- Rulesheet
@@ -191,7 +193,8 @@ paths:
191193
schema:
192194
$ref: '#/definitions/v1.Error'
193195
security:
194-
- Authentication: []
196+
- Authentication Api Key: []
197+
- Authentication Bearer Token: []
195198
summary: Delete Rulesheet by ID
196199
tags:
197200
- Rulesheet
@@ -233,7 +236,8 @@ paths:
233236
schema:
234237
$ref: '#/definitions/v1.Error'
235238
security:
236-
- Authentication: []
239+
- Authentication Api Key: []
240+
- Authentication Bearer Token: []
237241
summary: Get Rulesheet by ID
238242
tags:
239243
- Rulesheet
@@ -281,12 +285,17 @@ paths:
281285
schema:
282286
$ref: '#/definitions/v1.Error'
283287
security:
284-
- Authentication: []
288+
- Authentication Api Key: []
289+
- Authentication Bearer Token: []
285290
summary: Update Rulesheet by ID
286291
tags:
287292
- Rulesheet
288293
securityDefinitions:
289-
Authentication:
294+
Authentication Api Key:
295+
in: header
296+
name: X-API-Key
297+
type: apiKey
298+
Authentication Bearer Token:
290299
in: header
291300
name: Authorization
292301
type: apiKey

main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func setupLog() {
4040
// @host localhost:9007
4141
// @BasePath /api/v1
4242

43-
// @securityDefinitions.apikey Authentication
43+
// @securityDefinitions.apikey Authentication Api Key
44+
// @in header
45+
// @name X-API-Key
46+
47+
// @securityDefinitions.apikey Authentication Bearer Token
4448
// @in header
4549
// @name Authorization
4650

models/rulesheet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
// Rulesheet ...
1111
type Rulesheet struct {
1212
gorm.Model
13-
Name string
13+
Name string `gorm:"type:varchar(255);uniqueIndex"`
1414
Description string
15-
Slug string //`gorm:"unique_index"`
15+
Slug string `gorm:"unique_index"`
1616
HasStringRule bool
1717
CreatedAt *time.Time
1818
UpdatedAt *time.Time

services/rulesheets.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,20 @@ func NewRulesheets(repository repository.Rulesheets, gitlabService Gitlab) Rules
4343
// CreateRulesheet ...
4444
func (rs rulesheets) Create(ctx context.Context, rulesheetDTO *dtos.Rulesheet) (err error) {
4545

46-
//TODO verifica unicidade do nome
4746
rulesheet, _ := models.NewRulesheetV1(*rulesheetDTO)
48-
rulesheet.Slug = slug.Make(rulesheet.Slug)
47+
if rulesheet.Slug == "" {
48+
rulesheet.Slug = slug.Make(rulesheet.Name)
49+
}
50+
4951
fmt.Print(rulesheet.Slug)
5052

5153
err = rs.repository.Create(ctx, &rulesheet)
5254
if err != nil {
5355
log.Errorf("Error on create rulesheet into repository: %v", err)
5456
return
5557
}
56-
58+
rulesheetDTO.ID = rulesheet.ID
59+
rulesheetDTO.Slug = rulesheet.Slug
5760
err = rs.gitlabService.Save(rulesheetDTO, "[FEATWS BOT] Create Repo")
5861
if err != nil {
5962
log.Errorf("Error on save rulesheet into repository: %v", err)

0 commit comments

Comments
 (0)