2fas-server/internal/api/mobile/app/queries/mobile_device.go
2022-12-31 10:22:38 +01:00

45 lines
905 B
Go

package query
import (
"github.com/doug-martin/goqu/v9"
"github.com/pkg/errors"
"gorm.io/gorm"
)
var (
MobileDeviceNotFound = errors.New("Mobile device can not be found")
)
type MobileDevicePresenter struct {
Id string `json:"id"`
Name string `json:"name"`
Platform string `json:"platform"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type MobileDeviceQuery struct {
Id string `uri:"id"`
}
type MobileDeviceQueryHandler struct {
Database *gorm.DB
Qb *goqu.Database
}
func (h *MobileDeviceQueryHandler) Handle(query *MobileDeviceQuery) (*MobileDevicePresenter, error) {
sql, _, _ := h.Qb.From("mobile_devices").Where(goqu.Ex{
"id": query.Id,
}).ToSQL()
presenter := &MobileDevicePresenter{}
result := h.Database.Raw(sql).Scan(&presenter)
if result.Error != nil {
return nil, MobileDeviceNotFound
}
return presenter, nil
}