/
comment-service.go
40 lines (34 loc) · 1.11 KB
/
comment-service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package service
import (
"github.com/abdurraufraihan/golang-blog-api/internal/dto"
"github.com/abdurraufraihan/golang-blog-api/internal/model"
"github.com/abdurraufraihan/golang-blog-api/internal/repository"
"github.com/mashingan/smapping"
)
type CommentService interface {
All(limit string, offset string, postId uint) []model.Comment
Insert(commentDto dto.Comment, postId, userId uint) model.Comment
}
type commentService struct {
commentRepo repository.CommentRepo
}
func NewCommentService(commentRepo repository.CommentRepo) *commentService {
return &commentService{commentRepo: commentRepo}
}
func (service *commentService) All(
limit string, offset string, postId uint,
) []model.Comment {
return service.commentRepo.AllCommentByPostId(limit, offset, postId)
}
func (service *commentService) Insert(
commentDto dto.Comment, postId, userId uint,
) model.Comment {
commentModel := model.Comment{}
err := smapping.FillStruct(&commentModel, smapping.MapFields(&commentDto))
if err != nil {
panic(err)
}
commentModel.PostID = postId
commentModel.UserID = userId
return service.commentRepo.Insert(commentModel)
}