feat(schema): 添加基础混入定义公共时间字段
This commit is contained in:
1 parent
5281459fcb
commit
fe86c081f4
16 files changed
+1715
-14
No files matched your search
@@ -5,7 +5,9 @@ package ent
|
||||
import (
|
||||
"EnGin/internal/ent/user"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/schema/field"
|
||||
@@ -18,6 +20,88 @@ type UserCreate struct {
|
||||
hooks []Hook
|
||||
}
|
||||
|
||||
// SetCreatedAt sets the "created_at" field.
|
||||
func (_c *UserCreate) SetCreatedAt(v time.Time) *UserCreate {
|
||||
_c.mutation.SetCreatedAt(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableCreatedAt(v *time.Time) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetCreatedAt(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetUpdatedAt sets the "updated_at" field.
|
||||
func (_c *UserCreate) SetUpdatedAt(v time.Time) *UserCreate {
|
||||
_c.mutation.SetUpdatedAt(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableUpdatedAt(v *time.Time) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetUpdatedAt(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetUsername sets the "username" field.
|
||||
func (_c *UserCreate) SetUsername(v string) *UserCreate {
|
||||
_c.mutation.SetUsername(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetPassword sets the "password" field.
|
||||
func (_c *UserCreate) SetPassword(v string) *UserCreate {
|
||||
_c.mutation.SetPassword(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetEmail sets the "email" field.
|
||||
func (_c *UserCreate) SetEmail(v string) *UserCreate {
|
||||
_c.mutation.SetEmail(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableEmail sets the "email" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableEmail(v *string) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetEmail(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetIP sets the "ip" field.
|
||||
func (_c *UserCreate) SetIP(v string) *UserCreate {
|
||||
_c.mutation.SetIP(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableIP sets the "ip" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableIP(v *string) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetIP(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetAddr sets the "addr" field.
|
||||
func (_c *UserCreate) SetAddr(v string) *UserCreate {
|
||||
_c.mutation.SetAddr(v)
|
||||
return _c
|
||||
}
|
||||
|
||||
// SetNillableAddr sets the "addr" field if the given value is not nil.
|
||||
func (_c *UserCreate) SetNillableAddr(v *string) *UserCreate {
|
||||
if v != nil {
|
||||
_c.SetAddr(*v)
|
||||
}
|
||||
return _c
|
||||
}
|
||||
|
||||
// Mutation returns the UserMutation object of the builder.
|
||||
func (_c *UserCreate) Mutation() *UserMutation {
|
||||
return _c.mutation
|
||||
@@ -25,6 +109,7 @@ func (_c *UserCreate) Mutation() *UserMutation {
|
||||
|
||||
// Save creates the User in the database.
|
||||
func (_c *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
_c.defaults()
|
||||
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
|
||||
}
|
||||
|
||||
@@ -50,8 +135,52 @@ func (_c *UserCreate) ExecX(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// defaults sets the default values of the builder before save.
|
||||
func (_c *UserCreate) defaults() {
|
||||
if _, ok := _c.mutation.CreatedAt(); !ok {
|
||||
v := user.DefaultCreatedAt()
|
||||
_c.mutation.SetCreatedAt(v)
|
||||
}
|
||||
if _, ok := _c.mutation.UpdatedAt(); !ok {
|
||||
v := user.DefaultUpdatedAt()
|
||||
_c.mutation.SetUpdatedAt(v)
|
||||
}
|
||||
}
|
||||
|
||||
// check runs all checks and user-defined validators on the builder.
|
||||
func (_c *UserCreate) check() error {
|
||||
if _, ok := _c.mutation.CreatedAt(); !ok {
|
||||
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "User.created_at"`)}
|
||||
}
|
||||
if _, ok := _c.mutation.UpdatedAt(); !ok {
|
||||
return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "User.updated_at"`)}
|
||||
}
|
||||
if _, ok := _c.mutation.Username(); !ok {
|
||||
return &ValidationError{Name: "username", err: errors.New(`ent: missing required field "User.username"`)}
|
||||
}
|
||||
if v, ok := _c.mutation.Username(); ok {
|
||||
if err := user.UsernameValidator(v); err != nil {
|
||||
return &ValidationError{Name: "username", err: fmt.Errorf(`ent: validator failed for field "User.username": %w`, err)}
|
||||
}
|
||||
}
|
||||
if _, ok := _c.mutation.Password(); !ok {
|
||||
return &ValidationError{Name: "password", err: errors.New(`ent: missing required field "User.password"`)}
|
||||
}
|
||||
if v, ok := _c.mutation.Email(); ok {
|
||||
if err := user.EmailValidator(v); err != nil {
|
||||
return &ValidationError{Name: "email", err: fmt.Errorf(`ent: validator failed for field "User.email": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _c.mutation.IP(); ok {
|
||||
if err := user.IPValidator(v); err != nil {
|
||||
return &ValidationError{Name: "ip", err: fmt.Errorf(`ent: validator failed for field "User.ip": %w`, err)}
|
||||
}
|
||||
}
|
||||
if v, ok := _c.mutation.Addr(); ok {
|
||||
if err := user.AddrValidator(v); err != nil {
|
||||
return &ValidationError{Name: "addr", err: fmt.Errorf(`ent: validator failed for field "User.addr": %w`, err)}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -78,6 +207,34 @@ func (_c *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
|
||||
_node = &User{config: _c.config}
|
||||
_spec = sqlgraph.NewCreateSpec(user.Table, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt))
|
||||
)
|
||||
if value, ok := _c.mutation.CreatedAt(); ok {
|
||||
_spec.SetField(user.FieldCreatedAt, field.TypeTime, value)
|
||||
_node.CreatedAt = value
|
||||
}
|
||||
if value, ok := _c.mutation.UpdatedAt(); ok {
|
||||
_spec.SetField(user.FieldUpdatedAt, field.TypeTime, value)
|
||||
_node.UpdatedAt = value
|
||||
}
|
||||
if value, ok := _c.mutation.Username(); ok {
|
||||
_spec.SetField(user.FieldUsername, field.TypeString, value)
|
||||
_node.Username = value
|
||||
}
|
||||
if value, ok := _c.mutation.Password(); ok {
|
||||
_spec.SetField(user.FieldPassword, field.TypeString, value)
|
||||
_node.Password = value
|
||||
}
|
||||
if value, ok := _c.mutation.Email(); ok {
|
||||
_spec.SetField(user.FieldEmail, field.TypeString, value)
|
||||
_node.Email = value
|
||||
}
|
||||
if value, ok := _c.mutation.IP(); ok {
|
||||
_spec.SetField(user.FieldIP, field.TypeString, value)
|
||||
_node.IP = value
|
||||
}
|
||||
if value, ok := _c.mutation.Addr(); ok {
|
||||
_spec.SetField(user.FieldAddr, field.TypeString, value)
|
||||
_node.Addr = value
|
||||
}
|
||||
return _node, _spec
|
||||
}
|
||||
|
||||
@@ -99,6 +256,7 @@ func (_c *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
|
||||
for i := range _c.builders {
|
||||
func(i int, root context.Context) {
|
||||
builder := _c.builders[i]
|
||||
builder.defaults()
|
||||
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
||||
mutation, ok := m.(*UserMutation)
|
||||
if !ok {
|
||||
|
||||
Reference in new issue
Block a user