mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-25 21:27:42 +00:00
don't let weird watchers through
This commit is contained in:
parent
95b58b2c01
commit
06b715fa6a
2 changed files with 21 additions and 1 deletions
|
@ -1,6 +1,10 @@
|
||||||
package domain
|
package domain
|
||||||
|
|
||||||
import "context"
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
|
)
|
||||||
|
|
||||||
type WatcherType int64
|
type WatcherType int64
|
||||||
|
|
||||||
|
@ -47,6 +51,14 @@ type Watcher struct {
|
||||||
Account Account
|
Account Account
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Watcher) Validate() error {
|
||||||
|
return validation.ValidateStruct(w,
|
||||||
|
validation.Field(&w.Label, validation.Required, validation.Length(1, 64)),
|
||||||
|
validation.Field(&w.Type, validation.Required, validation.In(SubredditWatcher, UserWatcher, TrendingWatcher)),
|
||||||
|
validation.Field(&w.WatcheeID, validation.Required, validation.Min(1)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type WatcherRepository interface {
|
type WatcherRepository interface {
|
||||||
GetByID(ctx context.Context, id int64) (Watcher, error)
|
GetByID(ctx context.Context, id int64) (Watcher, error)
|
||||||
GetBySubredditID(ctx context.Context, id int64) ([]Watcher, error)
|
GetBySubredditID(ctx context.Context, id int64) ([]Watcher, error)
|
||||||
|
|
|
@ -177,6 +177,10 @@ func (p *postgresWatcherRepository) GetByDeviceAPNSTokenAndAccountRedditID(ctx c
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.Watcher) error {
|
func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.Watcher) error {
|
||||||
|
if err := watcher.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
now := float64(time.Now().UTC().Unix())
|
now := float64(time.Now().UTC().Unix())
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
|
@ -204,6 +208,10 @@ func (p *postgresWatcherRepository) Create(ctx context.Context, watcher *domain.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.Watcher) error {
|
func (p *postgresWatcherRepository) Update(ctx context.Context, watcher *domain.Watcher) error {
|
||||||
|
if err := watcher.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
UPDATE watchers
|
UPDATE watchers
|
||||||
SET author = $2,
|
SET author = $2,
|
||||||
|
|
Loading…
Reference in a new issue