From 99aeef3b7d5e3ef39b604b0a5aa08face2bb006b Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Mon, 4 Nov 2024 21:37:07 +0300 Subject: [PATCH] Fix sorting notes (#128) * Fix sorting notes * Correct sort by MealType + DisplayOrder * Refactor NoteBuilder to be reusable --- .../Repositories/v2/NotesRepository.cs | 2 + .../Dsl/NoteBuilder.cs | 60 ++++++++++++------- .../Scenarios/Notes/NotesApiContext.cs | 4 +- .../Scenarios/Notes/NotesApiTests.cs | 13 ++-- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/backend/src/FoodDiary.Infrastructure/Repositories/v2/NotesRepository.cs b/src/backend/src/FoodDiary.Infrastructure/Repositories/v2/NotesRepository.cs index 6eab4a1ac..9f3141591 100644 --- a/src/backend/src/FoodDiary.Infrastructure/Repositories/v2/NotesRepository.cs +++ b/src/backend/src/FoodDiary.Infrastructure/Repositories/v2/NotesRepository.cs @@ -17,6 +17,8 @@ public async Task> FindByDate(DateOnly date, Cancellat .AsNoTracking() .Where(n => n.Date == date) .Include(n => n.Product) + .OrderBy(n => n.MealType) + .ThenBy(n => n.DisplayOrder) .ToListAsync(cancellationToken); } diff --git a/src/backend/tests/FoodDiary.ComponentTests/Dsl/NoteBuilder.cs b/src/backend/tests/FoodDiary.ComponentTests/Dsl/NoteBuilder.cs index eb3fc2d39..8a71383cf 100644 --- a/src/backend/tests/FoodDiary.ComponentTests/Dsl/NoteBuilder.cs +++ b/src/backend/tests/FoodDiary.ComponentTests/Dsl/NoteBuilder.cs @@ -6,46 +6,66 @@ namespace FoodDiary.ComponentTests.Dsl; public class NoteBuilder { - private readonly Note _note = new() + private int? _id; + private DateOnly _date = DateOnly.Parse(FakeDateTimeProvider.CurrentFakeDateAsString); + private MealType _mealType = MealType.Breakfast; + private Product _product = Create.Product().Please(); + private int _productId; + private int _productQuantity = 100; + private int _displayOrder = 1; + + public Note Please() => new() { - Id = Random.Shared.Next(), - Date = DateOnly.Parse(FakeDateTimeProvider.CurrentFakeDateAsString), - MealType = MealType.Breakfast, - Product = Create.Product().Please(), - ProductQuantity = 100 + Id = _id ?? Random.Shared.Next(), + Date = _date, + MealType = _mealType, + Product = _product, + ProductId = _productId, + ProductQuantity = _productQuantity, + DisplayOrder = _displayOrder }; - - public Note Please() => _note; public NoteBuilder WithDate(string date) { - _note.Date = DateOnly.Parse(date); + _date = DateOnly.Parse(date); return this; } public NoteBuilder WithProduct(Product product, int quantity) { - _note.Product = product; - _note.ProductQuantity = quantity; + _product = product; + _productQuantity = quantity; return this; } public NoteBuilder WithProduct(string productName, int quantity) { - _note.Product = Create.Product(productName).Please(); - _note.ProductQuantity = quantity; + _product = Create.Product(productName).Please(); + _productQuantity = quantity; + return this; + } + + public NoteBuilder WithMealType(MealType mealType) + { + _mealType = mealType; + return this; + } + + public NoteBuilder WithDisplayOrder(int displayOrder) + { + _displayOrder = displayOrder; return this; } public NoteBuilder From(Note note) { - _note.Id = note.Id; - _note.Date = note.Date; - _note.MealType = note.MealType; - _note.ProductId = note.ProductId; - _note.ProductQuantity = note.ProductQuantity; - _note.DisplayOrder = note.DisplayOrder; - _note.Product = note.Product; + _id = note.Id; + _date = note.Date; + _mealType = note.MealType; + _product = note.Product; + _productId = note.ProductId; + _productQuantity = note.ProductQuantity; + _displayOrder = note.DisplayOrder; return this; } } \ No newline at end of file diff --git a/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiContext.cs b/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiContext.cs index 3806f5e5e..b379c16f3 100644 --- a/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiContext.cs +++ b/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiContext.cs @@ -122,7 +122,9 @@ public Task Then_notes_list_contains_items(params Note[] items) .Excluding(note => note.Id) .Excluding(note => note.ProductId) .Excluding(note => note.Calories)) - .And.AllSatisfy(note => { note.Calories.Should().BePositive(); }); + .And.AllSatisfy(note => { note.Calories.Should().BePositive(); }) + .And.BeInAscendingOrder(note => note.MealType) + .And.ThenBeInAscendingOrder(note => note.DisplayOrder); return Task.CompletedTask; } diff --git a/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiTests.cs b/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiTests.cs index 0ab067af8..d250bc71b 100644 --- a/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiTests.cs +++ b/src/backend/tests/FoodDiary.ComponentTests/Scenarios/Notes/NotesApiTests.cs @@ -1,6 +1,7 @@ using System.Net; using FoodDiary.ComponentTests.Dsl; using FoodDiary.ComponentTests.Infrastructure; +using FoodDiary.Domain.Enums; namespace FoodDiary.ComponentTests.Scenarios.Notes; @@ -14,17 +15,21 @@ protected override NotesApiContext CreateContext( [Scenario] public Task I_can_retrieve_notes_list() { + var breakfast = Create.Note().WithDate("2024-01-04").WithMealType(MealType.Breakfast); + var lunch = Create.Note().WithDate("2024-01-04").WithMealType(MealType.Lunch); var notes = new { - Chicken = Create.Note().WithDate("2024-01-04").WithProduct("Chicken", 150).Please(), - Rice = Create.Note().WithDate("2024-01-04").WithProduct("Rice", 100).Please() + Chicken = breakfast.WithProduct("Chicken", 150).WithDisplayOrder(0).Please(), + Rice = breakfast.WithProduct("Rice", 100).WithDisplayOrder(1).Please(), + Cheese = lunch.WithProduct("Cheese", 350).WithDisplayOrder(0).Please(), + Bread = lunch.WithProduct("Bread", 250).WithDisplayOrder(1).Please(), }; return Run( c => c.Given_authenticated_user(), - c => c.Given_notes(notes.Chicken, notes.Rice), + c => c.Given_notes(notes.Rice, notes.Bread, notes.Chicken, notes.Cheese), c => c.When_user_retrieves_notes_list_for_date("2024-01-04"), - c => c.Then_notes_list_contains_items(notes.Chicken, notes.Rice)); + c => c.Then_notes_list_contains_items(notes.Chicken, notes.Rice, notes.Cheese, notes.Bread)); } [Scenario]