Skip to content

Commit

Permalink
feat(publisher): Update existing publisher with additional Repo URLs (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoS92 authored Sep 20, 2022
1 parent 9f425c3 commit 6ed7493
Show file tree
Hide file tree
Showing 15 changed files with 380 additions and 114 deletions.
19 changes: 17 additions & 2 deletions developers-italia.oas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ paths:
description: Only results after this cursor
post:
summary: Create a new Publisher
description: Create a new Publisher
description: Create a new Publisher. The URL will be normalized. Publisher with same URL, Email or ExternalCode will be considered the same and an error will be thrown.
tags:
- publishers
security:
Expand All @@ -874,6 +874,8 @@ paths:
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthorized'
'409':
$ref: '#/components/responses/Conflict'
'429':
$ref: '#/components/responses/TooManyRequests'
requestBody:
Expand Down Expand Up @@ -911,7 +913,7 @@ paths:
operationId: show-publisher-publisherId
patch:
summary: Update a Publisher
description: Update a Publisher by its id
description: Update a Publisher by its id. Only the fields that are provided will be updated, codeHosting field will be overwritten entirely.
tags:
- publishers
security:
Expand Down Expand Up @@ -1353,6 +1355,12 @@ components:
application/problem+json:
schema:
$ref: '#/components/schemas/Error'
Conflict:
description: Conflict
content:
application/problem+json:
schema:
$ref: '#/components/schemas/Error'
NotFound:
description: Not found
content:
Expand Down Expand Up @@ -1489,6 +1497,13 @@ components:
publishers (fe. when they are not complying with some rule, but the problem is
expected to be fixed in the near future) without removing them.
default: true
externalCode:
type: string
description: |
External code used to identify this publisher in external systems.
maxLength: 255
example: 'exampleExternalCode'
pattern: '.*'
Log:
title: Log
type: object
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ module github.com/italia/developers-italia-api
go 1.18

require (
github.com/PuerkitoBio/purell v1.2.0
github.com/caarlos0/env/v6 v6.9.3
github.com/go-playground/validator/v10 v10.11.0
github.com/go-testfixtures/testfixtures/v3 v3.8.0
github.com/gofiber/contrib/paseto v0.0.0-20220621082844-83549332c36e
github.com/gofiber/fiber/v2 v2.34.1
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa
github.com/stretchr/testify v1.7.5
gorm.io/driver/postgres v1.3.7
gorm.io/driver/sqlite v1.3.4
gorm.io/gorm v1.23.6
)

require golang.org/x/exp v0.0.0-20220827204233-334a2380cb91
require (
golang.org/x/exp v0.0.0-20220827204233-334a2380cb91
)

require golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 // indirect

require (
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect
Expand All @@ -32,7 +38,7 @@ require (
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.12.1 // indirect
github.com/jackc/pgconn v1.12.1
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc=
github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs=
github.com/PuerkitoBio/purell v1.2.0 h1:/Jdm5QfyM8zdlqT6WVZU4cfP23sot6CEHA4CS49Ezig=
github.com/PuerkitoBio/purell v1.2.0/go.mod h1:OhLRTaaIzhvIyofkJfB24gokC7tM42Px5UhoT32THBk=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da h1:KjTM2ks9d14ZYCvmHS9iAKVt9AyzRSqNU1qabPih5BY=
github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da/go.mod h1:eHEWzANqSiWQsof+nXEI9bUVUyV6F53Fp89EuCh2EAA=
github.com/aead/chacha20poly1305 v0.0.0-20170617001512-233f39982aeb/go.mod h1:UzH9IX1MMqOcwhoNOIjmTQeAxrFgzs50j4golQtXXxU=
Expand Down Expand Up @@ -65,6 +67,8 @@ github.com/jackc/pgconn v1.9.0/go.mod h1:YctiPyvzfU11JFxoXokUOOKQXQmDMoJL9vJzHH8
github.com/jackc/pgconn v1.9.1-0.20210724152538-d89c8390a530/go.mod h1:4z2w8XhRbP1hYxkpTuBjTS3ne3J48K83+u0zoyvg2pI=
github.com/jackc/pgconn v1.12.1 h1:rsDFzIpRk7xT4B8FufgpCCeyjdNpKyghZeSefViE5W8=
github.com/jackc/pgconn v1.12.1/go.mod h1:ZkhRC59Llhrq3oSfrikvwQ5NaxYExr6twkdkMLaKono=
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa h1:s+4MhCQ6YrzisK6hFJUX53drDT4UsSW3DEhKn0ifuHw=
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
github.com/jackc/pgio v1.0.0 h1:g12B9UwVnzGhueNavwioyEEpAmqMe1E/BN9ES+8ovkE=
github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bYf8=
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
Expand Down Expand Up @@ -242,6 +246,8 @@ golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9 h1:Yqz/iviulwKwAREEeUd3nbBFn0XuyJqkoft2IlrvOhc=
golang.org/x/net v0.0.0-20220617184016-355a448f1bc9/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
2 changes: 2 additions & 0 deletions internal/common/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (

type Base64Key [SymmetricKeyLen]byte

var EnvironmentConfig Environment //nolint:gochecknoglobals

type Environment struct {
MaxRequests int `env:"MAX_REQUESTS" envDefault:"0"`
CurrentEnvironment string `env:"ENVIRONMENT" envDefault:"production"`
Expand Down
4 changes: 3 additions & 1 deletion internal/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
var (
ErrAuthentication = errors.New("token authentication failed")
ErrInvalidDateTime = errors.New("invalid date time format (RFC 3339 needed)")
ErrKeyLen = errors.New("PASETO_KEY must be 32 bytes long once base64-decoded")

ErrKeyLen = errors.New("PASETO_KEY must be 32 bytes long once base64-decoded")
ErrDBUniqueConstraint = errors.New("db constraint violation")
ErrDBRecordNotFound = errors.New("record not found")
)

func Error(status int, title string, detail string) ProblemJSONError {
Expand Down
25 changes: 20 additions & 5 deletions internal/common/requests.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package common

type Publisher struct {
CodeHosting []CodeHosting `json:"codeHosting" validate:"required"`
Description string `json:"description"`
Email string `json:"email" validate:"email"`
Active *bool `json:"active"`
import "strings"

type PublisherPost struct {
CodeHosting []CodeHosting `json:"codeHosting" validate:"required,gt=0,dive"`
Description string `json:"description"`
Email string `json:"email" validate:"email,required"`
Active *bool `json:"active"`
ExternalCode string `json:"externalCode" validate:"max=255"`
}

type PublisherPatch struct {
CodeHosting []CodeHosting `json:"codeHosting" validate:"gt=0"`
Description string `json:"description"`
Email string `json:"email" validate:"email"`
Active *bool `json:"active"`
ExternalCode string `json:"externalCode" validate:"max=255"`
}

type CodeHosting struct {
Expand Down Expand Up @@ -34,3 +45,7 @@ type Webhook struct {
URL string `json:"url" validate:"required,url"`
Secret string `json:"secret"`
}

func NormalizeEmail(email string) string {
return strings.TrimSpace(strings.ToLower(email))
}
16 changes: 16 additions & 0 deletions internal/common/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"strings"

"github.com/gofiber/fiber/v2"

"github.com/go-playground/validator/v10"
)

Expand Down Expand Up @@ -57,3 +59,17 @@ func ValidateStruct(validateStruct interface{}) []ValidationError {

return validationErrors
}

func ValidateRequestEntity(ctx *fiber.Ctx, request interface{}, errorMessage string) error {
if err := ctx.BodyParser(request); err != nil {
return Error(fiber.StatusBadRequest, errorMessage, "invalid json")
}

if err := ValidateStruct(request); err != nil {
return ErrorWithValidationErrors(
fiber.StatusUnprocessableEntity, errorMessage, "invalid format", err,
)
}

return nil
}
26 changes: 26 additions & 0 deletions internal/database/database.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package database

import (
"errors"
"log"

"github.com/jackc/pgconn"
"github.com/mattn/go-sqlite3"

"github.com/italia/developers-italia-api/internal/common"
"github.com/jackc/pgerrcode"
"gorm.io/gorm"
)

Expand All @@ -27,3 +32,24 @@ func NewDatabase(env common.Environment) Database {
dsn: env.Database,
}
}

//nolint:errorlint
func WrapErrors(dbError error) error {
if e, ok := dbError.(sqlite3.Error); ok {
if e.ExtendedCode == sqlite3.ErrConstraintUnique {
return common.ErrDBUniqueConstraint
}
}

if e, ok := dbError.(*pgconn.PgError); ok {
if e.Code == pgerrcode.UniqueViolation {
return common.ErrDBUniqueConstraint
}
}

if errors.Is(dbError, gorm.ErrRecordNotFound) {
return common.ErrDBRecordNotFound
}

return dbError
}
Loading

0 comments on commit 6ed7493

Please sign in to comment.