75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"eslogad-be/internal/entities"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type RepositoryAttachmentRepositoryImpl struct {
|
|
b *gorm.DB
|
|
}
|
|
|
|
func NewRepositoryAttachmentRepositoryImpl(db *gorm.DB) *RepositoryAttachmentRepositoryImpl {
|
|
return &RepositoryAttachmentRepositoryImpl{
|
|
b: db,
|
|
}
|
|
}
|
|
|
|
func (r *RepositoryAttachmentRepositoryImpl) Create(ctx context.Context, user *entities.RepositoryAttachment) error {
|
|
return r.b.WithContext(ctx).Create(user).Error
|
|
}
|
|
|
|
func (r *RepositoryAttachmentRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entities.RepositoryAttachment, error) {
|
|
var attachment entities.RepositoryAttachment
|
|
err := r.b.WithContext(ctx).
|
|
First(&attachment, "id = ?", id).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &attachment, nil
|
|
}
|
|
|
|
func (r *RepositoryAttachmentRepositoryImpl) Update(ctx context.Context, user *entities.RepositoryAttachment) error {
|
|
return r.b.WithContext(ctx).Save(user).Error
|
|
}
|
|
|
|
func (r *RepositoryAttachmentRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error {
|
|
return r.b.WithContext(ctx).Delete(&entities.RepositoryAttachment{}, "id = ?", id).Error
|
|
}
|
|
|
|
func (r *RepositoryAttachmentRepositoryImpl) List(ctx context.Context, search *string, limit, offset int) ([]*entities.RepositoryAttachment, int64, error) {
|
|
var attachments []*entities.RepositoryAttachment
|
|
var total int64
|
|
|
|
baseQuery := r.b.WithContext(ctx).Model(&entities.RepositoryAttachment{})
|
|
|
|
if search != nil && *search != "" {
|
|
like := "%" + *search + "%"
|
|
baseQuery = baseQuery.Where("name ILIKE ? OR email ILIKE ?", like, like)
|
|
}
|
|
|
|
countQuery := baseQuery
|
|
if err := countQuery.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
dataQuery := r.b.WithContext(ctx).Model(&entities.RepositoryAttachment{})
|
|
|
|
if search != nil && *search != "" {
|
|
like := "%" + *search + "%"
|
|
dataQuery = dataQuery.Where("name ILIKE ? OR category ILIKE ?", like, like)
|
|
}
|
|
|
|
if err := dataQuery.
|
|
Limit(limit).
|
|
Offset(offset).
|
|
Find(&attachments).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return attachments, total, nil
|
|
}
|