-
Notifications
You must be signed in to change notification settings - Fork 894
/
groupinfo_create.go
161 lines (148 loc) · 4.03 KB
/
groupinfo_create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by entc, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"github.com/facebookincubator/ent/dialect/sql/sqlgraph"
"github.com/facebookincubator/ent/entc/integration/ent/group"
"github.com/facebookincubator/ent/entc/integration/ent/groupinfo"
"github.com/facebookincubator/ent/schema/field"
)
// GroupInfoCreate is the builder for creating a GroupInfo entity.
type GroupInfoCreate struct {
config
mutation *GroupInfoMutation
hooks []Hook
}
// SetDesc sets the desc field.
func (gic *GroupInfoCreate) SetDesc(s string) *GroupInfoCreate {
gic.mutation.SetDesc(s)
return gic
}
// SetMaxUsers sets the max_users field.
func (gic *GroupInfoCreate) SetMaxUsers(i int) *GroupInfoCreate {
gic.mutation.SetMaxUsers(i)
return gic
}
// SetNillableMaxUsers sets the max_users field if the given value is not nil.
func (gic *GroupInfoCreate) SetNillableMaxUsers(i *int) *GroupInfoCreate {
if i != nil {
gic.SetMaxUsers(*i)
}
return gic
}
// AddGroupIDs adds the groups edge to Group by ids.
func (gic *GroupInfoCreate) AddGroupIDs(ids ...int) *GroupInfoCreate {
gic.mutation.AddGroupIDs(ids...)
return gic
}
// AddGroups adds the groups edges to Group.
func (gic *GroupInfoCreate) AddGroups(g ...*Group) *GroupInfoCreate {
ids := make([]int, len(g))
for i := range g {
ids[i] = g[i].ID
}
return gic.AddGroupIDs(ids...)
}
// Save creates the GroupInfo in the database.
func (gic *GroupInfoCreate) Save(ctx context.Context) (*GroupInfo, error) {
if _, ok := gic.mutation.Desc(); !ok {
return nil, errors.New("ent: missing required field \"desc\"")
}
if _, ok := gic.mutation.MaxUsers(); !ok {
v := groupinfo.DefaultMaxUsers
gic.mutation.SetMaxUsers(v)
}
var (
err error
node *GroupInfo
)
if len(gic.hooks) == 0 {
node, err = gic.sqlSave(ctx)
} else {
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*GroupInfoMutation)
if !ok {
return nil, fmt.Errorf("unexpected mutation type %T", m)
}
gic.mutation = mutation
node, err = gic.sqlSave(ctx)
return node, err
})
for i := len(gic.hooks) - 1; i >= 0; i-- {
mut = gic.hooks[i](mut)
}
if _, err := mut.Mutate(ctx, gic.mutation); err != nil {
return nil, err
}
}
return node, err
}
// SaveX calls Save and panics if Save returns an error.
func (gic *GroupInfoCreate) SaveX(ctx context.Context) *GroupInfo {
v, err := gic.Save(ctx)
if err != nil {
panic(err)
}
return v
}
func (gic *GroupInfoCreate) sqlSave(ctx context.Context) (*GroupInfo, error) {
var (
gi = &GroupInfo{config: gic.config}
_spec = &sqlgraph.CreateSpec{
Table: groupinfo.Table,
ID: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: groupinfo.FieldID,
},
}
)
if value, ok := gic.mutation.Desc(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeString,
Value: value,
Column: groupinfo.FieldDesc,
})
gi.Desc = value
}
if value, ok := gic.mutation.MaxUsers(); ok {
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
Type: field.TypeInt,
Value: value,
Column: groupinfo.FieldMaxUsers,
})
gi.MaxUsers = value
}
if nodes := gic.mutation.GroupsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: true,
Table: groupinfo.GroupsTable,
Columns: []string{groupinfo.GroupsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: &sqlgraph.FieldSpec{
Type: field.TypeInt,
Column: group.FieldID,
},
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
if err := sqlgraph.CreateNode(ctx, gic.driver, _spec); err != nil {
if cerr, ok := isSQLConstraintError(err); ok {
err = cerr
}
return nil, err
}
id := _spec.ID.Value.(int64)
gi.ID = int(id)
return gi, nil
}