mirror of
https://github.com/simon-ding/polaris.git
synced 2026-04-22 03:37:30 +08:00
implement download feature
This commit is contained in:
149
ent/client.go
149
ent/client.go
@@ -17,6 +17,7 @@ import (
|
||||
"polaris/ent/indexers"
|
||||
"polaris/ent/series"
|
||||
"polaris/ent/settings"
|
||||
"polaris/ent/storage"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
@@ -41,6 +42,8 @@ type Client struct {
|
||||
Series *SeriesClient
|
||||
// Settings is the client for interacting with the Settings builders.
|
||||
Settings *SettingsClient
|
||||
// Storage is the client for interacting with the Storage builders.
|
||||
Storage *StorageClient
|
||||
}
|
||||
|
||||
// NewClient creates a new client configured with the given options.
|
||||
@@ -58,6 +61,7 @@ func (c *Client) init() {
|
||||
c.Indexers = NewIndexersClient(c.config)
|
||||
c.Series = NewSeriesClient(c.config)
|
||||
c.Settings = NewSettingsClient(c.config)
|
||||
c.Storage = NewStorageClient(c.config)
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -156,6 +160,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
||||
Indexers: NewIndexersClient(cfg),
|
||||
Series: NewSeriesClient(cfg),
|
||||
Settings: NewSettingsClient(cfg),
|
||||
Storage: NewStorageClient(cfg),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -181,6 +186,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
|
||||
Indexers: NewIndexersClient(cfg),
|
||||
Series: NewSeriesClient(cfg),
|
||||
Settings: NewSettingsClient(cfg),
|
||||
Storage: NewStorageClient(cfg),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -211,6 +217,7 @@ func (c *Client) Close() error {
|
||||
func (c *Client) Use(hooks ...Hook) {
|
||||
for _, n := range []interface{ Use(...Hook) }{
|
||||
c.DownloadClients, c.Episode, c.History, c.Indexers, c.Series, c.Settings,
|
||||
c.Storage,
|
||||
} {
|
||||
n.Use(hooks...)
|
||||
}
|
||||
@@ -221,6 +228,7 @@ func (c *Client) Use(hooks ...Hook) {
|
||||
func (c *Client) Intercept(interceptors ...Interceptor) {
|
||||
for _, n := range []interface{ Intercept(...Interceptor) }{
|
||||
c.DownloadClients, c.Episode, c.History, c.Indexers, c.Series, c.Settings,
|
||||
c.Storage,
|
||||
} {
|
||||
n.Intercept(interceptors...)
|
||||
}
|
||||
@@ -241,6 +249,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
||||
return c.Series.mutate(ctx, m)
|
||||
case *SettingsMutation:
|
||||
return c.Settings.mutate(ctx, m)
|
||||
case *StorageMutation:
|
||||
return c.Storage.mutate(ctx, m)
|
||||
default:
|
||||
return nil, fmt.Errorf("ent: unknown mutation type %T", m)
|
||||
}
|
||||
@@ -1076,12 +1086,147 @@ func (c *SettingsClient) mutate(ctx context.Context, m *SettingsMutation) (Value
|
||||
}
|
||||
}
|
||||
|
||||
// StorageClient is a client for the Storage schema.
|
||||
type StorageClient struct {
|
||||
config
|
||||
}
|
||||
|
||||
// NewStorageClient returns a client for the Storage from the given config.
|
||||
func NewStorageClient(c config) *StorageClient {
|
||||
return &StorageClient{config: c}
|
||||
}
|
||||
|
||||
// Use adds a list of mutation hooks to the hooks stack.
|
||||
// A call to `Use(f, g, h)` equals to `storage.Hooks(f(g(h())))`.
|
||||
func (c *StorageClient) Use(hooks ...Hook) {
|
||||
c.hooks.Storage = append(c.hooks.Storage, hooks...)
|
||||
}
|
||||
|
||||
// Intercept adds a list of query interceptors to the interceptors stack.
|
||||
// A call to `Intercept(f, g, h)` equals to `storage.Intercept(f(g(h())))`.
|
||||
func (c *StorageClient) Intercept(interceptors ...Interceptor) {
|
||||
c.inters.Storage = append(c.inters.Storage, interceptors...)
|
||||
}
|
||||
|
||||
// Create returns a builder for creating a Storage entity.
|
||||
func (c *StorageClient) Create() *StorageCreate {
|
||||
mutation := newStorageMutation(c.config, OpCreate)
|
||||
return &StorageCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// CreateBulk returns a builder for creating a bulk of Storage entities.
|
||||
func (c *StorageClient) CreateBulk(builders ...*StorageCreate) *StorageCreateBulk {
|
||||
return &StorageCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates
|
||||
// a builder and applies setFunc on it.
|
||||
func (c *StorageClient) MapCreateBulk(slice any, setFunc func(*StorageCreate, int)) *StorageCreateBulk {
|
||||
rv := reflect.ValueOf(slice)
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return &StorageCreateBulk{err: fmt.Errorf("calling to StorageClient.MapCreateBulk with wrong type %T, need slice", slice)}
|
||||
}
|
||||
builders := make([]*StorageCreate, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
builders[i] = c.Create()
|
||||
setFunc(builders[i], i)
|
||||
}
|
||||
return &StorageCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Storage.
|
||||
func (c *StorageClient) Update() *StorageUpdate {
|
||||
mutation := newStorageMutation(c.config, OpUpdate)
|
||||
return &StorageUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOne returns an update builder for the given entity.
|
||||
func (c *StorageClient) UpdateOne(s *Storage) *StorageUpdateOne {
|
||||
mutation := newStorageMutation(c.config, OpUpdateOne, withStorage(s))
|
||||
return &StorageUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOneID returns an update builder for the given id.
|
||||
func (c *StorageClient) UpdateOneID(id int) *StorageUpdateOne {
|
||||
mutation := newStorageMutation(c.config, OpUpdateOne, withStorageID(id))
|
||||
return &StorageUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// Delete returns a delete builder for Storage.
|
||||
func (c *StorageClient) Delete() *StorageDelete {
|
||||
mutation := newStorageMutation(c.config, OpDelete)
|
||||
return &StorageDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// DeleteOne returns a builder for deleting the given entity.
|
||||
func (c *StorageClient) DeleteOne(s *Storage) *StorageDeleteOne {
|
||||
return c.DeleteOneID(s.ID)
|
||||
}
|
||||
|
||||
// DeleteOneID returns a builder for deleting the given entity by its id.
|
||||
func (c *StorageClient) DeleteOneID(id int) *StorageDeleteOne {
|
||||
builder := c.Delete().Where(storage.ID(id))
|
||||
builder.mutation.id = &id
|
||||
builder.mutation.op = OpDeleteOne
|
||||
return &StorageDeleteOne{builder}
|
||||
}
|
||||
|
||||
// Query returns a query builder for Storage.
|
||||
func (c *StorageClient) Query() *StorageQuery {
|
||||
return &StorageQuery{
|
||||
config: c.config,
|
||||
ctx: &QueryContext{Type: TypeStorage},
|
||||
inters: c.Interceptors(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a Storage entity by its id.
|
||||
func (c *StorageClient) Get(ctx context.Context, id int) (*Storage, error) {
|
||||
return c.Query().Where(storage.ID(id)).Only(ctx)
|
||||
}
|
||||
|
||||
// GetX is like Get, but panics if an error occurs.
|
||||
func (c *StorageClient) GetX(ctx context.Context, id int) *Storage {
|
||||
obj, err := c.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// Hooks returns the client hooks.
|
||||
func (c *StorageClient) Hooks() []Hook {
|
||||
return c.hooks.Storage
|
||||
}
|
||||
|
||||
// Interceptors returns the client interceptors.
|
||||
func (c *StorageClient) Interceptors() []Interceptor {
|
||||
return c.inters.Storage
|
||||
}
|
||||
|
||||
func (c *StorageClient) mutate(ctx context.Context, m *StorageMutation) (Value, error) {
|
||||
switch m.Op() {
|
||||
case OpCreate:
|
||||
return (&StorageCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdate:
|
||||
return (&StorageUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdateOne:
|
||||
return (&StorageUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpDelete, OpDeleteOne:
|
||||
return (&StorageDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("ent: unknown Storage mutation op: %q", m.Op())
|
||||
}
|
||||
}
|
||||
|
||||
// hooks and interceptors per client, for fast access.
|
||||
type (
|
||||
hooks struct {
|
||||
DownloadClients, Episode, History, Indexers, Series, Settings []ent.Hook
|
||||
DownloadClients, Episode, History, Indexers, Series, Settings,
|
||||
Storage []ent.Hook
|
||||
}
|
||||
inters struct {
|
||||
DownloadClients, Episode, History, Indexers, Series, Settings []ent.Interceptor
|
||||
DownloadClients, Episode, History, Indexers, Series, Settings,
|
||||
Storage []ent.Interceptor
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user