Redmage/api/devices_update.go

35 lines
945 B
Go
Raw Normal View History

package api
import (
"context"
"errors"
"github.com/mattn/go-sqlite3"
"github.com/tigorlazuardi/redmage/models"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
2024-04-30 14:12:33 +07:00
func (api *API) DevicesUpdate(ctx context.Context, slug string, update *models.DeviceSetter) (device *models.Device, err error) {
ctx, span := tracer.Start(ctx, "*API.DevicesUpdate")
defer span.End()
2024-04-30 14:12:33 +07:00
device = &models.Device{Slug: slug}
err = models.Devices.Update(ctx, api.db, update, device)
if err != nil {
var sqliteErr sqlite3.Error
if errors.As(err, &sqliteErr) {
if sqliteErr.Code == sqlite3.ErrConstraint {
return device, errs.Wrapw(err, "a device with the same slug id already exists").Code(409)
}
}
2024-04-30 14:12:33 +07:00
return device, errs.Wrapw(err, "failed to update device", "slug", slug, "values", update)
}
if err := device.Reload(ctx, api.db); err != nil {
2024-04-30 14:12:33 +07:00
return device, errs.Wrapw(err, "failed to reload device", "slug", slug)
}
return device, nil
}