2022-12-31 10:22:38 +01:00
|
|
|
package adapters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-11-29 13:13:38 +01:00
|
|
|
|
2022-12-31 10:22:38 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
2023-11-29 13:13:38 +01:00
|
|
|
|
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/domain"
|
|
|
|
"github.com/twofas/2fas-server/internal/common/db"
|
2022-12-31 10:22:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IconCouldNotBeFound struct {
|
|
|
|
IconId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e IconCouldNotBeFound) Error() string {
|
|
|
|
return fmt.Sprintf("Icon could not be found: %s", e.IconId)
|
|
|
|
}
|
|
|
|
|
|
|
|
type IconMysqlRepository struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIconMysqlRepository(db *gorm.DB) *IconMysqlRepository {
|
|
|
|
return &IconMysqlRepository{db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IconMysqlRepository) Save(Icon *domain.Icon) error {
|
|
|
|
if err := r.db.Create(Icon).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IconMysqlRepository) Update(Icon *domain.Icon) error {
|
|
|
|
if err := r.db.Updates(Icon).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IconMysqlRepository) Delete(Icon *domain.Icon) error {
|
|
|
|
if err := r.db.Delete(Icon).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IconMysqlRepository) FindById(id uuid.UUID) (*domain.Icon, error) {
|
|
|
|
Icon := &domain.Icon{}
|
|
|
|
|
|
|
|
result := r.db.First(&Icon, "id = ?", id.String())
|
|
|
|
|
2023-11-29 13:13:38 +01:00
|
|
|
if err := result.Error; err != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
|
|
return nil, IconCouldNotBeFound{IconId: id.String()}
|
|
|
|
}
|
|
|
|
return nil, db.WrapError(err)
|
2022-12-31 10:22:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return Icon, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *IconMysqlRepository) FindAll() []*domain.Icon {
|
|
|
|
var Icons []*domain.Icon
|
|
|
|
|
|
|
|
r.db.Find(&Icons)
|
|
|
|
|
|
|
|
return Icons
|
|
|
|
}
|