You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.5 KiB
60 lines
1.5 KiB
1 month ago
|
package repository
|
||
|
|
||
|
import (
|
||
|
"gofaster/internal/model"
|
||
|
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type PermissionRepo struct {
|
||
|
BaseRepo
|
||
|
}
|
||
|
|
||
|
func NewPermissionRepo(db *gorm.DB) *PermissionRepo {
|
||
|
return &PermissionRepo{BaseRepo{db: db}}
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) Create(permission *model.Permission) error {
|
||
|
return r.db.Create(permission).Error
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) GetByID(id uint) (*model.Permission, error) {
|
||
|
var permission model.Permission
|
||
|
err := r.db.First(&permission, id).Error
|
||
|
return &permission, err
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) GetByName(name string) (*model.Permission, error) {
|
||
|
var permission model.Permission
|
||
|
err := r.db.Where("name = ?", name).First(&permission).Error
|
||
|
return &permission, err
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) Update(permission *model.Permission) error {
|
||
|
return r.db.Save(permission).Error
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) Delete(id uint) error {
|
||
|
return r.db.Delete(&model.Permission{}, id).Error
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) List(page, pageSize int) ([]model.Permission, int64, error) {
|
||
|
var permissions []model.Permission
|
||
|
var count int64
|
||
|
|
||
|
err := r.db.Model(&model.Permission{}).Count(&count).Error
|
||
|
if err != nil {
|
||
|
return nil, 0, err
|
||
|
}
|
||
|
|
||
|
offset := (page - 1) * pageSize
|
||
|
err = r.db.Offset(offset).Limit(pageSize).Find(&permissions).Error
|
||
|
return permissions, count, err
|
||
|
}
|
||
|
|
||
|
func (r *PermissionRepo) GetByResourceAction(resource, action string) (*model.Permission, error) {
|
||
|
var permission model.Permission
|
||
|
err := r.db.Where("resource = ? AND action = ?", resource, action).First(&permission).Error
|
||
|
return &permission, err
|
||
|
}
|