mirror of
https://github.com/hay-kot/homebox.git
synced 2024-12-27 09:16:30 +00:00
23b5892aef
* introduce scaffold for new models * wip: shoutrrr wrapper (may remove) * update schema files * gen: ent code * gen: migrations * go mod tidy * add group_id to notifier * db migration * new mapper helpers * notifier repo * introduce experimental adapter pattern for hdlrs * refactor adapters to fit more common use cases * new routes for notifiers * update errors to fix validation panic * go tidy * reverse checkbox label display * wip: notifiers UI * use badges instead of text * improve documentation * add scaffold schema reference * remove notifier service * refactor schema folder * support group edges via scaffold * delete test file * include link to API docs * audit and update documentation + improve format * refactor schema edges * refactor * add custom validator * set validate + order fields by name * fix failing tests
110 lines
2.8 KiB
Go
110 lines
2.8 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent"
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/notifier"
|
|
)
|
|
|
|
type NotifierRepository struct {
|
|
db *ent.Client
|
|
mapper MapFunc[*ent.Notifier, NotifierOut]
|
|
}
|
|
|
|
func NewNotifierRepository(db *ent.Client) *NotifierRepository {
|
|
return &NotifierRepository{
|
|
db: db,
|
|
mapper: func(n *ent.Notifier) NotifierOut {
|
|
return NotifierOut{
|
|
ID: n.ID,
|
|
UserID: n.UserID,
|
|
GroupID: n.GroupID,
|
|
CreatedAt: n.CreatedAt,
|
|
UpdatedAt: n.UpdatedAt,
|
|
|
|
Name: n.Name,
|
|
IsActive: n.IsActive,
|
|
URL: n.URL,
|
|
}
|
|
},
|
|
}
|
|
}
|
|
|
|
type (
|
|
NotifierCreate struct {
|
|
Name string `json:"name" validate:"required,min=1,max=255"`
|
|
IsActive bool `json:"isActive"`
|
|
URL string `json:"url" validate:"required,shoutrrr"`
|
|
}
|
|
|
|
NotifierUpdate struct {
|
|
Name string `json:"name" validate:"required,min=1,max=255"`
|
|
IsActive bool `json:"isActive"`
|
|
URL *string `json:"url" validate:"omitempty,shoutrrr" extensions:"x-nullable" `
|
|
}
|
|
|
|
NotifierOut struct {
|
|
ID uuid.UUID `json:"id"`
|
|
UserID uuid.UUID `json:"userId"`
|
|
GroupID uuid.UUID `json:"groupId"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
|
|
Name string `json:"name"`
|
|
IsActive bool `json:"isActive"`
|
|
URL string `json:"-"` // URL field is not exposed to the client
|
|
}
|
|
)
|
|
|
|
func (r *NotifierRepository) GetByUser(ctx context.Context, userID uuid.UUID) ([]NotifierOut, error) {
|
|
notifier, err := r.db.Notifier.Query().
|
|
Where(notifier.UserID(userID)).
|
|
Order(ent.Asc(notifier.FieldName)).
|
|
All(ctx)
|
|
|
|
return r.mapper.MapEachErr(notifier, err)
|
|
}
|
|
|
|
func (r *NotifierRepository) GetByGroup(ctx context.Context, groupID uuid.UUID) ([]NotifierOut, error) {
|
|
notifier, err := r.db.Notifier.Query().
|
|
Where(notifier.GroupID(groupID)).
|
|
Order(ent.Asc(notifier.FieldName)).
|
|
All(ctx)
|
|
return r.mapper.MapEachErr(notifier, err)
|
|
}
|
|
|
|
func (r *NotifierRepository) Create(ctx context.Context, groupID, userID uuid.UUID, input NotifierCreate) (NotifierOut, error) {
|
|
notifier, err := r.db.Notifier.
|
|
Create().
|
|
SetGroupID(groupID).
|
|
SetUserID(userID).
|
|
SetName(input.Name).
|
|
SetIsActive(input.IsActive).
|
|
SetURL(input.URL).
|
|
Save(ctx)
|
|
|
|
return r.mapper.MapErr(notifier, err)
|
|
}
|
|
|
|
func (r *NotifierRepository) Update(ctx context.Context, userID uuid.UUID, id uuid.UUID, input NotifierUpdate) (NotifierOut, error) {
|
|
q := r.db.Notifier.
|
|
UpdateOneID(id).
|
|
SetName(input.Name).
|
|
SetIsActive(input.IsActive)
|
|
|
|
if input.URL != nil {
|
|
q.SetURL(*input.URL)
|
|
}
|
|
|
|
notifier, err := q.Save(ctx)
|
|
|
|
return r.mapper.MapErr(notifier, err)
|
|
}
|
|
|
|
func (r *NotifierRepository) Delete(ctx context.Context, userID uuid.UUID, ID uuid.UUID) error {
|
|
_, err := r.db.Notifier.Delete().Where(notifier.UserID(userID), notifier.ID(ID)).Exec(ctx)
|
|
return err
|
|
}
|