2022-12-31 10:22:38 +01:00
|
|
|
package queries
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/doug-martin/goqu/v9"
|
2023-01-30 19:59:42 +01:00
|
|
|
"github.com/twofas/2fas-server/internal/api/icons/adapters"
|
2022-12-31 10:22:38 +01:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IconPresenter struct {
|
|
|
|
Id string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Url string `json:"url"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
UpdatedAt string `json:"updated_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type IconQuery struct {
|
|
|
|
Id string `uri:"icon_id" validate:"omitempty,uuid4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type IconQueryHandler struct {
|
|
|
|
Database *gorm.DB
|
|
|
|
Qb *goqu.Database
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *IconQueryHandler) FindOne(query *IconQuery) (*IconPresenter, error) {
|
|
|
|
sql, _, _ := h.Qb.From("icons").Where(
|
|
|
|
goqu.C("id").Eq(query.Id),
|
|
|
|
goqu.C("deleted_at").IsNull(),
|
|
|
|
).ToSQL()
|
|
|
|
|
|
|
|
presenter := &IconPresenter{}
|
|
|
|
|
|
|
|
result := h.Database.Raw(sql).First(&presenter)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
|
|
|
return nil, adapters.IconCouldNotBeFound{IconId: query.Id}
|
|
|
|
}
|
|
|
|
|
|
|
|
return presenter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *IconQueryHandler) FindAll(query *IconQuery) ([]*IconPresenter, error) {
|
|
|
|
var presenter []*IconPresenter
|
|
|
|
|
|
|
|
ds := h.Qb.From("icons").Where(goqu.And(
|
|
|
|
goqu.C("deleted_at").IsNull(),
|
|
|
|
))
|
|
|
|
|
|
|
|
sql, _, _ := ds.ToSQL()
|
|
|
|
|
|
|
|
h.Database.Raw(sql).Find(&presenter)
|
|
|
|
|
|
|
|
return presenter, nil
|
|
|
|
}
|