mirror of
https://github.com/hay-kot/homebox.git
synced 2024-11-23 17:15:42 +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
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package schema
|
|
|
|
import (
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/entsql"
|
|
"entgo.io/ent/schema/edge"
|
|
"entgo.io/ent/schema/field"
|
|
"entgo.io/ent/schema/mixin"
|
|
"github.com/google/uuid"
|
|
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
|
|
)
|
|
|
|
// Group holds the schema definition for the Group entity.
|
|
type Group struct {
|
|
ent.Schema
|
|
}
|
|
|
|
func (Group) Mixin() []ent.Mixin {
|
|
return []ent.Mixin{
|
|
mixins.BaseMixin{},
|
|
}
|
|
}
|
|
|
|
// Fields of the Home.
|
|
func (Group) Fields() []ent.Field {
|
|
return []ent.Field{
|
|
field.String("name").
|
|
MaxLen(255).
|
|
NotEmpty(),
|
|
field.Enum("currency").
|
|
Default("usd").
|
|
Values("usd", "eur", "gbp", "jpy", "zar", "aud", "nok", "sek", "dkk", "inr", "rmb", "bgn", "chf", "pln", "try", "ron"),
|
|
}
|
|
}
|
|
|
|
// Edges of the Home.
|
|
func (Group) Edges() []ent.Edge {
|
|
owned := func(name string, t any) ent.Edge {
|
|
return edge.To(name, t).
|
|
Annotations(entsql.Annotation{
|
|
OnDelete: entsql.Cascade,
|
|
})
|
|
}
|
|
|
|
return []ent.Edge{
|
|
owned("users", User.Type),
|
|
owned("locations", Location.Type),
|
|
owned("items", Item.Type),
|
|
owned("labels", Label.Type),
|
|
owned("documents", Document.Type),
|
|
owned("invitation_tokens", GroupInvitationToken.Type),
|
|
owned("notifiers", Notifier.Type),
|
|
// $scaffold_edge
|
|
}
|
|
}
|
|
|
|
// GroupMixin when embedded in an ent.Schema, adds a reference to
|
|
// the Group entity.
|
|
type GroupMixin struct {
|
|
ref string
|
|
field string
|
|
mixin.Schema
|
|
}
|
|
|
|
func (g GroupMixin) Fields() []ent.Field {
|
|
if g.field != "" {
|
|
return []ent.Field{
|
|
field.UUID(g.field, uuid.UUID{}),
|
|
}
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (g GroupMixin) Edges() []ent.Edge {
|
|
edge := edge.From("group", Group.Type).
|
|
Ref(g.ref).
|
|
Unique().
|
|
Required()
|
|
|
|
if g.field != "" {
|
|
edge = edge.Field(g.field)
|
|
}
|
|
|
|
return []ent.Edge{edge}
|
|
}
|