group_update.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. )
  14. // GroupUpdate is the builder for updating Group entities.
  15. type GroupUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *GroupMutation
  19. }
  20. // Where appends a list predicates to the GroupUpdate builder.
  21. func (gu *GroupUpdate) Where(ps ...predicate.Group) *GroupUpdate {
  22. gu.mutation.Where(ps...)
  23. return gu
  24. }
  25. // SetName sets the "name" field.
  26. func (gu *GroupUpdate) SetName(s string) *GroupUpdate {
  27. gu.mutation.SetName(s)
  28. return gu
  29. }
  30. // AddUserIDs adds the "users" edge to the User entity by IDs.
  31. func (gu *GroupUpdate) AddUserIDs(ids ...int) *GroupUpdate {
  32. gu.mutation.AddUserIDs(ids...)
  33. return gu
  34. }
  35. // AddUsers adds the "users" edges to the User entity.
  36. func (gu *GroupUpdate) AddUsers(u ...*User) *GroupUpdate {
  37. ids := make([]int, len(u))
  38. for i := range u {
  39. ids[i] = u[i].ID
  40. }
  41. return gu.AddUserIDs(ids...)
  42. }
  43. // Mutation returns the GroupMutation object of the builder.
  44. func (gu *GroupUpdate) Mutation() *GroupMutation {
  45. return gu.mutation
  46. }
  47. // ClearUsers clears all "users" edges to the User entity.
  48. func (gu *GroupUpdate) ClearUsers() *GroupUpdate {
  49. gu.mutation.ClearUsers()
  50. return gu
  51. }
  52. // RemoveUserIDs removes the "users" edge to User entities by IDs.
  53. func (gu *GroupUpdate) RemoveUserIDs(ids ...int) *GroupUpdate {
  54. gu.mutation.RemoveUserIDs(ids...)
  55. return gu
  56. }
  57. // RemoveUsers removes "users" edges to User entities.
  58. func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate {
  59. ids := make([]int, len(u))
  60. for i := range u {
  61. ids[i] = u[i].ID
  62. }
  63. return gu.RemoveUserIDs(ids...)
  64. }
  65. // Save executes the query and returns the number of nodes affected by the update operation.
  66. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) {
  67. var (
  68. err error
  69. affected int
  70. )
  71. if len(gu.hooks) == 0 {
  72. if err = gu.check(); err != nil {
  73. return 0, err
  74. }
  75. affected, err = gu.sqlSave(ctx)
  76. } else {
  77. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  78. mutation, ok := m.(*GroupMutation)
  79. if !ok {
  80. return nil, fmt.Errorf("unexpected mutation type %T", m)
  81. }
  82. if err = gu.check(); err != nil {
  83. return 0, err
  84. }
  85. gu.mutation = mutation
  86. affected, err = gu.sqlSave(ctx)
  87. mutation.done = true
  88. return affected, err
  89. })
  90. for i := len(gu.hooks) - 1; i >= 0; i-- {
  91. if gu.hooks[i] == nil {
  92. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  93. }
  94. mut = gu.hooks[i](mut)
  95. }
  96. if _, err := mut.Mutate(ctx, gu.mutation); err != nil {
  97. return 0, err
  98. }
  99. }
  100. return affected, err
  101. }
  102. // SaveX is like Save, but panics if an error occurs.
  103. func (gu *GroupUpdate) SaveX(ctx context.Context) int {
  104. affected, err := gu.Save(ctx)
  105. if err != nil {
  106. panic(err)
  107. }
  108. return affected
  109. }
  110. // Exec executes the query.
  111. func (gu *GroupUpdate) Exec(ctx context.Context) error {
  112. _, err := gu.Save(ctx)
  113. return err
  114. }
  115. // ExecX is like Exec, but panics if an error occurs.
  116. func (gu *GroupUpdate) ExecX(ctx context.Context) {
  117. if err := gu.Exec(ctx); err != nil {
  118. panic(err)
  119. }
  120. }
  121. // check runs all checks and user-defined validators on the builder.
  122. func (gu *GroupUpdate) check() error {
  123. if v, ok := gu.mutation.Name(); ok {
  124. if err := group.NameValidator(v); err != nil {
  125. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)}
  126. }
  127. }
  128. return nil
  129. }
  130. func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) {
  131. _spec := &sqlgraph.UpdateSpec{
  132. Node: &sqlgraph.NodeSpec{
  133. Table: group.Table,
  134. Columns: group.Columns,
  135. ID: &sqlgraph.FieldSpec{
  136. Type: field.TypeInt,
  137. Column: group.FieldID,
  138. },
  139. },
  140. }
  141. if ps := gu.mutation.predicates; len(ps) > 0 {
  142. _spec.Predicate = func(selector *sql.Selector) {
  143. for i := range ps {
  144. ps[i](selector)
  145. }
  146. }
  147. }
  148. if value, ok := gu.mutation.Name(); ok {
  149. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  150. Type: field.TypeString,
  151. Value: value,
  152. Column: group.FieldName,
  153. })
  154. }
  155. if gu.mutation.UsersCleared() {
  156. edge := &sqlgraph.EdgeSpec{
  157. Rel: sqlgraph.M2M,
  158. Inverse: false,
  159. Table: group.UsersTable,
  160. Columns: group.UsersPrimaryKey,
  161. Bidi: false,
  162. Target: &sqlgraph.EdgeTarget{
  163. IDSpec: &sqlgraph.FieldSpec{
  164. Type: field.TypeInt,
  165. Column: user.FieldID,
  166. },
  167. },
  168. }
  169. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  170. }
  171. if nodes := gu.mutation.RemovedUsersIDs(); len(nodes) > 0 && !gu.mutation.UsersCleared() {
  172. edge := &sqlgraph.EdgeSpec{
  173. Rel: sqlgraph.M2M,
  174. Inverse: false,
  175. Table: group.UsersTable,
  176. Columns: group.UsersPrimaryKey,
  177. Bidi: false,
  178. Target: &sqlgraph.EdgeTarget{
  179. IDSpec: &sqlgraph.FieldSpec{
  180. Type: field.TypeInt,
  181. Column: user.FieldID,
  182. },
  183. },
  184. }
  185. for _, k := range nodes {
  186. edge.Target.Nodes = append(edge.Target.Nodes, k)
  187. }
  188. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  189. }
  190. if nodes := gu.mutation.UsersIDs(); len(nodes) > 0 {
  191. edge := &sqlgraph.EdgeSpec{
  192. Rel: sqlgraph.M2M,
  193. Inverse: false,
  194. Table: group.UsersTable,
  195. Columns: group.UsersPrimaryKey,
  196. Bidi: false,
  197. Target: &sqlgraph.EdgeTarget{
  198. IDSpec: &sqlgraph.FieldSpec{
  199. Type: field.TypeInt,
  200. Column: user.FieldID,
  201. },
  202. },
  203. }
  204. for _, k := range nodes {
  205. edge.Target.Nodes = append(edge.Target.Nodes, k)
  206. }
  207. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  208. }
  209. if n, err = sqlgraph.UpdateNodes(ctx, gu.driver, _spec); err != nil {
  210. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  211. err = &NotFoundError{group.Label}
  212. } else if sqlgraph.IsConstraintError(err) {
  213. err = &ConstraintError{err.Error(), err}
  214. }
  215. return 0, err
  216. }
  217. return n, nil
  218. }
  219. // GroupUpdateOne is the builder for updating a single Group entity.
  220. type GroupUpdateOne struct {
  221. config
  222. fields []string
  223. hooks []Hook
  224. mutation *GroupMutation
  225. }
  226. // SetName sets the "name" field.
  227. func (guo *GroupUpdateOne) SetName(s string) *GroupUpdateOne {
  228. guo.mutation.SetName(s)
  229. return guo
  230. }
  231. // AddUserIDs adds the "users" edge to the User entity by IDs.
  232. func (guo *GroupUpdateOne) AddUserIDs(ids ...int) *GroupUpdateOne {
  233. guo.mutation.AddUserIDs(ids...)
  234. return guo
  235. }
  236. // AddUsers adds the "users" edges to the User entity.
  237. func (guo *GroupUpdateOne) AddUsers(u ...*User) *GroupUpdateOne {
  238. ids := make([]int, len(u))
  239. for i := range u {
  240. ids[i] = u[i].ID
  241. }
  242. return guo.AddUserIDs(ids...)
  243. }
  244. // Mutation returns the GroupMutation object of the builder.
  245. func (guo *GroupUpdateOne) Mutation() *GroupMutation {
  246. return guo.mutation
  247. }
  248. // ClearUsers clears all "users" edges to the User entity.
  249. func (guo *GroupUpdateOne) ClearUsers() *GroupUpdateOne {
  250. guo.mutation.ClearUsers()
  251. return guo
  252. }
  253. // RemoveUserIDs removes the "users" edge to User entities by IDs.
  254. func (guo *GroupUpdateOne) RemoveUserIDs(ids ...int) *GroupUpdateOne {
  255. guo.mutation.RemoveUserIDs(ids...)
  256. return guo
  257. }
  258. // RemoveUsers removes "users" edges to User entities.
  259. func (guo *GroupUpdateOne) RemoveUsers(u ...*User) *GroupUpdateOne {
  260. ids := make([]int, len(u))
  261. for i := range u {
  262. ids[i] = u[i].ID
  263. }
  264. return guo.RemoveUserIDs(ids...)
  265. }
  266. // Select allows selecting one or more fields (columns) of the returned entity.
  267. // The default is selecting all fields defined in the entity schema.
  268. func (guo *GroupUpdateOne) Select(field string, fields ...string) *GroupUpdateOne {
  269. guo.fields = append([]string{field}, fields...)
  270. return guo
  271. }
  272. // Save executes the query and returns the updated Group entity.
  273. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
  274. var (
  275. err error
  276. node *Group
  277. )
  278. if len(guo.hooks) == 0 {
  279. if err = guo.check(); err != nil {
  280. return nil, err
  281. }
  282. node, err = guo.sqlSave(ctx)
  283. } else {
  284. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  285. mutation, ok := m.(*GroupMutation)
  286. if !ok {
  287. return nil, fmt.Errorf("unexpected mutation type %T", m)
  288. }
  289. if err = guo.check(); err != nil {
  290. return nil, err
  291. }
  292. guo.mutation = mutation
  293. node, err = guo.sqlSave(ctx)
  294. mutation.done = true
  295. return node, err
  296. })
  297. for i := len(guo.hooks) - 1; i >= 0; i-- {
  298. if guo.hooks[i] == nil {
  299. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  300. }
  301. mut = guo.hooks[i](mut)
  302. }
  303. if _, err := mut.Mutate(ctx, guo.mutation); err != nil {
  304. return nil, err
  305. }
  306. }
  307. return node, err
  308. }
  309. // SaveX is like Save, but panics if an error occurs.
  310. func (guo *GroupUpdateOne) SaveX(ctx context.Context) *Group {
  311. node, err := guo.Save(ctx)
  312. if err != nil {
  313. panic(err)
  314. }
  315. return node
  316. }
  317. // Exec executes the query on the entity.
  318. func (guo *GroupUpdateOne) Exec(ctx context.Context) error {
  319. _, err := guo.Save(ctx)
  320. return err
  321. }
  322. // ExecX is like Exec, but panics if an error occurs.
  323. func (guo *GroupUpdateOne) ExecX(ctx context.Context) {
  324. if err := guo.Exec(ctx); err != nil {
  325. panic(err)
  326. }
  327. }
  328. // check runs all checks and user-defined validators on the builder.
  329. func (guo *GroupUpdateOne) check() error {
  330. if v, ok := guo.mutation.Name(); ok {
  331. if err := group.NameValidator(v); err != nil {
  332. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Group.name": %w`, err)}
  333. }
  334. }
  335. return nil
  336. }
  337. func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (_node *Group, err error) {
  338. _spec := &sqlgraph.UpdateSpec{
  339. Node: &sqlgraph.NodeSpec{
  340. Table: group.Table,
  341. Columns: group.Columns,
  342. ID: &sqlgraph.FieldSpec{
  343. Type: field.TypeInt,
  344. Column: group.FieldID,
  345. },
  346. },
  347. }
  348. id, ok := guo.mutation.ID()
  349. if !ok {
  350. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Group.id" for update`)}
  351. }
  352. _spec.Node.ID.Value = id
  353. if fields := guo.fields; len(fields) > 0 {
  354. _spec.Node.Columns = make([]string, 0, len(fields))
  355. _spec.Node.Columns = append(_spec.Node.Columns, group.FieldID)
  356. for _, f := range fields {
  357. if !group.ValidColumn(f) {
  358. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  359. }
  360. if f != group.FieldID {
  361. _spec.Node.Columns = append(_spec.Node.Columns, f)
  362. }
  363. }
  364. }
  365. if ps := guo.mutation.predicates; len(ps) > 0 {
  366. _spec.Predicate = func(selector *sql.Selector) {
  367. for i := range ps {
  368. ps[i](selector)
  369. }
  370. }
  371. }
  372. if value, ok := guo.mutation.Name(); ok {
  373. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  374. Type: field.TypeString,
  375. Value: value,
  376. Column: group.FieldName,
  377. })
  378. }
  379. if guo.mutation.UsersCleared() {
  380. edge := &sqlgraph.EdgeSpec{
  381. Rel: sqlgraph.M2M,
  382. Inverse: false,
  383. Table: group.UsersTable,
  384. Columns: group.UsersPrimaryKey,
  385. Bidi: false,
  386. Target: &sqlgraph.EdgeTarget{
  387. IDSpec: &sqlgraph.FieldSpec{
  388. Type: field.TypeInt,
  389. Column: user.FieldID,
  390. },
  391. },
  392. }
  393. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  394. }
  395. if nodes := guo.mutation.RemovedUsersIDs(); len(nodes) > 0 && !guo.mutation.UsersCleared() {
  396. edge := &sqlgraph.EdgeSpec{
  397. Rel: sqlgraph.M2M,
  398. Inverse: false,
  399. Table: group.UsersTable,
  400. Columns: group.UsersPrimaryKey,
  401. Bidi: false,
  402. Target: &sqlgraph.EdgeTarget{
  403. IDSpec: &sqlgraph.FieldSpec{
  404. Type: field.TypeInt,
  405. Column: user.FieldID,
  406. },
  407. },
  408. }
  409. for _, k := range nodes {
  410. edge.Target.Nodes = append(edge.Target.Nodes, k)
  411. }
  412. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  413. }
  414. if nodes := guo.mutation.UsersIDs(); len(nodes) > 0 {
  415. edge := &sqlgraph.EdgeSpec{
  416. Rel: sqlgraph.M2M,
  417. Inverse: false,
  418. Table: group.UsersTable,
  419. Columns: group.UsersPrimaryKey,
  420. Bidi: false,
  421. Target: &sqlgraph.EdgeTarget{
  422. IDSpec: &sqlgraph.FieldSpec{
  423. Type: field.TypeInt,
  424. Column: user.FieldID,
  425. },
  426. },
  427. }
  428. for _, k := range nodes {
  429. edge.Target.Nodes = append(edge.Target.Nodes, k)
  430. }
  431. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  432. }
  433. _node = &Group{config: guo.config}
  434. _spec.Assign = _node.assignValues
  435. _spec.ScanValues = _node.scanValues
  436. if err = sqlgraph.UpdateNode(ctx, guo.driver, _spec); err != nil {
  437. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  438. err = &NotFoundError{group.Label}
  439. } else if sqlgraph.IsConstraintError(err) {
  440. err = &ConstraintError{err.Error(), err}
  441. }
  442. return nil, err
  443. }
  444. return _node, nil
  445. }