ozon-task/internal/storage/storage.go
erius e0aa12b126 Separated graphql schema into multiple files and moved them to graph/schema dir, update the gqlgen.yml accordingly
Removed unnecesary bindings for ID and Int in gqlgen.yml
Minor changes to Makefile docker-clean target
Reduced docker-compose postgres healthcheck interval for faster local db connections
Added pagination to graphql schema, the paginated queries are WIP
Removed unneeded fields from the model structs
Added allowComment check when adding comments
Switched gorm logger to Info mode
App now panics if the specified APP_STORAGE in env doesn't exist
Reworked database methods, still barely working and are WIP
2024-06-26 04:45:04 +03:00

63 lines
1.9 KiB
Go

package storage
import (
"context"
"fmt"
"log"
"os"
"git.obamna.ru/erius/ozon-task/graph/model"
"git.obamna.ru/erius/ozon-task/internal/storage/db"
)
type IDNotFoundError struct {
objName string
id uint
}
func (e *IDNotFoundError) Error() string {
return fmt.Sprintf("%s with id %d doesn't exist", e.objName, e.id)
}
// storage types enum
const (
inMemory = "inmemory"
postgres = "postgres"
)
var storage, storageSpecified = os.LookupEnv("APP_STORAGE")
type Storage interface {
AddPost(input *model.PostInput) (*model.AddResult, error)
// assumes that input.ParentCommentID is not nil
AddReplyToComment(input *model.CommentInput) (*model.AddResult, error)
// assumes that input.ParentPostID is not nil
AddCommentToPost(input *model.CommentInput) (*model.AddResult, error)
// passing query context to analyze requested fields and prevent overfetching
GetPost(id uint, ctx context.Context) (*model.Post, error)
GetComment(id uint, ctx context.Context) (*model.Comment, error)
// returns paginated data in the form of model.*Connection (passing context to prevent overfetching)
GetPosts(first uint, after uint, ctx context.Context) (*model.PostsConnection, error)
GetComments(post *model.Post, first uint, after uint, ctx context.Context) (*model.CommentsConnection, error)
GetReplies(comment *model.Comment, first uint, after uint, ctx context.Context) (*model.CommentsConnection, error)
}
func InitStorage() (Storage, error) {
if !storageSpecified {
log.Println("APP_STORAGE isn't specified, falling back to default in-memory storage", storage)
return InitInMemory(), nil
}
log.Printf("initializing storage of type %s...", storage)
switch storage {
case inMemory:
return InitInMemory(), nil
case postgres:
return db.InitPostgres()
default:
return nil, fmt.Errorf("storage of type %s doesn't exists, change the value of APP_STORAGE env variable", storage)
}
}