homebox/backend/internal/data/ent/schema/item.go

123 lines
2.5 KiB
Go
Raw Permalink Normal View History

2022-08-30 18:04:50 +00:00
package schema
import (
"entgo.io/ent"
2022-09-04 02:42:03 +00:00
"entgo.io/ent/dialect/entsql"
2022-08-30 18:04:50 +00:00
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
2022-09-09 18:20:38 +00:00
"entgo.io/ent/schema/index"
"github.com/hay-kot/homebox/backend/internal/data/ent/schema/mixins"
2022-08-30 18:04:50 +00:00
)
// Item holds the schema definition for the Item entity.
type Item struct {
ent.Schema
}
func (Item) Mixin() []ent.Mixin {
return []ent.Mixin{
mixins.BaseMixin{},
mixins.DetailsMixin{},
GroupMixin{ref: "items"},
2022-08-30 18:04:50 +00:00
}
}
2022-09-09 18:20:38 +00:00
func (Item) Indexes() []ent.Index {
return []ent.Index{
// Unique index on the "title" field.
index.Fields("name"),
index.Fields("manufacturer"),
index.Fields("model_number"),
index.Fields("serial_number"),
index.Fields("archived"),
index.Fields("asset_id"),
2022-09-09 18:20:38 +00:00
}
}
2022-08-30 18:04:50 +00:00
// Fields of the Item.
func (Item) Fields() []ent.Field {
return []ent.Field{
field.String("import_ref").
Optional().
MaxLen(100),
2022-08-30 18:04:50 +00:00
field.String("notes").
MaxLen(1000).
Optional(),
field.Int("quantity").
Default(1),
field.Bool("insured").
Default(false),
field.Bool("archived").
Default(false),
field.Int("asset_id").
Default(0),
2022-09-09 18:20:38 +00:00
2022-08-30 18:04:50 +00:00
// ------------------------------------
// item identification
field.String("serial_number").
MaxLen(255).
Optional(),
field.String("model_number").
MaxLen(255).
Optional(),
field.String("manufacturer").
MaxLen(255).
Optional(),
2022-09-09 18:20:38 +00:00
// ------------------------------------
// Item Warranty
field.Bool("lifetime_warranty").
Default(false),
field.Time("warranty_expires").
Optional(),
field.Text("warranty_details").
MaxLen(1000).
Optional(),
2022-08-30 18:04:50 +00:00
// ------------------------------------
// item purchase
field.Time("purchase_time").
Optional(),
field.String("purchase_from").
Optional(),
field.Float("purchase_price").
Default(0),
2022-09-09 18:20:38 +00:00
2022-08-30 18:04:50 +00:00
// ------------------------------------
// Sold Details
field.Time("sold_time").
Optional(),
field.String("sold_to").
Optional(),
field.Float("sold_price").
Default(0),
field.String("sold_notes").
MaxLen(1000).
Optional(),
}
}
// Edges of the Item.
func (Item) Edges() []ent.Edge {
owned := func(s string, t any) ent.Edge {
return edge.To(s, t).
Annotations(entsql.Annotation{
OnDelete: entsql.Cascade,
})
}
2022-08-30 18:04:50 +00:00
return []ent.Edge{
edge.To("children", Item.Type).
From("parent").
Unique(),
edge.From("label", Label.Type).
Ref("items"),
2022-08-30 18:04:50 +00:00
edge.From("location", Location.Type).
Ref("items").
Unique(),
owned("fields", ItemField.Type),
owned("maintenance_entries", MaintenanceEntry.Type),
owned("attachments", Attachment.Type),
2022-08-30 18:04:50 +00:00
}
}