Skip to content

Commit

Permalink
Add devices endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuen committed Dec 26, 2023
1 parent 43d7746 commit 70e2404
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/bwizard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ func main() {

app := application.NewApplication(db)

wizardapi.StartAPIServer(app)
wizardapi.StartAPIServer(app, app.DeviceRepo)
}
10 changes: 5 additions & 5 deletions internal/pkg/wizard/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type Application interface {
}

type Impl struct {
deviceRepo deviceRepo.Repository
deviceInspectionSvc deviceSvc.InspectionService
DeviceRepo deviceRepo.Repository
DeviceInspectionSvc deviceSvc.InspectionService
}

var _ Application = &Impl{}
Expand All @@ -28,14 +28,14 @@ func NewApplication(db *sql.DB) *Impl {
deviceInspectionSvc := deviceSvcImpl.NewInspectionSvc()

return &Impl{
deviceRepo: deviceRepository,
deviceInspectionSvc: deviceInspectionSvc,
DeviceRepo: deviceRepository,
DeviceInspectionSvc: deviceInspectionSvc,
}
}

// SetupDevice setups a new device
func (i *Impl) SetupDevice(ips []deviceValue.IPAddress, kind deviceValue.Kind, name *string) (*device.Device, error) {
inspection, err := i.deviceInspectionSvc.Inspect(ips)
inspection, err := i.DeviceInspectionSvc.Inspect(ips)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/wizard/domain/repo/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (

type Repository interface {
GetDeviceByID(ctx context.Context, id uuid.UUID) (*device.Device, error)
GetDevices(ctx context.Context) ([]*device.Device, error)
SaveDevice(ctx context.Context, device *device.Device) error
}
32 changes: 32 additions & 0 deletions internal/pkg/wizard/infrastructure/mysql/repo/device/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ func (i *Impl) GetDeviceByID(ctx context.Context, id uuid.UUID) (*device.Device,
}
}

// GetDevices returns all registered devices
func (i *Impl) GetDevices(ctx context.Context) ([]*device.Device, error) {
query, _, err := goqu.
Dialect(Dialect).
From(TableName).
Select(&device.Device{}).
ToSQL()

if err != nil {
return nil, err
}

rows, err := i.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}

defer func(rows *sql.Rows) {
_ = rows.Close()
}(rows)

devices := make([]*device.Device, 0)
for rows.Next() {
deviceEntity := new(device.Device)
if scanErr := rows.Scan(deviceEntity); scanErr != nil {
return nil, scanErr
}
}

return devices, nil
}

// SaveDevice inserts or updates a device in the database
func (i *Impl) SaveDevice(ctx context.Context, device *device.Device) error {
insertSQL, _, _ := goqu.
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/wizardapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wizardapi
import (
api "bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/application"
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device"
"flag"
"github.com/labstack/echo/v4"
echoMiddleware "github.com/labstack/echo/v4/middleware"
Expand All @@ -13,11 +14,11 @@ const (
DefaultPort = "8099"
)

func StartAPIServer(app application.Application) {
func StartAPIServer(app application.Application, deviceRepository deviceRepo.Repository) {
port := flag.String("port", DefaultPort, "Default port to start the api and ui on")
flag.Parse()

handler := NewHandler(app)
handler := NewHandler(app, deviceRepository)

e := echo.New()
e.Use(echoMiddleware.Logger())
Expand Down
23 changes: 18 additions & 5 deletions internal/pkg/wizardapi/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,39 @@ package wizardapi
import (
"bwizard/api/openapi/wizard"
"bwizard/internal/pkg/wizard/application"
deviceRepo "bwizard/internal/pkg/wizard/domain/repo/device"
deviceValue "bwizard/internal/pkg/wizard/domain/valueobject/device"
"bwizard/internal/pkg/wizardapi/mappings"
"context"
)

type Handler struct {
app application.Application
app application.Application
deviceRepository deviceRepo.Repository
}

var _ wizard.StrictServerInterface = &Handler{}

func NewHandler(app application.Application) *Handler {
func NewHandler(app application.Application, deviceRepository deviceRepo.Repository) *Handler {
return &Handler{
app: app,
app: app,
deviceRepository: deviceRepository,
}
}

func (h *Handler) FindDevices(ctx context.Context, request wizard.FindDevicesRequestObject) (wizard.FindDevicesResponseObject, error) {
//TODO implement me
panic("implement me")
devices, err := h.deviceRepository.GetDevices(ctx)
if err != nil {
// TODO: Add error handling here
return nil, err
}

mappedDevices := mappings.MapDevices(devices)

return wizard.FindDevices200JSONResponse{
Success: true,
Data: mappedDevices,
}, nil
}

func (h *Handler) RegisterDevice(ctx context.Context, request wizard.RegisterDeviceRequestObject) (wizard.RegisterDeviceResponseObject, error) {
Expand Down
9 changes: 9 additions & 0 deletions internal/pkg/wizardapi/mappings/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ func MapDevice(device *device.Device) wizard.Device {
LastBackup: device.LastBackup,
}
}

func MapDevices(devices []*device.Device) []wizard.Device {
mappedDevices := make([]wizard.Device, 0, len(devices))
for _, deviceEntity := range devices {
mappedDevices = append(mappedDevices, MapDevice(deviceEntity))
}

return mappedDevices
}

0 comments on commit 70e2404

Please sign in to comment.