2024-04-24 21:57:13 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/tigorlazuardi/redmage/models"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (api *API) DevicesUpdate(ctx context.Context, id int, update *models.DeviceSetter) (err error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "*API.DevicesUpdate")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
err = models.Devices.Update(ctx, api.exec, update, &models.Device{ID: int32(id)})
|
|
|
|
if err != nil {
|
|
|
|
var sqliteErr sqlite3.Error
|
|
|
|
if errors.As(err, &sqliteErr) {
|
2024-04-24 22:00:15 +07:00
|
|
|
if sqliteErr.Code == sqlite3.ErrConstraint {
|
2024-04-24 21:57:13 +07:00
|
|
|
return errs.Wrapw(err, "a device with the same slug id already exists").Code(409)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errs.Wrapw(err, "failed to update device", "id", id, "values", update)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|