160 lines
4.7 KiB
Go
160 lines
4.7 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/rs/zerolog"
|
|
"go-humas-be/app/database"
|
|
"go-humas-be/app/database/entity"
|
|
"go-humas-be/app/module/ppid_datas/request"
|
|
"go-humas-be/utils/paginator"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type ppidDatasRepository struct {
|
|
DB *database.Database
|
|
Log zerolog.Logger
|
|
}
|
|
|
|
// PpidDatasRepository define interface of IPpidDatasRepository
|
|
type PpidDatasRepository interface {
|
|
GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error)
|
|
FindOne(id uint) (ppidDatas *entity.PpidDatas, err error)
|
|
FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error)
|
|
FindOneLastPosition() (ppidDatas *entity.PpidDatas, err error)
|
|
Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error)
|
|
Update(id uint, ppidDatas *entity.PpidDatas) (err error)
|
|
UpdateAll(ppidDatas []*entity.PpidDatas) (err error)
|
|
Delete(id uint) (err error)
|
|
}
|
|
|
|
func NewPpidDatasRepository(db *database.Database, logger zerolog.Logger) PpidDatasRepository {
|
|
return &ppidDatasRepository{
|
|
DB: db,
|
|
Log: logger,
|
|
}
|
|
}
|
|
|
|
// implement interface of IPpidDatasRepository
|
|
func (_i *ppidDatasRepository) GetAll(req request.PpidDatasQueryRequest) (ppidDatass []*entity.PpidDatas, paging paginator.Pagination, err error) {
|
|
var count int64
|
|
|
|
query := _i.DB.DB.Model(&entity.PpidDatas{})
|
|
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.CategoryId != nil {
|
|
query = query.Where("category_id = ?", req.CategoryId)
|
|
}
|
|
if req.Group != nil && *req.Group != "" {
|
|
title := strings.ToLower(*req.Title)
|
|
query = query.Where("LOWER(title) LIKE ?", "%"+strings.ToLower(title)+"%")
|
|
}
|
|
if req.IsPublish != nil {
|
|
query = query.Where("is_publish = ?", req.IsPublish)
|
|
}
|
|
if req.StatusId != nil {
|
|
query = query.Where("status_id = ?", req.StatusId)
|
|
}
|
|
if req.UserId != nil {
|
|
userRoleIdStr := strconv.Itoa(int(*req.UserRoleId))
|
|
userLevelIdStr := strconv.Itoa(int(*req.UserLevelId))
|
|
query = query.
|
|
Where("created_by_id = ?", req.UserId).
|
|
Or("need_approval_from_user_role LIKE ?", ":"+userRoleIdStr+":").
|
|
Or("need_approval_from_user_level LIKE ?", userLevelIdStr).
|
|
Or("back_approval_to_user_role = ?", userRoleIdStr).
|
|
Or("back_approval_to_user_level = ?", userLevelIdStr)
|
|
}
|
|
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)
|
|
|
|
if req.Pagination.Limit == -1 {
|
|
query.Limit(req.Pagination.Limit)
|
|
}
|
|
|
|
err = query.Offset(req.Pagination.Offset).Find(&ppidDatass).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
paging = *req.Pagination
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) FindOne(id uint) (ppidDatas *entity.PpidDatas, err error) {
|
|
if err := _i.DB.DB.First(&ppidDatas, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ppidDatas, nil
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) FindOneUsingSlug(slug string) (ppidDatas *entity.PpidDatas, err error) {
|
|
if err := _i.DB.DB.Where("slug = ?", slug).First(&ppidDatas).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ppidDatas, nil
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) FindOneLastPosition() (ppidDatas *entity.PpidDatas, err error) {
|
|
if err := _i.DB.DB.Where("position IS NOT NULL").Last(&ppidDatas).
|
|
Order(fmt.Sprintf("%s %s", "position", "DESC")).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ppidDatas, nil
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) Create(ppidDatas *entity.PpidDatas) (ppidDataReturn *entity.PpidDatas, err error) {
|
|
result := _i.DB.DB.Create(ppidDatas)
|
|
|
|
_i.Log.Info().Str("timestamp", time.Now().
|
|
Format(time.RFC3339)).Str("Service:Create", "PpidData:Create").
|
|
Interface("result", ppidDatas).Msg("")
|
|
|
|
return ppidDatas, result.Error
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) Update(id uint, ppidDatas *entity.PpidDatas) (err error) {
|
|
return _i.DB.DB.Model(&entity.PpidDatas{}).
|
|
Where(&entity.PpidDatas{ID: id}).
|
|
Updates(ppidDatas).Error
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) UpdateAll(ppidDatas []*entity.PpidDatas) (err error) {
|
|
for _, req := range ppidDatas {
|
|
err := _i.DB.DB.Model(&entity.PpidDatas{}).
|
|
Where(&entity.PpidDatas{ID: req.ID}).
|
|
Updates(req).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (_i *ppidDatasRepository) Delete(id uint) error {
|
|
return _i.DB.DB.Delete(&entity.PpidDatas{}, id).Error
|
|
}
|