2024-06-24 23:34:10 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2024-06-26 01:45:04 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2024-06-24 23:34:10 +00:00
|
|
|
"git.obamna.ru/erius/ozon-task/graph/model"
|
2024-06-26 01:45:04 +00:00
|
|
|
"github.com/99designs/gqlgen/graphql"
|
2024-06-24 23:34:10 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-06-26 01:45:04 +00:00
|
|
|
func (s *Database) AddReplyToComment(input *model.CommentInput) (*model.AddResult, error) {
|
2024-06-24 23:34:10 +00:00
|
|
|
comment := model.CommentFromInput(input)
|
2024-06-26 01:45:04 +00:00
|
|
|
// multiple operations performed in one transaction
|
|
|
|
err := s.db.Transaction(func(tx *gorm.DB) error {
|
|
|
|
// insert comment
|
|
|
|
err := s.db.Create(comment).Error
|
2024-06-24 23:34:10 +00:00
|
|
|
if err != nil {
|
2024-06-26 01:45:04 +00:00
|
|
|
return err
|
2024-06-24 23:34:10 +00:00
|
|
|
}
|
2024-06-26 01:45:04 +00:00
|
|
|
// add new reply to parent
|
|
|
|
err = s.db.Table("comment_replies").Create(map[string]interface{}{
|
|
|
|
"comment_id": *input.ParentCommentID,
|
|
|
|
"reply_id": comment.ID,
|
|
|
|
}).Error
|
2024-06-24 23:34:10 +00:00
|
|
|
if err != nil {
|
2024-06-26 01:45:04 +00:00
|
|
|
return err
|
2024-06-24 23:34:10 +00:00
|
|
|
}
|
2024-06-26 01:45:04 +00:00
|
|
|
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
|
2024-06-24 23:34:10 +00:00
|
|
|
}
|
2024-06-26 01:45:04 +00:00
|
|
|
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
|
2024-06-24 23:34:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 01:45:04 +00:00
|
|
|
func (s *Database) GetReplies(comment *model.Comment, first uint, after uint, ctx context.Context) (*model.CommentsConnection, error) {
|
|
|
|
return nil, nil
|
2024-06-24 23:34:10 +00:00
|
|
|
}
|