Skip to content

Commit b81e895

Browse files
feat: documentação da rota api/ongs POST (#211)
* feat: documentação da rota api/ongs POST fix: alteração do tipo RawJson para correção na falha de doc. * refact: remoção de resposta json para o corpo 201.
1 parent 42696f1 commit b81e895

7 files changed

Lines changed: 217 additions & 27 deletions

File tree

api/controllers/ong.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ func NewOngcontroller(usecase *usecase.OngUsecase) *OngController {
2525
}
2626
}
2727

28+
// Add Ong to the database.
29+
// @Summary Create Ong
30+
// @Description Sends the Ong registration data via the request body for persistence in the database.
31+
// @Tags Ong
32+
// @Accept json
33+
// @Param ongDto body dto.OngInsertDto true "Ong object information for registration"
34+
// @Success 201
35+
// @Failure 400
36+
// @Failure 500
37+
// @Router /ongs/ [post]
2838
func (oc *OngController) Insert(w http.ResponseWriter, r *http.Request) {
2939
var ongDto dto.OngInsertDto
3040
err := json.NewDecoder(r.Body).Decode(&ongDto)

entity/dto/ong_insert_dto.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package dto
22

33
import (
4-
"encoding/json"
54
"time"
65
)
76

87
type OngInsertDto struct {
98
User UserInsertDto
10-
OpeningHours string `json:"openingHours"`
11-
AdoptionPolicy string `json:"adoptionPolicy"`
12-
BirthDate *time.Time `json:"birthdate"`
13-
Links *json.RawMessage `json:"links"`
14-
CreatedAt *time.Time `json:"createdAt" db:"created_at"`
15-
UpdatedAt *time.Time `json:"updatedAt" db:"updated_at"`
9+
OpeningHours string `json:"openingHours"`
10+
AdoptionPolicy string `json:"adoptionPolicy"`
11+
BirthDate *time.Time `json:"birthdate"`
12+
Links []Link `json:"links"`
13+
CreatedAt *time.Time `json:"createdAt" db:"created_at"`
14+
UpdatedAt *time.Time `json:"updatedAt" db:"updated_at"`
15+
}
16+
17+
type Link struct {
18+
URL string
19+
Text string
1620
}

entity/ong.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package entity
22

33
import (
4-
"encoding/json"
5-
"fmt"
6-
"log"
74
"pet-dex-backend/v2/entity/dto"
85
"pet-dex-backend/v2/pkg/uniqueEntityId"
96
"time"
@@ -16,7 +13,7 @@ type Ong struct {
1613
Phone string `json:"phone" db:"phone"`
1714
OpeningHours string `json:"openingHours" db:"openingHours"`
1815
AdoptionPolicy string `json:"adoptionPolicy" db:"adoptionPolicy"`
19-
Links *json.RawMessage `json:"links"`
16+
Links []dto.LinkDto `json:"links"`
2017

2118
CreatedAt *time.Time `json:"createdAt" db:"created_at"`
2219
UpdatedAt *time.Time `json:"updatedAt" db:"updated_at"`
@@ -28,18 +25,12 @@ func NewOng(ong dto.OngInsertDto) *Ong {
2825

2926
user := NewUser(ong.User)
3027

31-
var socials *json.RawMessage
32-
err := json.Unmarshal(*ong.Links, &socials)
33-
if err != nil {
34-
log.Fatalln("error:", err)
35-
}
36-
3728
return &Ong{
3829
ID: ongId,
3930
UserID: user.ID,
4031
User: *user,
4132
Phone: user.Phone,
42-
Links: socials,
33+
Links: []dto.LinkDto{},
4334
OpeningHours: ong.OpeningHours,
4435
AdoptionPolicy: ong.AdoptionPolicy,
4536
}
@@ -55,17 +46,10 @@ func OngToUpdate(ong dto.OngUpdateDto) (*Ong, error) {
5546
BirthDate: ong.User.BirthDate,
5647
}
5748

58-
linksJson, err := json.Marshal(ong.Links)
59-
if err != nil {
60-
return nil, fmt.Errorf("serializing links error: %w", err)
61-
}
62-
63-
linksRawMessage := json.RawMessage(linksJson)
64-
6549
return &Ong{
6650
User: user,
6751
Phone: user.Phone,
68-
Links: &linksRawMessage,
52+
Links: ong.Links,
6953
OpeningHours: ong.OpeningHours,
7054
AdoptionPolicy: ong.AdoptionPolicy,
7155
}, nil

infra/db/ong_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (or *OngRepository) Update(id uniqueEntityId.ID, ongToUpdate entity.Ong) er
132132
values = append(values, ongToUpdate.AdoptionPolicy)
133133
}
134134

135-
if string(*ongToUpdate.Links) != "" {
135+
if len(ongToUpdate.Links) != 0 {
136136
query = query + " links =?"
137137
values = append(values, ongToUpdate.Links)
138138
}

swagger/docs.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,38 @@ const docTemplate = `{
7676
"description": "Internal Server Error"
7777
}
7878
}
79+
},
80+
"post": {
81+
"description": "Sends the Ong registration data via the request body for persistence in the database.",
82+
"consumes": [
83+
"application/json"
84+
],
85+
"tags": [
86+
"Ong"
87+
],
88+
"summary": "Create Ong",
89+
"parameters": [
90+
{
91+
"description": "Ong object information for registration",
92+
"name": "ongDto",
93+
"in": "body",
94+
"required": true,
95+
"schema": {
96+
"$ref": "#/definitions/dto.OngInsertDto"
97+
}
98+
}
99+
],
100+
"responses": {
101+
"201": {
102+
"description": "Created"
103+
},
104+
"400": {
105+
"description": "Bad Request"
106+
},
107+
"500": {
108+
"description": "Internal Server Error"
109+
}
110+
}
79111
}
80112
},
81113
"/ongs/{ongID}": {
@@ -651,6 +683,17 @@ const docTemplate = `{
651683
}
652684
}
653685
},
686+
"dto.Link": {
687+
"type": "object",
688+
"properties": {
689+
"text": {
690+
"type": "string"
691+
},
692+
"url": {
693+
"type": "string"
694+
}
695+
}
696+
},
654697
"dto.LinkDto": {
655698
"type": "object",
656699
"properties": {
@@ -664,6 +707,35 @@ const docTemplate = `{
664707
}
665708
}
666709
},
710+
"dto.OngInsertDto": {
711+
"type": "object",
712+
"properties": {
713+
"adoptionPolicy": {
714+
"type": "string"
715+
},
716+
"birthdate": {
717+
"type": "string"
718+
},
719+
"createdAt": {
720+
"type": "string"
721+
},
722+
"links": {
723+
"type": "array",
724+
"items": {
725+
"$ref": "#/definitions/dto.Link"
726+
}
727+
},
728+
"openingHours": {
729+
"type": "string"
730+
},
731+
"updatedAt": {
732+
"type": "string"
733+
},
734+
"user": {
735+
"$ref": "#/definitions/dto.UserInsertDto"
736+
}
737+
}
738+
},
667739
"dto.OngListMapper": {
668740
"type": "object",
669741
"properties": {

swagger/swagger.json

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,38 @@
7070
"description": "Internal Server Error"
7171
}
7272
}
73+
},
74+
"post": {
75+
"description": "Sends the Ong registration data via the request body for persistence in the database.",
76+
"consumes": [
77+
"application/json"
78+
],
79+
"tags": [
80+
"Ong"
81+
],
82+
"summary": "Create Ong",
83+
"parameters": [
84+
{
85+
"description": "Ong object information for registration",
86+
"name": "ongDto",
87+
"in": "body",
88+
"required": true,
89+
"schema": {
90+
"$ref": "#/definitions/dto.OngInsertDto"
91+
}
92+
}
93+
],
94+
"responses": {
95+
"201": {
96+
"description": "Created"
97+
},
98+
"400": {
99+
"description": "Bad Request"
100+
},
101+
"500": {
102+
"description": "Internal Server Error"
103+
}
104+
}
73105
}
74106
},
75107
"/ongs/{ongID}": {
@@ -645,6 +677,17 @@
645677
}
646678
}
647679
},
680+
"dto.Link": {
681+
"type": "object",
682+
"properties": {
683+
"text": {
684+
"type": "string"
685+
},
686+
"url": {
687+
"type": "string"
688+
}
689+
}
690+
},
648691
"dto.LinkDto": {
649692
"type": "object",
650693
"properties": {
@@ -658,6 +701,35 @@
658701
}
659702
}
660703
},
704+
"dto.OngInsertDto": {
705+
"type": "object",
706+
"properties": {
707+
"adoptionPolicy": {
708+
"type": "string"
709+
},
710+
"birthdate": {
711+
"type": "string"
712+
},
713+
"createdAt": {
714+
"type": "string"
715+
},
716+
"links": {
717+
"type": "array",
718+
"items": {
719+
"$ref": "#/definitions/dto.Link"
720+
}
721+
},
722+
"openingHours": {
723+
"type": "string"
724+
},
725+
"updatedAt": {
726+
"type": "string"
727+
},
728+
"user": {
729+
"$ref": "#/definitions/dto.UserInsertDto"
730+
}
731+
}
732+
},
661733
"dto.OngListMapper": {
662734
"type": "object",
663735
"properties": {

swagger/swagger.yaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ definitions:
1212
example: Pastor Alemão
1313
type: string
1414
type: object
15+
dto.Link:
16+
properties:
17+
text:
18+
type: string
19+
url:
20+
type: string
21+
type: object
1522
dto.LinkDto:
1623
properties:
1724
text:
@@ -21,6 +28,25 @@ definitions:
2128
example: https://www.facebook.com/
2229
type: string
2330
type: object
31+
dto.OngInsertDto:
32+
properties:
33+
adoptionPolicy:
34+
type: string
35+
birthdate:
36+
type: string
37+
createdAt:
38+
type: string
39+
links:
40+
items:
41+
$ref: '#/definitions/dto.Link'
42+
type: array
43+
openingHours:
44+
type: string
45+
updatedAt:
46+
type: string
47+
user:
48+
$ref: '#/definitions/dto.UserInsertDto'
49+
type: object
2450
dto.OngListMapper:
2551
properties:
2652
address:
@@ -386,6 +412,28 @@ paths:
386412
summary: View list of Ong.
387413
tags:
388414
- Ong
415+
post:
416+
consumes:
417+
- application/json
418+
description: Sends the Ong registration data via the request body for persistence
419+
in the database.
420+
parameters:
421+
- description: Ong object information for registration
422+
in: body
423+
name: ongDto
424+
required: true
425+
schema:
426+
$ref: '#/definitions/dto.OngInsertDto'
427+
responses:
428+
"201":
429+
description: Created
430+
"400":
431+
description: Bad Request
432+
"500":
433+
description: Internal Server Error
434+
summary: Create Ong
435+
tags:
436+
- Ong
389437
/ongs/{ongID}:
390438
delete:
391439
consumes:

0 commit comments

Comments
 (0)