Skip to content

Commit

Permalink
fix(#101): fix for publishers
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenzo Saraniti committed Aug 3, 2022
1 parent 74497d4 commit b00cfec
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
21 changes: 7 additions & 14 deletions developers-italia.oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,12 @@ paths:
security:
- bearerAuth: []
operationId: post-publishers
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Publisher'
responses:
'200':
description: OK
Expand All @@ -560,8 +566,6 @@ paths:
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthorized'
'404':
$ref: '#/components/responses/NotFound'
'429':
$ref: '#/components/responses/TooManyRequests'
'/publishers/{publisherId}':
Expand Down Expand Up @@ -618,7 +622,7 @@ paths:
content:
application/merge-patch+json:
schema:
$ref: '#/components/schemas/PublisherUpdate'
$ref: '#/components/schemas/Publisher'
delete:
summary: Delete a Publisher
description: Delete a Publisher by its id
Expand Down Expand Up @@ -707,17 +711,6 @@ components:
Publisher:
title: Publisher
type: object
properties:
url:
type: string
description:
type: string
email:
type: string
format: email
PublisherUpdate:
title: PublisherUpdate
type: object
properties:
codeHosting:
type: array
Expand Down
6 changes: 0 additions & 6 deletions internal/common/requests.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package common

type Publisher struct {
URL string `json:"url" validate:"required"`
Description string `json:"description"`
Email string `json:"email" validate:"email"`
}

type PublisherUpdate struct {
CodeHosting []CodeHosting `json:"codeHosting" validate:"required"`
Description string `json:"description"`
Email string `json:"email" validate:"email"`
Expand Down
13 changes: 7 additions & 6 deletions internal/handlers/publishers.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ func (p *Publisher) PostPublisher(ctx *fiber.Ctx) error {

if err := common.ValidateStruct(&request); err != nil {
return common.ErrorWithValidationErrors(
fiber.StatusUnprocessableEntity, "can't create Publisher", "invalid json", err,
fiber.StatusUnprocessableEntity, "can't create Publisher", "invalid format", err,
)
}

publisher := &models.Publisher{
ID: utils.UUIDv4(),
Email: request.Email,
CodeHosting: []models.CodeHosting{
{URL: request.URL},
},
}

for _, URLAddress := range request.CodeHosting {
publisher.CodeHosting = append(publisher.CodeHosting, models.CodeHosting{URL: URLAddress.URL})
}

if err := p.db.Create(&publisher).Error; err != nil {
Expand All @@ -89,7 +90,7 @@ func (p *Publisher) PostPublisher(ctx *fiber.Ctx) error {

// PatchPublisher updates the publisher with the given ID.
func (p *Publisher) PatchPublisher(ctx *fiber.Ctx) error {
publisherReq := new(common.PublisherUpdate)
publisherReq := new(common.Publisher)

if err := ctx.BodyParser(publisherReq); err != nil {
return common.Error(fiber.StatusBadRequest, "can't update Publisher", "invalid json")
Expand All @@ -110,7 +111,7 @@ func (p *Publisher) PatchPublisher(ctx *fiber.Ctx) error {
return ctx.JSON(&publisher)
}

func (p *Publisher) updatePublisher(ctx *fiber.Ctx, publisher models.Publisher, req *common.PublisherUpdate) error {
func (p *Publisher) updatePublisher(ctx *fiber.Ctx, publisher models.Publisher, req *common.Publisher) error {
err := p.db.Transaction(func(gormTrx *gorm.DB) error {
if err := gormTrx.Model(&models.Publisher{}).
Preload("CodeHosting").
Expand Down

0 comments on commit b00cfec

Please sign in to comment.