user devices
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"apskel-pos-be/internal/entities"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type UserDeviceRepositoryImpl struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserDeviceRepositoryImpl(db *gorm.DB) *UserDeviceRepositoryImpl {
|
||||
return &UserDeviceRepositoryImpl{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) Create(ctx context.Context, device *entities.UserDevice) error {
|
||||
return r.db.WithContext(ctx).Create(device).Error
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.UserDevice, error) {
|
||||
var device entities.UserDevice
|
||||
err := r.db.WithContext(ctx).First(&device, "id = ?", id).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) GetByDeviceID(ctx context.Context, deviceID string, userID uuid.UUID) (*entities.UserDevice, error) {
|
||||
var device entities.UserDevice
|
||||
err := r.db.WithContext(ctx).Where("device_id = ? AND user_id = ?", deviceID, userID).First(&device).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &device, nil
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) GetByUserID(ctx context.Context, userID uuid.UUID) ([]*entities.UserDevice, error) {
|
||||
var devices []*entities.UserDevice
|
||||
err := r.db.WithContext(ctx).Where("user_id = ?", userID).Order("created_at DESC").Find(&devices).Error
|
||||
return devices, err
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) Update(ctx context.Context, device *entities.UserDevice) error {
|
||||
return r.db.WithContext(ctx).Save(device).Error
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.UserDevice{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) DeleteByUserID(ctx context.Context, userID uuid.UUID) error {
|
||||
return r.db.WithContext(ctx).Delete(&entities.UserDevice{}, "user_id = ?", userID).Error
|
||||
}
|
||||
|
||||
func (r *UserDeviceRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.UserDevice, int64, error) {
|
||||
var devices []*entities.UserDevice
|
||||
var total int64
|
||||
|
||||
query := r.db.WithContext(ctx).Model(&entities.UserDevice{})
|
||||
|
||||
for key, value := range filters {
|
||||
switch key {
|
||||
case "user_id":
|
||||
query = query.Where("user_id = ?", value)
|
||||
case "platform":
|
||||
if platform, ok := value.(string); ok && platform != "" {
|
||||
query = query.Where("platform = ?", platform)
|
||||
}
|
||||
case "search":
|
||||
if searchStr, ok := value.(string); ok && searchStr != "" {
|
||||
searchPattern := "%" + strings.ToLower(searchStr) + "%"
|
||||
query = query.Where("LOWER(device_name) LIKE ? OR LOWER(device_id) LIKE ?",
|
||||
searchPattern, searchPattern)
|
||||
}
|
||||
default:
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := query.Order("created_at DESC").Limit(limit).Offset(offset).Find(&devices).Error
|
||||
return devices, total, err
|
||||
}
|
||||
Reference in New Issue
Block a user