Add the event service
This commit is contained in:
+146
-3
@@ -14,6 +14,7 @@ import (
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"git.gorlug.de/code/ersteller/schema/ent/event"
|
||||
"git.gorlug.de/code/ersteller/schema/ent/generalqueue"
|
||||
"git.gorlug.de/code/ersteller/schema/ent/generalqueuestate"
|
||||
)
|
||||
@@ -23,6 +24,8 @@ type Client struct {
|
||||
config
|
||||
// Schema is the client for creating, migrating and dropping schema.
|
||||
Schema *migrate.Schema
|
||||
// Event is the client for interacting with the Event builders.
|
||||
Event *EventClient
|
||||
// GeneralQueue is the client for interacting with the GeneralQueue builders.
|
||||
GeneralQueue *GeneralQueueClient
|
||||
// GeneralQueueState is the client for interacting with the GeneralQueueState builders.
|
||||
@@ -38,6 +41,7 @@ func NewClient(opts ...Option) *Client {
|
||||
|
||||
func (c *Client) init() {
|
||||
c.Schema = migrate.NewSchema(c.driver)
|
||||
c.Event = NewEventClient(c.config)
|
||||
c.GeneralQueue = NewGeneralQueueClient(c.config)
|
||||
c.GeneralQueueState = NewGeneralQueueStateClient(c.config)
|
||||
}
|
||||
@@ -132,6 +136,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
|
||||
return &Tx{
|
||||
ctx: ctx,
|
||||
config: cfg,
|
||||
Event: NewEventClient(cfg),
|
||||
GeneralQueue: NewGeneralQueueClient(cfg),
|
||||
GeneralQueueState: NewGeneralQueueStateClient(cfg),
|
||||
}, nil
|
||||
@@ -153,6 +158,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
|
||||
return &Tx{
|
||||
ctx: ctx,
|
||||
config: cfg,
|
||||
Event: NewEventClient(cfg),
|
||||
GeneralQueue: NewGeneralQueueClient(cfg),
|
||||
GeneralQueueState: NewGeneralQueueStateClient(cfg),
|
||||
}, nil
|
||||
@@ -161,7 +167,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
|
||||
// Debug returns a new debug-client. It's used to get verbose logging on specific operations.
|
||||
//
|
||||
// client.Debug().
|
||||
// GeneralQueue.
|
||||
// Event.
|
||||
// Query().
|
||||
// Count(ctx)
|
||||
func (c *Client) Debug() *Client {
|
||||
@@ -183,6 +189,7 @@ func (c *Client) Close() error {
|
||||
// Use adds the mutation hooks to all the entity clients.
|
||||
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
|
||||
func (c *Client) Use(hooks ...Hook) {
|
||||
c.Event.Use(hooks...)
|
||||
c.GeneralQueue.Use(hooks...)
|
||||
c.GeneralQueueState.Use(hooks...)
|
||||
}
|
||||
@@ -190,6 +197,7 @@ func (c *Client) Use(hooks ...Hook) {
|
||||
// Intercept adds the query interceptors to all the entity clients.
|
||||
// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
|
||||
func (c *Client) Intercept(interceptors ...Interceptor) {
|
||||
c.Event.Intercept(interceptors...)
|
||||
c.GeneralQueue.Intercept(interceptors...)
|
||||
c.GeneralQueueState.Intercept(interceptors...)
|
||||
}
|
||||
@@ -197,6 +205,8 @@ func (c *Client) Intercept(interceptors ...Interceptor) {
|
||||
// Mutate implements the ent.Mutator interface.
|
||||
func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
||||
switch m := m.(type) {
|
||||
case *EventMutation:
|
||||
return c.Event.mutate(ctx, m)
|
||||
case *GeneralQueueMutation:
|
||||
return c.GeneralQueue.mutate(ctx, m)
|
||||
case *GeneralQueueStateMutation:
|
||||
@@ -206,6 +216,139 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// EventClient is a client for the Event schema.
|
||||
type EventClient struct {
|
||||
config
|
||||
}
|
||||
|
||||
// NewEventClient returns a client for the Event from the given config.
|
||||
func NewEventClient(c config) *EventClient {
|
||||
return &EventClient{config: c}
|
||||
}
|
||||
|
||||
// Use adds a list of mutation hooks to the hooks stack.
|
||||
// A call to `Use(f, g, h)` equals to `event.Hooks(f(g(h())))`.
|
||||
func (c *EventClient) Use(hooks ...Hook) {
|
||||
c.hooks.Event = append(c.hooks.Event, hooks...)
|
||||
}
|
||||
|
||||
// Intercept adds a list of query interceptors to the interceptors stack.
|
||||
// A call to `Intercept(f, g, h)` equals to `event.Intercept(f(g(h())))`.
|
||||
func (c *EventClient) Intercept(interceptors ...Interceptor) {
|
||||
c.inters.Event = append(c.inters.Event, interceptors...)
|
||||
}
|
||||
|
||||
// Create returns a builder for creating a Event entity.
|
||||
func (c *EventClient) Create() *EventCreate {
|
||||
mutation := newEventMutation(c.config, OpCreate)
|
||||
return &EventCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// CreateBulk returns a builder for creating a bulk of Event entities.
|
||||
func (c *EventClient) CreateBulk(builders ...*EventCreate) *EventCreateBulk {
|
||||
return &EventCreateBulk{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 *EventClient) MapCreateBulk(slice any, setFunc func(*EventCreate, int)) *EventCreateBulk {
|
||||
rv := reflect.ValueOf(slice)
|
||||
if rv.Kind() != reflect.Slice {
|
||||
return &EventCreateBulk{err: fmt.Errorf("calling to EventClient.MapCreateBulk with wrong type %T, need slice", slice)}
|
||||
}
|
||||
builders := make([]*EventCreate, rv.Len())
|
||||
for i := 0; i < rv.Len(); i++ {
|
||||
builders[i] = c.Create()
|
||||
setFunc(builders[i], i)
|
||||
}
|
||||
return &EventCreateBulk{config: c.config, builders: builders}
|
||||
}
|
||||
|
||||
// Update returns an update builder for Event.
|
||||
func (c *EventClient) Update() *EventUpdate {
|
||||
mutation := newEventMutation(c.config, OpUpdate)
|
||||
return &EventUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOne returns an update builder for the given entity.
|
||||
func (c *EventClient) UpdateOne(_m *Event) *EventUpdateOne {
|
||||
mutation := newEventMutation(c.config, OpUpdateOne, withEvent(_m))
|
||||
return &EventUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// UpdateOneID returns an update builder for the given id.
|
||||
func (c *EventClient) UpdateOneID(id int) *EventUpdateOne {
|
||||
mutation := newEventMutation(c.config, OpUpdateOne, withEventID(id))
|
||||
return &EventUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// Delete returns a delete builder for Event.
|
||||
func (c *EventClient) Delete() *EventDelete {
|
||||
mutation := newEventMutation(c.config, OpDelete)
|
||||
return &EventDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
|
||||
}
|
||||
|
||||
// DeleteOne returns a builder for deleting the given entity.
|
||||
func (c *EventClient) DeleteOne(_m *Event) *EventDeleteOne {
|
||||
return c.DeleteOneID(_m.ID)
|
||||
}
|
||||
|
||||
// DeleteOneID returns a builder for deleting the given entity by its id.
|
||||
func (c *EventClient) DeleteOneID(id int) *EventDeleteOne {
|
||||
builder := c.Delete().Where(event.ID(id))
|
||||
builder.mutation.id = &id
|
||||
builder.mutation.op = OpDeleteOne
|
||||
return &EventDeleteOne{builder}
|
||||
}
|
||||
|
||||
// Query returns a query builder for Event.
|
||||
func (c *EventClient) Query() *EventQuery {
|
||||
return &EventQuery{
|
||||
config: c.config,
|
||||
ctx: &QueryContext{Type: TypeEvent},
|
||||
inters: c.Interceptors(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns a Event entity by its id.
|
||||
func (c *EventClient) Get(ctx context.Context, id int) (*Event, error) {
|
||||
return c.Query().Where(event.ID(id)).Only(ctx)
|
||||
}
|
||||
|
||||
// GetX is like Get, but panics if an error occurs.
|
||||
func (c *EventClient) GetX(ctx context.Context, id int) *Event {
|
||||
obj, err := c.Get(ctx, id)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return obj
|
||||
}
|
||||
|
||||
// Hooks returns the client hooks.
|
||||
func (c *EventClient) Hooks() []Hook {
|
||||
return c.hooks.Event
|
||||
}
|
||||
|
||||
// Interceptors returns the client interceptors.
|
||||
func (c *EventClient) Interceptors() []Interceptor {
|
||||
return c.inters.Event
|
||||
}
|
||||
|
||||
func (c *EventClient) mutate(ctx context.Context, m *EventMutation) (Value, error) {
|
||||
switch m.Op() {
|
||||
case OpCreate:
|
||||
return (&EventCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdate:
|
||||
return (&EventUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpUpdateOne:
|
||||
return (&EventUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
|
||||
case OpDelete, OpDeleteOne:
|
||||
return (&EventDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
|
||||
default:
|
||||
return nil, fmt.Errorf("ent: unknown Event mutation op: %q", m.Op())
|
||||
}
|
||||
}
|
||||
|
||||
// GeneralQueueClient is a client for the GeneralQueue schema.
|
||||
type GeneralQueueClient struct {
|
||||
config
|
||||
@@ -475,9 +618,9 @@ func (c *GeneralQueueStateClient) mutate(ctx context.Context, m *GeneralQueueSta
|
||||
// hooks and interceptors per client, for fast access.
|
||||
type (
|
||||
hooks struct {
|
||||
GeneralQueue, GeneralQueueState []ent.Hook
|
||||
Event, GeneralQueue, GeneralQueueState []ent.Hook
|
||||
}
|
||||
inters struct {
|
||||
GeneralQueue, GeneralQueueState []ent.Interceptor
|
||||
Event, GeneralQueue, GeneralQueueState []ent.Interceptor
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user