apikey_update.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "wechat-api/ent/agent"
  9. "wechat-api/ent/apikey"
  10. "wechat-api/ent/predicate"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // ApiKeyUpdate is the builder for updating ApiKey entities.
  16. type ApiKeyUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *ApiKeyMutation
  20. }
  21. // Where appends a list predicates to the ApiKeyUpdate builder.
  22. func (aku *ApiKeyUpdate) Where(ps ...predicate.ApiKey) *ApiKeyUpdate {
  23. aku.mutation.Where(ps...)
  24. return aku
  25. }
  26. // SetUpdatedAt sets the "updated_at" field.
  27. func (aku *ApiKeyUpdate) SetUpdatedAt(t time.Time) *ApiKeyUpdate {
  28. aku.mutation.SetUpdatedAt(t)
  29. return aku
  30. }
  31. // SetDeletedAt sets the "deleted_at" field.
  32. func (aku *ApiKeyUpdate) SetDeletedAt(t time.Time) *ApiKeyUpdate {
  33. aku.mutation.SetDeletedAt(t)
  34. return aku
  35. }
  36. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  37. func (aku *ApiKeyUpdate) SetNillableDeletedAt(t *time.Time) *ApiKeyUpdate {
  38. if t != nil {
  39. aku.SetDeletedAt(*t)
  40. }
  41. return aku
  42. }
  43. // ClearDeletedAt clears the value of the "deleted_at" field.
  44. func (aku *ApiKeyUpdate) ClearDeletedAt() *ApiKeyUpdate {
  45. aku.mutation.ClearDeletedAt()
  46. return aku
  47. }
  48. // SetTitle sets the "title" field.
  49. func (aku *ApiKeyUpdate) SetTitle(s string) *ApiKeyUpdate {
  50. aku.mutation.SetTitle(s)
  51. return aku
  52. }
  53. // SetNillableTitle sets the "title" field if the given value is not nil.
  54. func (aku *ApiKeyUpdate) SetNillableTitle(s *string) *ApiKeyUpdate {
  55. if s != nil {
  56. aku.SetTitle(*s)
  57. }
  58. return aku
  59. }
  60. // ClearTitle clears the value of the "title" field.
  61. func (aku *ApiKeyUpdate) ClearTitle() *ApiKeyUpdate {
  62. aku.mutation.ClearTitle()
  63. return aku
  64. }
  65. // SetKey sets the "key" field.
  66. func (aku *ApiKeyUpdate) SetKey(s string) *ApiKeyUpdate {
  67. aku.mutation.SetKey(s)
  68. return aku
  69. }
  70. // SetNillableKey sets the "key" field if the given value is not nil.
  71. func (aku *ApiKeyUpdate) SetNillableKey(s *string) *ApiKeyUpdate {
  72. if s != nil {
  73. aku.SetKey(*s)
  74. }
  75. return aku
  76. }
  77. // ClearKey clears the value of the "key" field.
  78. func (aku *ApiKeyUpdate) ClearKey() *ApiKeyUpdate {
  79. aku.mutation.ClearKey()
  80. return aku
  81. }
  82. // SetOrganizationID sets the "organization_id" field.
  83. func (aku *ApiKeyUpdate) SetOrganizationID(u uint64) *ApiKeyUpdate {
  84. aku.mutation.ResetOrganizationID()
  85. aku.mutation.SetOrganizationID(u)
  86. return aku
  87. }
  88. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  89. func (aku *ApiKeyUpdate) SetNillableOrganizationID(u *uint64) *ApiKeyUpdate {
  90. if u != nil {
  91. aku.SetOrganizationID(*u)
  92. }
  93. return aku
  94. }
  95. // AddOrganizationID adds u to the "organization_id" field.
  96. func (aku *ApiKeyUpdate) AddOrganizationID(u int64) *ApiKeyUpdate {
  97. aku.mutation.AddOrganizationID(u)
  98. return aku
  99. }
  100. // SetAgentID sets the "agent_id" field.
  101. func (aku *ApiKeyUpdate) SetAgentID(u uint64) *ApiKeyUpdate {
  102. aku.mutation.SetAgentID(u)
  103. return aku
  104. }
  105. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  106. func (aku *ApiKeyUpdate) SetNillableAgentID(u *uint64) *ApiKeyUpdate {
  107. if u != nil {
  108. aku.SetAgentID(*u)
  109. }
  110. return aku
  111. }
  112. // SetCustomAgentBase sets the "custom_agent_base" field.
  113. func (aku *ApiKeyUpdate) SetCustomAgentBase(s string) *ApiKeyUpdate {
  114. aku.mutation.SetCustomAgentBase(s)
  115. return aku
  116. }
  117. // SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
  118. func (aku *ApiKeyUpdate) SetNillableCustomAgentBase(s *string) *ApiKeyUpdate {
  119. if s != nil {
  120. aku.SetCustomAgentBase(*s)
  121. }
  122. return aku
  123. }
  124. // ClearCustomAgentBase clears the value of the "custom_agent_base" field.
  125. func (aku *ApiKeyUpdate) ClearCustomAgentBase() *ApiKeyUpdate {
  126. aku.mutation.ClearCustomAgentBase()
  127. return aku
  128. }
  129. // SetCustomAgentKey sets the "custom_agent_key" field.
  130. func (aku *ApiKeyUpdate) SetCustomAgentKey(s string) *ApiKeyUpdate {
  131. aku.mutation.SetCustomAgentKey(s)
  132. return aku
  133. }
  134. // SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
  135. func (aku *ApiKeyUpdate) SetNillableCustomAgentKey(s *string) *ApiKeyUpdate {
  136. if s != nil {
  137. aku.SetCustomAgentKey(*s)
  138. }
  139. return aku
  140. }
  141. // ClearCustomAgentKey clears the value of the "custom_agent_key" field.
  142. func (aku *ApiKeyUpdate) ClearCustomAgentKey() *ApiKeyUpdate {
  143. aku.mutation.ClearCustomAgentKey()
  144. return aku
  145. }
  146. // SetOpenaiBase sets the "openai_base" field.
  147. func (aku *ApiKeyUpdate) SetOpenaiBase(s string) *ApiKeyUpdate {
  148. aku.mutation.SetOpenaiBase(s)
  149. return aku
  150. }
  151. // SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
  152. func (aku *ApiKeyUpdate) SetNillableOpenaiBase(s *string) *ApiKeyUpdate {
  153. if s != nil {
  154. aku.SetOpenaiBase(*s)
  155. }
  156. return aku
  157. }
  158. // ClearOpenaiBase clears the value of the "openai_base" field.
  159. func (aku *ApiKeyUpdate) ClearOpenaiBase() *ApiKeyUpdate {
  160. aku.mutation.ClearOpenaiBase()
  161. return aku
  162. }
  163. // SetOpenaiKey sets the "openai_key" field.
  164. func (aku *ApiKeyUpdate) SetOpenaiKey(s string) *ApiKeyUpdate {
  165. aku.mutation.SetOpenaiKey(s)
  166. return aku
  167. }
  168. // SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
  169. func (aku *ApiKeyUpdate) SetNillableOpenaiKey(s *string) *ApiKeyUpdate {
  170. if s != nil {
  171. aku.SetOpenaiKey(*s)
  172. }
  173. return aku
  174. }
  175. // ClearOpenaiKey clears the value of the "openai_key" field.
  176. func (aku *ApiKeyUpdate) ClearOpenaiKey() *ApiKeyUpdate {
  177. aku.mutation.ClearOpenaiKey()
  178. return aku
  179. }
  180. // SetAgent sets the "agent" edge to the Agent entity.
  181. func (aku *ApiKeyUpdate) SetAgent(a *Agent) *ApiKeyUpdate {
  182. return aku.SetAgentID(a.ID)
  183. }
  184. // Mutation returns the ApiKeyMutation object of the builder.
  185. func (aku *ApiKeyUpdate) Mutation() *ApiKeyMutation {
  186. return aku.mutation
  187. }
  188. // ClearAgent clears the "agent" edge to the Agent entity.
  189. func (aku *ApiKeyUpdate) ClearAgent() *ApiKeyUpdate {
  190. aku.mutation.ClearAgent()
  191. return aku
  192. }
  193. // Save executes the query and returns the number of nodes affected by the update operation.
  194. func (aku *ApiKeyUpdate) Save(ctx context.Context) (int, error) {
  195. if err := aku.defaults(); err != nil {
  196. return 0, err
  197. }
  198. return withHooks(ctx, aku.sqlSave, aku.mutation, aku.hooks)
  199. }
  200. // SaveX is like Save, but panics if an error occurs.
  201. func (aku *ApiKeyUpdate) SaveX(ctx context.Context) int {
  202. affected, err := aku.Save(ctx)
  203. if err != nil {
  204. panic(err)
  205. }
  206. return affected
  207. }
  208. // Exec executes the query.
  209. func (aku *ApiKeyUpdate) Exec(ctx context.Context) error {
  210. _, err := aku.Save(ctx)
  211. return err
  212. }
  213. // ExecX is like Exec, but panics if an error occurs.
  214. func (aku *ApiKeyUpdate) ExecX(ctx context.Context) {
  215. if err := aku.Exec(ctx); err != nil {
  216. panic(err)
  217. }
  218. }
  219. // defaults sets the default values of the builder before save.
  220. func (aku *ApiKeyUpdate) defaults() error {
  221. if _, ok := aku.mutation.UpdatedAt(); !ok {
  222. if apikey.UpdateDefaultUpdatedAt == nil {
  223. return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  224. }
  225. v := apikey.UpdateDefaultUpdatedAt()
  226. aku.mutation.SetUpdatedAt(v)
  227. }
  228. return nil
  229. }
  230. // check runs all checks and user-defined validators on the builder.
  231. func (aku *ApiKeyUpdate) check() error {
  232. if _, ok := aku.mutation.AgentID(); aku.mutation.AgentCleared() && !ok {
  233. return errors.New(`ent: clearing a required unique edge "ApiKey.agent"`)
  234. }
  235. return nil
  236. }
  237. func (aku *ApiKeyUpdate) sqlSave(ctx context.Context) (n int, err error) {
  238. if err := aku.check(); err != nil {
  239. return n, err
  240. }
  241. _spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64))
  242. if ps := aku.mutation.predicates; len(ps) > 0 {
  243. _spec.Predicate = func(selector *sql.Selector) {
  244. for i := range ps {
  245. ps[i](selector)
  246. }
  247. }
  248. }
  249. if value, ok := aku.mutation.UpdatedAt(); ok {
  250. _spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
  251. }
  252. if value, ok := aku.mutation.DeletedAt(); ok {
  253. _spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
  254. }
  255. if aku.mutation.DeletedAtCleared() {
  256. _spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
  257. }
  258. if value, ok := aku.mutation.Title(); ok {
  259. _spec.SetField(apikey.FieldTitle, field.TypeString, value)
  260. }
  261. if aku.mutation.TitleCleared() {
  262. _spec.ClearField(apikey.FieldTitle, field.TypeString)
  263. }
  264. if value, ok := aku.mutation.Key(); ok {
  265. _spec.SetField(apikey.FieldKey, field.TypeString, value)
  266. }
  267. if aku.mutation.KeyCleared() {
  268. _spec.ClearField(apikey.FieldKey, field.TypeString)
  269. }
  270. if value, ok := aku.mutation.OrganizationID(); ok {
  271. _spec.SetField(apikey.FieldOrganizationID, field.TypeUint64, value)
  272. }
  273. if value, ok := aku.mutation.AddedOrganizationID(); ok {
  274. _spec.AddField(apikey.FieldOrganizationID, field.TypeUint64, value)
  275. }
  276. if value, ok := aku.mutation.CustomAgentBase(); ok {
  277. _spec.SetField(apikey.FieldCustomAgentBase, field.TypeString, value)
  278. }
  279. if aku.mutation.CustomAgentBaseCleared() {
  280. _spec.ClearField(apikey.FieldCustomAgentBase, field.TypeString)
  281. }
  282. if value, ok := aku.mutation.CustomAgentKey(); ok {
  283. _spec.SetField(apikey.FieldCustomAgentKey, field.TypeString, value)
  284. }
  285. if aku.mutation.CustomAgentKeyCleared() {
  286. _spec.ClearField(apikey.FieldCustomAgentKey, field.TypeString)
  287. }
  288. if value, ok := aku.mutation.OpenaiBase(); ok {
  289. _spec.SetField(apikey.FieldOpenaiBase, field.TypeString, value)
  290. }
  291. if aku.mutation.OpenaiBaseCleared() {
  292. _spec.ClearField(apikey.FieldOpenaiBase, field.TypeString)
  293. }
  294. if value, ok := aku.mutation.OpenaiKey(); ok {
  295. _spec.SetField(apikey.FieldOpenaiKey, field.TypeString, value)
  296. }
  297. if aku.mutation.OpenaiKeyCleared() {
  298. _spec.ClearField(apikey.FieldOpenaiKey, field.TypeString)
  299. }
  300. if aku.mutation.AgentCleared() {
  301. edge := &sqlgraph.EdgeSpec{
  302. Rel: sqlgraph.M2O,
  303. Inverse: true,
  304. Table: apikey.AgentTable,
  305. Columns: []string{apikey.AgentColumn},
  306. Bidi: false,
  307. Target: &sqlgraph.EdgeTarget{
  308. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  309. },
  310. }
  311. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  312. }
  313. if nodes := aku.mutation.AgentIDs(); len(nodes) > 0 {
  314. edge := &sqlgraph.EdgeSpec{
  315. Rel: sqlgraph.M2O,
  316. Inverse: true,
  317. Table: apikey.AgentTable,
  318. Columns: []string{apikey.AgentColumn},
  319. Bidi: false,
  320. Target: &sqlgraph.EdgeTarget{
  321. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  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. if n, err = sqlgraph.UpdateNodes(ctx, aku.driver, _spec); err != nil {
  330. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  331. err = &NotFoundError{apikey.Label}
  332. } else if sqlgraph.IsConstraintError(err) {
  333. err = &ConstraintError{msg: err.Error(), wrap: err}
  334. }
  335. return 0, err
  336. }
  337. aku.mutation.done = true
  338. return n, nil
  339. }
  340. // ApiKeyUpdateOne is the builder for updating a single ApiKey entity.
  341. type ApiKeyUpdateOne struct {
  342. config
  343. fields []string
  344. hooks []Hook
  345. mutation *ApiKeyMutation
  346. }
  347. // SetUpdatedAt sets the "updated_at" field.
  348. func (akuo *ApiKeyUpdateOne) SetUpdatedAt(t time.Time) *ApiKeyUpdateOne {
  349. akuo.mutation.SetUpdatedAt(t)
  350. return akuo
  351. }
  352. // SetDeletedAt sets the "deleted_at" field.
  353. func (akuo *ApiKeyUpdateOne) SetDeletedAt(t time.Time) *ApiKeyUpdateOne {
  354. akuo.mutation.SetDeletedAt(t)
  355. return akuo
  356. }
  357. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  358. func (akuo *ApiKeyUpdateOne) SetNillableDeletedAt(t *time.Time) *ApiKeyUpdateOne {
  359. if t != nil {
  360. akuo.SetDeletedAt(*t)
  361. }
  362. return akuo
  363. }
  364. // ClearDeletedAt clears the value of the "deleted_at" field.
  365. func (akuo *ApiKeyUpdateOne) ClearDeletedAt() *ApiKeyUpdateOne {
  366. akuo.mutation.ClearDeletedAt()
  367. return akuo
  368. }
  369. // SetTitle sets the "title" field.
  370. func (akuo *ApiKeyUpdateOne) SetTitle(s string) *ApiKeyUpdateOne {
  371. akuo.mutation.SetTitle(s)
  372. return akuo
  373. }
  374. // SetNillableTitle sets the "title" field if the given value is not nil.
  375. func (akuo *ApiKeyUpdateOne) SetNillableTitle(s *string) *ApiKeyUpdateOne {
  376. if s != nil {
  377. akuo.SetTitle(*s)
  378. }
  379. return akuo
  380. }
  381. // ClearTitle clears the value of the "title" field.
  382. func (akuo *ApiKeyUpdateOne) ClearTitle() *ApiKeyUpdateOne {
  383. akuo.mutation.ClearTitle()
  384. return akuo
  385. }
  386. // SetKey sets the "key" field.
  387. func (akuo *ApiKeyUpdateOne) SetKey(s string) *ApiKeyUpdateOne {
  388. akuo.mutation.SetKey(s)
  389. return akuo
  390. }
  391. // SetNillableKey sets the "key" field if the given value is not nil.
  392. func (akuo *ApiKeyUpdateOne) SetNillableKey(s *string) *ApiKeyUpdateOne {
  393. if s != nil {
  394. akuo.SetKey(*s)
  395. }
  396. return akuo
  397. }
  398. // ClearKey clears the value of the "key" field.
  399. func (akuo *ApiKeyUpdateOne) ClearKey() *ApiKeyUpdateOne {
  400. akuo.mutation.ClearKey()
  401. return akuo
  402. }
  403. // SetOrganizationID sets the "organization_id" field.
  404. func (akuo *ApiKeyUpdateOne) SetOrganizationID(u uint64) *ApiKeyUpdateOne {
  405. akuo.mutation.ResetOrganizationID()
  406. akuo.mutation.SetOrganizationID(u)
  407. return akuo
  408. }
  409. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  410. func (akuo *ApiKeyUpdateOne) SetNillableOrganizationID(u *uint64) *ApiKeyUpdateOne {
  411. if u != nil {
  412. akuo.SetOrganizationID(*u)
  413. }
  414. return akuo
  415. }
  416. // AddOrganizationID adds u to the "organization_id" field.
  417. func (akuo *ApiKeyUpdateOne) AddOrganizationID(u int64) *ApiKeyUpdateOne {
  418. akuo.mutation.AddOrganizationID(u)
  419. return akuo
  420. }
  421. // SetAgentID sets the "agent_id" field.
  422. func (akuo *ApiKeyUpdateOne) SetAgentID(u uint64) *ApiKeyUpdateOne {
  423. akuo.mutation.SetAgentID(u)
  424. return akuo
  425. }
  426. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  427. func (akuo *ApiKeyUpdateOne) SetNillableAgentID(u *uint64) *ApiKeyUpdateOne {
  428. if u != nil {
  429. akuo.SetAgentID(*u)
  430. }
  431. return akuo
  432. }
  433. // SetCustomAgentBase sets the "custom_agent_base" field.
  434. func (akuo *ApiKeyUpdateOne) SetCustomAgentBase(s string) *ApiKeyUpdateOne {
  435. akuo.mutation.SetCustomAgentBase(s)
  436. return akuo
  437. }
  438. // SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
  439. func (akuo *ApiKeyUpdateOne) SetNillableCustomAgentBase(s *string) *ApiKeyUpdateOne {
  440. if s != nil {
  441. akuo.SetCustomAgentBase(*s)
  442. }
  443. return akuo
  444. }
  445. // ClearCustomAgentBase clears the value of the "custom_agent_base" field.
  446. func (akuo *ApiKeyUpdateOne) ClearCustomAgentBase() *ApiKeyUpdateOne {
  447. akuo.mutation.ClearCustomAgentBase()
  448. return akuo
  449. }
  450. // SetCustomAgentKey sets the "custom_agent_key" field.
  451. func (akuo *ApiKeyUpdateOne) SetCustomAgentKey(s string) *ApiKeyUpdateOne {
  452. akuo.mutation.SetCustomAgentKey(s)
  453. return akuo
  454. }
  455. // SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
  456. func (akuo *ApiKeyUpdateOne) SetNillableCustomAgentKey(s *string) *ApiKeyUpdateOne {
  457. if s != nil {
  458. akuo.SetCustomAgentKey(*s)
  459. }
  460. return akuo
  461. }
  462. // ClearCustomAgentKey clears the value of the "custom_agent_key" field.
  463. func (akuo *ApiKeyUpdateOne) ClearCustomAgentKey() *ApiKeyUpdateOne {
  464. akuo.mutation.ClearCustomAgentKey()
  465. return akuo
  466. }
  467. // SetOpenaiBase sets the "openai_base" field.
  468. func (akuo *ApiKeyUpdateOne) SetOpenaiBase(s string) *ApiKeyUpdateOne {
  469. akuo.mutation.SetOpenaiBase(s)
  470. return akuo
  471. }
  472. // SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
  473. func (akuo *ApiKeyUpdateOne) SetNillableOpenaiBase(s *string) *ApiKeyUpdateOne {
  474. if s != nil {
  475. akuo.SetOpenaiBase(*s)
  476. }
  477. return akuo
  478. }
  479. // ClearOpenaiBase clears the value of the "openai_base" field.
  480. func (akuo *ApiKeyUpdateOne) ClearOpenaiBase() *ApiKeyUpdateOne {
  481. akuo.mutation.ClearOpenaiBase()
  482. return akuo
  483. }
  484. // SetOpenaiKey sets the "openai_key" field.
  485. func (akuo *ApiKeyUpdateOne) SetOpenaiKey(s string) *ApiKeyUpdateOne {
  486. akuo.mutation.SetOpenaiKey(s)
  487. return akuo
  488. }
  489. // SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
  490. func (akuo *ApiKeyUpdateOne) SetNillableOpenaiKey(s *string) *ApiKeyUpdateOne {
  491. if s != nil {
  492. akuo.SetOpenaiKey(*s)
  493. }
  494. return akuo
  495. }
  496. // ClearOpenaiKey clears the value of the "openai_key" field.
  497. func (akuo *ApiKeyUpdateOne) ClearOpenaiKey() *ApiKeyUpdateOne {
  498. akuo.mutation.ClearOpenaiKey()
  499. return akuo
  500. }
  501. // SetAgent sets the "agent" edge to the Agent entity.
  502. func (akuo *ApiKeyUpdateOne) SetAgent(a *Agent) *ApiKeyUpdateOne {
  503. return akuo.SetAgentID(a.ID)
  504. }
  505. // Mutation returns the ApiKeyMutation object of the builder.
  506. func (akuo *ApiKeyUpdateOne) Mutation() *ApiKeyMutation {
  507. return akuo.mutation
  508. }
  509. // ClearAgent clears the "agent" edge to the Agent entity.
  510. func (akuo *ApiKeyUpdateOne) ClearAgent() *ApiKeyUpdateOne {
  511. akuo.mutation.ClearAgent()
  512. return akuo
  513. }
  514. // Where appends a list predicates to the ApiKeyUpdate builder.
  515. func (akuo *ApiKeyUpdateOne) Where(ps ...predicate.ApiKey) *ApiKeyUpdateOne {
  516. akuo.mutation.Where(ps...)
  517. return akuo
  518. }
  519. // Select allows selecting one or more fields (columns) of the returned entity.
  520. // The default is selecting all fields defined in the entity schema.
  521. func (akuo *ApiKeyUpdateOne) Select(field string, fields ...string) *ApiKeyUpdateOne {
  522. akuo.fields = append([]string{field}, fields...)
  523. return akuo
  524. }
  525. // Save executes the query and returns the updated ApiKey entity.
  526. func (akuo *ApiKeyUpdateOne) Save(ctx context.Context) (*ApiKey, error) {
  527. if err := akuo.defaults(); err != nil {
  528. return nil, err
  529. }
  530. return withHooks(ctx, akuo.sqlSave, akuo.mutation, akuo.hooks)
  531. }
  532. // SaveX is like Save, but panics if an error occurs.
  533. func (akuo *ApiKeyUpdateOne) SaveX(ctx context.Context) *ApiKey {
  534. node, err := akuo.Save(ctx)
  535. if err != nil {
  536. panic(err)
  537. }
  538. return node
  539. }
  540. // Exec executes the query on the entity.
  541. func (akuo *ApiKeyUpdateOne) Exec(ctx context.Context) error {
  542. _, err := akuo.Save(ctx)
  543. return err
  544. }
  545. // ExecX is like Exec, but panics if an error occurs.
  546. func (akuo *ApiKeyUpdateOne) ExecX(ctx context.Context) {
  547. if err := akuo.Exec(ctx); err != nil {
  548. panic(err)
  549. }
  550. }
  551. // defaults sets the default values of the builder before save.
  552. func (akuo *ApiKeyUpdateOne) defaults() error {
  553. if _, ok := akuo.mutation.UpdatedAt(); !ok {
  554. if apikey.UpdateDefaultUpdatedAt == nil {
  555. return fmt.Errorf("ent: uninitialized apikey.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  556. }
  557. v := apikey.UpdateDefaultUpdatedAt()
  558. akuo.mutation.SetUpdatedAt(v)
  559. }
  560. return nil
  561. }
  562. // check runs all checks and user-defined validators on the builder.
  563. func (akuo *ApiKeyUpdateOne) check() error {
  564. if _, ok := akuo.mutation.AgentID(); akuo.mutation.AgentCleared() && !ok {
  565. return errors.New(`ent: clearing a required unique edge "ApiKey.agent"`)
  566. }
  567. return nil
  568. }
  569. func (akuo *ApiKeyUpdateOne) sqlSave(ctx context.Context) (_node *ApiKey, err error) {
  570. if err := akuo.check(); err != nil {
  571. return _node, err
  572. }
  573. _spec := sqlgraph.NewUpdateSpec(apikey.Table, apikey.Columns, sqlgraph.NewFieldSpec(apikey.FieldID, field.TypeUint64))
  574. id, ok := akuo.mutation.ID()
  575. if !ok {
  576. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ApiKey.id" for update`)}
  577. }
  578. _spec.Node.ID.Value = id
  579. if fields := akuo.fields; len(fields) > 0 {
  580. _spec.Node.Columns = make([]string, 0, len(fields))
  581. _spec.Node.Columns = append(_spec.Node.Columns, apikey.FieldID)
  582. for _, f := range fields {
  583. if !apikey.ValidColumn(f) {
  584. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  585. }
  586. if f != apikey.FieldID {
  587. _spec.Node.Columns = append(_spec.Node.Columns, f)
  588. }
  589. }
  590. }
  591. if ps := akuo.mutation.predicates; len(ps) > 0 {
  592. _spec.Predicate = func(selector *sql.Selector) {
  593. for i := range ps {
  594. ps[i](selector)
  595. }
  596. }
  597. }
  598. if value, ok := akuo.mutation.UpdatedAt(); ok {
  599. _spec.SetField(apikey.FieldUpdatedAt, field.TypeTime, value)
  600. }
  601. if value, ok := akuo.mutation.DeletedAt(); ok {
  602. _spec.SetField(apikey.FieldDeletedAt, field.TypeTime, value)
  603. }
  604. if akuo.mutation.DeletedAtCleared() {
  605. _spec.ClearField(apikey.FieldDeletedAt, field.TypeTime)
  606. }
  607. if value, ok := akuo.mutation.Title(); ok {
  608. _spec.SetField(apikey.FieldTitle, field.TypeString, value)
  609. }
  610. if akuo.mutation.TitleCleared() {
  611. _spec.ClearField(apikey.FieldTitle, field.TypeString)
  612. }
  613. if value, ok := akuo.mutation.Key(); ok {
  614. _spec.SetField(apikey.FieldKey, field.TypeString, value)
  615. }
  616. if akuo.mutation.KeyCleared() {
  617. _spec.ClearField(apikey.FieldKey, field.TypeString)
  618. }
  619. if value, ok := akuo.mutation.OrganizationID(); ok {
  620. _spec.SetField(apikey.FieldOrganizationID, field.TypeUint64, value)
  621. }
  622. if value, ok := akuo.mutation.AddedOrganizationID(); ok {
  623. _spec.AddField(apikey.FieldOrganizationID, field.TypeUint64, value)
  624. }
  625. if value, ok := akuo.mutation.CustomAgentBase(); ok {
  626. _spec.SetField(apikey.FieldCustomAgentBase, field.TypeString, value)
  627. }
  628. if akuo.mutation.CustomAgentBaseCleared() {
  629. _spec.ClearField(apikey.FieldCustomAgentBase, field.TypeString)
  630. }
  631. if value, ok := akuo.mutation.CustomAgentKey(); ok {
  632. _spec.SetField(apikey.FieldCustomAgentKey, field.TypeString, value)
  633. }
  634. if akuo.mutation.CustomAgentKeyCleared() {
  635. _spec.ClearField(apikey.FieldCustomAgentKey, field.TypeString)
  636. }
  637. if value, ok := akuo.mutation.OpenaiBase(); ok {
  638. _spec.SetField(apikey.FieldOpenaiBase, field.TypeString, value)
  639. }
  640. if akuo.mutation.OpenaiBaseCleared() {
  641. _spec.ClearField(apikey.FieldOpenaiBase, field.TypeString)
  642. }
  643. if value, ok := akuo.mutation.OpenaiKey(); ok {
  644. _spec.SetField(apikey.FieldOpenaiKey, field.TypeString, value)
  645. }
  646. if akuo.mutation.OpenaiKeyCleared() {
  647. _spec.ClearField(apikey.FieldOpenaiKey, field.TypeString)
  648. }
  649. if akuo.mutation.AgentCleared() {
  650. edge := &sqlgraph.EdgeSpec{
  651. Rel: sqlgraph.M2O,
  652. Inverse: true,
  653. Table: apikey.AgentTable,
  654. Columns: []string{apikey.AgentColumn},
  655. Bidi: false,
  656. Target: &sqlgraph.EdgeTarget{
  657. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  658. },
  659. }
  660. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  661. }
  662. if nodes := akuo.mutation.AgentIDs(); len(nodes) > 0 {
  663. edge := &sqlgraph.EdgeSpec{
  664. Rel: sqlgraph.M2O,
  665. Inverse: true,
  666. Table: apikey.AgentTable,
  667. Columns: []string{apikey.AgentColumn},
  668. Bidi: false,
  669. Target: &sqlgraph.EdgeTarget{
  670. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  671. },
  672. }
  673. for _, k := range nodes {
  674. edge.Target.Nodes = append(edge.Target.Nodes, k)
  675. }
  676. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  677. }
  678. _node = &ApiKey{config: akuo.config}
  679. _spec.Assign = _node.assignValues
  680. _spec.ScanValues = _node.scanValues
  681. if err = sqlgraph.UpdateNode(ctx, akuo.driver, _spec); err != nil {
  682. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  683. err = &NotFoundError{apikey.Label}
  684. } else if sqlgraph.IsConstraintError(err) {
  685. err = &ConstraintError{msg: err.Error(), wrap: err}
  686. }
  687. return nil, err
  688. }
  689. akuo.mutation.done = true
  690. return _node, nil
  691. }