2024-03-05 19:15:53 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
2024-04-28 18:39:43 +00:00
|
|
|
"fmt"
|
2024-03-31 15:19:45 +00:00
|
|
|
"github.com/rs/zerolog"
|
2024-03-05 19:15:53 +00:00
|
|
|
"go-humas-be/app/database"
|
|
|
|
|
"go-humas-be/app/database/entity"
|
|
|
|
|
"go-humas-be/app/module/articles/request"
|
|
|
|
|
"go-humas-be/utils/paginator"
|
2024-03-31 15:19:45 +00:00
|
|
|
"strings"
|
2024-03-05 19:15:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type articlesRepository struct {
|
2024-03-31 15:19:45 +00:00
|
|
|
DB *database.Database
|
|
|
|
|
Log zerolog.Logger
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArticlesRepository define interface of IArticlesRepository
|
|
|
|
|
type ArticlesRepository interface {
|
|
|
|
|
GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error)
|
|
|
|
|
FindOne(id uint) (articles *entity.Articles, err error)
|
2024-05-05 09:24:49 +00:00
|
|
|
FindByFilename(thumbnailName string) (articleReturn *entity.Articles, err error)
|
|
|
|
|
Create(articles *entity.Articles) (articleReturn *entity.Articles, err error)
|
2024-03-05 19:15:53 +00:00
|
|
|
Update(id uint, articles *entity.Articles) (err error)
|
|
|
|
|
Delete(id uint) (err error)
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-31 15:19:45 +00:00
|
|
|
func NewArticlesRepository(db *database.Database, log zerolog.Logger) ArticlesRepository {
|
2024-03-05 19:15:53 +00:00
|
|
|
return &articlesRepository{
|
2024-03-31 15:19:45 +00:00
|
|
|
DB: db,
|
|
|
|
|
Log: log,
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// implement interface of IArticlesRepository
|
|
|
|
|
func (_i *articlesRepository) GetAll(req request.ArticlesQueryRequest) (articless []*entity.Articles, paging paginator.Pagination, err error) {
|
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
|
|
query := _i.DB.DB.Model(&entity.Articles{})
|
2024-03-31 15:19:45 +00:00
|
|
|
query = query.Where("is_active = ?", true)
|
|
|
|
|
|
|
|
|
|
if req.Title != nil && *req.Title != "" {
|
|
|
|
|
title := strings.ToLower(*req.Title)
|
|
|
|
|
query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
|
|
|
|
|
}
|
|
|
|
|
if req.Description != nil && *req.Description != "" {
|
|
|
|
|
description := strings.ToLower(*req.Description)
|
|
|
|
|
query = query.Where("LOWER(description) LIKE ?", "%"+strings.ToLower(description)+"%")
|
|
|
|
|
}
|
|
|
|
|
if req.Tags != nil && *req.Tags != "" {
|
|
|
|
|
tags := strings.ToLower(*req.Tags)
|
|
|
|
|
query = query.Where("LOWER(tags) LIKE ?", "%"+strings.ToLower(tags)+"%")
|
|
|
|
|
}
|
2024-05-05 09:24:49 +00:00
|
|
|
if req.CategoryId != nil {
|
|
|
|
|
query = query.Where("category_id = ?", req.CategoryId)
|
|
|
|
|
}
|
2024-03-31 15:19:45 +00:00
|
|
|
if req.TypeId != nil {
|
|
|
|
|
query = query.Where("type_id = ?", req.TypeId)
|
|
|
|
|
}
|
|
|
|
|
if req.IsPublish != nil {
|
|
|
|
|
query = query.Where("is_publish = ?", req.IsPublish)
|
|
|
|
|
}
|
2025-02-06 21:14:25 +00:00
|
|
|
if req.IsDraft != nil {
|
|
|
|
|
query = query.Where("is_draft = ?", req.IsDraft)
|
|
|
|
|
}
|
2024-03-31 15:19:45 +00:00
|
|
|
if req.StatusId != nil {
|
|
|
|
|
query = query.Where("status_id = ?", req.StatusId)
|
|
|
|
|
}
|
2024-04-16 02:08:00 +00:00
|
|
|
if req.CreatedById != nil {
|
|
|
|
|
query = query.Where("created_by_id = ?", req.CreatedById)
|
|
|
|
|
}
|
2024-03-05 19:15:53 +00:00
|
|
|
query.Count(&count)
|
|
|
|
|
|
2024-04-28 18:39:43 +00:00
|
|
|
if req.Pagination.SortBy != "" {
|
|
|
|
|
direction := "ASC"
|
|
|
|
|
if req.Pagination.Sort == "desc" {
|
|
|
|
|
direction = "DESC"
|
|
|
|
|
}
|
|
|
|
|
query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
|
2025-02-13 15:32:59 +00:00
|
|
|
} else {
|
|
|
|
|
direction := "DESC"
|
|
|
|
|
sortBy := "created_at"
|
|
|
|
|
query.Order(fmt.Sprintf("%s %s", sortBy, direction))
|
2024-04-28 18:39:43 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-05 19:15:53 +00:00
|
|
|
req.Pagination.Count = count
|
|
|
|
|
req.Pagination = paginator.Paging(req.Pagination)
|
|
|
|
|
|
|
|
|
|
err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&articless).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
paging = *req.Pagination
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) FindOne(id uint) (articles *entity.Articles, err error) {
|
|
|
|
|
if err := _i.DB.DB.First(&articles, id).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return articles, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-05 09:24:49 +00:00
|
|
|
func (_i *articlesRepository) FindByFilename(thumbnailName string) (articles *entity.Articles, err error) {
|
|
|
|
|
|
|
|
|
|
if err := _i.DB.DB.Where("thumbnail_name = ?", thumbnailName).First(&articles).Error; err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return articles, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Create(articles *entity.Articles) (articleReturn *entity.Articles, err error) {
|
|
|
|
|
result := _i.DB.DB.Create(articles)
|
|
|
|
|
return articles, result.Error
|
2024-03-05 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Update(id uint, articles *entity.Articles) (err error) {
|
|
|
|
|
return _i.DB.DB.Model(&entity.Articles{}).
|
|
|
|
|
Where(&entity.Articles{ID: id}).
|
|
|
|
|
Updates(articles).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (_i *articlesRepository) Delete(id uint) error {
|
|
|
|
|
return _i.DB.DB.Delete(&entity.Articles{}, id).Error
|
2024-03-31 15:19:45 +00:00
|
|
|
}
|