109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"narasi-ahli-be/app/database"
|
|
"narasi-ahli-be/app/database/entity"
|
|
"narasi-ahli-be/app/module/article_comments/request"
|
|
"narasi-ahli-be/utils/paginator"
|
|
utilSvc "narasi-ahli-be/utils/service"
|
|
"strings"
|
|
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
type articleCommentsRepository struct {
|
|
DB *database.Database
|
|
Log zerolog.Logger
|
|
}
|
|
|
|
// ArticleCommentsRepository define interface of IArticleCommentsRepository
|
|
type ArticleCommentsRepository interface {
|
|
GetAll(req request.ArticleCommentsQueryRequest) (articleCommentss []*entity.ArticleComments, paging paginator.Pagination, err error)
|
|
FindOne(id uint) (articleComments *entity.ArticleComments, err error)
|
|
Create(articleComments *entity.ArticleComments) (articleCommentsReturn *entity.ArticleComments, err error)
|
|
Update(id uint, articleComments *entity.ArticleComments) (err error)
|
|
Delete(id uint) (err error)
|
|
}
|
|
|
|
func NewArticleCommentsRepository(db *database.Database, logger zerolog.Logger) ArticleCommentsRepository {
|
|
return &articleCommentsRepository{
|
|
DB: db,
|
|
Log: logger,
|
|
}
|
|
}
|
|
|
|
// implement interface of IArticleCommentsRepository
|
|
func (_i *articleCommentsRepository) GetAll(req request.ArticleCommentsQueryRequest) (articleCommentss []*entity.ArticleComments, paging paginator.Pagination, err error) {
|
|
var count int64
|
|
|
|
query := _i.DB.DB.Model(&entity.ArticleComments{})
|
|
query = query.Where("is_active = ?", true)
|
|
|
|
if req.Message != nil && *req.Message != "" {
|
|
message := strings.ToLower(*req.Message)
|
|
query = query.Where("LOWER(message) LIKE ?", "%"+strings.ToLower(message)+"%")
|
|
}
|
|
if req.ArticleId != nil {
|
|
query = query.Where("article_id = ?", req.ArticleId)
|
|
}
|
|
if req.CommentFrom != nil {
|
|
query = query.Where("comment_from = ?", req.CommentFrom)
|
|
}
|
|
if req.ParentId != nil {
|
|
query = query.Where("parent_id = ?", req.ParentId)
|
|
}
|
|
if req.IsPublic != nil {
|
|
query = query.Where("is_public = ?", req.IsPublic)
|
|
}
|
|
query.Count(&count)
|
|
|
|
if req.Pagination.SortBy != "" {
|
|
direction := "ASC"
|
|
if req.Pagination.Sort == "desc" {
|
|
direction = "DESC"
|
|
}
|
|
query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
|
|
}
|
|
|
|
req.Pagination.Count = count
|
|
req.Pagination = paginator.Paging(req.Pagination)
|
|
|
|
err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&articleCommentss).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
paging = *req.Pagination
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *articleCommentsRepository) FindOne(id uint) (articleComments *entity.ArticleComments, err error) {
|
|
query := _i.DB.DB
|
|
if err := query.First(&articleComments, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return articleComments, nil
|
|
}
|
|
|
|
func (_i *articleCommentsRepository) Create(articleComments *entity.ArticleComments) (articleCommentsReturn *entity.ArticleComments, err error) {
|
|
result := _i.DB.DB.Create(articleComments)
|
|
return articleComments, result.Error
|
|
}
|
|
|
|
func (_i *articleCommentsRepository) Update(id uint, articleComments *entity.ArticleComments) (err error) {
|
|
articleCommentsMap, err := utilSvc.StructToMap(articleComments)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return _i.DB.DB.Model(&entity.ArticleComments{}).
|
|
Where(&entity.ArticleComments{ID: id}).
|
|
Updates(articleCommentsMap).Error
|
|
}
|
|
|
|
func (_i *articleCommentsRepository) Delete(id uint) error {
|
|
return _i.DB.DB.Delete(&entity.ArticleComments{}, id).Error
|
|
}
|