141 lines
4.1 KiB
Go
141 lines
4.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"fmt"
|
|
"netidhub-saas-be/app/database"
|
|
"netidhub-saas-be/app/database/entity"
|
|
"netidhub-saas-be/app/module/menu_actions/request"
|
|
"netidhub-saas-be/utils/paginator"
|
|
)
|
|
|
|
type menuActionsRepository struct {
|
|
DB *database.Database
|
|
}
|
|
|
|
// MenuActionsRepository define interface of IMenuActionsRepository
|
|
type MenuActionsRepository interface {
|
|
GetAll(req request.MenuActionsQueryRequest) (menuActions []*entity.MenuActions, paging paginator.Pagination, err error)
|
|
GetByMenuId(menuId uint) (menuActions []*entity.MenuActions, err error)
|
|
GetByActionCode(menuId uint, actionCode string) (menuAction *entity.MenuActions, err error)
|
|
FindOne(id uint) (menuAction *entity.MenuActions, err error)
|
|
Create(menuAction *entity.MenuActions) (err error)
|
|
CreateBatch(menuActions []*entity.MenuActions) (err error)
|
|
Update(id uint, menuAction *entity.MenuActions) (err error)
|
|
Delete(id uint) (err error)
|
|
DeleteByMenuId(menuId uint) (err error)
|
|
}
|
|
|
|
func NewMenuActionsRepository(db *database.Database) MenuActionsRepository {
|
|
return &menuActionsRepository{
|
|
DB: db,
|
|
}
|
|
}
|
|
|
|
// implement interface of IMenuActionsRepository
|
|
func (_i *menuActionsRepository) GetAll(req request.MenuActionsQueryRequest) (menuActions []*entity.MenuActions, paging paginator.Pagination, err error) {
|
|
var count int64
|
|
|
|
query := _i.DB.DB.Model(&entity.MenuActions{})
|
|
query = query.Where("is_active = ?", true)
|
|
|
|
if req.MenuId != nil {
|
|
query = query.Where("menu_id = ?", req.MenuId)
|
|
}
|
|
if req.ActionCode != nil && *req.ActionCode != "" {
|
|
query = query.Where("action_code = ?", req.ActionCode)
|
|
}
|
|
if req.ClientId != nil {
|
|
query = query.Where("client_id = ?", req.ClientId)
|
|
}
|
|
|
|
// Preload relations
|
|
query = query.Preload("Menu")
|
|
|
|
query.Count(&count)
|
|
|
|
// Apply sorting
|
|
if req.Pagination.SortBy != "" {
|
|
direction := "ASC"
|
|
if req.Pagination.Sort == "desc" {
|
|
direction = "DESC"
|
|
}
|
|
query.Order(fmt.Sprintf("%s %s", req.Pagination.SortBy, direction))
|
|
} else {
|
|
query.Order("position ASC")
|
|
}
|
|
|
|
// Apply pagination (manual calculation like articles)
|
|
page := req.Pagination.Page
|
|
limit := req.Pagination.Limit
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if limit <= 0 {
|
|
limit = 10
|
|
}
|
|
|
|
offset := (page - 1) * limit
|
|
err = query.Offset(offset).Limit(limit).Find(&menuActions).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Create pagination response
|
|
paging = paginator.Pagination{
|
|
Page: page,
|
|
Limit: limit,
|
|
Count: count,
|
|
TotalPage: int((count + int64(limit) - 1) / int64(limit)),
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (_i *menuActionsRepository) GetByMenuId(menuId uint) (menuActions []*entity.MenuActions, err error) {
|
|
query := _i.DB.DB.Model(&entity.MenuActions{})
|
|
query = query.Where("menu_id = ? AND is_active = ?", menuId, true)
|
|
query = query.Preload("Menu")
|
|
query = query.Order("position ASC")
|
|
err = query.Find(&menuActions).Error
|
|
return
|
|
}
|
|
|
|
func (_i *menuActionsRepository) GetByActionCode(menuId uint, actionCode string) (menuAction *entity.MenuActions, err error) {
|
|
query := _i.DB.DB.Model(&entity.MenuActions{})
|
|
query = query.Where("menu_id = ? AND action_code = ? AND is_active = ?", menuId, actionCode, true)
|
|
query = query.Preload("Menu")
|
|
err = query.First(&menuAction).Error
|
|
return
|
|
}
|
|
|
|
func (_i *menuActionsRepository) FindOne(id uint) (menuAction *entity.MenuActions, err error) {
|
|
query := _i.DB.DB.Preload("Menu")
|
|
if err := query.First(&menuAction, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return menuAction, nil
|
|
}
|
|
|
|
func (_i *menuActionsRepository) Create(menuAction *entity.MenuActions) (err error) {
|
|
return _i.DB.DB.Create(menuAction).Error
|
|
}
|
|
|
|
func (_i *menuActionsRepository) CreateBatch(menuActions []*entity.MenuActions) (err error) {
|
|
return _i.DB.DB.Create(&menuActions).Error
|
|
}
|
|
|
|
func (_i *menuActionsRepository) Update(id uint, menuAction *entity.MenuActions) (err error) {
|
|
return _i.DB.DB.Model(&entity.MenuActions{}).
|
|
Where(&entity.MenuActions{ID: id}).
|
|
Updates(menuAction).Error
|
|
}
|
|
|
|
func (_i *menuActionsRepository) Delete(id uint) (err error) {
|
|
return _i.DB.DB.Delete(&entity.MenuActions{}, id).Error
|
|
}
|
|
|
|
func (_i *menuActionsRepository) DeleteByMenuId(menuId uint) (err error) {
|
|
return _i.DB.DB.Where("menu_id = ?", menuId).Delete(&entity.MenuActions{}).Error
|
|
}
|
|
|