mirror of
https://github.com/twofas/2fas-server.git
synced 2024-12-12 20:20:19 +01:00
70 lines
1.3 KiB
Go
70 lines
1.3 KiB
Go
|
package adapters
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"github.com/2fas/api/internal/api/icons/domain"
|
||
|
"github.com/google/uuid"
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
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())
|
||
|
|
||
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||
|
return nil, IconCouldNotBeFound{IconId: id.String()}
|
||
|
}
|
||
|
|
||
|
return Icon, nil
|
||
|
}
|
||
|
|
||
|
func (r *IconMysqlRepository) FindAll() []*domain.Icon {
|
||
|
var Icons []*domain.Icon
|
||
|
|
||
|
r.db.Find(&Icons)
|
||
|
|
||
|
return Icons
|
||
|
}
|