|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/tailwarden/komiser/models" |
| 7 | + "github.com/tailwarden/komiser/repository" |
| 8 | +) |
| 9 | + |
| 10 | +func (ctrl *Controller) ListAccounts(c context.Context) (accounts []models.Account, err error) { |
| 11 | + _, err = ctrl.repo.HandleQuery(c, repository.ListKey, &accounts, nil) |
| 12 | + return |
| 13 | +} |
| 14 | + |
| 15 | +func (ctrl *Controller) CountResources(c context.Context, provider, name string) (output totalOutput, err error) { |
| 16 | + conditions := [][3]string{} |
| 17 | + if provider != "" { |
| 18 | + conditions = append(conditions, [3]string{"provider", "=", provider}) |
| 19 | + } |
| 20 | + if name != "" { |
| 21 | + conditions = append(conditions, [3]string{"account", "=", name}) |
| 22 | + } |
| 23 | + _, err = ctrl.repo.HandleQuery(c, repository.ResourceCountKey, &output, conditions) |
| 24 | + return |
| 25 | +} |
| 26 | + |
| 27 | +func (ctrl *Controller) InsertAccount(c context.Context, account models.Account) (lastId int64, err error) { |
| 28 | + result, err := ctrl.repo.HandleQuery(c, repository.InsertKey, &account, nil) |
| 29 | + if err != nil { |
| 30 | + return |
| 31 | + } |
| 32 | + return result.LastInsertId() |
| 33 | +} |
| 34 | + |
| 35 | +func (ctrl *Controller) RescanAccount(c context.Context, account *models.Account, accountId string) (rows int64, err error) { |
| 36 | + res, err := ctrl.repo.HandleQuery(c, repository.ReScanAccountKey, account, [][3]string{{"id", "=", accountId}, {"status", "=", "CONNECTED"}}) |
| 37 | + if err != nil { |
| 38 | + return 0, err |
| 39 | + } |
| 40 | + return res.RowsAffected() |
| 41 | +} |
| 42 | + |
| 43 | +func (ctrl *Controller) DeleteAccount(c context.Context, accountId string) (err error) { |
| 44 | + _, err = ctrl.repo.HandleQuery(c, repository.DeleteKey, new(models.Account), [][3]string{{"id", "=", accountId}}) |
| 45 | + return |
| 46 | +} |
| 47 | + |
| 48 | +func (ctrl *Controller) UpdateAccount(c context.Context, account models.Account, accountId string) (err error) { |
| 49 | + _, err = ctrl.repo.HandleQuery(c, repository.UpdateAccountKey, &account, [][3]string{{"id", "=", accountId}}) |
| 50 | + return |
| 51 | +} |
0 commit comments