diff --git a/ent/history.go b/ent/history.go index af264fc..c71a591 100644 --- a/ent/history.go +++ b/ent/history.go @@ -27,6 +27,8 @@ type History struct { Date time.Time `json:"date,omitempty"` // TargetDir holds the value of the "target_dir" field. TargetDir string `json:"target_dir,omitempty"` + // Size holds the value of the "size" field. + Size int `json:"size,omitempty"` // Status holds the value of the "status" field. Status history.Status `json:"status,omitempty"` // Saved holds the value of the "saved" field. @@ -39,7 +41,7 @@ func (*History) scanValues(columns []string) ([]any, error) { values := make([]any, len(columns)) for i := range columns { switch columns[i] { - case history.FieldID, history.FieldSeriesID, history.FieldEpisodeID: + case history.FieldID, history.FieldSeriesID, history.FieldEpisodeID, history.FieldSize: values[i] = new(sql.NullInt64) case history.FieldSourceTitle, history.FieldTargetDir, history.FieldStatus, history.FieldSaved: values[i] = new(sql.NullString) @@ -96,6 +98,12 @@ func (h *History) assignValues(columns []string, values []any) error { } else if value.Valid { h.TargetDir = value.String } + case history.FieldSize: + if value, ok := values[i].(*sql.NullInt64); !ok { + return fmt.Errorf("unexpected type %T for field size", values[i]) + } else if value.Valid { + h.Size = int(value.Int64) + } case history.FieldStatus: if value, ok := values[i].(*sql.NullString); !ok { return fmt.Errorf("unexpected type %T for field status", values[i]) @@ -159,6 +167,9 @@ func (h *History) String() string { builder.WriteString("target_dir=") builder.WriteString(h.TargetDir) builder.WriteString(", ") + builder.WriteString("size=") + builder.WriteString(fmt.Sprintf("%v", h.Size)) + builder.WriteString(", ") builder.WriteString("status=") builder.WriteString(fmt.Sprintf("%v", h.Status)) builder.WriteString(", ") diff --git a/ent/history/history.go b/ent/history/history.go index 8305e05..76f9941 100644 --- a/ent/history/history.go +++ b/ent/history/history.go @@ -23,6 +23,8 @@ const ( FieldDate = "date" // FieldTargetDir holds the string denoting the target_dir field in the database. FieldTargetDir = "target_dir" + // FieldSize holds the string denoting the size field in the database. + FieldSize = "size" // FieldStatus holds the string denoting the status field in the database. FieldStatus = "status" // FieldSaved holds the string denoting the saved field in the database. @@ -39,6 +41,7 @@ var Columns = []string{ FieldSourceTitle, FieldDate, FieldTargetDir, + FieldSize, FieldStatus, FieldSaved, } @@ -53,6 +56,11 @@ func ValidColumn(column string) bool { return false } +var ( + // DefaultSize holds the default value on creation for the "size" field. + DefaultSize int +) + // Status defines the type for the "status" enum field. type Status string @@ -111,6 +119,11 @@ func ByTargetDir(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldTargetDir, opts...).ToFunc() } +// BySize orders the results by the size field. +func BySize(opts ...sql.OrderTermOption) OrderOption { + return sql.OrderByField(FieldSize, opts...).ToFunc() +} + // ByStatus orders the results by the status field. func ByStatus(opts ...sql.OrderTermOption) OrderOption { return sql.OrderByField(FieldStatus, opts...).ToFunc() diff --git a/ent/history/where.go b/ent/history/where.go index 8f185f6..c4d6d06 100644 --- a/ent/history/where.go +++ b/ent/history/where.go @@ -79,6 +79,11 @@ func TargetDir(v string) predicate.History { return predicate.History(sql.FieldEQ(FieldTargetDir, v)) } +// Size applies equality check predicate on the "size" field. It's identical to SizeEQ. +func Size(v int) predicate.History { + return predicate.History(sql.FieldEQ(FieldSize, v)) +} + // Saved applies equality check predicate on the "saved" field. It's identical to SavedEQ. func Saved(v string) predicate.History { return predicate.History(sql.FieldEQ(FieldSaved, v)) @@ -334,6 +339,46 @@ func TargetDirContainsFold(v string) predicate.History { return predicate.History(sql.FieldContainsFold(FieldTargetDir, v)) } +// SizeEQ applies the EQ predicate on the "size" field. +func SizeEQ(v int) predicate.History { + return predicate.History(sql.FieldEQ(FieldSize, v)) +} + +// SizeNEQ applies the NEQ predicate on the "size" field. +func SizeNEQ(v int) predicate.History { + return predicate.History(sql.FieldNEQ(FieldSize, v)) +} + +// SizeIn applies the In predicate on the "size" field. +func SizeIn(vs ...int) predicate.History { + return predicate.History(sql.FieldIn(FieldSize, vs...)) +} + +// SizeNotIn applies the NotIn predicate on the "size" field. +func SizeNotIn(vs ...int) predicate.History { + return predicate.History(sql.FieldNotIn(FieldSize, vs...)) +} + +// SizeGT applies the GT predicate on the "size" field. +func SizeGT(v int) predicate.History { + return predicate.History(sql.FieldGT(FieldSize, v)) +} + +// SizeGTE applies the GTE predicate on the "size" field. +func SizeGTE(v int) predicate.History { + return predicate.History(sql.FieldGTE(FieldSize, v)) +} + +// SizeLT applies the LT predicate on the "size" field. +func SizeLT(v int) predicate.History { + return predicate.History(sql.FieldLT(FieldSize, v)) +} + +// SizeLTE applies the LTE predicate on the "size" field. +func SizeLTE(v int) predicate.History { + return predicate.History(sql.FieldLTE(FieldSize, v)) +} + // StatusEQ applies the EQ predicate on the "status" field. func StatusEQ(v Status) predicate.History { return predicate.History(sql.FieldEQ(FieldStatus, v)) diff --git a/ent/history_create.go b/ent/history_create.go index 09b405b..d9232cf 100644 --- a/ent/history_create.go +++ b/ent/history_create.go @@ -50,6 +50,20 @@ func (hc *HistoryCreate) SetTargetDir(s string) *HistoryCreate { return hc } +// SetSize sets the "size" field. +func (hc *HistoryCreate) SetSize(i int) *HistoryCreate { + hc.mutation.SetSize(i) + return hc +} + +// SetNillableSize sets the "size" field if the given value is not nil. +func (hc *HistoryCreate) SetNillableSize(i *int) *HistoryCreate { + if i != nil { + hc.SetSize(*i) + } + return hc +} + // SetStatus sets the "status" field. func (hc *HistoryCreate) SetStatus(h history.Status) *HistoryCreate { hc.mutation.SetStatus(h) @@ -77,6 +91,7 @@ func (hc *HistoryCreate) Mutation() *HistoryMutation { // Save creates the History in the database. func (hc *HistoryCreate) Save(ctx context.Context) (*History, error) { + hc.defaults() return withHooks(ctx, hc.sqlSave, hc.mutation, hc.hooks) } @@ -102,6 +117,14 @@ func (hc *HistoryCreate) ExecX(ctx context.Context) { } } +// defaults sets the default values of the builder before save. +func (hc *HistoryCreate) defaults() { + if _, ok := hc.mutation.Size(); !ok { + v := history.DefaultSize + hc.mutation.SetSize(v) + } +} + // check runs all checks and user-defined validators on the builder. func (hc *HistoryCreate) check() error { if _, ok := hc.mutation.SeriesID(); !ok { @@ -119,6 +142,9 @@ func (hc *HistoryCreate) check() error { if _, ok := hc.mutation.TargetDir(); !ok { return &ValidationError{Name: "target_dir", err: errors.New(`ent: missing required field "History.target_dir"`)} } + if _, ok := hc.mutation.Size(); !ok { + return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "History.size"`)} + } if _, ok := hc.mutation.Status(); !ok { return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "History.status"`)} } @@ -173,6 +199,10 @@ func (hc *HistoryCreate) createSpec() (*History, *sqlgraph.CreateSpec) { _spec.SetField(history.FieldTargetDir, field.TypeString, value) _node.TargetDir = value } + if value, ok := hc.mutation.Size(); ok { + _spec.SetField(history.FieldSize, field.TypeInt, value) + _node.Size = value + } if value, ok := hc.mutation.Status(); ok { _spec.SetField(history.FieldStatus, field.TypeEnum, value) _node.Status = value @@ -202,6 +232,7 @@ func (hcb *HistoryCreateBulk) Save(ctx context.Context) ([]*History, error) { for i := range hcb.builders { func(i int, root context.Context) { builder := hcb.builders[i] + builder.defaults() var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { mutation, ok := m.(*HistoryMutation) if !ok { diff --git a/ent/history_update.go b/ent/history_update.go index 8ad0e98..2774b94 100644 --- a/ent/history_update.go +++ b/ent/history_update.go @@ -112,6 +112,27 @@ func (hu *HistoryUpdate) SetNillableTargetDir(s *string) *HistoryUpdate { return hu } +// SetSize sets the "size" field. +func (hu *HistoryUpdate) SetSize(i int) *HistoryUpdate { + hu.mutation.ResetSize() + hu.mutation.SetSize(i) + return hu +} + +// SetNillableSize sets the "size" field if the given value is not nil. +func (hu *HistoryUpdate) SetNillableSize(i *int) *HistoryUpdate { + if i != nil { + hu.SetSize(*i) + } + return hu +} + +// AddSize adds i to the "size" field. +func (hu *HistoryUpdate) AddSize(i int) *HistoryUpdate { + hu.mutation.AddSize(i) + return hu +} + // SetStatus sets the "status" field. func (hu *HistoryUpdate) SetStatus(h history.Status) *HistoryUpdate { hu.mutation.SetStatus(h) @@ -221,6 +242,12 @@ func (hu *HistoryUpdate) sqlSave(ctx context.Context) (n int, err error) { if value, ok := hu.mutation.TargetDir(); ok { _spec.SetField(history.FieldTargetDir, field.TypeString, value) } + if value, ok := hu.mutation.Size(); ok { + _spec.SetField(history.FieldSize, field.TypeInt, value) + } + if value, ok := hu.mutation.AddedSize(); ok { + _spec.AddField(history.FieldSize, field.TypeInt, value) + } if value, ok := hu.mutation.Status(); ok { _spec.SetField(history.FieldStatus, field.TypeEnum, value) } @@ -334,6 +361,27 @@ func (huo *HistoryUpdateOne) SetNillableTargetDir(s *string) *HistoryUpdateOne { return huo } +// SetSize sets the "size" field. +func (huo *HistoryUpdateOne) SetSize(i int) *HistoryUpdateOne { + huo.mutation.ResetSize() + huo.mutation.SetSize(i) + return huo +} + +// SetNillableSize sets the "size" field if the given value is not nil. +func (huo *HistoryUpdateOne) SetNillableSize(i *int) *HistoryUpdateOne { + if i != nil { + huo.SetSize(*i) + } + return huo +} + +// AddSize adds i to the "size" field. +func (huo *HistoryUpdateOne) AddSize(i int) *HistoryUpdateOne { + huo.mutation.AddSize(i) + return huo +} + // SetStatus sets the "status" field. func (huo *HistoryUpdateOne) SetStatus(h history.Status) *HistoryUpdateOne { huo.mutation.SetStatus(h) @@ -473,6 +521,12 @@ func (huo *HistoryUpdateOne) sqlSave(ctx context.Context) (_node *History, err e if value, ok := huo.mutation.TargetDir(); ok { _spec.SetField(history.FieldTargetDir, field.TypeString, value) } + if value, ok := huo.mutation.Size(); ok { + _spec.SetField(history.FieldSize, field.TypeInt, value) + } + if value, ok := huo.mutation.AddedSize(); ok { + _spec.AddField(history.FieldSize, field.TypeInt, value) + } if value, ok := huo.mutation.Status(); ok { _spec.SetField(history.FieldStatus, field.TypeEnum, value) } diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index 592e797..550619e 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -63,6 +63,7 @@ var ( {Name: "source_title", Type: field.TypeString}, {Name: "date", Type: field.TypeTime}, {Name: "target_dir", Type: field.TypeString}, + {Name: "size", Type: field.TypeInt, Default: 0}, {Name: "status", Type: field.TypeEnum, Enums: []string{"running", "success", "fail", "uploading"}}, {Name: "saved", Type: field.TypeString, Nullable: true}, } diff --git a/ent/mutation.go b/ent/mutation.go index 4e55164..8a9cd52 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -1786,6 +1786,8 @@ type HistoryMutation struct { source_title *string date *time.Time target_dir *string + size *int + addsize *int status *history.Status saved *string clearedFields map[string]struct{} @@ -2112,6 +2114,62 @@ func (m *HistoryMutation) ResetTargetDir() { m.target_dir = nil } +// SetSize sets the "size" field. +func (m *HistoryMutation) SetSize(i int) { + m.size = &i + m.addsize = nil +} + +// Size returns the value of the "size" field in the mutation. +func (m *HistoryMutation) Size() (r int, exists bool) { + v := m.size + if v == nil { + return + } + return *v, true +} + +// OldSize returns the old "size" field's value of the History entity. +// If the History object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *HistoryMutation) OldSize(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldSize is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldSize requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldSize: %w", err) + } + return oldValue.Size, nil +} + +// AddSize adds i to the "size" field. +func (m *HistoryMutation) AddSize(i int) { + if m.addsize != nil { + *m.addsize += i + } else { + m.addsize = &i + } +} + +// AddedSize returns the value that was added to the "size" field in this mutation. +func (m *HistoryMutation) AddedSize() (r int, exists bool) { + v := m.addsize + if v == nil { + return + } + return *v, true +} + +// ResetSize resets all changes to the "size" field. +func (m *HistoryMutation) ResetSize() { + m.size = nil + m.addsize = nil +} + // SetStatus sets the "status" field. func (m *HistoryMutation) SetStatus(h history.Status) { m.status = &h @@ -2231,7 +2289,7 @@ func (m *HistoryMutation) Type() string { // order to get all numeric fields that were incremented/decremented, call // AddedFields(). func (m *HistoryMutation) Fields() []string { - fields := make([]string, 0, 7) + fields := make([]string, 0, 8) if m.series_id != nil { fields = append(fields, history.FieldSeriesID) } @@ -2247,6 +2305,9 @@ func (m *HistoryMutation) Fields() []string { if m.target_dir != nil { fields = append(fields, history.FieldTargetDir) } + if m.size != nil { + fields = append(fields, history.FieldSize) + } if m.status != nil { fields = append(fields, history.FieldStatus) } @@ -2271,6 +2332,8 @@ func (m *HistoryMutation) Field(name string) (ent.Value, bool) { return m.Date() case history.FieldTargetDir: return m.TargetDir() + case history.FieldSize: + return m.Size() case history.FieldStatus: return m.Status() case history.FieldSaved: @@ -2294,6 +2357,8 @@ func (m *HistoryMutation) OldField(ctx context.Context, name string) (ent.Value, return m.OldDate(ctx) case history.FieldTargetDir: return m.OldTargetDir(ctx) + case history.FieldSize: + return m.OldSize(ctx) case history.FieldStatus: return m.OldStatus(ctx) case history.FieldSaved: @@ -2342,6 +2407,13 @@ func (m *HistoryMutation) SetField(name string, value ent.Value) error { } m.SetTargetDir(v) return nil + case history.FieldSize: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetSize(v) + return nil case history.FieldStatus: v, ok := value.(history.Status) if !ok { @@ -2370,6 +2442,9 @@ func (m *HistoryMutation) AddedFields() []string { if m.addepisode_id != nil { fields = append(fields, history.FieldEpisodeID) } + if m.addsize != nil { + fields = append(fields, history.FieldSize) + } return fields } @@ -2382,6 +2457,8 @@ func (m *HistoryMutation) AddedField(name string) (ent.Value, bool) { return m.AddedSeriesID() case history.FieldEpisodeID: return m.AddedEpisodeID() + case history.FieldSize: + return m.AddedSize() } return nil, false } @@ -2405,6 +2482,13 @@ func (m *HistoryMutation) AddField(name string, value ent.Value) error { } m.AddEpisodeID(v) return nil + case history.FieldSize: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddSize(v) + return nil } return fmt.Errorf("unknown History numeric field %s", name) } @@ -2456,6 +2540,9 @@ func (m *HistoryMutation) ResetField(name string) error { case history.FieldTargetDir: m.ResetTargetDir() return nil + case history.FieldSize: + m.ResetSize() + return nil case history.FieldStatus: m.ResetStatus() return nil diff --git a/ent/runtime.go b/ent/runtime.go index 6998b5f..28b4b6c 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -4,6 +4,7 @@ package ent import ( "polaris/ent/downloadclients" + "polaris/ent/history" "polaris/ent/indexers" "polaris/ent/schema" "polaris/ent/series" @@ -47,6 +48,12 @@ func init() { downloadclients.DefaultTags = downloadclientsDescTags.Default.(string) episodeFields := schema.Episode{}.Fields() _ = episodeFields + historyFields := schema.History{}.Fields() + _ = historyFields + // historyDescSize is the schema descriptor for size field. + historyDescSize := historyFields[5].Descriptor() + // history.DefaultSize holds the default value on creation for the size field. + history.DefaultSize = historyDescSize.Default.(int) indexersFields := schema.Indexers{}.Fields() _ = indexersFields // indexersDescEnableRss is the schema descriptor for enable_rss field. diff --git a/ent/schema/history.go b/ent/schema/history.go index 0920a9e..2f9d022 100644 --- a/ent/schema/history.go +++ b/ent/schema/history.go @@ -18,6 +18,7 @@ func (History) Fields() []ent.Field { field.String("source_title"), field.Time("date"), field.String("target_dir"), + field.Int("size").Default(0), field.Enum("status").Values("running", "success", "fail", "uploading"), field.String("saved").Optional(), } diff --git a/server/resources.go b/server/resources.go index 5bc7048..a815788 100644 --- a/server/resources.go +++ b/server/resources.go @@ -122,6 +122,7 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string SourceTitle: r1.Name, TargetDir: dir, Status: history.StatusRunning, + Size: r1.Size, Saved: torrent.Save(), }) s.db.SetEpisodeStatus(ep.ID, episode.StatusDownloading)