Handler for both add and delete
(c *gin.Context, taskNamePrefix string, cb func(list *deb.PackageList, p *deb.Package, out aptly.Progress) error)
| 412 | |
| 413 | // Handler for both add and delete |
| 414 | func apiReposPackagesAddDelete(c *gin.Context, taskNamePrefix string, cb func(list *deb.PackageList, p *deb.Package, out aptly.Progress) error) { |
| 415 | var b reposPackagesAddDeleteParams |
| 416 | |
| 417 | if c.Bind(&b) != nil { |
| 418 | return |
| 419 | } |
| 420 | |
| 421 | // Load shallowly for 404 check and resource key. |
| 422 | // Full load and mutations happen inside the task. |
| 423 | collectionFactory := context.NewCollectionFactory() |
| 424 | collection := collectionFactory.LocalRepoCollection() |
| 425 | |
| 426 | name := c.Params.ByName("name") |
| 427 | repo, err := collection.ByName(name) |
| 428 | if err != nil { |
| 429 | AbortWithJSONError(c, 404, err) |
| 430 | return |
| 431 | } |
| 432 | |
| 433 | resources := []string{string(repo.Key())} |
| 434 | |
| 435 | maybeRunTaskInBackground(c, taskNamePrefix+repo.Name, resources, func(out aptly.Progress, _ *task.Detail) (*task.ProcessReturnValue, error) { |
| 436 | // Task: Create fresh factory and collection inside task after lock |
| 437 | taskCollectionFactory := context.NewCollectionFactory() |
| 438 | taskCollection := taskCollectionFactory.LocalRepoCollection() |
| 439 | |
| 440 | // Fresh load after lock acquired (use captured `name` variable, not gin context) |
| 441 | repo, err := taskCollection.ByName(name) |
| 442 | if err != nil { |
| 443 | return &task.ProcessReturnValue{Code: http.StatusNotFound, Value: nil}, err |
| 444 | } |
| 445 | |
| 446 | err = taskCollection.LoadComplete(repo) |
| 447 | if err != nil { |
| 448 | return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, err |
| 449 | } |
| 450 | |
| 451 | out.Printf("Loading packages...\n") |
| 452 | list, err := deb.NewPackageListFromRefList(repo.RefList(), taskCollectionFactory.PackageCollection(), nil) |
| 453 | if err != nil { |
| 454 | return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, err |
| 455 | } |
| 456 | |
| 457 | // verify package refs and build package list |
| 458 | for _, ref := range b.PackageRefs { |
| 459 | var p *deb.Package |
| 460 | |
| 461 | p, err = taskCollectionFactory.PackageCollection().ByKey([]byte(ref)) |
| 462 | if err != nil { |
| 463 | if err == database.ErrNotFound { |
| 464 | return &task.ProcessReturnValue{Code: http.StatusNotFound, Value: nil}, fmt.Errorf("packages %s: %s", ref, err) |
| 465 | } |
| 466 | |
| 467 | return &task.ProcessReturnValue{Code: http.StatusInternalServerError, Value: nil}, err |
| 468 | } |
| 469 | err = cb(list, p, out) |
| 470 | if err != nil { |
| 471 | return &task.ProcessReturnValue{Code: http.StatusBadRequest, Value: nil}, err |
no test coverage detected