| 193 | return labels |
| 194 | } |
| 195 | func (i *imlAPIService) Create(ctx context.Context, input *Create) (err error) { |
| 196 | operater := utils.UserId(ctx) |
| 197 | return i.store.Transaction(ctx, func(ctx context.Context) error { |
| 198 | if input.UUID != "" { |
| 199 | _, err = i.store.GetByUUID(ctx, input.UUID) |
| 200 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | } else { |
| 205 | input.UUID = uuid.NewString() |
| 206 | } |
| 207 | if input.Upstream == "" { |
| 208 | input.Upstream = input.Service |
| 209 | } |
| 210 | |
| 211 | ne := api.API{ |
| 212 | UUID: input.UUID, |
| 213 | Name: input.Name, |
| 214 | Service: input.Service, |
| 215 | Team: input.Team, |
| 216 | Creator: operater, |
| 217 | CreateAt: time.Now(), |
| 218 | Upstream: input.Upstream, |
| 219 | Method: input.Methods, |
| 220 | Path: input.Path, |
| 221 | } |
| 222 | err = i.store.Insert(ctx, &ne) |
| 223 | if err != nil { |
| 224 | return err |
| 225 | } |
| 226 | ev := &api.Info{ |
| 227 | Id: ne.Id, |
| 228 | UUID: ne.UUID, |
| 229 | Name: ne.Name, |
| 230 | Description: input.Description, |
| 231 | Updater: operater, |
| 232 | UpdateAt: time.Now(), |
| 233 | Creator: operater, |
| 234 | CreateAt: time.Now(), |
| 235 | Method: input.Methods, |
| 236 | Path: input.Path, |
| 237 | Match: input.Match, |
| 238 | Upstream: input.Upstream, |
| 239 | Service: input.Service, |
| 240 | Team: input.Team, |
| 241 | } |
| 242 | err = i.apiInfoStore.Save(ctx, ev) |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | err = i.store.SetLabels(ctx, ne.Id, getLabels(ev)...) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | return nil |
| 251 | }) |
| 252 | } |