kontenhumas-be/app/module/article_comments/service/article_comments.service.go

106 lines
3.7 KiB
Go

package service
import (
"github.com/google/uuid"
"github.com/rs/zerolog"
"netidhub-saas-be/app/database/entity"
"netidhub-saas-be/app/module/article_comments/mapper"
"netidhub-saas-be/app/module/article_comments/repository"
"netidhub-saas-be/app/module/article_comments/request"
"netidhub-saas-be/app/module/article_comments/response"
usersRepository "netidhub-saas-be/app/module/users/repository"
"netidhub-saas-be/utils/paginator"
utilSvc "netidhub-saas-be/utils/service"
)
// ArticleCommentsService
type articleCommentsService struct {
Repo repository.ArticleCommentsRepository
UsersRepo usersRepository.UsersRepository
Log zerolog.Logger
}
// ArticleCommentsService define interface of IArticleCommentsService
type ArticleCommentsService interface {
All(clientId *uuid.UUID, req request.ArticleCommentsQueryRequest) (articleComments []*response.ArticleCommentsResponse, paging paginator.Pagination, err error)
Show(clientId *uuid.UUID, id uint) (articleComments *response.ArticleCommentsResponse, err error)
Save(clientId *uuid.UUID, req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error)
Update(clientId *uuid.UUID, id uint, req request.ArticleCommentsUpdateRequest) (err error)
Delete(clientId *uuid.UUID, id uint) error
Approval(clientId *uuid.UUID, id uint, req request.ArticleCommentsApprovalRequest) (err error)
}
// NewArticleCommentsService init ArticleCommentsService
func NewArticleCommentsService(repo repository.ArticleCommentsRepository, log zerolog.Logger, usersRepo usersRepository.UsersRepository) ArticleCommentsService {
return &articleCommentsService{
Repo: repo,
Log: log,
UsersRepo: usersRepo,
}
}
// All implement interface of ArticleCommentsService
func (_i *articleCommentsService) All(clientId *uuid.UUID, req request.ArticleCommentsQueryRequest) (articleCommentss []*response.ArticleCommentsResponse, paging paginator.Pagination, err error) {
results, paging, err := _i.Repo.GetAll(clientId, req)
if err != nil {
return
}
for _, result := range results {
articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(clientId, result, _i.UsersRepo))
}
return
}
func (_i *articleCommentsService) Show(clientId *uuid.UUID, id uint) (articleComments *response.ArticleCommentsResponse, err error) {
result, err := _i.Repo.FindOne(clientId, id)
if err != nil {
return nil, err
}
return mapper.ArticleCommentsResponseMapper(clientId, result, _i.UsersRepo), nil
}
func (_i *articleCommentsService) Save(clientId *uuid.UUID, req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error) {
_i.Log.Info().Interface("data", req).Msg("")
newReq := req.ToEntity()
createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
newReq.CommentFrom = &createdBy.ID
newReq.IsActive = true
return _i.Repo.Create(clientId, newReq)
}
func (_i *articleCommentsService) Update(clientId *uuid.UUID, id uint, req request.ArticleCommentsUpdateRequest) (err error) {
_i.Log.Info().Interface("data", req).Msg("")
return _i.Repo.Update(clientId, id, req.ToEntity())
}
func (_i *articleCommentsService) Delete(clientId *uuid.UUID, id uint) error {
result, err := _i.Repo.FindOne(clientId, id)
if err != nil {
return err
}
result.IsActive = false
return _i.Repo.Update(clientId, id, result)
}
func (_i *articleCommentsService) Approval(clientId *uuid.UUID, id uint, req request.ArticleCommentsApprovalRequest) (err error) {
_i.Log.Info().Interface("data", req).Msg("")
newReq := req.ToEntity()
if newReq.StatusId == 1 || newReq.StatusId == 2 {
newReq.IsPublic = true
} else {
newReq.IsPublic = false
}
return _i.Repo.Update(clientId, id, newReq)
}