pushregistration_create.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "w303a/server/app/gate/internal/data/ent/pushregistration"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/google/uuid"
  12. )
  13. // PushRegistrationCreate is the builder for creating a PushRegistration entity.
  14. type PushRegistrationCreate struct {
  15. config
  16. mutation *PushRegistrationMutation
  17. hooks []Hook
  18. }
  19. // SetRegID sets the "reg_id" field.
  20. func (prc *PushRegistrationCreate) SetRegID(s string) *PushRegistrationCreate {
  21. prc.mutation.SetRegID(s)
  22. return prc
  23. }
  24. // SetRegistrantPlatformType sets the "registrant_platform_type" field.
  25. func (prc *PushRegistrationCreate) SetRegistrantPlatformType(i int8) *PushRegistrationCreate {
  26. prc.mutation.SetRegistrantPlatformType(i)
  27. return prc
  28. }
  29. // SetLanguage sets the "language" field.
  30. func (prc *PushRegistrationCreate) SetLanguage(s string) *PushRegistrationCreate {
  31. prc.mutation.SetLanguage(s)
  32. return prc
  33. }
  34. // SetNillableLanguage sets the "language" field if the given value is not nil.
  35. func (prc *PushRegistrationCreate) SetNillableLanguage(s *string) *PushRegistrationCreate {
  36. if s != nil {
  37. prc.SetLanguage(*s)
  38. }
  39. return prc
  40. }
  41. // SetDeviceModel sets the "device_model" field.
  42. func (prc *PushRegistrationCreate) SetDeviceModel(s string) *PushRegistrationCreate {
  43. prc.mutation.SetDeviceModel(s)
  44. return prc
  45. }
  46. // SetDeviceManufacturer sets the "device_manufacturer" field.
  47. func (prc *PushRegistrationCreate) SetDeviceManufacturer(s string) *PushRegistrationCreate {
  48. prc.mutation.SetDeviceManufacturer(s)
  49. return prc
  50. }
  51. // SetDeviceSystemVersion sets the "device_system_version" field.
  52. func (prc *PushRegistrationCreate) SetDeviceSystemVersion(s string) *PushRegistrationCreate {
  53. prc.mutation.SetDeviceSystemVersion(s)
  54. return prc
  55. }
  56. // SetDeviceSystem sets the "device_system" field.
  57. func (prc *PushRegistrationCreate) SetDeviceSystem(s string) *PushRegistrationCreate {
  58. prc.mutation.SetDeviceSystem(s)
  59. return prc
  60. }
  61. // SetCreatedAt sets the "created_at" field.
  62. func (prc *PushRegistrationCreate) SetCreatedAt(t time.Time) *PushRegistrationCreate {
  63. prc.mutation.SetCreatedAt(t)
  64. return prc
  65. }
  66. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  67. func (prc *PushRegistrationCreate) SetNillableCreatedAt(t *time.Time) *PushRegistrationCreate {
  68. if t != nil {
  69. prc.SetCreatedAt(*t)
  70. }
  71. return prc
  72. }
  73. // SetUpdatedAt sets the "updated_at" field.
  74. func (prc *PushRegistrationCreate) SetUpdatedAt(t time.Time) *PushRegistrationCreate {
  75. prc.mutation.SetUpdatedAt(t)
  76. return prc
  77. }
  78. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  79. func (prc *PushRegistrationCreate) SetNillableUpdatedAt(t *time.Time) *PushRegistrationCreate {
  80. if t != nil {
  81. prc.SetUpdatedAt(*t)
  82. }
  83. return prc
  84. }
  85. // SetID sets the "id" field.
  86. func (prc *PushRegistrationCreate) SetID(u uuid.UUID) *PushRegistrationCreate {
  87. prc.mutation.SetID(u)
  88. return prc
  89. }
  90. // Mutation returns the PushRegistrationMutation object of the builder.
  91. func (prc *PushRegistrationCreate) Mutation() *PushRegistrationMutation {
  92. return prc.mutation
  93. }
  94. // Save creates the PushRegistration in the database.
  95. func (prc *PushRegistrationCreate) Save(ctx context.Context) (*PushRegistration, error) {
  96. prc.defaults()
  97. return withHooks(ctx, prc.sqlSave, prc.mutation, prc.hooks)
  98. }
  99. // SaveX calls Save and panics if Save returns an error.
  100. func (prc *PushRegistrationCreate) SaveX(ctx context.Context) *PushRegistration {
  101. v, err := prc.Save(ctx)
  102. if err != nil {
  103. panic(err)
  104. }
  105. return v
  106. }
  107. // Exec executes the query.
  108. func (prc *PushRegistrationCreate) Exec(ctx context.Context) error {
  109. _, err := prc.Save(ctx)
  110. return err
  111. }
  112. // ExecX is like Exec, but panics if an error occurs.
  113. func (prc *PushRegistrationCreate) ExecX(ctx context.Context) {
  114. if err := prc.Exec(ctx); err != nil {
  115. panic(err)
  116. }
  117. }
  118. // defaults sets the default values of the builder before save.
  119. func (prc *PushRegistrationCreate) defaults() {
  120. if _, ok := prc.mutation.Language(); !ok {
  121. v := pushregistration.DefaultLanguage
  122. prc.mutation.SetLanguage(v)
  123. }
  124. }
  125. // check runs all checks and user-defined validators on the builder.
  126. func (prc *PushRegistrationCreate) check() error {
  127. if _, ok := prc.mutation.RegID(); !ok {
  128. return &ValidationError{Name: "reg_id", err: errors.New(`ent: missing required field "PushRegistration.reg_id"`)}
  129. }
  130. if _, ok := prc.mutation.RegistrantPlatformType(); !ok {
  131. return &ValidationError{Name: "registrant_platform_type", err: errors.New(`ent: missing required field "PushRegistration.registrant_platform_type"`)}
  132. }
  133. if _, ok := prc.mutation.Language(); !ok {
  134. return &ValidationError{Name: "language", err: errors.New(`ent: missing required field "PushRegistration.language"`)}
  135. }
  136. if v, ok := prc.mutation.Language(); ok {
  137. if err := pushregistration.LanguageValidator(v); err != nil {
  138. return &ValidationError{Name: "language", err: fmt.Errorf(`ent: validator failed for field "PushRegistration.language": %w`, err)}
  139. }
  140. }
  141. if _, ok := prc.mutation.DeviceModel(); !ok {
  142. return &ValidationError{Name: "device_model", err: errors.New(`ent: missing required field "PushRegistration.device_model"`)}
  143. }
  144. if _, ok := prc.mutation.DeviceManufacturer(); !ok {
  145. return &ValidationError{Name: "device_manufacturer", err: errors.New(`ent: missing required field "PushRegistration.device_manufacturer"`)}
  146. }
  147. if _, ok := prc.mutation.DeviceSystemVersion(); !ok {
  148. return &ValidationError{Name: "device_system_version", err: errors.New(`ent: missing required field "PushRegistration.device_system_version"`)}
  149. }
  150. if _, ok := prc.mutation.DeviceSystem(); !ok {
  151. return &ValidationError{Name: "device_system", err: errors.New(`ent: missing required field "PushRegistration.device_system"`)}
  152. }
  153. return nil
  154. }
  155. func (prc *PushRegistrationCreate) sqlSave(ctx context.Context) (*PushRegistration, error) {
  156. if err := prc.check(); err != nil {
  157. return nil, err
  158. }
  159. _node, _spec := prc.createSpec()
  160. if err := sqlgraph.CreateNode(ctx, prc.driver, _spec); err != nil {
  161. if sqlgraph.IsConstraintError(err) {
  162. err = &ConstraintError{msg: err.Error(), wrap: err}
  163. }
  164. return nil, err
  165. }
  166. if _spec.ID.Value != nil {
  167. if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
  168. _node.ID = *id
  169. } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
  170. return nil, err
  171. }
  172. }
  173. prc.mutation.id = &_node.ID
  174. prc.mutation.done = true
  175. return _node, nil
  176. }
  177. func (prc *PushRegistrationCreate) createSpec() (*PushRegistration, *sqlgraph.CreateSpec) {
  178. var (
  179. _node = &PushRegistration{config: prc.config}
  180. _spec = sqlgraph.NewCreateSpec(pushregistration.Table, sqlgraph.NewFieldSpec(pushregistration.FieldID, field.TypeUUID))
  181. )
  182. if id, ok := prc.mutation.ID(); ok {
  183. _node.ID = id
  184. _spec.ID.Value = &id
  185. }
  186. if value, ok := prc.mutation.RegID(); ok {
  187. _spec.SetField(pushregistration.FieldRegID, field.TypeString, value)
  188. _node.RegID = value
  189. }
  190. if value, ok := prc.mutation.RegistrantPlatformType(); ok {
  191. _spec.SetField(pushregistration.FieldRegistrantPlatformType, field.TypeInt8, value)
  192. _node.RegistrantPlatformType = value
  193. }
  194. if value, ok := prc.mutation.Language(); ok {
  195. _spec.SetField(pushregistration.FieldLanguage, field.TypeString, value)
  196. _node.Language = value
  197. }
  198. if value, ok := prc.mutation.DeviceModel(); ok {
  199. _spec.SetField(pushregistration.FieldDeviceModel, field.TypeString, value)
  200. _node.DeviceModel = &value
  201. }
  202. if value, ok := prc.mutation.DeviceManufacturer(); ok {
  203. _spec.SetField(pushregistration.FieldDeviceManufacturer, field.TypeString, value)
  204. _node.DeviceManufacturer = &value
  205. }
  206. if value, ok := prc.mutation.DeviceSystemVersion(); ok {
  207. _spec.SetField(pushregistration.FieldDeviceSystemVersion, field.TypeString, value)
  208. _node.DeviceSystemVersion = &value
  209. }
  210. if value, ok := prc.mutation.DeviceSystem(); ok {
  211. _spec.SetField(pushregistration.FieldDeviceSystem, field.TypeString, value)
  212. _node.DeviceSystem = &value
  213. }
  214. if value, ok := prc.mutation.CreatedAt(); ok {
  215. _spec.SetField(pushregistration.FieldCreatedAt, field.TypeTime, value)
  216. _node.CreatedAt = value
  217. }
  218. if value, ok := prc.mutation.UpdatedAt(); ok {
  219. _spec.SetField(pushregistration.FieldUpdatedAt, field.TypeTime, value)
  220. _node.UpdatedAt = value
  221. }
  222. return _node, _spec
  223. }
  224. // PushRegistrationCreateBulk is the builder for creating many PushRegistration entities in bulk.
  225. type PushRegistrationCreateBulk struct {
  226. config
  227. err error
  228. builders []*PushRegistrationCreate
  229. }
  230. // Save creates the PushRegistration entities in the database.
  231. func (prcb *PushRegistrationCreateBulk) Save(ctx context.Context) ([]*PushRegistration, error) {
  232. if prcb.err != nil {
  233. return nil, prcb.err
  234. }
  235. specs := make([]*sqlgraph.CreateSpec, len(prcb.builders))
  236. nodes := make([]*PushRegistration, len(prcb.builders))
  237. mutators := make([]Mutator, len(prcb.builders))
  238. for i := range prcb.builders {
  239. func(i int, root context.Context) {
  240. builder := prcb.builders[i]
  241. builder.defaults()
  242. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  243. mutation, ok := m.(*PushRegistrationMutation)
  244. if !ok {
  245. return nil, fmt.Errorf("unexpected mutation type %T", m)
  246. }
  247. if err := builder.check(); err != nil {
  248. return nil, err
  249. }
  250. builder.mutation = mutation
  251. var err error
  252. nodes[i], specs[i] = builder.createSpec()
  253. if i < len(mutators)-1 {
  254. _, err = mutators[i+1].Mutate(root, prcb.builders[i+1].mutation)
  255. } else {
  256. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  257. // Invoke the actual operation on the latest mutation in the chain.
  258. if err = sqlgraph.BatchCreate(ctx, prcb.driver, spec); err != nil {
  259. if sqlgraph.IsConstraintError(err) {
  260. err = &ConstraintError{msg: err.Error(), wrap: err}
  261. }
  262. }
  263. }
  264. if err != nil {
  265. return nil, err
  266. }
  267. mutation.id = &nodes[i].ID
  268. mutation.done = true
  269. return nodes[i], nil
  270. })
  271. for i := len(builder.hooks) - 1; i >= 0; i-- {
  272. mut = builder.hooks[i](mut)
  273. }
  274. mutators[i] = mut
  275. }(i, ctx)
  276. }
  277. if len(mutators) > 0 {
  278. if _, err := mutators[0].Mutate(ctx, prcb.builders[0].mutation); err != nil {
  279. return nil, err
  280. }
  281. }
  282. return nodes, nil
  283. }
  284. // SaveX is like Save, but panics if an error occurs.
  285. func (prcb *PushRegistrationCreateBulk) SaveX(ctx context.Context) []*PushRegistration {
  286. v, err := prcb.Save(ctx)
  287. if err != nil {
  288. panic(err)
  289. }
  290. return v
  291. }
  292. // Exec executes the query.
  293. func (prcb *PushRegistrationCreateBulk) Exec(ctx context.Context) error {
  294. _, err := prcb.Save(ctx)
  295. return err
  296. }
  297. // ExecX is like Exec, but panics if an error occurs.
  298. func (prcb *PushRegistrationCreateBulk) ExecX(ctx context.Context) {
  299. if err := prcb.Exec(ctx); err != nil {
  300. panic(err)
  301. }
  302. }