todo_create.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "todo/ent/todo"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. )
  12. // TodoCreate is the builder for creating a Todo entity.
  13. type TodoCreate struct {
  14. config
  15. mutation *TodoMutation
  16. hooks []Hook
  17. }
  18. // SetText sets the "text" field.
  19. func (tc *TodoCreate) SetText(s string) *TodoCreate {
  20. tc.mutation.SetText(s)
  21. return tc
  22. }
  23. // SetCreatedAt sets the "created_at" field.
  24. func (tc *TodoCreate) SetCreatedAt(t time.Time) *TodoCreate {
  25. tc.mutation.SetCreatedAt(t)
  26. return tc
  27. }
  28. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  29. func (tc *TodoCreate) SetNillableCreatedAt(t *time.Time) *TodoCreate {
  30. if t != nil {
  31. tc.SetCreatedAt(*t)
  32. }
  33. return tc
  34. }
  35. // SetStatus sets the "status" field.
  36. func (tc *TodoCreate) SetStatus(t todo.Status) *TodoCreate {
  37. tc.mutation.SetStatus(t)
  38. return tc
  39. }
  40. // SetNillableStatus sets the "status" field if the given value is not nil.
  41. func (tc *TodoCreate) SetNillableStatus(t *todo.Status) *TodoCreate {
  42. if t != nil {
  43. tc.SetStatus(*t)
  44. }
  45. return tc
  46. }
  47. // SetPriority sets the "priority" field.
  48. func (tc *TodoCreate) SetPriority(i int) *TodoCreate {
  49. tc.mutation.SetPriority(i)
  50. return tc
  51. }
  52. // SetNillablePriority sets the "priority" field if the given value is not nil.
  53. func (tc *TodoCreate) SetNillablePriority(i *int) *TodoCreate {
  54. if i != nil {
  55. tc.SetPriority(*i)
  56. }
  57. return tc
  58. }
  59. // AddChildIDs adds the "children" edge to the Todo entity by IDs.
  60. func (tc *TodoCreate) AddChildIDs(ids ...int) *TodoCreate {
  61. tc.mutation.AddChildIDs(ids...)
  62. return tc
  63. }
  64. // AddChildren adds the "children" edges to the Todo entity.
  65. func (tc *TodoCreate) AddChildren(t ...*Todo) *TodoCreate {
  66. ids := make([]int, len(t))
  67. for i := range t {
  68. ids[i] = t[i].ID
  69. }
  70. return tc.AddChildIDs(ids...)
  71. }
  72. // SetParentID sets the "parent" edge to the Todo entity by ID.
  73. func (tc *TodoCreate) SetParentID(id int) *TodoCreate {
  74. tc.mutation.SetParentID(id)
  75. return tc
  76. }
  77. // SetNillableParentID sets the "parent" edge to the Todo entity by ID if the given value is not nil.
  78. func (tc *TodoCreate) SetNillableParentID(id *int) *TodoCreate {
  79. if id != nil {
  80. tc = tc.SetParentID(*id)
  81. }
  82. return tc
  83. }
  84. // SetParent sets the "parent" edge to the Todo entity.
  85. func (tc *TodoCreate) SetParent(t *Todo) *TodoCreate {
  86. return tc.SetParentID(t.ID)
  87. }
  88. // Mutation returns the TodoMutation object of the builder.
  89. func (tc *TodoCreate) Mutation() *TodoMutation {
  90. return tc.mutation
  91. }
  92. // Save creates the Todo in the database.
  93. func (tc *TodoCreate) Save(ctx context.Context) (*Todo, error) {
  94. var (
  95. err error
  96. node *Todo
  97. )
  98. tc.defaults()
  99. if len(tc.hooks) == 0 {
  100. if err = tc.check(); err != nil {
  101. return nil, err
  102. }
  103. node, err = tc.sqlSave(ctx)
  104. } else {
  105. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  106. mutation, ok := m.(*TodoMutation)
  107. if !ok {
  108. return nil, fmt.Errorf("unexpected mutation type %T", m)
  109. }
  110. if err = tc.check(); err != nil {
  111. return nil, err
  112. }
  113. tc.mutation = mutation
  114. if node, err = tc.sqlSave(ctx); err != nil {
  115. return nil, err
  116. }
  117. mutation.id = &node.ID
  118. mutation.done = true
  119. return node, err
  120. })
  121. for i := len(tc.hooks) - 1; i >= 0; i-- {
  122. if tc.hooks[i] == nil {
  123. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  124. }
  125. mut = tc.hooks[i](mut)
  126. }
  127. if _, err := mut.Mutate(ctx, tc.mutation); err != nil {
  128. return nil, err
  129. }
  130. }
  131. return node, err
  132. }
  133. // SaveX calls Save and panics if Save returns an error.
  134. func (tc *TodoCreate) SaveX(ctx context.Context) *Todo {
  135. v, err := tc.Save(ctx)
  136. if err != nil {
  137. panic(err)
  138. }
  139. return v
  140. }
  141. // Exec executes the query.
  142. func (tc *TodoCreate) Exec(ctx context.Context) error {
  143. _, err := tc.Save(ctx)
  144. return err
  145. }
  146. // ExecX is like Exec, but panics if an error occurs.
  147. func (tc *TodoCreate) ExecX(ctx context.Context) {
  148. if err := tc.Exec(ctx); err != nil {
  149. panic(err)
  150. }
  151. }
  152. // defaults sets the default values of the builder before save.
  153. func (tc *TodoCreate) defaults() {
  154. if _, ok := tc.mutation.CreatedAt(); !ok {
  155. v := todo.DefaultCreatedAt()
  156. tc.mutation.SetCreatedAt(v)
  157. }
  158. if _, ok := tc.mutation.Status(); !ok {
  159. v := todo.DefaultStatus
  160. tc.mutation.SetStatus(v)
  161. }
  162. if _, ok := tc.mutation.Priority(); !ok {
  163. v := todo.DefaultPriority
  164. tc.mutation.SetPriority(v)
  165. }
  166. }
  167. // check runs all checks and user-defined validators on the builder.
  168. func (tc *TodoCreate) check() error {
  169. if _, ok := tc.mutation.Text(); !ok {
  170. return &ValidationError{Name: "text", err: errors.New(`ent: missing required field "Todo.text"`)}
  171. }
  172. if v, ok := tc.mutation.Text(); ok {
  173. if err := todo.TextValidator(v); err != nil {
  174. return &ValidationError{Name: "text", err: fmt.Errorf(`ent: validator failed for field "Todo.text": %w`, err)}
  175. }
  176. }
  177. if _, ok := tc.mutation.CreatedAt(); !ok {
  178. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Todo.created_at"`)}
  179. }
  180. if _, ok := tc.mutation.Status(); !ok {
  181. return &ValidationError{Name: "status", err: errors.New(`ent: missing required field "Todo.status"`)}
  182. }
  183. if v, ok := tc.mutation.Status(); ok {
  184. if err := todo.StatusValidator(v); err != nil {
  185. return &ValidationError{Name: "status", err: fmt.Errorf(`ent: validator failed for field "Todo.status": %w`, err)}
  186. }
  187. }
  188. if _, ok := tc.mutation.Priority(); !ok {
  189. return &ValidationError{Name: "priority", err: errors.New(`ent: missing required field "Todo.priority"`)}
  190. }
  191. return nil
  192. }
  193. func (tc *TodoCreate) sqlSave(ctx context.Context) (*Todo, error) {
  194. _node, _spec := tc.createSpec()
  195. if err := sqlgraph.CreateNode(ctx, tc.driver, _spec); err != nil {
  196. if sqlgraph.IsConstraintError(err) {
  197. err = &ConstraintError{err.Error(), err}
  198. }
  199. return nil, err
  200. }
  201. id := _spec.ID.Value.(int64)
  202. _node.ID = int(id)
  203. return _node, nil
  204. }
  205. func (tc *TodoCreate) createSpec() (*Todo, *sqlgraph.CreateSpec) {
  206. var (
  207. _node = &Todo{config: tc.config}
  208. _spec = &sqlgraph.CreateSpec{
  209. Table: todo.Table,
  210. ID: &sqlgraph.FieldSpec{
  211. Type: field.TypeInt,
  212. Column: todo.FieldID,
  213. },
  214. }
  215. )
  216. if value, ok := tc.mutation.Text(); ok {
  217. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  218. Type: field.TypeString,
  219. Value: value,
  220. Column: todo.FieldText,
  221. })
  222. _node.Text = value
  223. }
  224. if value, ok := tc.mutation.CreatedAt(); ok {
  225. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  226. Type: field.TypeTime,
  227. Value: value,
  228. Column: todo.FieldCreatedAt,
  229. })
  230. _node.CreatedAt = value
  231. }
  232. if value, ok := tc.mutation.Status(); ok {
  233. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  234. Type: field.TypeEnum,
  235. Value: value,
  236. Column: todo.FieldStatus,
  237. })
  238. _node.Status = value
  239. }
  240. if value, ok := tc.mutation.Priority(); ok {
  241. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  242. Type: field.TypeInt,
  243. Value: value,
  244. Column: todo.FieldPriority,
  245. })
  246. _node.Priority = value
  247. }
  248. if nodes := tc.mutation.ChildrenIDs(); len(nodes) > 0 {
  249. edge := &sqlgraph.EdgeSpec{
  250. Rel: sqlgraph.O2M,
  251. Inverse: true,
  252. Table: todo.ChildrenTable,
  253. Columns: []string{todo.ChildrenColumn},
  254. Bidi: false,
  255. Target: &sqlgraph.EdgeTarget{
  256. IDSpec: &sqlgraph.FieldSpec{
  257. Type: field.TypeInt,
  258. Column: todo.FieldID,
  259. },
  260. },
  261. }
  262. for _, k := range nodes {
  263. edge.Target.Nodes = append(edge.Target.Nodes, k)
  264. }
  265. _spec.Edges = append(_spec.Edges, edge)
  266. }
  267. if nodes := tc.mutation.ParentIDs(); len(nodes) > 0 {
  268. edge := &sqlgraph.EdgeSpec{
  269. Rel: sqlgraph.M2O,
  270. Inverse: false,
  271. Table: todo.ParentTable,
  272. Columns: []string{todo.ParentColumn},
  273. Bidi: false,
  274. Target: &sqlgraph.EdgeTarget{
  275. IDSpec: &sqlgraph.FieldSpec{
  276. Type: field.TypeInt,
  277. Column: todo.FieldID,
  278. },
  279. },
  280. }
  281. for _, k := range nodes {
  282. edge.Target.Nodes = append(edge.Target.Nodes, k)
  283. }
  284. _node.todo_parent = &nodes[0]
  285. _spec.Edges = append(_spec.Edges, edge)
  286. }
  287. return _node, _spec
  288. }
  289. // TodoCreateBulk is the builder for creating many Todo entities in bulk.
  290. type TodoCreateBulk struct {
  291. config
  292. builders []*TodoCreate
  293. }
  294. // Save creates the Todo entities in the database.
  295. func (tcb *TodoCreateBulk) Save(ctx context.Context) ([]*Todo, error) {
  296. specs := make([]*sqlgraph.CreateSpec, len(tcb.builders))
  297. nodes := make([]*Todo, len(tcb.builders))
  298. mutators := make([]Mutator, len(tcb.builders))
  299. for i := range tcb.builders {
  300. func(i int, root context.Context) {
  301. builder := tcb.builders[i]
  302. builder.defaults()
  303. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  304. mutation, ok := m.(*TodoMutation)
  305. if !ok {
  306. return nil, fmt.Errorf("unexpected mutation type %T", m)
  307. }
  308. if err := builder.check(); err != nil {
  309. return nil, err
  310. }
  311. builder.mutation = mutation
  312. nodes[i], specs[i] = builder.createSpec()
  313. var err error
  314. if i < len(mutators)-1 {
  315. _, err = mutators[i+1].Mutate(root, tcb.builders[i+1].mutation)
  316. } else {
  317. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  318. // Invoke the actual operation on the latest mutation in the chain.
  319. if err = sqlgraph.BatchCreate(ctx, tcb.driver, spec); err != nil {
  320. if sqlgraph.IsConstraintError(err) {
  321. err = &ConstraintError{err.Error(), err}
  322. }
  323. }
  324. }
  325. if err != nil {
  326. return nil, err
  327. }
  328. mutation.id = &nodes[i].ID
  329. mutation.done = true
  330. if specs[i].ID.Value != nil {
  331. id := specs[i].ID.Value.(int64)
  332. nodes[i].ID = int(id)
  333. }
  334. return nodes[i], nil
  335. })
  336. for i := len(builder.hooks) - 1; i >= 0; i-- {
  337. mut = builder.hooks[i](mut)
  338. }
  339. mutators[i] = mut
  340. }(i, ctx)
  341. }
  342. if len(mutators) > 0 {
  343. if _, err := mutators[0].Mutate(ctx, tcb.builders[0].mutation); err != nil {
  344. return nil, err
  345. }
  346. }
  347. return nodes, nil
  348. }
  349. // SaveX is like Save, but panics if an error occurs.
  350. func (tcb *TodoCreateBulk) SaveX(ctx context.Context) []*Todo {
  351. v, err := tcb.Save(ctx)
  352. if err != nil {
  353. panic(err)
  354. }
  355. return v
  356. }
  357. // Exec executes the query.
  358. func (tcb *TodoCreateBulk) Exec(ctx context.Context) error {
  359. _, err := tcb.Save(ctx)
  360. return err
  361. }
  362. // ExecX is like Exec, but panics if an error occurs.
  363. func (tcb *TodoCreateBulk) ExecX(ctx context.Context) {
  364. if err := tcb.Exec(ctx); err != nil {
  365. panic(err)
  366. }
  367. }