[WIP]
This commit is contained in:
parent
34ac03d252
commit
9d20460f16
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -2,11 +2,10 @@ package auth
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type Usecase interface {
|
||||
SignIn(ctx context.Context, email, password string, staySignedIn bool) (*model.User, string, error)
|
||||
ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*model.User, error)
|
||||
SignIn(ctx context.Context, email, password string, staySignedIn bool) (*internal.User, string, error)
|
||||
ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*internal.User, error)
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth/jwt"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
)
|
||||
|
@ -33,7 +33,7 @@ func New(cfg *Config) (*Usecase, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) SignIn(ctx context.Context, email, password string, staySignedIn bool) (*model.User, string, error) {
|
||||
func (ucase *Usecase) SignIn(ctx context.Context, email, password string, staySignedIn bool) (*internal.User, string, error) {
|
||||
u, err := ucase.GetUserByCredentials(ctx, email, password)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
|
@ -53,7 +53,7 @@ func (ucase *Usecase) SignIn(ctx context.Context, email, password string, staySi
|
|||
return u, token, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*model.User, error) {
|
||||
func (ucase *Usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToken string) (*internal.User, error) {
|
||||
metadata, err := ucase.tokenGenerator.ExtractAccessTokenMetadata(accessToken)
|
||||
if err != nil {
|
||||
return nil, errorutil.Wrap(err, messageInvalidAccessToken)
|
||||
|
@ -62,11 +62,11 @@ func (ucase *Usecase) ExtractAccessTokenMetadata(ctx context.Context, accessToke
|
|||
return ucase.GetUserByCredentials(ctx, metadata.Credentials.Email, metadata.Credentials.Password)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetUserByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
func (ucase *Usecase) GetUserByCredentials(ctx context.Context, email, password string) (*internal.User, error) {
|
||||
users, _, err := ucase.userRepository.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.UserFilter{
|
||||
Filter: &internal.UserFilter{
|
||||
Email: []string{email},
|
||||
},
|
||||
})
|
||||
|
|
|
@ -3,10 +3,10 @@ package middleware
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"net/http"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/auth"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -34,14 +34,14 @@ func Authenticate(ucase auth.Usecase) func(next http.Handler) http.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func UserFromContext(ctx context.Context) (*model.User, error) {
|
||||
func UserFromContext(ctx context.Context) (*internal.User, error) {
|
||||
user := ctx.Value(authenticateKey)
|
||||
if user == nil {
|
||||
err := errors.New("couldn't retrieve *model.User")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u, ok := user.(*model.User)
|
||||
u, ok := user.(*internal.User)
|
||||
if !ok {
|
||||
err := errors.New("*model.User has wrong type")
|
||||
return nil, err
|
||||
|
|
|
@ -2,11 +2,11 @@ package dataloader
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"time"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
|
@ -28,9 +28,9 @@ func New(cfg Config) *DataLoader {
|
|||
return &DataLoader{
|
||||
QualificationByID: NewQualificationLoader(QualificationLoaderConfig{
|
||||
Wait: wait,
|
||||
Fetch: func(ids []int) ([]*model.Qualification, []error) {
|
||||
Fetch: func(ids []int) ([]*internal.Qualification, []error) {
|
||||
qualificationsNotInOrder, _, err := cfg.QualificationRepo.Fetch(context.Background(), &qualification.FetchConfig{
|
||||
Filter: &model.QualificationFilter{
|
||||
Filter: &internal.QualificationFilter{
|
||||
ID: ids,
|
||||
},
|
||||
Count: false,
|
||||
|
@ -38,11 +38,11 @@ func New(cfg Config) *DataLoader {
|
|||
if err != nil {
|
||||
return nil, []error{err}
|
||||
}
|
||||
qualificationByID := make(map[int]*model.Qualification)
|
||||
qualificationByID := make(map[int]*internal.Qualification)
|
||||
for _, qualification := range qualificationsNotInOrder {
|
||||
qualificationByID[qualification.ID] = qualification
|
||||
}
|
||||
qualifications := make([]*model.Qualification, len(ids))
|
||||
qualifications := make([]*internal.Qualification, len(ids))
|
||||
for i, id := range ids {
|
||||
qualifications[i] = qualificationByID[id]
|
||||
}
|
||||
|
@ -51,13 +51,13 @@ func New(cfg Config) *DataLoader {
|
|||
}),
|
||||
QualificationsByProfessionID: NewQualificationSliceByProfessionIDLoader(QualificationSliceByProfessionIDLoaderConfig{
|
||||
Wait: wait,
|
||||
Fetch: func(ids []int) ([][]*model.Qualification, []error) {
|
||||
Fetch: func(ids []int) ([][]*internal.Qualification, []error) {
|
||||
m, err := cfg.ProfessionRepo.GetAssociatedQualifications(context.Background(), ids...)
|
||||
if err != nil {
|
||||
return nil, []error{err}
|
||||
}
|
||||
|
||||
qualifications := make([][]*model.Qualification, len(ids))
|
||||
qualifications := make([][]*internal.Qualification, len(ids))
|
||||
|
||||
for i, id := range ids {
|
||||
qualifications[i] = m[id]
|
||||
|
|
|
@ -3,16 +3,15 @@
|
|||
package dataloader
|
||||
|
||||
import (
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
)
|
||||
|
||||
// QualificationLoaderConfig captures the config to create a new QualificationLoader
|
||||
type QualificationLoaderConfig struct {
|
||||
// Fetch is a method that provides the data for the loader
|
||||
Fetch func(keys []int) ([]*model.Qualification, []error)
|
||||
Fetch func(keys []int) ([]*internal.Qualification, []error)
|
||||
|
||||
// Wait is how long wait before sending a batch
|
||||
Wait time.Duration
|
||||
|
@ -33,7 +32,7 @@ func NewQualificationLoader(config QualificationLoaderConfig) *QualificationLoad
|
|||
// QualificationLoader batches and caches requests
|
||||
type QualificationLoader struct {
|
||||
// this method provides the data for the loader
|
||||
fetch func(keys []int) ([]*model.Qualification, []error)
|
||||
fetch func(keys []int) ([]*internal.Qualification, []error)
|
||||
|
||||
// how long to done before sending a batch
|
||||
wait time.Duration
|
||||
|
@ -44,7 +43,7 @@ type QualificationLoader struct {
|
|||
// INTERNAL
|
||||
|
||||
// lazily created cache
|
||||
cache map[int]*model.Qualification
|
||||
cache map[int]*internal.Qualification
|
||||
|
||||
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
// then everything will be sent to the fetch method and out to the listeners
|
||||
|
@ -56,25 +55,25 @@ type QualificationLoader struct {
|
|||
|
||||
type qualificationLoaderBatch struct {
|
||||
keys []int
|
||||
data []*model.Qualification
|
||||
data []*internal.Qualification
|
||||
error []error
|
||||
closing bool
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// Load a Qualification by key, batching and caching will be applied automatically
|
||||
func (l *QualificationLoader) Load(key int) (*model.Qualification, error) {
|
||||
func (l *QualificationLoader) Load(key int) (*internal.Qualification, error) {
|
||||
return l.LoadThunk(key)()
|
||||
}
|
||||
|
||||
// LoadThunk returns a function that when called will block waiting for a Qualification.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationLoader) LoadThunk(key int) func() (*model.Qualification, error) {
|
||||
func (l *QualificationLoader) LoadThunk(key int) func() (*internal.Qualification, error) {
|
||||
l.mu.Lock()
|
||||
if it, ok := l.cache[key]; ok {
|
||||
l.mu.Unlock()
|
||||
return func() (*model.Qualification, error) {
|
||||
return func() (*internal.Qualification, error) {
|
||||
return it, nil
|
||||
}
|
||||
}
|
||||
|
@ -85,10 +84,10 @@ func (l *QualificationLoader) LoadThunk(key int) func() (*model.Qualification, e
|
|||
pos := batch.keyIndex(l, key)
|
||||
l.mu.Unlock()
|
||||
|
||||
return func() (*model.Qualification, error) {
|
||||
return func() (*internal.Qualification, error) {
|
||||
<-batch.done
|
||||
|
||||
var data *model.Qualification
|
||||
var data *internal.Qualification
|
||||
if pos < len(batch.data) {
|
||||
data = batch.data[pos]
|
||||
}
|
||||
|
@ -113,14 +112,14 @@ func (l *QualificationLoader) LoadThunk(key int) func() (*model.Qualification, e
|
|||
|
||||
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
// sub batches depending on how the loader is configured
|
||||
func (l *QualificationLoader) LoadAll(keys []int) ([]*model.Qualification, []error) {
|
||||
results := make([]func() (*model.Qualification, error), len(keys))
|
||||
func (l *QualificationLoader) LoadAll(keys []int) ([]*internal.Qualification, []error) {
|
||||
results := make([]func() (*internal.Qualification, error), len(keys))
|
||||
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
|
||||
qualifications := make([]*model.Qualification, len(keys))
|
||||
qualifications := make([]*internal.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
|
@ -131,13 +130,13 @@ func (l *QualificationLoader) LoadAll(keys []int) ([]*model.Qualification, []err
|
|||
// LoadAllThunk returns a function that when called will block waiting for a Qualifications.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationLoader) LoadAllThunk(keys []int) func() ([]*model.Qualification, []error) {
|
||||
results := make([]func() (*model.Qualification, error), len(keys))
|
||||
func (l *QualificationLoader) LoadAllThunk(keys []int) func() ([]*internal.Qualification, []error) {
|
||||
results := make([]func() (*internal.Qualification, error), len(keys))
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
return func() ([]*model.Qualification, []error) {
|
||||
qualifications := make([]*model.Qualification, len(keys))
|
||||
return func() ([]*internal.Qualification, []error) {
|
||||
qualifications := make([]*internal.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
|
@ -149,7 +148,7 @@ func (l *QualificationLoader) LoadAllThunk(keys []int) func() ([]*model.Qualific
|
|||
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
// and false is returned.
|
||||
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
func (l *QualificationLoader) Prime(key int, value *model.Qualification) bool {
|
||||
func (l *QualificationLoader) Prime(key int, value *internal.Qualification) bool {
|
||||
l.mu.Lock()
|
||||
var found bool
|
||||
if _, found = l.cache[key]; !found {
|
||||
|
@ -169,9 +168,9 @@ func (l *QualificationLoader) Clear(key int) {
|
|||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *QualificationLoader) unsafeSet(key int, value *model.Qualification) {
|
||||
func (l *QualificationLoader) unsafeSet(key int, value *internal.Qualification) {
|
||||
if l.cache == nil {
|
||||
l.cache = map[int]*model.Qualification{}
|
||||
l.cache = map[int]*internal.Qualification{}
|
||||
}
|
||||
l.cache[key] = value
|
||||
}
|
||||
|
|
|
@ -3,16 +3,15 @@
|
|||
package dataloader
|
||||
|
||||
import (
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
)
|
||||
|
||||
// QualificationSliceByProfessionIDLoaderConfig captures the config to create a new QualificationSliceByProfessionIDLoader
|
||||
type QualificationSliceByProfessionIDLoaderConfig struct {
|
||||
// Fetch is a method that provides the data for the loader
|
||||
Fetch func(keys []int) ([][]*model.Qualification, []error)
|
||||
Fetch func(keys []int) ([][]*internal.Qualification, []error)
|
||||
|
||||
// Wait is how long wait before sending a batch
|
||||
Wait time.Duration
|
||||
|
@ -33,7 +32,7 @@ func NewQualificationSliceByProfessionIDLoader(config QualificationSliceByProfes
|
|||
// QualificationSliceByProfessionIDLoader batches and caches requests
|
||||
type QualificationSliceByProfessionIDLoader struct {
|
||||
// this method provides the data for the loader
|
||||
fetch func(keys []int) ([][]*model.Qualification, []error)
|
||||
fetch func(keys []int) ([][]*internal.Qualification, []error)
|
||||
|
||||
// how long to done before sending a batch
|
||||
wait time.Duration
|
||||
|
@ -44,7 +43,7 @@ type QualificationSliceByProfessionIDLoader struct {
|
|||
// INTERNAL
|
||||
|
||||
// lazily created cache
|
||||
cache map[int][]*model.Qualification
|
||||
cache map[int][]*internal.Qualification
|
||||
|
||||
// the current batch. keys will continue to be collected until timeout is hit,
|
||||
// then everything will be sent to the fetch method and out to the listeners
|
||||
|
@ -56,25 +55,25 @@ type QualificationSliceByProfessionIDLoader struct {
|
|||
|
||||
type qualificationSliceByProfessionIDLoaderBatch struct {
|
||||
keys []int
|
||||
data [][]*model.Qualification
|
||||
data [][]*internal.Qualification
|
||||
error []error
|
||||
closing bool
|
||||
done chan struct{}
|
||||
}
|
||||
|
||||
// Load a Qualification by key, batching and caching will be applied automatically
|
||||
func (l *QualificationSliceByProfessionIDLoader) Load(key int) ([]*model.Qualification, error) {
|
||||
func (l *QualificationSliceByProfessionIDLoader) Load(key int) ([]*internal.Qualification, error) {
|
||||
return l.LoadThunk(key)()
|
||||
}
|
||||
|
||||
// LoadThunk returns a function that when called will block waiting for a Qualification.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadThunk(key int) func() ([]*model.Qualification, error) {
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadThunk(key int) func() ([]*internal.Qualification, error) {
|
||||
l.mu.Lock()
|
||||
if it, ok := l.cache[key]; ok {
|
||||
l.mu.Unlock()
|
||||
return func() ([]*model.Qualification, error) {
|
||||
return func() ([]*internal.Qualification, error) {
|
||||
return it, nil
|
||||
}
|
||||
}
|
||||
|
@ -85,10 +84,10 @@ func (l *QualificationSliceByProfessionIDLoader) LoadThunk(key int) func() ([]*m
|
|||
pos := batch.keyIndex(l, key)
|
||||
l.mu.Unlock()
|
||||
|
||||
return func() ([]*model.Qualification, error) {
|
||||
return func() ([]*internal.Qualification, error) {
|
||||
<-batch.done
|
||||
|
||||
var data []*model.Qualification
|
||||
var data []*internal.Qualification
|
||||
if pos < len(batch.data) {
|
||||
data = batch.data[pos]
|
||||
}
|
||||
|
@ -113,14 +112,14 @@ func (l *QualificationSliceByProfessionIDLoader) LoadThunk(key int) func() ([]*m
|
|||
|
||||
// LoadAll fetches many keys at once. It will be broken into appropriate sized
|
||||
// sub batches depending on how the loader is configured
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadAll(keys []int) ([][]*model.Qualification, []error) {
|
||||
results := make([]func() ([]*model.Qualification, error), len(keys))
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadAll(keys []int) ([][]*internal.Qualification, []error) {
|
||||
results := make([]func() ([]*internal.Qualification, error), len(keys))
|
||||
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
|
||||
qualifications := make([][]*model.Qualification, len(keys))
|
||||
qualifications := make([][]*internal.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
|
@ -131,13 +130,13 @@ func (l *QualificationSliceByProfessionIDLoader) LoadAll(keys []int) ([][]*model
|
|||
// LoadAllThunk returns a function that when called will block waiting for a Qualifications.
|
||||
// This method should be used if you want one goroutine to make requests to many
|
||||
// different data loaders without blocking until the thunk is called.
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadAllThunk(keys []int) func() ([][]*model.Qualification, []error) {
|
||||
results := make([]func() ([]*model.Qualification, error), len(keys))
|
||||
func (l *QualificationSliceByProfessionIDLoader) LoadAllThunk(keys []int) func() ([][]*internal.Qualification, []error) {
|
||||
results := make([]func() ([]*internal.Qualification, error), len(keys))
|
||||
for i, key := range keys {
|
||||
results[i] = l.LoadThunk(key)
|
||||
}
|
||||
return func() ([][]*model.Qualification, []error) {
|
||||
qualifications := make([][]*model.Qualification, len(keys))
|
||||
return func() ([][]*internal.Qualification, []error) {
|
||||
qualifications := make([][]*internal.Qualification, len(keys))
|
||||
errors := make([]error, len(keys))
|
||||
for i, thunk := range results {
|
||||
qualifications[i], errors[i] = thunk()
|
||||
|
@ -149,13 +148,13 @@ func (l *QualificationSliceByProfessionIDLoader) LoadAllThunk(keys []int) func()
|
|||
// Prime the cache with the provided key and value. If the key already exists, no change is made
|
||||
// and false is returned.
|
||||
// (To forcefully prime the cache, clear the key first with loader.clear(key).prime(key, value).)
|
||||
func (l *QualificationSliceByProfessionIDLoader) Prime(key int, value []*model.Qualification) bool {
|
||||
func (l *QualificationSliceByProfessionIDLoader) Prime(key int, value []*internal.Qualification) bool {
|
||||
l.mu.Lock()
|
||||
var found bool
|
||||
if _, found = l.cache[key]; !found {
|
||||
// make a copy when writing to the cache, its easy to pass a pointer in from a loop var
|
||||
// and end up with the whole cache pointing to the same value.
|
||||
cpy := make([]*model.Qualification, len(value))
|
||||
cpy := make([]*internal.Qualification, len(value))
|
||||
copy(cpy, value)
|
||||
l.unsafeSet(key, cpy)
|
||||
}
|
||||
|
@ -170,9 +169,9 @@ func (l *QualificationSliceByProfessionIDLoader) Clear(key int) {
|
|||
l.mu.Unlock()
|
||||
}
|
||||
|
||||
func (l *QualificationSliceByProfessionIDLoader) unsafeSet(key int, value []*model.Qualification) {
|
||||
func (l *QualificationSliceByProfessionIDLoader) unsafeSet(key int, value []*internal.Qualification) {
|
||||
if l.cache == nil {
|
||||
l.cache = map[int][]*model.Qualification{}
|
||||
l.cache = map[int][]*internal.Qualification{}
|
||||
}
|
||||
l.cache[key] = value
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ package directive
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/99designs/gqlgen/graphql"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/chi/middleware"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
)
|
||||
|
||||
|
@ -24,7 +24,7 @@ func (d *Directive) Authenticated(ctx context.Context, _ interface{}, next graph
|
|||
return next(ctx)
|
||||
}
|
||||
|
||||
func (d *Directive) HasRole(ctx context.Context, _ interface{}, next graphql.Resolver, role model.Role) (interface{}, error) {
|
||||
func (d *Directive) HasRole(ctx context.Context, _ interface{}, next graphql.Resolver, role internal.Role) (interface{}, error) {
|
||||
user, err := middleware.UserFromContext(ctx)
|
||||
if err != nil {
|
||||
return nil, errorutil.Wrap(err, messageMustBeSignedIn)
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -3,30 +3,30 @@
|
|||
package generated
|
||||
|
||||
import (
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type ProfessionList struct {
|
||||
Total int `json:"total"`
|
||||
Items []*model.Profession `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Items []*internal.Profession `json:"items"`
|
||||
}
|
||||
|
||||
type QualificationList struct {
|
||||
Total int `json:"total"`
|
||||
Items []*model.Qualification `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Items []*internal.Qualification `json:"items"`
|
||||
}
|
||||
|
||||
type QuestionList struct {
|
||||
Total int `json:"total"`
|
||||
Items []*model.Question `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Items []*internal.Question `json:"items"`
|
||||
}
|
||||
|
||||
type UserList struct {
|
||||
Total int `json:"total"`
|
||||
Items []*model.User `json:"items"`
|
||||
Total int `json:"total"`
|
||||
Items []*internal.User `json:"items"`
|
||||
}
|
||||
|
||||
type UserWithToken struct {
|
||||
Token string `json:"token"`
|
||||
User *model.User `json:"user"`
|
||||
Token string `json:"token"`
|
||||
User *internal.User `json:"user"`
|
||||
}
|
||||
|
|
|
@ -19,52 +19,52 @@ models:
|
|||
- github.com/99designs/gqlgen/graphql.Int32
|
||||
Role:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.Role
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.Role
|
||||
User:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.User
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.User
|
||||
UserFilter:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.UserFilter
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.UserFilter
|
||||
UserFilterOr:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.UserFilterOr
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.UserFilterOr
|
||||
UserInput:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.UserInput
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.UserInput
|
||||
UpdateManyUsersInput:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.UserInput
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.UserInput
|
||||
Profession:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.Profession
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.Profession
|
||||
ProfessionFilter:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.ProfessionFilter
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.ProfessionFilter
|
||||
ProfessionInput:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.ProfessionInput
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.ProfessionInput
|
||||
Qualification:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.Qualification
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.Qualification
|
||||
QualificationFilter:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.QualificationFilter
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.QualificationFilter
|
||||
QualificationFilterOr:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.QualificationFilterOr
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.QualificationFilterOr
|
||||
QualificationInput:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.QualificationInput
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.QualificationInput
|
||||
Answer:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.Answer
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.Answer
|
||||
Question:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.Question
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.Question
|
||||
QuestionFilter:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.QuestionFilter
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.QuestionFilter
|
||||
QuestionInput:
|
||||
model:
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal/model.QuestionInput
|
||||
- github.com/zdam-egzamin-zawodowy/backend/internal.QuestionInput
|
||||
|
|
|
@ -3,9 +3,9 @@ package querycomplexity
|
|||
import (
|
||||
"github.com/99designs/gqlgen/graphql/handler/extension"
|
||||
"github.com/Kichiyaki/goutil/safeptr"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
|
@ -34,7 +34,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.ProfessionList.Total = getCountComplexity
|
||||
complexityRoot.Query.Professions = func(
|
||||
childComplexity int,
|
||||
filter *model.ProfessionFilter,
|
||||
filter *internal.ProfessionFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -50,7 +50,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.QualificationList.Total = getCountComplexity
|
||||
complexityRoot.Query.Qualifications = func(
|
||||
childComplexity int,
|
||||
filter *model.QualificationFilter,
|
||||
filter *internal.QualificationFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -66,7 +66,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.QuestionList.Total = getCountComplexity
|
||||
complexityRoot.Query.Questions = func(
|
||||
childComplexity int,
|
||||
filter *model.QuestionFilter,
|
||||
filter *internal.QuestionFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -90,7 +90,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.UserList.Total = getCountComplexity
|
||||
complexityRoot.Query.Users = func(
|
||||
childComplexity int,
|
||||
filter *model.UserFilter,
|
||||
filter *internal.UserFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -103,22 +103,22 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
)
|
||||
}
|
||||
|
||||
complexityRoot.Mutation.CreateProfession = func(childComplexity int, input model.ProfessionInput) int {
|
||||
complexityRoot.Mutation.CreateProfession = func(childComplexity int, input internal.ProfessionInput) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
||||
complexityRoot.Mutation.CreateQualification = func(
|
||||
childComplexity int,
|
||||
input model.QualificationInput,
|
||||
input internal.QualificationInput,
|
||||
) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
||||
complexityRoot.Mutation.CreateQuestion = func(childComplexity int, input model.QuestionInput) int {
|
||||
complexityRoot.Mutation.CreateQuestion = func(childComplexity int, input internal.QuestionInput) int {
|
||||
return (complexityLimit / 4) + childComplexity
|
||||
}
|
||||
|
||||
complexityRoot.Mutation.CreateUser = func(childComplexity int, input model.UserInput) int {
|
||||
complexityRoot.Mutation.CreateUser = func(childComplexity int, input internal.UserInput) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
||||
|
@ -134,7 +134,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.Mutation.UpdateManyUsers = func(
|
||||
childComplexity int,
|
||||
ids []int,
|
||||
input model.UserInput,
|
||||
input internal.UserInput,
|
||||
) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.Mutation.UpdateProfession = func(
|
||||
childComplexity int,
|
||||
id int,
|
||||
input model.ProfessionInput,
|
||||
input internal.ProfessionInput,
|
||||
) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
@ -150,7 +150,7 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.Mutation.UpdateQualification = func(
|
||||
childComplexity int,
|
||||
id int,
|
||||
input model.QualificationInput,
|
||||
input internal.QualificationInput,
|
||||
) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
@ -158,12 +158,12 @@ func GetComplexityRoot() generated.ComplexityRoot {
|
|||
complexityRoot.Mutation.UpdateQuestion = func(
|
||||
childComplexity int,
|
||||
id int,
|
||||
input model.QuestionInput,
|
||||
input internal.QuestionInput,
|
||||
) int {
|
||||
return (complexityLimit / 4) + childComplexity
|
||||
}
|
||||
|
||||
complexityRoot.Mutation.UpdateUser = func(childComplexity int, id int, input model.UserInput) int {
|
||||
complexityRoot.Mutation.UpdateUser = func(childComplexity int, id int, input internal.UserInput) int {
|
||||
return (complexityLimit / 5) + childComplexity
|
||||
}
|
||||
|
||||
|
|
|
@ -6,31 +6,31 @@ package resolvers
|
|||
import (
|
||||
"context"
|
||||
"github.com/Kichiyaki/goutil/safeptr"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/chi/middleware"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) CreateProfession(ctx context.Context, input model.ProfessionInput) (*model.Profession, error) {
|
||||
func (r *mutationResolver) CreateProfession(ctx context.Context, input internal.ProfessionInput) (*internal.Profession, error) {
|
||||
return r.ProfessionUsecase.Store(ctx, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateProfession(ctx context.Context, id int, input model.ProfessionInput) (*model.Profession, error) {
|
||||
func (r *mutationResolver) UpdateProfession(ctx context.Context, id int, input internal.ProfessionInput) (*internal.Profession, error) {
|
||||
return r.ProfessionUsecase.UpdateOneByID(ctx, id, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteProfessions(ctx context.Context, ids []int) ([]*model.Profession, error) {
|
||||
return r.ProfessionUsecase.Delete(ctx, &model.ProfessionFilter{
|
||||
func (r *mutationResolver) DeleteProfessions(ctx context.Context, ids []int) ([]*internal.Profession, error) {
|
||||
return r.ProfessionUsecase.Delete(ctx, &internal.ProfessionFilter{
|
||||
ID: ids,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *queryResolver) Professions(
|
||||
ctx context.Context,
|
||||
filter *model.ProfessionFilter,
|
||||
filter *internal.ProfessionFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -50,7 +50,7 @@ func (r *queryResolver) Professions(
|
|||
return list, err
|
||||
}
|
||||
|
||||
func (r *queryResolver) Profession(ctx context.Context, id *int, slug *string) (*model.Profession, error) {
|
||||
func (r *queryResolver) Profession(ctx context.Context, id *int, slug *string) (*internal.Profession, error) {
|
||||
if id != nil {
|
||||
return r.ProfessionUsecase.GetByID(ctx, *id)
|
||||
} else if slug != nil {
|
||||
|
@ -62,12 +62,12 @@ func (r *queryResolver) Profession(ctx context.Context, id *int, slug *string) (
|
|||
|
||||
func (r *professionResolver) Qualifications(
|
||||
ctx context.Context,
|
||||
obj *model.Profession,
|
||||
) ([]*model.Qualification, error) {
|
||||
obj *internal.Profession,
|
||||
) ([]*internal.Qualification, error) {
|
||||
if obj != nil {
|
||||
if dataloader, err := middleware.DataLoaderFromContext(ctx); err == nil && dataloader != nil {
|
||||
return dataloader.QualificationsByProfessionID.Load(obj.ID)
|
||||
}
|
||||
}
|
||||
return []*model.Qualification{}, nil
|
||||
return []*internal.Qualification{}, nil
|
||||
}
|
||||
|
|
|
@ -6,29 +6,29 @@ package resolvers
|
|||
import (
|
||||
"context"
|
||||
"github.com/Kichiyaki/goutil/safeptr"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) CreateQualification(ctx context.Context, input model.QualificationInput) (*model.Qualification, error) {
|
||||
func (r *mutationResolver) CreateQualification(ctx context.Context, input internal.QualificationInput) (*internal.Qualification, error) {
|
||||
return r.QualificationUsecase.Store(ctx, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateQualification(ctx context.Context, id int, input model.QualificationInput) (*model.Qualification, error) {
|
||||
func (r *mutationResolver) UpdateQualification(ctx context.Context, id int, input internal.QualificationInput) (*internal.Qualification, error) {
|
||||
return r.QualificationUsecase.UpdateOneByID(ctx, id, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteQualifications(ctx context.Context, ids []int) ([]*model.Qualification, error) {
|
||||
return r.QualificationUsecase.Delete(ctx, &model.QualificationFilter{
|
||||
func (r *mutationResolver) DeleteQualifications(ctx context.Context, ids []int) ([]*internal.Qualification, error) {
|
||||
return r.QualificationUsecase.Delete(ctx, &internal.QualificationFilter{
|
||||
ID: ids,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *queryResolver) Qualifications(
|
||||
ctx context.Context,
|
||||
filter *model.QualificationFilter,
|
||||
filter *internal.QualificationFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -70,7 +70,7 @@ func (r *queryResolver) SimilarQualifications(
|
|||
return list, err
|
||||
}
|
||||
|
||||
func (r *queryResolver) Qualification(ctx context.Context, id *int, slug *string) (*model.Qualification, error) {
|
||||
func (r *queryResolver) Qualification(ctx context.Context, id *int, slug *string) (*internal.Qualification, error) {
|
||||
if id != nil {
|
||||
return r.QualificationUsecase.GetByID(ctx, *id)
|
||||
} else if slug != nil {
|
||||
|
|
|
@ -6,28 +6,28 @@ package resolvers
|
|||
import (
|
||||
"context"
|
||||
"github.com/Kichiyaki/goutil/safeptr"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/chi/middleware"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) CreateQuestion(ctx context.Context, input model.QuestionInput) (*model.Question, error) {
|
||||
func (r *mutationResolver) CreateQuestion(ctx context.Context, input internal.QuestionInput) (*internal.Question, error) {
|
||||
return r.QuestionUsecase.Store(ctx, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateQuestion(ctx context.Context, id int, input model.QuestionInput) (*model.Question, error) {
|
||||
func (r *mutationResolver) UpdateQuestion(ctx context.Context, id int, input internal.QuestionInput) (*internal.Question, error) {
|
||||
return r.QuestionUsecase.UpdateOneByID(ctx, id, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteQuestions(ctx context.Context, ids []int) ([]*model.Question, error) {
|
||||
return r.QuestionUsecase.Delete(ctx, &model.QuestionFilter{
|
||||
func (r *mutationResolver) DeleteQuestions(ctx context.Context, ids []int) ([]*internal.Question, error) {
|
||||
return r.QuestionUsecase.Delete(ctx, &internal.QuestionFilter{
|
||||
ID: ids,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *queryResolver) GenerateTest(ctx context.Context, qualificationIDs []int, limit *int) ([]*model.Question, error) {
|
||||
func (r *queryResolver) GenerateTest(ctx context.Context, qualificationIDs []int, limit *int) ([]*internal.Question, error) {
|
||||
return r.QuestionUsecase.GenerateTest(ctx, &question.GenerateTestConfig{
|
||||
Qualifications: qualificationIDs,
|
||||
Limit: safeptr.SafeIntPointer(limit, question.TestMaxLimit),
|
||||
|
@ -36,7 +36,7 @@ func (r *queryResolver) GenerateTest(ctx context.Context, qualificationIDs []int
|
|||
|
||||
func (r *queryResolver) Questions(
|
||||
ctx context.Context,
|
||||
filter *model.QuestionFilter,
|
||||
filter *internal.QuestionFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -56,7 +56,7 @@ func (r *queryResolver) Questions(
|
|||
return list, err
|
||||
}
|
||||
|
||||
func (r *questionResolver) Qualification(ctx context.Context, obj *model.Question) (*model.Qualification, error) {
|
||||
func (r *questionResolver) Qualification(ctx context.Context, obj *internal.Question) (*internal.Qualification, error) {
|
||||
if obj != nil && obj.Qualification != nil {
|
||||
return obj.Qualification, nil
|
||||
}
|
||||
|
|
|
@ -6,33 +6,33 @@ package resolvers
|
|||
import (
|
||||
"context"
|
||||
"github.com/Kichiyaki/goutil/safeptr"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/chi/middleware"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/graphql/generated"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
)
|
||||
|
||||
func (r *mutationResolver) CreateUser(ctx context.Context, input model.UserInput) (*model.User, error) {
|
||||
func (r *mutationResolver) CreateUser(ctx context.Context, input internal.UserInput) (*internal.User, error) {
|
||||
return r.UserUsecase.Store(ctx, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateUser(ctx context.Context, id int, input model.UserInput) (*model.User, error) {
|
||||
func (r *mutationResolver) UpdateUser(ctx context.Context, id int, input internal.UserInput) (*internal.User, error) {
|
||||
return r.UserUsecase.UpdateOneByID(ctx, id, &input)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) UpdateManyUsers(ctx context.Context, ids []int, input model.UserInput) ([]*model.User, error) {
|
||||
func (r *mutationResolver) UpdateManyUsers(ctx context.Context, ids []int, input internal.UserInput) ([]*internal.User, error) {
|
||||
return r.UserUsecase.UpdateMany(
|
||||
ctx,
|
||||
&model.UserFilter{
|
||||
&internal.UserFilter{
|
||||
ID: ids,
|
||||
},
|
||||
&input,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *mutationResolver) DeleteUsers(ctx context.Context, ids []int) ([]*model.User, error) {
|
||||
return r.UserUsecase.Delete(ctx, &model.UserFilter{
|
||||
func (r *mutationResolver) DeleteUsers(ctx context.Context, ids []int) ([]*internal.User, error) {
|
||||
return r.UserUsecase.Delete(ctx, &internal.UserFilter{
|
||||
ID: ids,
|
||||
})
|
||||
}
|
||||
|
@ -59,7 +59,7 @@ func (r *mutationResolver) SignIn(
|
|||
|
||||
func (r *queryResolver) Users(
|
||||
ctx context.Context,
|
||||
filter *model.UserFilter,
|
||||
filter *internal.UserFilter,
|
||||
limit *int,
|
||||
offset *int,
|
||||
sort []string,
|
||||
|
@ -79,11 +79,11 @@ func (r *queryResolver) Users(
|
|||
return userList, err
|
||||
}
|
||||
|
||||
func (r *queryResolver) User(ctx context.Context, id int) (*model.User, error) {
|
||||
func (r *queryResolver) User(ctx context.Context, id int) (*internal.User, error) {
|
||||
return r.UserUsecase.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (r *queryResolver) Me(ctx context.Context) (*model.User, error) {
|
||||
func (r *queryResolver) Me(ctx context.Context) (*internal.User, error) {
|
||||
u, _ := middleware.UserFromContext(ctx)
|
||||
return u, nil
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"reflect"
|
|
@ -4,14 +4,13 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/goutil/envutil"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/Kichiyaki/go-pg-logrus-query-logger/v10"
|
||||
"github.com/go-pg/pg/v10"
|
||||
"github.com/go-pg/pg/v10/orm"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sethvargo/go-password/password"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
)
|
||||
|
||||
var log = logrus.WithField("package", "internal/postgres")
|
||||
|
@ -21,7 +20,7 @@ type Config struct {
|
|||
}
|
||||
|
||||
func init() {
|
||||
orm.RegisterTable((*model.QualificationToProfession)(nil))
|
||||
orm.RegisterTable((*internal.QualificationToProfession)(nil))
|
||||
}
|
||||
|
||||
func Connect(cfg *Config) (*pg.DB, error) {
|
||||
|
@ -56,11 +55,11 @@ func prepareOptions() *pg.Options {
|
|||
func createSchema(db *pg.DB) error {
|
||||
return db.RunInTransaction(context.Background(), func(tx *pg.Tx) error {
|
||||
modelsToCreate := []interface{}{
|
||||
(*model.User)(nil),
|
||||
(*model.Profession)(nil),
|
||||
(*model.Qualification)(nil),
|
||||
(*model.QualificationToProfession)(nil),
|
||||
(*model.Question)(nil),
|
||||
(*User)(nil),
|
||||
(*Profession)(nil),
|
||||
(*Qualification)(nil),
|
||||
(*QualificationToProfession)(nil),
|
||||
(*Question)(nil),
|
||||
}
|
||||
|
||||
for _, model := range modelsToCreate {
|
||||
|
@ -73,7 +72,7 @@ func createSchema(db *pg.DB) error {
|
|||
}
|
||||
}
|
||||
|
||||
total, err := tx.Model(modelsToCreate[0]).Where("role = ?", model.RoleAdmin).Count()
|
||||
total, err := tx.Model(modelsToCreate[0]).Where("role = ?", internal.RoleAdmin).Count()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "couldn't count admins")
|
||||
}
|
||||
|
@ -85,10 +84,10 @@ func createSchema(db *pg.DB) error {
|
|||
}
|
||||
email := "admin@admin.com"
|
||||
_, err = tx.
|
||||
Model(&model.User{
|
||||
Model(&User{
|
||||
DisplayName: "admin",
|
||||
Email: email,
|
||||
Role: model.RoleAdmin,
|
||||
Role: RoleAdmin,
|
||||
Activated: &activated,
|
||||
Password: pswd,
|
||||
}).
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -2,12 +2,11 @@ package profession
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type FetchConfig struct {
|
||||
Filter *model.ProfessionFilter
|
||||
Filter *internal.ProfessionFilter
|
||||
Offset int
|
||||
Limit int
|
||||
Sort []string
|
||||
|
@ -15,9 +14,9 @@ type FetchConfig struct {
|
|||
}
|
||||
|
||||
type Repository interface {
|
||||
Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error)
|
||||
UpdateMany(ctx context.Context, f *model.ProfessionFilter, input *model.ProfessionInput) ([]*model.Profession, error)
|
||||
Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Profession, int, error)
|
||||
GetAssociatedQualifications(ctx context.Context, ids ...int) (map[int][]*model.Qualification, error)
|
||||
Store(ctx context.Context, input *internal.ProfessionInput) (*internal.Profession, error)
|
||||
UpdateMany(ctx context.Context, f *internal.ProfessionFilter, input *internal.ProfessionInput) ([]*internal.Profession, error)
|
||||
Delete(ctx context.Context, f *internal.ProfessionFilter) ([]*internal.Profession, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Profession, int, error)
|
||||
GetAssociatedQualifications(ctx context.Context, ids ...int) (map[int][]*internal.Qualification, error)
|
||||
}
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"strings"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
)
|
||||
|
||||
|
@ -33,7 +33,7 @@ func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *internal.ProfessionInput) (*internal.Profession, error) {
|
||||
item := input.ToProfession()
|
||||
if _, err := repo.
|
||||
Model(item).
|
||||
|
@ -45,9 +45,9 @@ func (repo *PGRepository) Store(ctx context.Context, input *model.ProfessionInpu
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.ProfessionFilter, input *model.ProfessionInput) ([]*model.Profession, error) {
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *internal.ProfessionFilter, input *internal.ProfessionInput) ([]*internal.Profession, error) {
|
||||
if _, err := repo.
|
||||
Model(&model.Profession{}).
|
||||
Model(&internal.Profession{}).
|
||||
Context(ctx).
|
||||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
|
@ -64,8 +64,8 @@ func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.ProfessionFil
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
items := make([]*model.Profession, 0)
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *internal.ProfessionFilter) ([]*internal.Profession, error) {
|
||||
items := make([]*internal.Profession, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
|
@ -77,9 +77,9 @@ func (repo *PGRepository) Delete(ctx context.Context, f *model.ProfessionFilter)
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*internal.Profession, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Profession, 0)
|
||||
items := make([]*internal.Profession, 0)
|
||||
total := 0
|
||||
query := repo.
|
||||
Model(&items).
|
||||
|
@ -105,12 +105,12 @@ func (repo *PGRepository) Fetch(ctx context.Context, cfg *profession.FetchConfig
|
|||
func (repo *PGRepository) GetAssociatedQualifications(
|
||||
ctx context.Context,
|
||||
ids ...int,
|
||||
) (map[int][]*model.Qualification, error) {
|
||||
m := make(map[int][]*model.Qualification)
|
||||
) (map[int][]*internal.Qualification, error) {
|
||||
m := make(map[int][]*internal.Qualification)
|
||||
for _, id := range ids {
|
||||
m[id] = make([]*model.Qualification, 0)
|
||||
m[id] = make([]*internal.Qualification, 0)
|
||||
}
|
||||
var qualificationToProfession []*model.QualificationToProfession
|
||||
var qualificationToProfession []*internal.QualificationToProfession
|
||||
if err := repo.
|
||||
Model(&qualificationToProfession).
|
||||
Context(ctx).
|
||||
|
|
|
@ -2,15 +2,14 @@ package profession
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type Usecase interface {
|
||||
Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *model.ProfessionInput) (*model.Profession, error)
|
||||
Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Profession, int, error)
|
||||
GetByID(ctx context.Context, id int) (*model.Profession, error)
|
||||
GetBySlug(ctx context.Context, slug string) (*model.Profession, error)
|
||||
Store(ctx context.Context, input *internal.ProfessionInput) (*internal.Profession, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *internal.ProfessionInput) (*internal.Profession, error)
|
||||
Delete(ctx context.Context, f *internal.ProfessionFilter) ([]*internal.Profession, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Profession, int, error)
|
||||
GetByID(ctx context.Context, id int) (*internal.Profession, error)
|
||||
GetBySlug(ctx context.Context, slug string) (*internal.Profession, error)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/profession"
|
||||
)
|
||||
|
||||
|
@ -27,14 +27,14 @@ func New(cfg *Config) (*Usecase, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *internal.ProfessionInput) (*internal.Profession, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.professionRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.ProfessionInput) (*model.Profession, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *internal.ProfessionInput) (*internal.Profession, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Pr
|
|||
return nil, err
|
||||
}
|
||||
items, err := ucase.professionRepository.UpdateMany(ctx,
|
||||
&model.ProfessionFilter{
|
||||
&internal.ProfessionFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
input)
|
||||
|
@ -55,11 +55,11 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Pr
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.ProfessionFilter) ([]*model.Profession, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *internal.ProfessionFilter) ([]*internal.Profession, error) {
|
||||
return ucase.professionRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*model.Profession, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([]*internal.Profession, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &profession.FetchConfig{
|
||||
Limit: profession.FetchDefaultLimit,
|
||||
|
@ -73,11 +73,11 @@ func (ucase *Usecase) Fetch(ctx context.Context, cfg *profession.FetchConfig) ([
|
|||
return ucase.professionRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Profession, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*internal.Profession, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.ProfessionFilter{
|
||||
Filter: &internal.ProfessionFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
})
|
||||
|
@ -90,11 +90,11 @@ func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Profession, e
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Profession, error) {
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*internal.Profession, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &profession.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.ProfessionFilter{
|
||||
Filter: &internal.ProfessionFilter{
|
||||
Slug: []string{slug},
|
||||
},
|
||||
})
|
||||
|
@ -111,7 +111,7 @@ type validateOptions struct {
|
|||
allowNilValues bool
|
||||
}
|
||||
|
||||
func validateInput(input *model.ProfessionInput, opts validateOptions) error {
|
||||
func validateInput(input *internal.ProfessionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -58,9 +58,8 @@ type QualificationInput struct {
|
|||
DissociateProfession []int `json:"dissociateProfession" xml:"dissociateProfession" gqlgen:"dissociateProfession"`
|
||||
}
|
||||
|
||||
func (input *QualificationInput) IsEmpty() bool {
|
||||
return input == nil &&
|
||||
input.Name == nil &&
|
||||
func (input QualificationInput) IsEmpty() bool {
|
||||
return input.Name == nil &&
|
||||
input.Code == nil &&
|
||||
input.Formula == nil &&
|
||||
input.Description == nil &&
|
||||
|
@ -68,12 +67,11 @@ func (input *QualificationInput) IsEmpty() bool {
|
|||
len(input.DissociateProfession) == 0
|
||||
}
|
||||
|
||||
func (input *QualificationInput) HasBasicDataToUpdate() bool {
|
||||
return input != nil &&
|
||||
(input.Name != nil ||
|
||||
input.Code != nil ||
|
||||
input.Formula != nil ||
|
||||
input.Description != nil)
|
||||
func (input QualificationInput) HasBasicDataToUpdate() bool {
|
||||
return input.Name != nil ||
|
||||
input.Code != nil ||
|
||||
input.Formula != nil ||
|
||||
input.Description != nil
|
||||
}
|
||||
|
||||
func (input *QualificationInput) Sanitize() *QualificationInput {
|
||||
|
@ -193,7 +191,7 @@ type QualificationFilter struct {
|
|||
CreatedAtLT time.Time `gqlgen:"createdAtLT" json:"createdAtLT" xml:"createdAtLT"`
|
||||
CreatedAtLTE time.Time `json:"createdAtLTE" xml:"createdAtLTE" gqlgen:"createdAtLTE"`
|
||||
|
||||
Or *QualificationFilterOr `json:"or" xml:"or" gqlgen:"or"`
|
||||
Or QualificationFilterOr `json:"or" xml:"or" gqlgen:"or"`
|
||||
}
|
||||
|
||||
func (f *QualificationFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Query, error) {
|
||||
|
@ -286,9 +284,7 @@ func (f *QualificationFilter) WhereWithAlias(q *orm.Query, alias string) (*orm.Q
|
|||
q = q.Where(gopgutil.BuildConditionLTE("?"), gopgutil.AddAliasToColumnName("created_at", alias), f.CreatedAtLTE)
|
||||
}
|
||||
|
||||
if f.Or != nil {
|
||||
q = f.Or.WhereWithAlias(q, alias)
|
||||
}
|
||||
q = f.Or.WhereWithAlias(q, alias)
|
||||
|
||||
return q, nil
|
||||
}
|
|
@ -2,12 +2,11 @@ package qualification
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type FetchConfig struct {
|
||||
Filter *model.QualificationFilter
|
||||
Filter *internal.QualificationFilter
|
||||
Offset int
|
||||
Limit int
|
||||
Sort []string
|
||||
|
@ -23,9 +22,9 @@ type GetSimilarConfig struct {
|
|||
}
|
||||
|
||||
type Repository interface {
|
||||
Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error)
|
||||
UpdateMany(ctx context.Context, f *model.QualificationFilter, input *model.QualificationInput) ([]*model.Qualification, error)
|
||||
Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Qualification, int, error)
|
||||
GetSimilar(ctx context.Context, cfg *GetSimilarConfig) ([]*model.Qualification, int, error)
|
||||
Store(ctx context.Context, input *internal.QualificationInput) (*internal.Qualification, error)
|
||||
UpdateMany(ctx context.Context, f *internal.QualificationFilter, input *internal.QualificationInput) ([]*internal.Qualification, error)
|
||||
Delete(ctx context.Context, f *internal.QualificationFilter) ([]*internal.Qualification, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Qualification, int, error)
|
||||
GetSimilar(ctx context.Context, cfg *GetSimilarConfig) ([]*internal.Qualification, int, error)
|
||||
}
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"strings"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
|
@ -33,7 +33,7 @@ func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *internal.QualificationInput) (*internal.Qualification, error) {
|
||||
item := input.ToQualification()
|
||||
err := repo.RunInTransaction(ctx, func(tx *pg.Tx) error {
|
||||
if _, err := tx.
|
||||
|
@ -57,14 +57,14 @@ func (repo *PGRepository) Store(ctx context.Context, input *model.QualificationI
|
|||
|
||||
func (repo *PGRepository) UpdateMany(
|
||||
ctx context.Context,
|
||||
f *model.QualificationFilter,
|
||||
input *model.QualificationInput,
|
||||
) ([]*model.Qualification, error) {
|
||||
items := make([]*model.Qualification, 0)
|
||||
f *internal.QualificationFilter,
|
||||
input *internal.QualificationInput,
|
||||
) ([]*internal.Qualification, error) {
|
||||
items := make([]*internal.Qualification, 0)
|
||||
err := repo.RunInTransaction(ctx, func(tx *pg.Tx) error {
|
||||
if input.HasBasicDataToUpdate() {
|
||||
if _, err := tx.
|
||||
Model(&model.Qualification{}).
|
||||
Model(&internal.Qualification{}).
|
||||
Context(ctx).
|
||||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
|
@ -89,7 +89,7 @@ func (repo *PGRepository) UpdateMany(
|
|||
if len(qualificationIDs) > 0 {
|
||||
if len(input.DissociateProfession) > 0 {
|
||||
_, err := tx.
|
||||
Model(&model.QualificationToProfession{}).
|
||||
Model(&internal.QualificationToProfession{}).
|
||||
Where(gopgutil.BuildConditionArray("profession_id"), pg.Array(input.DissociateProfession)).
|
||||
Where(gopgutil.BuildConditionArray("qualification_id"), pg.Array(qualificationIDs)).
|
||||
Delete()
|
||||
|
@ -110,8 +110,8 @@ func (repo *PGRepository) UpdateMany(
|
|||
return items, err
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
items := make([]*model.Qualification, 0)
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *internal.QualificationFilter) ([]*internal.Qualification, error) {
|
||||
items := make([]*internal.Qualification, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
|
@ -123,9 +123,9 @@ func (repo *PGRepository) Delete(ctx context.Context, f *model.QualificationFilt
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*internal.Qualification, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Qualification, 0)
|
||||
items := make([]*internal.Qualification, 0)
|
||||
total := 0
|
||||
query := repo.
|
||||
Model(&items).
|
||||
|
@ -148,16 +148,16 @@ func (repo *PGRepository) Fetch(ctx context.Context, cfg *qualification.FetchCon
|
|||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
func (repo *PGRepository) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*internal.Qualification, int, error) {
|
||||
var err error
|
||||
subquery := repo.
|
||||
Model(&model.QualificationToProfession{}).
|
||||
Model(&internal.QualificationToProfession{}).
|
||||
Context(ctx).
|
||||
Where(gopgutil.BuildConditionEquals("qualification_id"), cfg.QualificationID).
|
||||
Column("profession_id")
|
||||
var qualificationIDs []int
|
||||
err = repo.
|
||||
Model(&model.QualificationToProfession{}).
|
||||
Model(&internal.QualificationToProfession{}).
|
||||
Context(ctx).
|
||||
Column("qualification_id").
|
||||
With("prof", subquery).
|
||||
|
@ -169,14 +169,14 @@ func (repo *PGRepository) GetSimilar(ctx context.Context, cfg *qualification.Get
|
|||
}
|
||||
|
||||
if len(qualificationIDs) == 0 {
|
||||
return []*model.Qualification{}, 0, nil
|
||||
return []*internal.Qualification{}, 0, nil
|
||||
}
|
||||
|
||||
return repo.Fetch(ctx, &qualification.FetchConfig{
|
||||
Sort: cfg.Sort,
|
||||
Limit: cfg.Limit,
|
||||
Offset: cfg.Offset,
|
||||
Filter: &model.QualificationFilter{
|
||||
Filter: &internal.QualificationFilter{
|
||||
ID: qualificationIDs,
|
||||
},
|
||||
Count: cfg.Count,
|
||||
|
@ -184,10 +184,10 @@ func (repo *PGRepository) GetSimilar(ctx context.Context, cfg *qualification.Get
|
|||
}
|
||||
|
||||
func (repo *PGRepository) associateQualificationWithProfession(tx *pg.Tx, qualificationIDs, professionIDs []int) error {
|
||||
var toInsert []*model.QualificationToProfession
|
||||
var toInsert []*internal.QualificationToProfession
|
||||
for _, professionID := range professionIDs {
|
||||
for _, qualificationID := range qualificationIDs {
|
||||
toInsert = append(toInsert, &model.QualificationToProfession{
|
||||
toInsert = append(toInsert, &internal.QualificationToProfession{
|
||||
ProfessionID: professionID,
|
||||
QualificationID: qualificationID,
|
||||
})
|
||||
|
|
|
@ -2,16 +2,15 @@ package qualification
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type Usecase interface {
|
||||
Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *model.QualificationInput) (*model.Qualification, error)
|
||||
Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Qualification, int, error)
|
||||
GetByID(ctx context.Context, id int) (*model.Qualification, error)
|
||||
GetBySlug(ctx context.Context, slug string) (*model.Qualification, error)
|
||||
GetSimilar(ctx context.Context, cfg *GetSimilarConfig) ([]*model.Qualification, int, error)
|
||||
Store(ctx context.Context, input *internal.QualificationInput) (*internal.Qualification, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *internal.QualificationInput) (*internal.Qualification, error)
|
||||
Delete(ctx context.Context, f *internal.QualificationFilter) ([]*internal.Qualification, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Qualification, int, error)
|
||||
GetByID(ctx context.Context, id int) (*internal.Qualification, error)
|
||||
GetBySlug(ctx context.Context, slug string) (*internal.Qualification, error)
|
||||
GetSimilar(ctx context.Context, cfg *GetSimilarConfig) ([]*internal.Qualification, int, error)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/qualification"
|
||||
)
|
||||
|
||||
|
@ -27,14 +27,14 @@ func New(cfg *Config) (*Usecase, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *internal.QualificationInput) (*internal.Qualification, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.qualificationRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.QualificationInput) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *internal.QualificationInput) (*internal.Qualification, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Qu
|
|||
return nil, err
|
||||
}
|
||||
items, err := ucase.qualificationRepository.UpdateMany(ctx,
|
||||
&model.QualificationFilter{
|
||||
&internal.QualificationFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
input)
|
||||
|
@ -55,11 +55,11 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Qu
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.QualificationFilter) ([]*model.Qualification, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *internal.QualificationFilter) ([]*internal.Qualification, error) {
|
||||
return ucase.qualificationRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*model.Qualification, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig) ([]*internal.Qualification, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &qualification.FetchConfig{
|
||||
Limit: qualification.FetchDefaultLimit,
|
||||
|
@ -72,11 +72,11 @@ func (ucase *Usecase) Fetch(ctx context.Context, cfg *qualification.FetchConfig)
|
|||
return ucase.qualificationRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*internal.Qualification, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &qualification.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.QualificationFilter{
|
||||
Filter: &internal.QualificationFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
})
|
||||
|
@ -89,11 +89,11 @@ func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Qualification
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Qualification, error) {
|
||||
func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*internal.Qualification, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &qualification.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.QualificationFilter{
|
||||
Filter: &internal.QualificationFilter{
|
||||
Slug: []string{slug},
|
||||
},
|
||||
})
|
||||
|
@ -106,7 +106,7 @@ func (ucase *Usecase) GetBySlug(ctx context.Context, slug string) (*model.Qualif
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*model.Qualification, int, error) {
|
||||
func (ucase *Usecase) GetSimilar(ctx context.Context, cfg *qualification.GetSimilarConfig) ([]*internal.Qualification, int, error) {
|
||||
if cfg == nil || cfg.QualificationID <= 0 {
|
||||
return nil, 0, errors.New(messageQualificationIDIsRequired)
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ type validateOptions struct {
|
|||
allowNilValues bool
|
||||
}
|
||||
|
||||
func validateInput(input *model.QualificationInput, opts validateOptions) error {
|
||||
func validateInput(input *internal.QualificationInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -2,12 +2,11 @@ package question
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type FetchConfig struct {
|
||||
Filter *model.QuestionFilter
|
||||
Filter *internal.QuestionFilter
|
||||
Offset int
|
||||
Limit int
|
||||
Sort []string
|
||||
|
@ -20,9 +19,9 @@ type GenerateTestConfig struct {
|
|||
}
|
||||
|
||||
type Repository interface {
|
||||
Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error)
|
||||
Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Question, int, error)
|
||||
GenerateTest(ctx context.Context, cfg *GenerateTestConfig) ([]*model.Question, error)
|
||||
Store(ctx context.Context, input *internal.QuestionInput) (*internal.Question, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *internal.QuestionInput) (*internal.Question, error)
|
||||
Delete(ctx context.Context, f *internal.QuestionFilter) ([]*internal.Question, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Question, int, error)
|
||||
GenerateTest(ctx context.Context, cfg *GenerateTestConfig) ([]*internal.Question, error)
|
||||
}
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/fstorage"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
)
|
||||
|
@ -42,7 +42,7 @@ func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *internal.QuestionInput) (*internal.Question, error) {
|
||||
item := input.ToQuestion()
|
||||
baseQuery := repo.
|
||||
Model(item).
|
||||
|
@ -70,8 +70,8 @@ func (repo *PGRepository) Store(ctx context.Context, input *model.QuestionInput)
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
item := &model.Question{}
|
||||
func (repo *PGRepository) UpdateOneByID(ctx context.Context, id int, input *internal.QuestionInput) (*internal.Question, error) {
|
||||
item := &internal.Question{}
|
||||
baseQuery := repo.
|
||||
Model(item).
|
||||
Context(ctx).
|
||||
|
@ -103,8 +103,8 @@ func (repo *PGRepository) UpdateOneByID(ctx context.Context, id int, input *mode
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
items := make([]*model.Question, 0)
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *internal.QuestionFilter) ([]*internal.Question, error) {
|
||||
items := make([]*internal.Question, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
|
@ -119,9 +119,9 @@ func (repo *PGRepository) Delete(ctx context.Context, f *model.QuestionFilter) (
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*internal.Question, int, error) {
|
||||
var err error
|
||||
items := make([]*model.Question, 0)
|
||||
items := make([]*internal.Question, 0)
|
||||
total := 0
|
||||
query := repo.
|
||||
Model(&items).
|
||||
|
@ -144,14 +144,14 @@ func (repo *PGRepository) Fetch(ctx context.Context, cfg *question.FetchConfig)
|
|||
return items, total, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
func (repo *PGRepository) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*internal.Question, error) {
|
||||
subquery := repo.
|
||||
Model(&model.Question{}).
|
||||
Model(&internal.Question{}).
|
||||
Column("id").
|
||||
Where(gopgutil.BuildConditionArray("qualification_id"), pg.Array(cfg.Qualifications)).
|
||||
OrderExpr("random()").
|
||||
Limit(cfg.Limit)
|
||||
items := make([]*model.Question, 0)
|
||||
items := make([]*internal.Question, 0)
|
||||
if err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package repository
|
||||
|
||||
import (
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/fstorage/fstorageutil"
|
||||
|
@ -8,14 +9,13 @@ import (
|
|||
"github.com/99designs/gqlgen/graphql"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/fstorage"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
fileStorage fstorage.FileStorage
|
||||
}
|
||||
|
||||
func (repo *repository) saveImages(destination *model.Question, input *model.QuestionInput) {
|
||||
func (repo *repository) saveImages(destination *internal.Question, input *internal.QuestionInput) {
|
||||
images := [...]*graphql.Upload{
|
||||
input.Image,
|
||||
input.AnswerAImage,
|
||||
|
@ -58,7 +58,7 @@ func (repo *repository) deleteImages(images []string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (repo *repository) deleteImagesBasedOnInput(question *model.Question, input *model.QuestionInput) {
|
||||
func (repo *repository) deleteImagesBasedOnInput(question *internal.Question, input *internal.QuestionInput) {
|
||||
images := []string{}
|
||||
|
||||
if input.DeleteImage != nil &&
|
||||
|
@ -104,7 +104,7 @@ func (repo *repository) deleteImagesBasedOnInput(question *model.Question, input
|
|||
repo.deleteImages(images)
|
||||
}
|
||||
|
||||
func (repo *repository) getAllImagesAndDelete(questions []*model.Question) {
|
||||
func (repo *repository) getAllImagesAndDelete(questions []*internal.Question) {
|
||||
images := []string{}
|
||||
|
||||
for _, question := range questions {
|
||||
|
|
|
@ -2,15 +2,14 @@ package question
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type Usecase interface {
|
||||
Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error)
|
||||
Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.Question, int, error)
|
||||
GetByID(ctx context.Context, id int) (*model.Question, error)
|
||||
GenerateTest(ctx context.Context, cfg *GenerateTestConfig) ([]*model.Question, error)
|
||||
Store(ctx context.Context, input *internal.QuestionInput) (*internal.Question, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *internal.QuestionInput) (*internal.Question, error)
|
||||
Delete(ctx context.Context, f *internal.QuestionFilter) ([]*internal.Question, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.Question, int, error)
|
||||
GetByID(ctx context.Context, id int) (*internal.Question, error)
|
||||
GenerateTest(ctx context.Context, cfg *GenerateTestConfig) ([]*internal.Question, error)
|
||||
}
|
||||
|
|
|
@ -3,8 +3,8 @@ package usecase
|
|||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/question"
|
||||
)
|
||||
|
||||
|
@ -35,14 +35,14 @@ func New(cfg *Config) (*Usecase, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *internal.QuestionInput) (*internal.Question, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.questionRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.QuestionInput) (*model.Question, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *internal.QuestionInput) (*internal.Question, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
|
@ -61,11 +61,11 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Qu
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.QuestionFilter) ([]*model.Question, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *internal.QuestionFilter) ([]*internal.Question, error) {
|
||||
return ucase.questionRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*model.Question, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*internal.Question, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &question.FetchConfig{
|
||||
Limit: question.FetchMaxLimit,
|
||||
|
@ -81,11 +81,11 @@ func (ucase *Usecase) Fetch(ctx context.Context, cfg *question.FetchConfig) ([]*
|
|||
return ucase.questionRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Question, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*internal.Question, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &question.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.QuestionFilter{
|
||||
Filter: &internal.QuestionFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
})
|
||||
|
@ -98,7 +98,7 @@ func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.Question, err
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*model.Question, error) {
|
||||
func (ucase *Usecase) GenerateTest(ctx context.Context, cfg *question.GenerateTestConfig) ([]*internal.Question, error) {
|
||||
if cfg == nil {
|
||||
cfg = &question.GenerateTestConfig{
|
||||
Limit: question.TestMaxLimit,
|
||||
|
@ -114,7 +114,7 @@ type validateOptions struct {
|
|||
allowNilValues bool
|
||||
}
|
||||
|
||||
func validateInput(input *model.QuestionInput, opts validateOptions) error {
|
||||
func validateInput(input *internal.QuestionInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"fmt"
|
|
@ -1,4 +1,4 @@
|
|||
package model
|
||||
package internal
|
||||
|
||||
import (
|
||||
"context"
|
|
@ -2,12 +2,11 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type FetchConfig struct {
|
||||
Filter *model.UserFilter
|
||||
Filter *internal.UserFilter
|
||||
Offset int
|
||||
Limit int
|
||||
Sort []string
|
||||
|
@ -15,8 +14,8 @@ type FetchConfig struct {
|
|||
}
|
||||
|
||||
type Repository interface {
|
||||
Store(ctx context.Context, input *model.UserInput) (*model.User, error)
|
||||
UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error)
|
||||
Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.User, int, error)
|
||||
Store(ctx context.Context, input *internal.UserInput) (*internal.User, error)
|
||||
UpdateMany(ctx context.Context, f *internal.UserFilter, input *internal.UserInput) ([]*internal.User, error)
|
||||
Delete(ctx context.Context, f *internal.UserFilter) ([]*internal.User, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.User, int, error)
|
||||
}
|
||||
|
|
|
@ -4,13 +4,13 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/gopgutil/v10"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
"strings"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/util/errorutil"
|
||||
|
||||
"github.com/go-pg/pg/v10"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
)
|
||||
|
||||
|
@ -33,7 +33,7 @@ func NewPGRepository(cfg *PGRepositoryConfig) (*PGRepository, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
func (repo *PGRepository) Store(ctx context.Context, input *internal.UserInput) (*internal.User, error) {
|
||||
item := input.ToUser()
|
||||
if _, err := repo.
|
||||
Model(item).
|
||||
|
@ -45,9 +45,9 @@ func (repo *PGRepository) Store(ctx context.Context, input *model.UserInput) (*m
|
|||
return item, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
func (repo *PGRepository) UpdateMany(ctx context.Context, f *internal.UserFilter, input *internal.UserInput) ([]*internal.User, error) {
|
||||
if _, err := repo.
|
||||
Model(&model.User{}).
|
||||
Model(&internal.User{}).
|
||||
Context(ctx).
|
||||
Apply(input.ApplyUpdate).
|
||||
Apply(f.Where).
|
||||
|
@ -64,8 +64,8 @@ func (repo *PGRepository) UpdateMany(ctx context.Context, f *model.UserFilter, i
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
items := make([]*model.User, 0)
|
||||
func (repo *PGRepository) Delete(ctx context.Context, f *internal.UserFilter) ([]*internal.User, error) {
|
||||
items := make([]*internal.User, 0)
|
||||
if _, err := repo.
|
||||
Model(&items).
|
||||
Context(ctx).
|
||||
|
@ -77,9 +77,9 @@ func (repo *PGRepository) Delete(ctx context.Context, f *model.UserFilter) ([]*m
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
func (repo *PGRepository) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*internal.User, int, error) {
|
||||
var err error
|
||||
items := make([]*model.User, 0)
|
||||
items := make([]*internal.User, 0)
|
||||
total := 0
|
||||
query := repo.
|
||||
Model(&items).
|
||||
|
|
|
@ -2,16 +2,15 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
)
|
||||
|
||||
type Usecase interface {
|
||||
Store(ctx context.Context, input *model.UserInput) (*model.User, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *model.UserInput) (*model.User, error)
|
||||
UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error)
|
||||
Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*model.User, int, error)
|
||||
GetByID(ctx context.Context, id int) (*model.User, error)
|
||||
GetByCredentials(ctx context.Context, email, password string) (*model.User, error)
|
||||
Store(ctx context.Context, input *internal.UserInput) (*internal.User, error)
|
||||
UpdateOneByID(ctx context.Context, id int, input *internal.UserInput) (*internal.User, error)
|
||||
UpdateMany(ctx context.Context, f *internal.UserFilter, input *internal.UserInput) ([]*internal.User, error)
|
||||
Delete(ctx context.Context, f *internal.UserFilter) ([]*internal.User, error)
|
||||
Fetch(ctx context.Context, cfg *FetchConfig) ([]*internal.User, int, error)
|
||||
GetByID(ctx context.Context, id int) (*internal.User, error)
|
||||
GetByCredentials(ctx context.Context, email, password string) (*internal.User, error)
|
||||
}
|
||||
|
|
|
@ -4,8 +4,8 @@ import (
|
|||
"context"
|
||||
"github.com/Kichiyaki/goutil/strutil"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal"
|
||||
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/model"
|
||||
"github.com/zdam-egzamin-zawodowy/backend/internal/user"
|
||||
)
|
||||
|
||||
|
@ -28,20 +28,20 @@ func New(cfg *Config) (*Usecase, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *model.UserInput) (*model.User, error) {
|
||||
func (ucase *Usecase) Store(ctx context.Context, input *internal.UserInput) (*internal.User, error) {
|
||||
if err := validateInput(input.Sanitize(), validateOptions{false}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ucase.userRepository.Store(ctx, input)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.UserInput) (*model.User, error) {
|
||||
func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *internal.UserInput) (*internal.User, error) {
|
||||
if id <= 0 {
|
||||
return nil, errors.New(messageInvalidID)
|
||||
}
|
||||
items, err := ucase.UpdateMany(
|
||||
ctx,
|
||||
&model.UserFilter{
|
||||
&internal.UserFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
input.Sanitize(),
|
||||
|
@ -55,9 +55,9 @@ func (ucase *Usecase) UpdateOneByID(ctx context.Context, id int, input *model.Us
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) UpdateMany(ctx context.Context, f *model.UserFilter, input *model.UserInput) ([]*model.User, error) {
|
||||
func (ucase *Usecase) UpdateMany(ctx context.Context, f *internal.UserFilter, input *internal.UserInput) ([]*internal.User, error) {
|
||||
if f == nil {
|
||||
return []*model.User{}, nil
|
||||
return []*internal.User{}, nil
|
||||
}
|
||||
if err := validateInput(input.Sanitize(), validateOptions{true}); err != nil {
|
||||
return nil, err
|
||||
|
@ -69,11 +69,11 @@ func (ucase *Usecase) UpdateMany(ctx context.Context, f *model.UserFilter, input
|
|||
return items, nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *model.UserFilter) ([]*model.User, error) {
|
||||
func (ucase *Usecase) Delete(ctx context.Context, f *internal.UserFilter) ([]*internal.User, error) {
|
||||
return ucase.userRepository.Delete(ctx, f)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*model.User, int, error) {
|
||||
func (ucase *Usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*internal.User, int, error) {
|
||||
if cfg == nil {
|
||||
cfg = &user.FetchConfig{
|
||||
Limit: user.FetchMaxLimit,
|
||||
|
@ -89,11 +89,11 @@ func (ucase *Usecase) Fetch(ctx context.Context, cfg *user.FetchConfig) ([]*mode
|
|||
return ucase.userRepository.Fetch(ctx, cfg)
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.User, error) {
|
||||
func (ucase *Usecase) GetByID(ctx context.Context, id int) (*internal.User, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.UserFilter{
|
||||
Filter: &internal.UserFilter{
|
||||
ID: []int{id},
|
||||
},
|
||||
})
|
||||
|
@ -106,11 +106,11 @@ func (ucase *Usecase) GetByID(ctx context.Context, id int) (*model.User, error)
|
|||
return items[0], nil
|
||||
}
|
||||
|
||||
func (ucase *Usecase) GetByCredentials(ctx context.Context, email, password string) (*model.User, error) {
|
||||
func (ucase *Usecase) GetByCredentials(ctx context.Context, email, password string) (*internal.User, error) {
|
||||
items, _, err := ucase.Fetch(ctx, &user.FetchConfig{
|
||||
Limit: 1,
|
||||
Count: false,
|
||||
Filter: &model.UserFilter{
|
||||
Filter: &internal.UserFilter{
|
||||
Email: []string{email},
|
||||
},
|
||||
})
|
||||
|
@ -130,7 +130,7 @@ type validateOptions struct {
|
|||
acceptNilValues bool
|
||||
}
|
||||
|
||||
func validateInput(input *model.UserInput, opts validateOptions) error {
|
||||
func validateInput(input *internal.UserInput, opts validateOptions) error {
|
||||
if input.IsEmpty() {
|
||||
return errors.New(messageEmptyPayload)
|
||||
}
|
||||
|
|
Reference in New Issue
Block a user