Add todo schema

This commit is contained in:
Achim Rohn
2025-09-18 09:59:40 +02:00
parent b788fb4898
commit 1c42c7dd7a
17 changed files with 2832 additions and 2 deletions
+161 -2
View File
@@ -12,6 +12,7 @@ import (
"ersteller-lib/starter/ent/migrate"
"ersteller-lib/starter/ent/googleauth"
"ersteller-lib/starter/ent/todo"
"ersteller-lib/starter/ent/user"
"entgo.io/ent"
@@ -27,6 +28,8 @@ type Client struct {
Schema *migrate.Schema
// GoogleAuth is the client for interacting with the GoogleAuth builders.
GoogleAuth *GoogleAuthClient
// Todo is the client for interacting with the Todo builders.
Todo *TodoClient
// User is the client for interacting with the User builders.
User *UserClient
}
@@ -41,6 +44,7 @@ func NewClient(opts ...Option) *Client {
func (c *Client) init() {
c.Schema = migrate.NewSchema(c.driver)
c.GoogleAuth = NewGoogleAuthClient(c.config)
c.Todo = NewTodoClient(c.config)
c.User = NewUserClient(c.config)
}
@@ -135,6 +139,7 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) {
ctx: ctx,
config: cfg,
GoogleAuth: NewGoogleAuthClient(cfg),
Todo: NewTodoClient(cfg),
User: NewUserClient(cfg),
}, nil
}
@@ -156,6 +161,7 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
ctx: ctx,
config: cfg,
GoogleAuth: NewGoogleAuthClient(cfg),
Todo: NewTodoClient(cfg),
User: NewUserClient(cfg),
}, nil
}
@@ -186,6 +192,7 @@ func (c *Client) Close() error {
// In order to add hooks to a specific client, call: `client.Node.Use(...)`.
func (c *Client) Use(hooks ...Hook) {
c.GoogleAuth.Use(hooks...)
c.Todo.Use(hooks...)
c.User.Use(hooks...)
}
@@ -193,6 +200,7 @@ func (c *Client) Use(hooks ...Hook) {
// In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`.
func (c *Client) Intercept(interceptors ...Interceptor) {
c.GoogleAuth.Intercept(interceptors...)
c.Todo.Intercept(interceptors...)
c.User.Intercept(interceptors...)
}
@@ -201,6 +209,8 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) {
switch m := m.(type) {
case *GoogleAuthMutation:
return c.GoogleAuth.mutate(ctx, m)
case *TodoMutation:
return c.Todo.mutate(ctx, m)
case *UserMutation:
return c.User.mutate(ctx, m)
default:
@@ -357,6 +367,155 @@ func (c *GoogleAuthClient) mutate(ctx context.Context, m *GoogleAuthMutation) (V
}
}
// TodoClient is a client for the Todo schema.
type TodoClient struct {
config
}
// NewTodoClient returns a client for the Todo from the given config.
func NewTodoClient(c config) *TodoClient {
return &TodoClient{config: c}
}
// Use adds a list of mutation hooks to the hooks stack.
// A call to `Use(f, g, h)` equals to `todo.Hooks(f(g(h())))`.
func (c *TodoClient) Use(hooks ...Hook) {
c.hooks.Todo = append(c.hooks.Todo, hooks...)
}
// Intercept adds a list of query interceptors to the interceptors stack.
// A call to `Intercept(f, g, h)` equals to `todo.Intercept(f(g(h())))`.
func (c *TodoClient) Intercept(interceptors ...Interceptor) {
c.inters.Todo = append(c.inters.Todo, interceptors...)
}
// Create returns a builder for creating a Todo entity.
func (c *TodoClient) Create() *TodoCreate {
mutation := newTodoMutation(c.config, OpCreate)
return &TodoCreate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// CreateBulk returns a builder for creating a bulk of Todo entities.
func (c *TodoClient) CreateBulk(builders ...*TodoCreate) *TodoCreateBulk {
return &TodoCreateBulk{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 *TodoClient) MapCreateBulk(slice any, setFunc func(*TodoCreate, int)) *TodoCreateBulk {
rv := reflect.ValueOf(slice)
if rv.Kind() != reflect.Slice {
return &TodoCreateBulk{err: fmt.Errorf("calling to TodoClient.MapCreateBulk with wrong type %T, need slice", slice)}
}
builders := make([]*TodoCreate, rv.Len())
for i := 0; i < rv.Len(); i++ {
builders[i] = c.Create()
setFunc(builders[i], i)
}
return &TodoCreateBulk{config: c.config, builders: builders}
}
// Update returns an update builder for Todo.
func (c *TodoClient) Update() *TodoUpdate {
mutation := newTodoMutation(c.config, OpUpdate)
return &TodoUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOne returns an update builder for the given entity.
func (c *TodoClient) UpdateOne(_m *Todo) *TodoUpdateOne {
mutation := newTodoMutation(c.config, OpUpdateOne, withTodo(_m))
return &TodoUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// UpdateOneID returns an update builder for the given id.
func (c *TodoClient) UpdateOneID(id int) *TodoUpdateOne {
mutation := newTodoMutation(c.config, OpUpdateOne, withTodoID(id))
return &TodoUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// Delete returns a delete builder for Todo.
func (c *TodoClient) Delete() *TodoDelete {
mutation := newTodoMutation(c.config, OpDelete)
return &TodoDelete{config: c.config, hooks: c.Hooks(), mutation: mutation}
}
// DeleteOne returns a builder for deleting the given entity.
func (c *TodoClient) DeleteOne(_m *Todo) *TodoDeleteOne {
return c.DeleteOneID(_m.ID)
}
// DeleteOneID returns a builder for deleting the given entity by its id.
func (c *TodoClient) DeleteOneID(id int) *TodoDeleteOne {
builder := c.Delete().Where(todo.ID(id))
builder.mutation.id = &id
builder.mutation.op = OpDeleteOne
return &TodoDeleteOne{builder}
}
// Query returns a query builder for Todo.
func (c *TodoClient) Query() *TodoQuery {
return &TodoQuery{
config: c.config,
ctx: &QueryContext{Type: TypeTodo},
inters: c.Interceptors(),
}
}
// Get returns a Todo entity by its id.
func (c *TodoClient) Get(ctx context.Context, id int) (*Todo, error) {
return c.Query().Where(todo.ID(id)).Only(ctx)
}
// GetX is like Get, but panics if an error occurs.
func (c *TodoClient) GetX(ctx context.Context, id int) *Todo {
obj, err := c.Get(ctx, id)
if err != nil {
panic(err)
}
return obj
}
// QueryUser queries the user edge of a Todo.
func (c *TodoClient) QueryUser(_m *Todo) *UserQuery {
query := (&UserClient{config: c.config}).Query()
query.path = func(context.Context) (fromV *sql.Selector, _ error) {
id := _m.ID
step := sqlgraph.NewStep(
sqlgraph.From(todo.Table, todo.FieldID, id),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, false, todo.UserTable, todo.UserColumn),
)
fromV = sqlgraph.Neighbors(_m.driver.Dialect(), step)
return fromV, nil
}
return query
}
// Hooks returns the client hooks.
func (c *TodoClient) Hooks() []Hook {
return c.hooks.Todo
}
// Interceptors returns the client interceptors.
func (c *TodoClient) Interceptors() []Interceptor {
return c.inters.Todo
}
func (c *TodoClient) mutate(ctx context.Context, m *TodoMutation) (Value, error) {
switch m.Op() {
case OpCreate:
return (&TodoCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdate:
return (&TodoUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpUpdateOne:
return (&TodoUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx)
case OpDelete, OpDeleteOne:
return (&TodoDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx)
default:
return nil, fmt.Errorf("ent: unknown Todo mutation op: %q", m.Op())
}
}
// UserClient is a client for the User schema.
type UserClient struct {
config
@@ -493,9 +652,9 @@ func (c *UserClient) mutate(ctx context.Context, m *UserMutation) (Value, error)
// hooks and interceptors per client, for fast access.
type (
hooks struct {
GoogleAuth, User []ent.Hook
GoogleAuth, Todo, User []ent.Hook
}
inters struct {
GoogleAuth, User []ent.Interceptor
GoogleAuth, Todo, User []ent.Interceptor
}
)