From 3384dbb2b50c2e412835908ad78a5a1310e05318 Mon Sep 17 00:00:00 2001 From: juliaM <35346206+juliaam@users.noreply.github.com> Date: Thu, 12 Sep 2024 16:59:49 -0300 Subject: [PATCH 1/6] feat: swagger user create (#199) * feat: doc user create * fix: right swagger indentation * fix: swagger rebbase * fix: edilton comments --- .tool-versions | 2 +- api/controllers/user.go | 11 +++++ entity/dto/user_insert.dto.go | 22 ++++----- swagger/docs.go | 86 +++++++++++++++++++++++++++++++++++ swagger/swagger.json | 86 +++++++++++++++++++++++++++++++++++ swagger/swagger.yaml | 60 ++++++++++++++++++++++++ 6 files changed, 255 insertions(+), 12 deletions(-) diff --git a/.tool-versions b/.tool-versions index e4ab54cc..3eaf48ee 100644 --- a/.tool-versions +++ b/.tool-versions @@ -1 +1 @@ -golang 1.21.1 +golang 1.21.5 diff --git a/api/controllers/user.go b/api/controllers/user.go index 139f83e7..68e675da 100644 --- a/api/controllers/user.go +++ b/api/controllers/user.go @@ -23,6 +23,17 @@ func NewUserController(usecase *usecase.UserUsecase) *UserController { } } +// Create a user in database +// @Summary Creates user +// @Description Creates user and insert into the database +// @Tags User +// @Accept json +// @Produce json +// @Param userDto body dto.UserInsertDto true "User object information to create" +// @Success 201 +// @Failure 400 +// @Failure 500 +// @Router /user/create-account [post] func (uc *UserController) Insert(w http.ResponseWriter, r *http.Request) { var userDto dto.UserInsertDto err := json.NewDecoder(r.Body).Decode(&userDto) diff --git a/entity/dto/user_insert.dto.go b/entity/dto/user_insert.dto.go index 301ad51e..bc80d059 100644 --- a/entity/dto/user_insert.dto.go +++ b/entity/dto/user_insert.dto.go @@ -11,17 +11,17 @@ import ( var userTypes = []string{"juridica", "fisica"} type UserInsertDto struct { - Name string `json:"name"` - Type string `json:"type"` - Document string `json:"document"` - AvatarURL string `json:"avatar_url"` - Email string `json:"email"` - Phone string `json:"phone"` - Pass string `json:"pass"` - BirthDate *time.Time `json:"birthdate"` - City string `json:"city"` - State string `json:"state"` - Role string `json:"role"` + Name string `json:"name" example:"Claúdio"` + Type string `json:"type" example:"fisica"` + Document string `json:"document" example:"12345678900"` + AvatarURL string `json:"avatar_url" example:"https://example.com/avatar.jpg"` + Email string `json:"email" example:"claudio@example.com"` + Phone string `json:"phone" example:"21912345678"` + Pass string `json:"pass" example:"Senhasegur@123"` + BirthDate *time.Time `json:"birthdate" example:"2006-01-02T15:04:05Z"` + City string `json:"city" example:"São Paulo"` + State string `json:"state" example:"São Paulo"` + Role string `json:"role" example:"developer"` } func (u *UserInsertDto) Validate() error { diff --git a/swagger/docs.go b/swagger/docs.go index d914c404..973179de 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -188,6 +188,43 @@ const docTemplate = `{ } } }, + "/user/create-account": { + "post": { + "description": "Creates user and insert into the database", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Creates user", + "parameters": [ + { + "description": "User object information to create", + "name": "userDto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserInsertDto" + } + } + ], + "responses": { + "201": { + "description": "Created" + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -439,6 +476,55 @@ const docTemplate = `{ } } }, + "dto.UserInsertDto": { + "type": "object", + "properties": { + "avatar_url": { + "type": "string", + "example": "https://example.com/avatar.jpg" + }, + "birthdate": { + "type": "string", + "example": "2006-01-02T15:04:05Z" + }, + "city": { + "type": "string", + "example": "São Paulo" + }, + "document": { + "type": "string", + "example": "12345678900" + }, + "email": { + "type": "string", + "example": "claudio@example.com" + }, + "name": { + "type": "string", + "example": "Claúdio" + }, + "pass": { + "type": "string", + "example": "Senhasegur@123" + }, + "phone": { + "type": "string", + "example": "21912345678" + }, + "role": { + "type": "string", + "example": "developer" + }, + "state": { + "type": "string", + "example": "São Paulo" + }, + "type": { + "type": "string", + "example": "fisica" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.json b/swagger/swagger.json index 2c1f875e..f2296993 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -182,6 +182,43 @@ } } }, + "/user/create-account": { + "post": { + "description": "Creates user and insert into the database", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "Creates user", + "parameters": [ + { + "description": "User object information to create", + "name": "userDto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserInsertDto" + } + } + ], + "responses": { + "201": { + "description": "Created" + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -433,6 +470,55 @@ } } }, + "dto.UserInsertDto": { + "type": "object", + "properties": { + "avatar_url": { + "type": "string", + "example": "https://example.com/avatar.jpg" + }, + "birthdate": { + "type": "string", + "example": "2006-01-02T15:04:05Z" + }, + "city": { + "type": "string", + "example": "São Paulo" + }, + "document": { + "type": "string", + "example": "12345678900" + }, + "email": { + "type": "string", + "example": "claudio@example.com" + }, + "name": { + "type": "string", + "example": "Claúdio" + }, + "pass": { + "type": "string", + "example": "Senhasegur@123" + }, + "phone": { + "type": "string", + "example": "21912345678" + }, + "role": { + "type": "string", + "example": "developer" + }, + "state": { + "type": "string", + "example": "São Paulo" + }, + "type": { + "type": "string", + "example": "fisica" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index d443a7a4..457cbd57 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -114,6 +114,42 @@ definitions: example: true type: boolean type: object + dto.UserInsertDto: + properties: + avatar_url: + example: https://example.com/avatar.jpg + type: string + birthdate: + example: "2006-01-02T15:04:05Z" + type: string + city: + example: São Paulo + type: string + document: + example: "12345678900" + type: string + email: + example: claudio@example.com + type: string + name: + example: Claúdio + type: string + pass: + example: Senhasegur@123 + type: string + phone: + example: "21912345678" + type: string + role: + example: developer + type: string + state: + example: São Paulo + type: string + type: + example: fisica + type: string + type: object dto.UserUpdateDto: properties: avatar_url: @@ -388,4 +424,28 @@ paths: summary: Update an Pet existing. tags: - User + /user/create-account: + post: + consumes: + - application/json + description: Creates user and insert into the database + parameters: + - description: User object information to create + in: body + name: userDto + required: true + schema: + $ref: '#/definitions/dto.UserInsertDto' + produces: + - application/json + responses: + "201": + description: Created + "400": + description: Bad Request + "500": + description: Internal Server Error + summary: Creates user + tags: + - User swagger: "2.0" From f83ab41ed2b3675e8017df1a6c1a07e62326211d Mon Sep 17 00:00:00 2001 From: Paulo Victor Date: Tue, 17 Sep 2024 11:31:30 -0300 Subject: [PATCH 2/6] Update CODEOWNERS --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 611ac31e..2c30c6c3 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @devhatt/hatts @devhatt/petdex-backend-administrators +* @devhatt/hatts @devhatt/petdex-backend-administrators @devhatt/revisores From 795dd15d7d84313c2f5da58722f086621cb76e93 Mon Sep 17 00:00:00 2001 From: David Date: Tue, 17 Sep 2024 19:49:50 -0300 Subject: [PATCH 3/6] docs: create login doc (#203) --- api/controllers/user.go | 13 +++++++++++ swagger/docs.go | 51 +++++++++++++++++++++++++++++++++++++++++ swagger/swagger.json | 51 +++++++++++++++++++++++++++++++++++++++++ swagger/swagger.yaml | 33 ++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) diff --git a/api/controllers/user.go b/api/controllers/user.go index 68e675da..0997de23 100644 --- a/api/controllers/user.go +++ b/api/controllers/user.go @@ -62,6 +62,19 @@ func (uc *UserController) Insert(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) } + +// User login +// @Summary User login +// @Description Logs in a user and returns a JWT token +// @Tags User +// @Accept json +// @Produce json +// @Param userLoginDto body dto.UserLoginDto true "User login information" +// @Success 200 +// @Failure 400 +// @Failure 401 +// @Failure 500 +// @Router /user/login [post] func (uc *UserController) Login(w http.ResponseWriter, r *http.Request) { var userLoginDto dto.UserLoginDto err := json.NewDecoder(r.Body).Decode(&userLoginDto) diff --git a/swagger/docs.go b/swagger/docs.go index 973179de..e60ff42d 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -225,6 +225,46 @@ const docTemplate = `{ } } }, + "/user/login": { + "post": { + "description": "Logs in a user and returns a JWT token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "User login", + "parameters": [ + { + "description": "User login information", + "name": "userLoginDto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserLoginDto" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request" + }, + "401": { + "description": "Unauthorized" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -525,6 +565,17 @@ const docTemplate = `{ } } }, + "dto.UserLoginDto": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "password": { + "type": "string" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.json b/swagger/swagger.json index f2296993..76b82235 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -219,6 +219,46 @@ } } }, + "/user/login": { + "post": { + "description": "Logs in a user and returns a JWT token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "User login", + "parameters": [ + { + "description": "User login information", + "name": "userLoginDto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserLoginDto" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request" + }, + "401": { + "description": "Unauthorized" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -519,6 +559,17 @@ } } }, + "dto.UserLoginDto": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "password": { + "type": "string" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index 457cbd57..041d33ea 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -150,6 +150,13 @@ definitions: example: fisica type: string type: object + dto.UserLoginDto: + properties: + email: + type: string + password: + type: string + type: object dto.UserUpdateDto: properties: avatar_url: @@ -448,4 +455,30 @@ paths: summary: Creates user tags: - User + /user/login: + post: + consumes: + - application/json + description: Logs in a user and returns a JWT token + parameters: + - description: User login information + in: body + name: userLoginDto + required: true + schema: + $ref: '#/definitions/dto.UserLoginDto' + produces: + - application/json + responses: + "200": + description: OK + "400": + description: Bad Request + "401": + description: Unauthorized + "500": + description: Internal Server Error + summary: User login + tags: + - User swagger: "2.0" From 0f2fdf2b1110fcd410f3f1daf81a2c22e008cee8 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 18 Sep 2024 12:45:23 -0300 Subject: [PATCH 4/6] docs: Ong FindByID (#202) * create link_dto * refactor: ong FindByID * docs: Ong FindByID doc --- api/controllers/ong.go | 11 +++ entity/dto/link_dto.go | 6 ++ entity/dto/ong_list_dto.go | 22 +++--- entity/dto/ong_update_dto.go | 12 +-- infra/db/ong_repository.go | 43 ++++++----- interfaces/ong_repository.go | 2 +- .../v2/interfaces/mock_OngRepository.go | 10 +-- swagger/docs.go | 75 ++++++++++++++++++- swagger/swagger.json | 75 ++++++++++++++++++- swagger/swagger.yaml | 51 ++++++++++++- usecase/ong.go | 12 +-- usecase/ong_test.go | 6 +- 12 files changed, 262 insertions(+), 63 deletions(-) create mode 100644 entity/dto/link_dto.go diff --git a/api/controllers/ong.go b/api/controllers/ong.go index 147285df..88009160 100644 --- a/api/controllers/ong.go +++ b/api/controllers/ong.go @@ -90,6 +90,17 @@ func (oc *OngController) List(w http.ResponseWriter, r *http.Request) { } } +// FindByID Retrieves ONG information from its provided ID. +// @Summary Find ONG by ID +// @Description Retrieves ONG details based on the ONG ID provided as a parameter. +// @Tags Ong +// @Accept json +// @Produce json +// @Param ongID path string true "ID of the ONG to be retrieved" +// @Success 200 {object} dto.OngListMapper +// @Failure 400 +// @Failure 500 +// @Router /ongs/{ongID} [get] func (oc *OngController) FindByID(w http.ResponseWriter, r *http.Request) { IDStr := chi.URLParam(r, "ongID") diff --git a/entity/dto/link_dto.go b/entity/dto/link_dto.go new file mode 100644 index 00000000..58a99a3e --- /dev/null +++ b/entity/dto/link_dto.go @@ -0,0 +1,6 @@ +package dto + +type LinkDto struct { + URL string `json:"url" example:"https://www.facebook.com/"` + Text string `json:"text" example:"Facebook da Ong"` +} diff --git a/entity/dto/ong_list_dto.go b/entity/dto/ong_list_dto.go index 07a161a1..02720165 100644 --- a/entity/dto/ong_list_dto.go +++ b/entity/dto/ong_list_dto.go @@ -5,14 +5,14 @@ import ( ) type OngListMapper struct { - ID uniqueEntityId.ID `json:"id" db:"id"` - UserId uniqueEntityId.ID `json:"userId" db:"userId"` - Name string `json:"name" db:"name"` - Address string `json:"address" db:"address"` - City string `json:"city" db:"city"` - State string `json:"state" db:"state"` - Phone string `json:"phone" db:"phone"` - OpeningHours string `json:"openingHours" db:"openingHours"` - AdoptionPolicy string `json:"adoptionPolicy" db:"adoptionPolicy"` - Links string `json:"links" db:"links"` -} \ No newline at end of file + ID uniqueEntityId.ID `json:"id" db:"id"` + UserID uniqueEntityId.ID `json:"userId" db:"userId"` + Name string `json:"name" db:"name"` + Address string `json:"address" db:"address"` + City string `json:"city" db:"city"` + State string `json:"state" db:"state"` + Phone string `json:"phone" db:"phone"` + OpeningHours string `json:"openingHours" db:"openingHours"` + AdoptionPolicy string `json:"adoptionPolicy" db:"adoptionPolicy"` + Links string `json:"links" db:"links"` +} diff --git a/entity/dto/ong_update_dto.go b/entity/dto/ong_update_dto.go index 646271f4..e1c16881 100644 --- a/entity/dto/ong_update_dto.go +++ b/entity/dto/ong_update_dto.go @@ -1,15 +1,9 @@ package dto -type Link struct { - URL string `json:"url" example:"https://www.facebook.com/"` - Text string `json:"text" example:"Facebook da Ong"` -} - type OngUpdateDto struct { Phone string `json:"phone" db:"phone" example:"119596995887"` User UserUpdateDto - OpeningHours string `json:"openingHours" example:"08:00"` - AdoptionPolicy string `json:"adoptionPolicy" example:"não pode rato"` - Links []Link `json:"links"` + OpeningHours string `json:"openingHours" example:"08:00"` + AdoptionPolicy string `json:"adoptionPolicy" example:"não pode rato"` + Links []LinkDto `json:"links"` } - diff --git a/infra/db/ong_repository.go b/infra/db/ong_repository.go index 76198729..4de91c05 100644 --- a/infra/db/ong_repository.go +++ b/infra/db/ong_repository.go @@ -77,21 +77,31 @@ func (or *OngRepository) List(limit, offset int, sortBy, order string) (ongs []* return ongs, nil } - - -func (or *OngRepository) FindByID(ID uniqueEntityId.ID) (*entity.Ong, error) { - var ong entity.Ong - - err := or.dbconnection.Get(&ong, `SELECT - l.id, - l.userId, - l.links, - l.openingHours, - l.adoptionPolicy -FROM - legal_persons l -WHERE - l.id = ?`, ID) + +func (or *OngRepository) FindByID(ID uniqueEntityId.ID) (*dto.OngListMapper, error) { + var ong dto.OngListMapper + + query := ` + SELECT + legal_persons.id, + legal_persons.userId, + legal_persons.phone, + legal_persons.openingHours, + legal_persons.links, + users.name, + addresses.address, + addresses.city, + addresses.state + FROM + legal_persons + INNER JOIN + users ON legal_persons.userId = users.id + INNER JOIN + addresses ON legal_persons.userId = addresses.userId + WHERE + legal_persons.id = ?` + + err := or.dbconnection.Get(&ong, query, ID) if err != nil { logger.Error("error on ong repository: ", err) @@ -102,7 +112,6 @@ WHERE return &ong, nil } - func (or *OngRepository) Update(id uniqueEntityId.ID, ongToUpdate entity.Ong) error { query := "UPDATE legal_persons SET" @@ -163,4 +172,4 @@ func (or *OngRepository) Delete(id uniqueEntityId.ID) error { } return nil -} \ No newline at end of file +} diff --git a/interfaces/ong_repository.go b/interfaces/ong_repository.go index b54cb796..9bcf5aa3 100644 --- a/interfaces/ong_repository.go +++ b/interfaces/ong_repository.go @@ -9,7 +9,7 @@ import ( type OngRepository interface { Save(ong *entity.Ong) error List(limit, offset int, sortBy, order string) (ongs []*dto.OngListMapper, err error) + FindByID(ID uniqueEntityId.ID) (*dto.OngListMapper, error) Update(id uniqueEntityId.ID, ong entity.Ong) error - FindByID(ID uniqueEntityId.ID) (*entity.Ong, error) Delete(id uniqueEntityId.ID) error } diff --git a/mocks/pet-dex-backend/v2/interfaces/mock_OngRepository.go b/mocks/pet-dex-backend/v2/interfaces/mock_OngRepository.go index 16baa4db..5ed5a162 100644 --- a/mocks/pet-dex-backend/v2/interfaces/mock_OngRepository.go +++ b/mocks/pet-dex-backend/v2/interfaces/mock_OngRepository.go @@ -31,23 +31,23 @@ func (_m *MockOngRepository) EXPECT() *MockOngRepository_Expecter { } // FindByID provides a mock function with given fields: ID -func (_m *MockOngRepository) FindByID(ID uuid.UUID) (*entity.Ong, error) { +func (_m *MockOngRepository) FindByID(ID uuid.UUID) (*dto.OngListMapper, error) { ret := _m.Called(ID) if len(ret) == 0 { panic("no return value specified for FindByID") } - var r0 *entity.Ong + var r0 *dto.OngListMapper var r1 error - if rf, ok := ret.Get(0).(func(uuid.UUID) (*entity.Ong, error)); ok { + if rf, ok := ret.Get(0).(func(uuid.UUID) (*dto.OngListMapper, error)); ok { return rf(ID) } - if rf, ok := ret.Get(0).(func(uuid.UUID) *entity.Ong); ok { + if rf, ok := ret.Get(0).(func(uuid.UUID) *dto.OngListMapper); ok { r0 = rf(ID) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(*entity.Ong) + r0 = ret.Get(0).(*dto.OngListMapper) } } diff --git a/swagger/docs.go b/swagger/docs.go index e60ff42d..9433e6e1 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -23,6 +23,42 @@ const docTemplate = `{ "basePath": "{{.BasePath}}", "paths": { "/ongs/{ongID}": { + "get": { + "description": "Retrieves ONG details based on the ONG ID provided as a parameter.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Ong" + ], + "summary": "Find ONG by ID", + "parameters": [ + { + "type": "string", + "description": "ID of the ONG to be retrieved", + "name": "ongID", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.OngListMapper" + } + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + }, "patch": { "description": "Updates the details of an existing Ong based on the provided Ong ID.", "consumes": [ @@ -373,7 +409,7 @@ const docTemplate = `{ } } }, - "dto.Link": { + "dto.LinkDto": { "type": "object", "properties": { "text": { @@ -386,6 +422,41 @@ const docTemplate = `{ } } }, + "dto.OngListMapper": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "adoptionPolicy": { + "type": "string" + }, + "city": { + "type": "string" + }, + "id": { + "type": "string" + }, + "links": { + "type": "string" + }, + "name": { + "type": "string" + }, + "openingHours": { + "type": "string" + }, + "phone": { + "type": "string" + }, + "state": { + "type": "string" + }, + "userId": { + "type": "string" + } + } + }, "dto.OngUpdateDto": { "type": "object", "properties": { @@ -396,7 +467,7 @@ const docTemplate = `{ "links": { "type": "array", "items": { - "$ref": "#/definitions/dto.Link" + "$ref": "#/definitions/dto.LinkDto" } }, "openingHours": { diff --git a/swagger/swagger.json b/swagger/swagger.json index 76b82235..c9abee1c 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -17,6 +17,42 @@ "basePath": "/", "paths": { "/ongs/{ongID}": { + "get": { + "description": "Retrieves ONG details based on the ONG ID provided as a parameter.", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Ong" + ], + "summary": "Find ONG by ID", + "parameters": [ + { + "type": "string", + "description": "ID of the ONG to be retrieved", + "name": "ongID", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.OngListMapper" + } + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + }, "patch": { "description": "Updates the details of an existing Ong based on the provided Ong ID.", "consumes": [ @@ -367,7 +403,7 @@ } } }, - "dto.Link": { + "dto.LinkDto": { "type": "object", "properties": { "text": { @@ -380,6 +416,41 @@ } } }, + "dto.OngListMapper": { + "type": "object", + "properties": { + "address": { + "type": "string" + }, + "adoptionPolicy": { + "type": "string" + }, + "city": { + "type": "string" + }, + "id": { + "type": "string" + }, + "links": { + "type": "string" + }, + "name": { + "type": "string" + }, + "openingHours": { + "type": "string" + }, + "phone": { + "type": "string" + }, + "state": { + "type": "string" + }, + "userId": { + "type": "string" + } + } + }, "dto.OngUpdateDto": { "type": "object", "properties": { @@ -390,7 +461,7 @@ "links": { "type": "array", "items": { - "$ref": "#/definitions/dto.Link" + "$ref": "#/definitions/dto.LinkDto" } }, "openingHours": { diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index 041d33ea..3c8dd1b4 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -12,7 +12,7 @@ definitions: example: Pastor Alemão type: string type: object - dto.Link: + dto.LinkDto: properties: text: example: Facebook da Ong @@ -21,6 +21,29 @@ definitions: example: https://www.facebook.com/ type: string type: object + dto.OngListMapper: + properties: + address: + type: string + adoptionPolicy: + type: string + city: + type: string + id: + type: string + links: + type: string + name: + type: string + openingHours: + type: string + phone: + type: string + state: + type: string + userId: + type: string + type: object dto.OngUpdateDto: properties: adoptionPolicy: @@ -28,7 +51,7 @@ definitions: type: string links: items: - $ref: '#/definitions/dto.Link' + $ref: '#/definitions/dto.LinkDto' type: array openingHours: example: "08:00" @@ -260,6 +283,30 @@ info: version: "1.0" paths: /ongs/{ongID}: + get: + consumes: + - application/json + description: Retrieves ONG details based on the ONG ID provided as a parameter. + parameters: + - description: ID of the ONG to be retrieved + in: path + name: ongID + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/dto.OngListMapper' + "400": + description: Bad Request + "500": + description: Internal Server Error + summary: Find ONG by ID + tags: + - Ong patch: consumes: - application/json diff --git a/usecase/ong.go b/usecase/ong.go index aca5929a..fe521be6 100644 --- a/usecase/ong.go +++ b/usecase/ong.go @@ -71,7 +71,7 @@ func (o *OngUsecase) List(limit, offset int, sortBy, order string) ([]*dto.OngLi return ong, nil } -func (c *OngUsecase) FindByID(ID uniqueEntityId.ID) (*entity.Ong, error) { +func (c *OngUsecase) FindByID(ID uniqueEntityId.ID) (*dto.OngListMapper, error) { ong, err := c.repo.FindByID(ID) @@ -81,16 +81,6 @@ func (c *OngUsecase) FindByID(ID uniqueEntityId.ID) (*entity.Ong, error) { return nil, err } - user, err := c.userRepo.FindByID(ong.UserID) - - if err != nil { - c.logger.Error("error on ong repository: ", err) - err = fmt.Errorf("failed to retrieve ong: %w", err) - return nil, err - } - - ong.User = *user - return ong, nil } diff --git a/usecase/ong_test.go b/usecase/ong_test.go index 7723c675..adc380e9 100755 --- a/usecase/ong_test.go +++ b/usecase/ong_test.go @@ -1,7 +1,7 @@ package usecase import ( - "pet-dex-backend/v2/entity" + "pet-dex-backend/v2/entity/dto" mockInterfaces "pet-dex-backend/v2/mocks/pet-dex-backend/v2/interfaces" "pet-dex-backend/v2/pkg/uniqueEntityId" "testing" @@ -13,7 +13,7 @@ func TestOngDelete(t *testing.T) { tcases := map[string]struct { repo *mockInterfaces.MockOngRepository inputID uniqueEntityId.ID - findByIDResp *entity.Ong + findByIDResp *dto.OngListMapper findByIDErr error deleteResp error expectOutput error @@ -21,7 +21,7 @@ func TestOngDelete(t *testing.T) { "success": { repo: mockInterfaces.NewMockOngRepository(t), inputID: uniqueEntityId.NewID(), - findByIDResp: &entity.Ong{}, + findByIDResp: &dto.OngListMapper{}, findByIDErr: nil, deleteResp: nil, expectOutput: nil, From b710846478c73f9cf2bfbd06b3db9737bf8d28db Mon Sep 17 00:00:00 2001 From: Edilton Oliveira <86030948+EdiltonOliveira@users.noreply.github.com> Date: Thu, 19 Sep 2024 16:35:43 -0300 Subject: [PATCH 5/6] feat: doc api/ongs/ GET (#205) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Implementação de notação markdown para melhorar a doc * Alteração do makefile para geração de doc swag com notação markdown --- Makefile | 2 +- api/controllers/ong.go | 14 +++++++++++ swagger/details.md | 8 ++++++ swagger/docs.go | 56 ++++++++++++++++++++++++++++++++++++++++++ swagger/swagger.json | 56 ++++++++++++++++++++++++++++++++++++++++++ swagger/swagger.yaml | 49 ++++++++++++++++++++++++++++++++++++ 6 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 swagger/details.md diff --git a/Makefile b/Makefile index 0d7d9268..965d26e5 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,4 @@ lint: docker run --rm -v ./:/app -w /app golangci/golangci-lint:v1.59.1 golangci-lint run -v swag: - swag init -g api/main.go -o swagger/ \ No newline at end of file + swag init -g api/main.go -o swagger/ --md swagger/ \ No newline at end of file diff --git a/api/controllers/ong.go b/api/controllers/ong.go index 88009160..0cb9cb61 100644 --- a/api/controllers/ong.go +++ b/api/controllers/ong.go @@ -46,6 +46,20 @@ func (oc *OngController) Insert(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusCreated) } +// List List of Ong information retrieval from query parameters. +// @Summary View list of Ong. +// @Description This endpoint allows you to retrieve a list Ong organized according to query parameters.. +// @Description.markdown details +// @Tags Ong +// @Produce json +// @Param limit query string false "Query limits the return of 10 data." example(10) +// @Param sortBy query string false "Property used to sort and organize displayed data" example(name) +// @Param order query string false "Data can be returned in ascending (asc) or descending (desc) order" example des" example(desc) +// @Param offset query string false "Initial position of the offset that marks the beginning of the display of the next elements" default(0) +// @Success 200 {object} dto.OngListMapper +// @Failure 400 +// @Failure 500 +// @Router /ongs/ [get] func (oc *OngController) List(w http.ResponseWriter, r *http.Request) { // pageStr := r.URL.Query().Get("page") limitStr := r.URL.Query().Get("limit") diff --git a/swagger/details.md b/swagger/details.md new file mode 100644 index 00000000..f5e13862 --- /dev/null +++ b/swagger/details.md @@ -0,0 +1,8 @@ +## Samples requests + +``` +GET /api/ongs/ +GET /api/ongs?limite=10&offset=2 +GET /api/ongs?sortBy=name&order=asc + +``` \ No newline at end of file diff --git a/swagger/docs.go b/swagger/docs.go index 9433e6e1..e209a601 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -22,6 +22,62 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/ongs/": { + "get": { + "description": "This endpoint allows you to retrieve a list Ong organized according to query parameters..\n## Samples requests\n\n` + "`" + `` + "`" + `` + "`" + `\nGET /api/ongs/\nGET /api/ongs?limite=10\u0026offset=2\nGET /api/ongs?sortBy=name\u0026order=asc\n\n` + "`" + `` + "`" + `` + "`" + `", + "produces": [ + "application/json" + ], + "tags": [ + "Ong" + ], + "summary": "View list of Ong.", + "parameters": [ + { + "type": "string", + "example": "10", + "description": "Query limits the return of 10 data.", + "name": "limit", + "in": "query" + }, + { + "type": "string", + "example": "name", + "description": "Property used to sort and organize displayed data", + "name": "sortBy", + "in": "query" + }, + { + "type": "string", + "example": "desc", + "description": "Data can be returned in ascending (asc) or descending (desc) order", + "name": "order", + "in": "query" + }, + { + "type": "string", + "default": "0", + "description": "Initial position of the offset that marks the beginning of the display of the next elements", + "name": "offset", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.OngListMapper" + } + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/ongs/{ongID}": { "get": { "description": "Retrieves ONG details based on the ONG ID provided as a parameter.", diff --git a/swagger/swagger.json b/swagger/swagger.json index c9abee1c..9b08a7ad 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -16,6 +16,62 @@ "host": "localhost:3000/api", "basePath": "/", "paths": { + "/ongs/": { + "get": { + "description": "This endpoint allows you to retrieve a list Ong organized according to query parameters..\n## Samples requests\n\n```\nGET /api/ongs/\nGET /api/ongs?limite=10\u0026offset=2\nGET /api/ongs?sortBy=name\u0026order=asc\n\n```", + "produces": [ + "application/json" + ], + "tags": [ + "Ong" + ], + "summary": "View list of Ong.", + "parameters": [ + { + "type": "string", + "example": "10", + "description": "Query limits the return of 10 data.", + "name": "limit", + "in": "query" + }, + { + "type": "string", + "example": "name", + "description": "Property used to sort and organize displayed data", + "name": "sortBy", + "in": "query" + }, + { + "type": "string", + "example": "desc", + "description": "Data can be returned in ascending (asc) or descending (desc) order", + "name": "order", + "in": "query" + }, + { + "type": "string", + "default": "0", + "description": "Initial position of the offset that marks the beginning of the display of the next elements", + "name": "offset", + "in": "query" + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.OngListMapper" + } + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/ongs/{ongID}": { "get": { "description": "Retrieves ONG details based on the ONG ID provided as a parameter.", diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index 3c8dd1b4..dab3b622 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -282,6 +282,55 @@ info: title: 'PetDex: Documentação API' version: "1.0" paths: + /ongs/: + get: + description: |- + This endpoint allows you to retrieve a list Ong organized according to query parameters.. + ## Samples requests + + ``` + GET /api/ongs/ + GET /api/ongs?limite=10&offset=2 + GET /api/ongs?sortBy=name&order=asc + + ``` + parameters: + - description: Query limits the return of 10 data. + example: "10" + in: query + name: limit + type: string + - description: Property used to sort and organize displayed data + example: name + in: query + name: sortBy + type: string + - description: Data can be returned in ascending (asc) or descending (desc) + order + example: desc + in: query + name: order + type: string + - default: "0" + description: Initial position of the offset that marks the beginning of the + display of the next elements + in: query + name: offset + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/dto.OngListMapper' + "400": + description: Bad Request + "500": + description: Internal Server Error + summary: View list of Ong. + tags: + - Ong /ongs/{ongID}: get: consumes: From 47a86ee70433d33c75ba6dd5f4ccaea88d8102cc Mon Sep 17 00:00:00 2001 From: David Date: Thu, 19 Sep 2024 16:48:57 -0300 Subject: [PATCH 6/6] docs: create login with SSO doc (#207) --- api/controllers/user.go | 12 +++++++++ swagger/docs.go | 55 +++++++++++++++++++++++++++++++++++++++++ swagger/swagger.json | 55 +++++++++++++++++++++++++++++++++++++++++ swagger/swagger.yaml | 37 +++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) diff --git a/api/controllers/user.go b/api/controllers/user.go index 0997de23..53e34da8 100644 --- a/api/controllers/user.go +++ b/api/controllers/user.go @@ -265,6 +265,18 @@ func (uc *UserController) ChangePassword(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusOK) } +// User login with provider +// @Summary User login with provider +// @Description Logs in a user using a specified provider (SSO) and returns a JWT token +// @Tags User +// @Accept json +// @Produce json +// @Param provider path string true "The provider for Single Sign-On (e.g., google, facebook)" +// @Param UserSSODto body dto.UserSSODto true "User login information with SSO" +// @Success 200 +// @Failure 400 +// @Failure 500 +// @Router /user/{provider}/login [post] func (uc *UserController) ProviderLogin(w http.ResponseWriter, r *http.Request) { provider := chi.URLParam(r, "provider") diff --git a/swagger/docs.go b/swagger/docs.go index e209a601..a8e0923c 100644 --- a/swagger/docs.go +++ b/swagger/docs.go @@ -357,6 +357,50 @@ const docTemplate = `{ } } }, + "/user/{provider}/login": { + "post": { + "description": "Logs in a user using a specified provider (SSO) and returns a JWT token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "User login with provider", + "parameters": [ + { + "type": "string", + "description": "The provider for Single Sign-On (e.g., google, facebook)", + "name": "provider", + "in": "path", + "required": true + }, + { + "description": "User login information with SSO", + "name": "UserSSODto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserSSODto" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -703,6 +747,17 @@ const docTemplate = `{ } } }, + "dto.UserSSODto": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.json b/swagger/swagger.json index 9b08a7ad..7eb57462 100644 --- a/swagger/swagger.json +++ b/swagger/swagger.json @@ -351,6 +351,50 @@ } } }, + "/user/{provider}/login": { + "post": { + "description": "Logs in a user using a specified provider (SSO) and returns a JWT token", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "User" + ], + "summary": "User login with provider", + "parameters": [ + { + "type": "string", + "description": "The provider for Single Sign-On (e.g., google, facebook)", + "name": "provider", + "in": "path", + "required": true + }, + { + "description": "User login information with SSO", + "name": "UserSSODto", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UserSSODto" + } + } + ], + "responses": { + "200": { + "description": "OK" + }, + "400": { + "description": "Bad Request" + }, + "500": { + "description": "Internal Server Error" + } + } + } + }, "/user/{userID}/my-pets": { "get": { "description": "List all pets owned by the user corresponding to the provided user ID", @@ -697,6 +741,17 @@ } } }, + "dto.UserSSODto": { + "type": "object", + "properties": { + "email": { + "type": "string" + }, + "name": { + "type": "string" + } + } + }, "dto.UserUpdateDto": { "type": "object", "properties": { diff --git a/swagger/swagger.yaml b/swagger/swagger.yaml index dab3b622..d8d0bfdd 100644 --- a/swagger/swagger.yaml +++ b/swagger/swagger.yaml @@ -180,6 +180,13 @@ definitions: password: type: string type: object + dto.UserSSODto: + properties: + email: + type: string + name: + type: string + type: object dto.UserUpdateDto: properties: avatar_url: @@ -466,6 +473,36 @@ paths: summary: View list of all Breeds tags: - Pet + /user/{provider}/login: + post: + consumes: + - application/json + description: Logs in a user using a specified provider (SSO) and returns a JWT + token + parameters: + - description: The provider for Single Sign-On (e.g., google, facebook) + in: path + name: provider + required: true + type: string + - description: User login information with SSO + in: body + name: UserSSODto + required: true + schema: + $ref: '#/definitions/dto.UserSSODto' + produces: + - application/json + responses: + "200": + description: OK + "400": + description: Bad Request + "500": + description: Internal Server Error + summary: User login with provider + tags: + - User /user/{userID}/my-pets: get: consumes: