mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-13 23:47:44 +00:00
Add validations
This commit is contained in:
parent
65792abd94
commit
553dc53eed
9 changed files with 61 additions and 2 deletions
1
go.mod
1
go.mod
|
@ -10,6 +10,7 @@ require (
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||||
github.com/dustin/go-humanize v1.0.0
|
github.com/dustin/go-humanize v1.0.0
|
||||||
github.com/go-co-op/gocron v1.9.0
|
github.com/go-co-op/gocron v1.9.0
|
||||||
|
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
|
||||||
github.com/go-redis/redis/v8 v8.11.4
|
github.com/go-redis/redis/v8 v8.11.4
|
||||||
github.com/gorilla/mux v1.8.0
|
github.com/gorilla/mux v1.8.0
|
||||||
github.com/heroku/x v0.0.32
|
github.com/heroku/x v0.0.32
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -55,6 +55,8 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
|
||||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||||
github.com/armon/go-proxyproto v0.0.0-20190211145416-68259f75880e/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
|
github.com/armon/go-proxyproto v0.0.0-20190211145416-68259f75880e/go.mod h1:QmP9hvJ91BbJmGVGSbutW19IC0Q9phDCLGaomwTJbgU=
|
||||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||||
|
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496 h1:zV3ejI06GQ59hwDQAvmK1qxOQGB3WuVTRoY0okPTAv0=
|
||||||
|
github.com/asaskevich/govalidator v0.0.0-20200108200545-475eaeb16496/go.mod h1:oGkLhpf+kjZl6xBf758TQhh5XrAeiJv/7FRz/2spLIg=
|
||||||
github.com/aws/aws-sdk-go v1.13.10/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
|
github.com/aws/aws-sdk-go v1.13.10/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
|
||||||
github.com/axiomhq/hyperloglog v0.0.0-20180317131949-fe9507de0228/go.mod h1:IOXAcuKIFq/mDyuQ4wyJuJ79XLMsmLM+5RdQ+vWrL7o=
|
github.com/axiomhq/hyperloglog v0.0.0-20180317131949-fe9507de0228/go.mod h1:IOXAcuKIFq/mDyuQ4wyJuJ79XLMsmLM+5RdQ+vWrL7o=
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
|
@ -120,6 +122,8 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb
|
||||||
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
|
||||||
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A=
|
||||||
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
|
github.com/go-ole/go-ole v1.2.1/go.mod h1:7FAglXiTm7HKlQRDeOQ6ZNUHidzCWXuZWq/1dTyBNF8=
|
||||||
|
github.com/go-ozzo/ozzo-validation/v4 v4.3.0 h1:byhDUpfEwjsVQb1vBunvIjh2BHQ9ead57VkAEY4V+Es=
|
||||||
|
github.com/go-ozzo/ozzo-validation/v4 v4.3.0/go.mod h1:2NKgrcHl3z6cJs+3Oo940FPRiTzuqKbvfrL2RxCj6Ew=
|
||||||
github.com/go-redis/redis/v8 v8.3.2/go.mod h1:jszGxBCez8QA1HWSmQxJO9Y82kNibbUmeYhKWrBejTU=
|
github.com/go-redis/redis/v8 v8.3.2/go.mod h1:jszGxBCez8QA1HWSmQxJO9Y82kNibbUmeYhKWrBejTU=
|
||||||
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
|
github.com/go-redis/redis/v8 v8.11.4 h1:kHoYkfZP6+pe04aFTnhDH6GDROa5yJdHJVNxV3F46Tg=
|
||||||
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
|
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
|
||||||
|
|
|
@ -3,6 +3,8 @@ package domain
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Account represents an account we need to periodically check in the notifications worker.
|
// Account represents an account we need to periodically check in the notifications worker.
|
||||||
|
@ -25,6 +27,13 @@ func (acct *Account) NormalizedUsername() string {
|
||||||
return strings.ToLower(acct.Username)
|
return strings.ToLower(acct.Username)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (acct *Account) Validate() error {
|
||||||
|
return validation.ValidateStruct(acct,
|
||||||
|
validation.Field(&acct.Username, validation.Required, validation.Length(3, 32)),
|
||||||
|
validation.Field(&acct.AccountID, validation.Required, validation.Length(5, 7)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// AccountRepository represents the account's repository contract
|
// AccountRepository represents the account's repository contract
|
||||||
type AccountRepository interface {
|
type AccountRepository interface {
|
||||||
GetByID(ctx context.Context, id int64) (Account, error)
|
GetByID(ctx context.Context, id int64) (Account, error)
|
||||||
|
|
|
@ -1,6 +1,10 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
DeviceGracePeriodDuration = 3600 // 1 hour
|
DeviceGracePeriodDuration = 3600 // 1 hour
|
||||||
|
@ -14,6 +18,12 @@ type Device struct {
|
||||||
ActiveUntil int64
|
ActiveUntil int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dev *Device) Validate() error {
|
||||||
|
return validation.ValidateStruct(dev,
|
||||||
|
validation.Field(&dev.APNSToken, validation.Required, validation.Length(64, 200)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type DeviceRepository interface {
|
type DeviceRepository interface {
|
||||||
GetByID(ctx context.Context, id int64) (Device, error)
|
GetByID(ctx context.Context, id int64) (Device, error)
|
||||||
GetByAPNSToken(ctx context.Context, token string) (Device, error)
|
GetByAPNSToken(ctx context.Context, token string) (Device, error)
|
||||||
|
|
|
@ -3,6 +3,8 @@ package domain
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Subreddit struct {
|
type Subreddit struct {
|
||||||
|
@ -18,6 +20,13 @@ func (sr *Subreddit) NormalizedName() string {
|
||||||
return strings.ToLower(sr.Name)
|
return strings.ToLower(sr.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sr *Subreddit) Validate() error {
|
||||||
|
return validation.ValidateStruct(sr,
|
||||||
|
validation.Field(&sr.Name, validation.Required, validation.Length(3, 32)),
|
||||||
|
validation.Field(&sr.SubredditID, validation.Required, validation.Length(5, 7)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type SubredditRepository interface {
|
type SubredditRepository interface {
|
||||||
GetByID(ctx context.Context, id int64) (Subreddit, error)
|
GetByID(ctx context.Context, id int64) (Subreddit, error)
|
||||||
GetByName(ctx context.Context, name string) (Subreddit, error)
|
GetByName(ctx context.Context, name string) (Subreddit, error)
|
||||||
|
|
|
@ -3,6 +3,8 @@ package domain
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
@ -18,6 +20,13 @@ func (u *User) NormalizedName() string {
|
||||||
return strings.ToLower(u.Name)
|
return strings.ToLower(u.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (u *User) Validate() error {
|
||||||
|
return validation.ValidateStruct(u,
|
||||||
|
validation.Field(&u.Name, validation.Required, validation.Length(3, 32)),
|
||||||
|
validation.Field(&u.UserID, validation.Required, validation.Length(5, 7)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type UserRepository interface {
|
type UserRepository interface {
|
||||||
GetByID(context.Context, int64) (User, error)
|
GetByID(context.Context, int64) (User, error)
|
||||||
GetByName(context.Context, string) (User, error)
|
GetByName(context.Context, string) (User, error)
|
||||||
|
|
|
@ -102,6 +102,10 @@ func (p *postgresDeviceRepository) CreateOrUpdate(ctx context.Context, dev *doma
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresDeviceRepository) Create(ctx context.Context, dev *domain.Device) error {
|
func (p *postgresDeviceRepository) Create(ctx context.Context, dev *domain.Device) error {
|
||||||
|
if err := dev.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO devices
|
INSERT INTO devices
|
||||||
(apns_token, sandbox, active_until)
|
(apns_token, sandbox, active_until)
|
||||||
|
@ -118,6 +122,10 @@ func (p *postgresDeviceRepository) Create(ctx context.Context, dev *domain.Devic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresDeviceRepository) Update(ctx context.Context, dev *domain.Device) error {
|
func (p *postgresDeviceRepository) Update(ctx context.Context, dev *domain.Device) error {
|
||||||
|
if err := dev.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
UPDATE devices
|
UPDATE devices
|
||||||
SET active_until = $2
|
SET active_until = $2
|
||||||
|
|
|
@ -76,6 +76,10 @@ func (p *postgresSubredditRepository) GetByName(ctx context.Context, name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresSubredditRepository) CreateOrUpdate(ctx context.Context, sr *domain.Subreddit) error {
|
func (p *postgresSubredditRepository) CreateOrUpdate(ctx context.Context, sr *domain.Subreddit) error {
|
||||||
|
if err := sr.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO subreddits (subreddit_id, name)
|
INSERT INTO subreddits (subreddit_id, name)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
|
|
|
@ -5,8 +5,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/christianselig/apollo-backend/internal/domain"
|
|
||||||
"github.com/jackc/pgx/v4/pgxpool"
|
"github.com/jackc/pgx/v4/pgxpool"
|
||||||
|
|
||||||
|
"github.com/christianselig/apollo-backend/internal/domain"
|
||||||
)
|
)
|
||||||
|
|
||||||
type postgresUserRepository struct {
|
type postgresUserRepository struct {
|
||||||
|
@ -77,6 +78,10 @@ func (p *postgresUserRepository) GetByName(ctx context.Context, name string) (do
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.User) error {
|
func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.User) error {
|
||||||
|
if err := u.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
INSERT INTO users (user_id, name)
|
INSERT INTO users (user_id, name)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2)
|
||||||
|
|
Loading…
Reference in a new issue