group_update.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/entdemo/ent/group"
  7. "code.osinet.fr/fgm/entdemo/ent/predicate"
  8. "code.osinet.fr/fgm/entdemo/ent/user"
  9. "github.com/facebookincubator/ent/dialect/sql"
  10. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  11. "github.com/facebookincubator/ent/schema/field"
  12. )
  13. // GroupUpdate is the builder for updating Group entities.
  14. type GroupUpdate struct {
  15. config
  16. hooks []Hook
  17. mutation *GroupMutation
  18. predicates []predicate.Group
  19. }
  20. // Where adds a new predicate for the builder.
  21. func (gu *GroupUpdate) Where(ps ...predicate.Group) *GroupUpdate {
  22. gu.predicates = append(gu.predicates, 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 User 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 User.
  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. // RemoveUserIDs removes the users edge to User by ids.
  44. func (gu *GroupUpdate) RemoveUserIDs(ids ...int) *GroupUpdate {
  45. gu.mutation.RemoveUserIDs(ids...)
  46. return gu
  47. }
  48. // RemoveUsers removes users edges to User.
  49. func (gu *GroupUpdate) RemoveUsers(u ...*User) *GroupUpdate {
  50. ids := make([]int, len(u))
  51. for i := range u {
  52. ids[i] = u[i].ID
  53. }
  54. return gu.RemoveUserIDs(ids...)
  55. }
  56. // Save executes the query and returns the number of rows/vertices matched by this operation.
  57. func (gu *GroupUpdate) Save(ctx context.Context) (int, error) {
  58. if v, ok := gu.mutation.Name(); ok {
  59. if err := group.NameValidator(v); err != nil {
  60. return 0, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
  61. }
  62. }
  63. var (
  64. err error
  65. affected int
  66. )
  67. if len(gu.hooks) == 0 {
  68. affected, err = gu.sqlSave(ctx)
  69. } else {
  70. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  71. mutation, ok := m.(*GroupMutation)
  72. if !ok {
  73. return nil, fmt.Errorf("unexpected mutation type %T", m)
  74. }
  75. gu.mutation = mutation
  76. affected, err = gu.sqlSave(ctx)
  77. return affected, err
  78. })
  79. for i := len(gu.hooks) - 1; i >= 0; i-- {
  80. mut = gu.hooks[i](mut)
  81. }
  82. if _, err := mut.Mutate(ctx, gu.mutation); err != nil {
  83. return 0, err
  84. }
  85. }
  86. return affected, err
  87. }
  88. // SaveX is like Save, but panics if an error occurs.
  89. func (gu *GroupUpdate) SaveX(ctx context.Context) int {
  90. affected, err := gu.Save(ctx)
  91. if err != nil {
  92. panic(err)
  93. }
  94. return affected
  95. }
  96. // Exec executes the query.
  97. func (gu *GroupUpdate) Exec(ctx context.Context) error {
  98. _, err := gu.Save(ctx)
  99. return err
  100. }
  101. // ExecX is like Exec, but panics if an error occurs.
  102. func (gu *GroupUpdate) ExecX(ctx context.Context) {
  103. if err := gu.Exec(ctx); err != nil {
  104. panic(err)
  105. }
  106. }
  107. func (gu *GroupUpdate) sqlSave(ctx context.Context) (n int, err error) {
  108. _spec := &sqlgraph.UpdateSpec{
  109. Node: &sqlgraph.NodeSpec{
  110. Table: group.Table,
  111. Columns: group.Columns,
  112. ID: &sqlgraph.FieldSpec{
  113. Type: field.TypeInt,
  114. Column: group.FieldID,
  115. },
  116. },
  117. }
  118. if ps := gu.predicates; len(ps) > 0 {
  119. _spec.Predicate = func(selector *sql.Selector) {
  120. for i := range ps {
  121. ps[i](selector)
  122. }
  123. }
  124. }
  125. if value, ok := gu.mutation.Name(); ok {
  126. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  127. Type: field.TypeString,
  128. Value: value,
  129. Column: group.FieldName,
  130. })
  131. }
  132. if nodes := gu.mutation.RemovedUsersIDs(); len(nodes) > 0 {
  133. edge := &sqlgraph.EdgeSpec{
  134. Rel: sqlgraph.M2M,
  135. Inverse: false,
  136. Table: group.UsersTable,
  137. Columns: group.UsersPrimaryKey,
  138. Bidi: false,
  139. Target: &sqlgraph.EdgeTarget{
  140. IDSpec: &sqlgraph.FieldSpec{
  141. Type: field.TypeInt,
  142. Column: user.FieldID,
  143. },
  144. },
  145. }
  146. for _, k := range nodes {
  147. edge.Target.Nodes = append(edge.Target.Nodes, k)
  148. }
  149. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  150. }
  151. if nodes := gu.mutation.UsersIDs(); len(nodes) > 0 {
  152. edge := &sqlgraph.EdgeSpec{
  153. Rel: sqlgraph.M2M,
  154. Inverse: false,
  155. Table: group.UsersTable,
  156. Columns: group.UsersPrimaryKey,
  157. Bidi: false,
  158. Target: &sqlgraph.EdgeTarget{
  159. IDSpec: &sqlgraph.FieldSpec{
  160. Type: field.TypeInt,
  161. Column: user.FieldID,
  162. },
  163. },
  164. }
  165. for _, k := range nodes {
  166. edge.Target.Nodes = append(edge.Target.Nodes, k)
  167. }
  168. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  169. }
  170. if n, err = sqlgraph.UpdateNodes(ctx, gu.driver, _spec); err != nil {
  171. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  172. err = &NotFoundError{group.Label}
  173. } else if cerr, ok := isSQLConstraintError(err); ok {
  174. err = cerr
  175. }
  176. return 0, err
  177. }
  178. return n, nil
  179. }
  180. // GroupUpdateOne is the builder for updating a single Group entity.
  181. type GroupUpdateOne struct {
  182. config
  183. hooks []Hook
  184. mutation *GroupMutation
  185. }
  186. // SetName sets the name field.
  187. func (guo *GroupUpdateOne) SetName(s string) *GroupUpdateOne {
  188. guo.mutation.SetName(s)
  189. return guo
  190. }
  191. // AddUserIDs adds the users edge to User by ids.
  192. func (guo *GroupUpdateOne) AddUserIDs(ids ...int) *GroupUpdateOne {
  193. guo.mutation.AddUserIDs(ids...)
  194. return guo
  195. }
  196. // AddUsers adds the users edges to User.
  197. func (guo *GroupUpdateOne) AddUsers(u ...*User) *GroupUpdateOne {
  198. ids := make([]int, len(u))
  199. for i := range u {
  200. ids[i] = u[i].ID
  201. }
  202. return guo.AddUserIDs(ids...)
  203. }
  204. // RemoveUserIDs removes the users edge to User by ids.
  205. func (guo *GroupUpdateOne) RemoveUserIDs(ids ...int) *GroupUpdateOne {
  206. guo.mutation.RemoveUserIDs(ids...)
  207. return guo
  208. }
  209. // RemoveUsers removes users edges to User.
  210. func (guo *GroupUpdateOne) RemoveUsers(u ...*User) *GroupUpdateOne {
  211. ids := make([]int, len(u))
  212. for i := range u {
  213. ids[i] = u[i].ID
  214. }
  215. return guo.RemoveUserIDs(ids...)
  216. }
  217. // Save executes the query and returns the updated entity.
  218. func (guo *GroupUpdateOne) Save(ctx context.Context) (*Group, error) {
  219. if v, ok := guo.mutation.Name(); ok {
  220. if err := group.NameValidator(v); err != nil {
  221. return nil, fmt.Errorf("ent: validator failed for field \"name\": %v", err)
  222. }
  223. }
  224. var (
  225. err error
  226. node *Group
  227. )
  228. if len(guo.hooks) == 0 {
  229. node, err = guo.sqlSave(ctx)
  230. } else {
  231. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  232. mutation, ok := m.(*GroupMutation)
  233. if !ok {
  234. return nil, fmt.Errorf("unexpected mutation type %T", m)
  235. }
  236. guo.mutation = mutation
  237. node, err = guo.sqlSave(ctx)
  238. return node, err
  239. })
  240. for i := len(guo.hooks) - 1; i >= 0; i-- {
  241. mut = guo.hooks[i](mut)
  242. }
  243. if _, err := mut.Mutate(ctx, guo.mutation); err != nil {
  244. return nil, err
  245. }
  246. }
  247. return node, err
  248. }
  249. // SaveX is like Save, but panics if an error occurs.
  250. func (guo *GroupUpdateOne) SaveX(ctx context.Context) *Group {
  251. gr, err := guo.Save(ctx)
  252. if err != nil {
  253. panic(err)
  254. }
  255. return gr
  256. }
  257. // Exec executes the query on the entity.
  258. func (guo *GroupUpdateOne) Exec(ctx context.Context) error {
  259. _, err := guo.Save(ctx)
  260. return err
  261. }
  262. // ExecX is like Exec, but panics if an error occurs.
  263. func (guo *GroupUpdateOne) ExecX(ctx context.Context) {
  264. if err := guo.Exec(ctx); err != nil {
  265. panic(err)
  266. }
  267. }
  268. func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (gr *Group, err error) {
  269. _spec := &sqlgraph.UpdateSpec{
  270. Node: &sqlgraph.NodeSpec{
  271. Table: group.Table,
  272. Columns: group.Columns,
  273. ID: &sqlgraph.FieldSpec{
  274. Type: field.TypeInt,
  275. Column: group.FieldID,
  276. },
  277. },
  278. }
  279. id, ok := guo.mutation.ID()
  280. if !ok {
  281. return nil, fmt.Errorf("missing Group.ID for update")
  282. }
  283. _spec.Node.ID.Value = id
  284. if value, ok := guo.mutation.Name(); ok {
  285. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  286. Type: field.TypeString,
  287. Value: value,
  288. Column: group.FieldName,
  289. })
  290. }
  291. if nodes := guo.mutation.RemovedUsersIDs(); len(nodes) > 0 {
  292. edge := &sqlgraph.EdgeSpec{
  293. Rel: sqlgraph.M2M,
  294. Inverse: false,
  295. Table: group.UsersTable,
  296. Columns: group.UsersPrimaryKey,
  297. Bidi: false,
  298. Target: &sqlgraph.EdgeTarget{
  299. IDSpec: &sqlgraph.FieldSpec{
  300. Type: field.TypeInt,
  301. Column: user.FieldID,
  302. },
  303. },
  304. }
  305. for _, k := range nodes {
  306. edge.Target.Nodes = append(edge.Target.Nodes, k)
  307. }
  308. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  309. }
  310. if nodes := guo.mutation.UsersIDs(); len(nodes) > 0 {
  311. edge := &sqlgraph.EdgeSpec{
  312. Rel: sqlgraph.M2M,
  313. Inverse: false,
  314. Table: group.UsersTable,
  315. Columns: group.UsersPrimaryKey,
  316. Bidi: false,
  317. Target: &sqlgraph.EdgeTarget{
  318. IDSpec: &sqlgraph.FieldSpec{
  319. Type: field.TypeInt,
  320. Column: user.FieldID,
  321. },
  322. },
  323. }
  324. for _, k := range nodes {
  325. edge.Target.Nodes = append(edge.Target.Nodes, k)
  326. }
  327. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  328. }
  329. gr = &Group{config: guo.config}
  330. _spec.Assign = gr.assignValues
  331. _spec.ScanValues = gr.scanValues()
  332. if err = sqlgraph.UpdateNode(ctx, guo.driver, _spec); err != nil {
  333. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  334. err = &NotFoundError{group.Label}
  335. } else if cerr, ok := isSQLConstraintError(err); ok {
  336. err = cerr
  337. }
  338. return nil, err
  339. }
  340. return gr, nil
  341. }