erius
e0aa12b126
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
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
"git.obamna.ru/erius/ozon-task/graph/model"
|
|
"github.com/99designs/gqlgen/graphql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Database struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (s *Database) AddPost(input *model.PostInput) (*model.AddResult, error) {
|
|
post := model.PostFromInput(input)
|
|
err := s.db.Create(post).Error
|
|
return &model.AddResult{ItemID: &post.ID}, err
|
|
}
|
|
|
|
func (s *Database) AddReplyToComment(input *model.CommentInput) (*model.AddResult, error) {
|
|
comment := model.CommentFromInput(input)
|
|
// multiple operations performed in one transaction
|
|
err := s.db.Transaction(func(tx *gorm.DB) error {
|
|
// insert comment
|
|
err := s.db.Create(comment).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// add new reply to parent
|
|
err = s.db.Table("comment_replies").Create(map[string]interface{}{
|
|
"comment_id": *input.ParentCommentID,
|
|
"reply_id": comment.ID,
|
|
}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
return &model.AddResult{ItemID: &comment.ID}, err
|
|
}
|
|
|
|
func (s *Database) AddCommentToPost(input *model.CommentInput) (*model.AddResult, error) {
|
|
comment := model.CommentFromInput(input)
|
|
var allowComments bool
|
|
err := s.db.Table("posts").Select("allow_comments").Where("id = ?", *input.ParentPostID).Scan(&allowComments).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !allowComments {
|
|
return nil, fmt.Errorf("author disabled comments for this post")
|
|
}
|
|
err = s.db.Create(comment).Error
|
|
return &model.AddResult{ItemID: &comment.ID}, err
|
|
}
|
|
|
|
func (s *Database) GetPost(id uint, ctx context.Context) (*model.Post, error) {
|
|
log.Println(graphql.CollectAllFields(ctx))
|
|
log.Println(graphql.CollectFieldsCtx(ctx, nil))
|
|
var post model.Post
|
|
err := s.db.Find(&post, id).Error
|
|
return &post, err
|
|
}
|
|
|
|
func (s *Database) GetComment(id uint, ctx context.Context) (*model.Comment, error) {
|
|
var comment model.Comment
|
|
err := s.db.Find(&comment, id).Error
|
|
return &comment, err
|
|
}
|
|
|
|
func (s *Database) GetPosts(first uint, after uint, ctx context.Context) (*model.PostsConnection, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *Database) GetComments(post *model.Post, first uint, after uint, ctx context.Context) (*model.CommentsConnection, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (s *Database) GetReplies(comment *model.Comment, first uint, after uint, ctx context.Context) (*model.CommentsConnection, error) {
|
|
return nil, nil
|
|
}
|