ozon-task/graph/model/data.go
2024-06-25 02:34:10 +03:00

61 lines
1.3 KiB
Go

package model
import (
"time"
"gorm.io/gorm"
)
const CommentLengthLimit = 2000
type Comment struct {
// db fields
ID uint `json:"id" gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
// db foreign key to reference parent post
PostID uint
Post *Post `json:"post"`
Author string `json:"author"`
Contents string `json:"contents"`
// db key to reference parent comment
ReplyToID *uint
ReplyTo *Comment `json:"reply_to,omitempty"`
Replies []*Comment `json:"replies" gorm:"many2many:comment_replies"`
}
type Post struct {
// db fields
ID uint `json:"id" gorm:"primarykey"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
Title string `json:"title"`
Author string `json:"author"`
Contents string `json:"contents"`
Comments []*Comment `json:"comments" gorm:"foreignKey:PostID"`
AllowComments bool `json:"allowComments"`
}
func PostFromInput(input *PostInput) *Post {
return &Post{
Author: input.Author,
Title: input.Title,
Contents: input.Contents,
AllowComments: input.AllowComments,
}
}
func CommentFromInput(input *CommentInput) *Comment {
return &Comment{
Author: input.Author,
Contents: input.Contents,
Replies: make([]*Comment, 0),
}
}