From 1df47ef6682d324a018c3c3c17d92c39883ddd48 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Wed, 2 Aug 2023 15:06:46 -0500 Subject: [PATCH] cleanup unnecessary calls --- backend/internal/data/repo/repo_items_test.go | 6 +++--- backend/internal/data/repo/repo_labels.go | 13 +++---------- backend/internal/data/repo/repo_labels_test.go | 10 +++++----- backend/internal/data/repo/repo_locations.go | 8 +++----- backend/internal/data/repo/repo_locations_test.go | 12 ++++++------ 5 files changed, 20 insertions(+), 29 deletions(-) diff --git a/backend/internal/data/repo/repo_items_test.go b/backend/internal/data/repo/repo_items_test.go index ac2814d..ba991f9 100644 --- a/backend/internal/data/repo/repo_items_test.go +++ b/backend/internal/data/repo/repo_items_test.go @@ -39,7 +39,7 @@ func useItems(t *testing.T, len int) []ItemOut { _ = tRepos.Items.Delete(context.Background(), item.ID) } - _ = tRepos.Locations.Delete(context.Background(), location.ID) + _ = tRepos.Locations.delete(context.Background(), location.ID) }) return items @@ -123,7 +123,7 @@ func TestItemsRepository_Create(t *testing.T) { assert.NotEmpty(t, result.ID) // Cleanup - Also deletes item - err = tRepos.Locations.Delete(context.Background(), location.ID) + err = tRepos.Locations.delete(context.Background(), location.ID) assert.NoError(t, err) } @@ -147,7 +147,7 @@ func TestItemsRepository_Create_Location(t *testing.T) { assert.Equal(t, location.ID, foundItem.Location.ID) // Cleanup - Also deletes item - err = tRepos.Locations.Delete(context.Background(), location.ID) + err = tRepos.Locations.delete(context.Background(), location.ID) assert.NoError(t, err) } diff --git a/backend/internal/data/repo/repo_labels.go b/backend/internal/data/repo/repo_labels.go index e9ad303..7814577 100644 --- a/backend/internal/data/repo/repo_labels.go +++ b/backend/internal/data/repo/repo_labels.go @@ -131,15 +131,6 @@ func (r *LabelRepository) update(ctx context.Context, data LabelUpdate, where .. Save(ctx) } -func (r *LabelRepository) Update(ctx context.Context, data LabelUpdate) (LabelOut, error) { - _, err := r.update(ctx, data, label.ID(data.ID)) - if err != nil { - return LabelOut{}, err - } - - return r.GetOne(ctx, data.ID) -} - func (r *LabelRepository) UpdateByGroup(ctx context.Context, GID uuid.UUID, data LabelUpdate) (LabelOut, error) { _, err := r.update(ctx, data, label.ID(data.ID), label.HasGroupWith(group.ID(GID))) if err != nil { @@ -150,7 +141,9 @@ func (r *LabelRepository) UpdateByGroup(ctx context.Context, GID uuid.UUID, data return r.GetOne(ctx, data.ID) } -func (r *LabelRepository) Delete(ctx context.Context, id uuid.UUID) error { +// delete removes the label from the database. This should only be used when +// the label's ownership is already confirmed/validated. +func (r *LabelRepository) delete(ctx context.Context, id uuid.UUID) error { return r.db.Label.DeleteOneID(id).Exec(ctx) } diff --git a/backend/internal/data/repo/repo_labels_test.go b/backend/internal/data/repo/repo_labels_test.go index 691b915..f3b331c 100644 --- a/backend/internal/data/repo/repo_labels_test.go +++ b/backend/internal/data/repo/repo_labels_test.go @@ -28,7 +28,7 @@ func useLabels(t *testing.T, len int) []LabelOut { t.Cleanup(func() { for _, item := range labels { - _ = tRepos.Labels.Delete(context.Background(), item.ID) + _ = tRepos.Labels.delete(context.Background(), item.ID) } }) @@ -62,7 +62,7 @@ func TestLabelRepository_Create(t *testing.T) { assert.NoError(t, err) assert.Equal(t, loc.ID, foundLoc.ID) - err = tRepos.Labels.Delete(context.Background(), loc.ID) + err = tRepos.Labels.delete(context.Background(), loc.ID) assert.NoError(t, err) } @@ -76,7 +76,7 @@ func TestLabelRepository_Update(t *testing.T) { Description: fk.Str(100), } - update, err := tRepos.Labels.Update(context.Background(), updateData) + update, err := tRepos.Labels.UpdateByGroup(context.Background(), tGroup.ID, updateData) assert.NoError(t, err) foundLoc, err := tRepos.Labels.GetOne(context.Background(), loc.ID) @@ -86,7 +86,7 @@ func TestLabelRepository_Update(t *testing.T) { assert.Equal(t, update.Name, foundLoc.Name) assert.Equal(t, update.Description, foundLoc.Description) - err = tRepos.Labels.Delete(context.Background(), loc.ID) + err = tRepos.Labels.delete(context.Background(), loc.ID) assert.NoError(t, err) } @@ -94,7 +94,7 @@ func TestLabelRepository_Delete(t *testing.T) { loc, err := tRepos.Labels.Create(context.Background(), tGroup.ID, labelFactory()) assert.NoError(t, err) - err = tRepos.Labels.Delete(context.Background(), loc.ID) + err = tRepos.Labels.delete(context.Background(), loc.ID) assert.NoError(t, err) _, err = tRepos.Labels.GetOne(context.Background(), loc.ID) diff --git a/backend/internal/data/repo/repo_locations.go b/backend/internal/data/repo/repo_locations.go index 12b7e97..8c0ebfc 100644 --- a/backend/internal/data/repo/repo_locations.go +++ b/backend/internal/data/repo/repo_locations.go @@ -222,10 +222,6 @@ func (r *LocationRepository) update(ctx context.Context, data LocationUpdate, wh return r.Get(ctx, data.ID) } -func (r *LocationRepository) Update(ctx context.Context, data LocationUpdate) (LocationOut, error) { - return r.update(ctx, data, location.ID(data.ID)) -} - func (r *LocationRepository) UpdateByGroup(ctx context.Context, GID, ID uuid.UUID, data LocationUpdate) (LocationOut, error) { v, err := r.update(ctx, data, location.ID(ID), location.HasGroupWith(group.ID(GID))) if err != nil { @@ -236,7 +232,9 @@ func (r *LocationRepository) UpdateByGroup(ctx context.Context, GID, ID uuid.UUI return v, err } -func (r *LocationRepository) Delete(ctx context.Context, ID uuid.UUID) error { +// delete should only be used after checking that the location is owned by the +// group. Otherwise, use DeleteByGroup +func (r *LocationRepository) delete(ctx context.Context, ID uuid.UUID) error { return r.db.Location.DeleteOneID(ID).Exec(ctx) } diff --git a/backend/internal/data/repo/repo_locations_test.go b/backend/internal/data/repo/repo_locations_test.go index 8840b51..c649bcb 100644 --- a/backend/internal/data/repo/repo_locations_test.go +++ b/backend/internal/data/repo/repo_locations_test.go @@ -30,7 +30,7 @@ func useLocations(t *testing.T, len int) []LocationOut { t.Cleanup(func() { for _, loc := range out { - err := tRepos.Locations.Delete(context.Background(), loc.ID) + err := tRepos.Locations.delete(context.Background(), loc.ID) if err != nil { assert.True(t, ent.IsNotFound(err)) } @@ -49,7 +49,7 @@ func TestLocationRepository_Get(t *testing.T) { assert.NoError(t, err) assert.Equal(t, loc.ID, foundLoc.ID) - err = tRepos.Locations.Delete(context.Background(), loc.ID) + err = tRepos.Locations.delete(context.Background(), loc.ID) assert.NoError(t, err) } @@ -83,7 +83,7 @@ func TestLocationRepository_Create(t *testing.T) { assert.NoError(t, err) assert.Equal(t, loc.ID, foundLoc.ID) - err = tRepos.Locations.Delete(context.Background(), loc.ID) + err = tRepos.Locations.delete(context.Background(), loc.ID) assert.NoError(t, err) } @@ -96,7 +96,7 @@ func TestLocationRepository_Update(t *testing.T) { Description: fk.Str(100), } - update, err := tRepos.Locations.Update(context.Background(), updateData) + update, err := tRepos.Locations.UpdateByGroup(context.Background(), tGroup.ID, updateData.ID, updateData) assert.NoError(t, err) foundLoc, err := tRepos.Locations.Get(context.Background(), loc.ID) @@ -106,14 +106,14 @@ func TestLocationRepository_Update(t *testing.T) { assert.Equal(t, update.Name, foundLoc.Name) assert.Equal(t, update.Description, foundLoc.Description) - err = tRepos.Locations.Delete(context.Background(), loc.ID) + err = tRepos.Locations.delete(context.Background(), loc.ID) assert.NoError(t, err) } func TestLocationRepository_Delete(t *testing.T) { loc := useLocations(t, 1)[0] - err := tRepos.Locations.Delete(context.Background(), loc.ID) + err := tRepos.Locations.delete(context.Background(), loc.ID) assert.NoError(t, err) _, err = tRepos.Locations.Get(context.Background(), loc.ID)