2024-08-05 23:06:32 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"connectrpc.com/connect"
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
"github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/gen/models"
|
|
|
|
"github.com/tigorlazuardi/bluemage/go/pkg/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (api *API) DevicesCreate(ctx context.Context, params *models.Device) (device *models.Device, err error) {
|
|
|
|
now := time.Now()
|
|
|
|
api.lockf(func() {
|
2024-08-06 22:13:37 +07:00
|
|
|
device, err = models.Devices.Insert(ctx, api.DB, &models.DeviceSetter{
|
2024-08-05 23:06:32 +07:00
|
|
|
Slug: omit.From(params.Slug),
|
|
|
|
Name: omit.From(params.Name),
|
|
|
|
ResolutionX: omit.From(params.ResolutionX),
|
|
|
|
ResolutionY: omit.From(params.ResolutionY),
|
|
|
|
AspectRatioTolerance: omit.From(params.AspectRatioTolerance),
|
|
|
|
MinX: omit.From(params.MinX),
|
|
|
|
MinY: omit.From(params.MinY),
|
|
|
|
MaxX: omit.From(params.MaxX),
|
|
|
|
MaxY: omit.From(params.MaxY),
|
|
|
|
NSFW: omit.From(params.NSFW),
|
|
|
|
SingleFolderMode: omit.From(params.SingleFolderMode),
|
|
|
|
Disabled: omit.From(params.Disabled),
|
|
|
|
CreatedAt: omit.From(now.Unix()),
|
|
|
|
UpdatedAt: omit.From(now.Unix()),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-08-05 23:20:07 +07:00
|
|
|
if sqliteErr := new(sqlite3.Error); errors.As(err, sqliteErr) {
|
2024-08-05 23:06:32 +07:00
|
|
|
if sqliteErr.Code == sqlite3.ErrConstraint {
|
|
|
|
return nil, errs.
|
|
|
|
Wrapw(sqliteErr, "device already exists", "params", params).
|
|
|
|
Code(connect.CodeAlreadyExists)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, errs.Wrapw(err, "failed to create device", "params", params)
|
|
|
|
}
|
|
|
|
return device, nil
|
|
|
|
}
|