2fas-server/internal/api/icons/app/queries/icons_collection.go

58 lines
1.4 KiB
Go
Raw Normal View History

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/datatypes"
"gorm.io/gorm"
)
type IconsCollectionPresenter struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Icons datatypes.JSON `json:"icons"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type IconsCollectionQuery struct {
Id string `uri:"collection_id" validate:"omitempty,uuid4"`
}
type IconsCollectionQueryHandler struct {
Database *gorm.DB
Qb *goqu.Database
}
func (h *IconsCollectionQueryHandler) FindOne(query *IconsCollectionQuery) (*IconsCollectionPresenter, error) {
sql, _, _ := h.Qb.From("icons_collections").Where(
goqu.C("id").Eq(query.Id),
goqu.C("deleted_at").IsNull(),
).ToSQL()
presenter := &IconsCollectionPresenter{}
result := h.Database.Raw(sql).First(&presenter)
if result.Error != nil {
return nil, adapters.IconsCollectionCouldNotBeFound{IconsCollectionId: query.Id}
}
return presenter, nil
}
func (h *IconsCollectionQueryHandler) FindAll(query *IconsCollectionQuery) ([]*IconsCollectionPresenter, error) {
var presenter []*IconsCollectionPresenter
ds := h.Qb.From("icons_collections").Where(goqu.And(
goqu.C("deleted_at").IsNull(),
))
sql, _, _ := ds.ToSQL()
h.Database.Raw(sql).Find(&presenter)
return presenter, nil
}