42 lines
923 B
Go
42 lines
923 B
Go
|
package storage
|
||
|
|
||
|
import (
|
||
|
"git.obamna.ru/erius/ozon-task/graph/model"
|
||
|
)
|
||
|
|
||
|
type InMemory struct {
|
||
|
postId uint
|
||
|
commentId uint
|
||
|
posts []*model.Post
|
||
|
comments []*model.Comment
|
||
|
}
|
||
|
|
||
|
func InitInMemory() *InMemory {
|
||
|
return &InMemory{
|
||
|
postId: 0,
|
||
|
commentId: 0,
|
||
|
posts: make([]*model.Post, 0),
|
||
|
comments: make([]*model.Comment, 0),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *InMemory) AddPost(input *model.PostInput) (*model.AddResult, error) {
|
||
|
post := model.PostFromInput(input)
|
||
|
post.ID = s.postId
|
||
|
s.posts = append(s.posts, post)
|
||
|
s.postId++
|
||
|
return &model.AddResult{ItemID: &post.ID}, nil
|
||
|
}
|
||
|
|
||
|
func (s *InMemory) AddComment(input *model.CommentInput) (*model.AddResult, error) {
|
||
|
comment := model.CommentFromInput(input)
|
||
|
comment.ID = s.commentId
|
||
|
s.comments = append(s.comments, comment)
|
||
|
s.commentId++
|
||
|
return &model.AddResult{ItemID: &comment.ID}, nil
|
||
|
}
|
||
|
|
||
|
func (s InMemory) GetPosts() ([]*model.Post, error) {
|
||
|
return s.posts, nil
|
||
|
}
|