Skip to content

Commit 111b0bf

Browse files
committed
Use sort order instead of date (enabling drag and drop)
1 parent 0e29b25 commit 111b0bf

File tree

5 files changed

+52
-21
lines changed

5 files changed

+52
-21
lines changed

LinkDotNet.Blog.IntegrationTests/Web/Shared/ProfileTests.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using Bunit;
43
using FluentAssertions;
54
using LinkDotNet.Blog.TestUtilities;
@@ -16,10 +15,10 @@ namespace LinkDotNet.Blog.IntegrationTests.Web.Shared
1615
public class ProfileTests : TestContext
1716
{
1817
[Fact]
19-
public void ShouldRenderAllItemsSortedByCreated()
18+
public void ShouldRenderAllItemsSortedByOrder()
2019
{
21-
var entry1 = new ProfileInformationEntryBuilder().WithContent("key 1").WithCreatedDate(new DateTime(1)).Build();
22-
var entry2 = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
20+
var entry1 = new ProfileInformationEntryBuilder().WithContent("key 1").WithSortOrder(1).Build();
21+
var entry2 = new ProfileInformationEntryBuilder().WithContent("key 2").WithSortOrder(2).Build();
2322
var repoMock = RegisterServices();
2423
repoMock.Setup(r => r.GetAllAsync())
2524
.ReturnsAsync(new List<ProfileInformationEntry> { entry1, entry2 });
@@ -62,12 +61,13 @@ public void ShouldAddEntry()
6261

6362
entryToDb.Should().NotBeNull();
6463
entryToDb.Content.Should().Be("key");
64+
entryToDb.SortOrder.Should().Be(0);
6565
}
6666

6767
[Fact]
6868
public void ShouldDeleteEntryWhenConfirmed()
6969
{
70-
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
70+
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").Build();
7171
entryToDelete.Id = "SomeId";
7272
var repoMock = RegisterServices();
7373
repoMock.Setup(r => r.GetAllAsync()).ReturnsAsync(new[] { entryToDelete });
@@ -82,7 +82,7 @@ public void ShouldDeleteEntryWhenConfirmed()
8282
[Fact]
8383
public void ShouldNotDeleteEntryWhenNotConfirmed()
8484
{
85-
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").WithCreatedDate(new DateTime(2)).Build();
85+
var entryToDelete = new ProfileInformationEntryBuilder().WithContent("key 2").Build();
8686
entryToDelete.Id = "SomeId";
8787
var repoMock = RegisterServices();
8888
repoMock.Setup(r => r.GetAllAsync()).ReturnsAsync(new[] { entryToDelete });
@@ -94,6 +94,26 @@ public void ShouldNotDeleteEntryWhenNotConfirmed()
9494
repoMock.Verify(r => r.DeleteAsync("SomeId"), Times.Never);
9595
}
9696

97+
[Fact]
98+
public void ShouldAddEntryWithCorrectSortOrder()
99+
{
100+
var repo = RegisterServices();
101+
var entry = new ProfileInformationEntryBuilder().WithSortOrder(1).Build();
102+
repo.Setup(p => p.GetAllAsync()).ReturnsAsync(new[] { entry });
103+
ProfileInformationEntry entryToDb = null;
104+
repo.Setup(p => p.AddAsync(It.IsAny<ProfileInformationEntry>()))
105+
.Callback<ProfileInformationEntry>(p => entryToDb = p);
106+
var cut = RenderComponent<Profile>(p => p.Add(s => s.IsAuthenticated, true));
107+
var addShortItemComponent = cut.FindComponent<AddProfileShortItem>();
108+
addShortItemComponent.Find("input").Change("key");
109+
110+
addShortItemComponent.Find("button").Click();
111+
112+
entryToDb.Should().NotBeNull();
113+
entryToDb.Content.Should().Be("key");
114+
entryToDb.SortOrder.Should().Be(1001);
115+
}
116+
97117
private static AppConfiguration CreateEmptyConfiguration()
98118
{
99119
return new()
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1-
using System;
2-
using LinkDotNet.Domain;
1+
using LinkDotNet.Domain;
32

43
namespace LinkDotNet.Blog.TestUtilities
54
{
65
public class ProfileInformationEntryBuilder
76
{
87
private string content = "Content";
9-
private DateTime? createdDate;
8+
private int sortOrder;
109

1110
public ProfileInformationEntryBuilder WithContent(string key)
1211
{
1312
this.content = key;
1413
return this;
1514
}
1615

17-
public ProfileInformationEntryBuilder WithCreatedDate(DateTime createdDate)
16+
public ProfileInformationEntryBuilder WithSortOrder(int sortOrder)
1817
{
19-
this.createdDate = createdDate;
18+
this.sortOrder = sortOrder;
2019
return this;
2120
}
2221

2322
public ProfileInformationEntry Build()
2423
{
25-
return ProfileInformationEntry.Create(content, createdDate);
24+
return ProfileInformationEntry.Create(content, sortOrder);
2625
}
2726
}
2827
}

LinkDotNet.Blog.UnitTests/Domain/ProfileInformationEntryTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ public class ProfileInformationEntryTests
1010
[Fact]
1111
public void ShouldCreateObject()
1212
{
13-
var result = ProfileInformationEntry.Create("key");
13+
var result = ProfileInformationEntry.Create("key", 12);
1414

1515
result.Content.Should().Be("key");
16+
result.SortOrder.Should().Be(12);
1617
}
1718

1819
[Theory]
@@ -21,15 +22,15 @@ public void ShouldCreateObject()
2122
[InlineData(null)]
2223
public void ShouldThrowExceptionWhenEmptyKeyOrValue(string content)
2324
{
24-
Action act = () => ProfileInformationEntry.Create(content);
25+
Action act = () => ProfileInformationEntry.Create(content, 0);
2526

2627
act.Should().Throw<ArgumentOutOfRangeException>();
2728
}
2829

2930
[Fact]
3031
public void ShouldTrim()
3132
{
32-
var result = ProfileInformationEntry.Create(" key ");
33+
var result = ProfileInformationEntry.Create(" key ", 0);
3334

3435
result.Content.Should().Be("key");
3536
}

LinkDotNet.Blog.Web/Shared/Profile.razor

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ OnYesPressed="DeleteItem"></ConfirmDialog>
4747

4848
protected override async Task OnInitializedAsync()
4949
{
50-
profileInformationEntries = (await repository.GetAllAsync()).OrderBy(d => d.CreatedDate).ToList();
50+
profileInformationEntries = (await repository.GetAllAsync()).OrderBy(d => d.SortOrder).ToList();
5151
}
5252

5353
private void ShowDeleteDialog(string key)
@@ -65,9 +65,20 @@ OnYesPressed="DeleteItem"></ConfirmDialog>
6565

6666
private async Task AddValue(string toAdd)
6767
{
68-
var newEntry = ProfileInformationEntry.Create(toAdd);
68+
var sortOrder = GetSortOrder();
69+
var newEntry = ProfileInformationEntry.Create(toAdd, sortOrder);
6970

7071
profileInformationEntries.Add(newEntry);
7172
await repository.AddAsync(newEntry);
7273
}
74+
75+
private int GetSortOrder()
76+
{
77+
if (profileInformationEntries.Any())
78+
{
79+
return profileInformationEntries.Max(p => p.SortOrder) + 1000;
80+
}
81+
82+
return 0;
83+
}
7384
}

LinkDotNet.Domain/ProfileInformationEntry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ private ProfileInformationEntry()
1212

1313
public string Content { get; private set; }
1414

15-
public DateTime CreatedDate { get; private set; }
15+
public int SortOrder { get; set; }
1616

17-
public static ProfileInformationEntry Create(string key, DateTime? createdDate = null)
17+
public static ProfileInformationEntry Create(string key, int sortOrder)
1818
{
1919
if (string.IsNullOrWhiteSpace(key))
2020
{
@@ -24,7 +24,7 @@ public static ProfileInformationEntry Create(string key, DateTime? createdDate =
2424
return new ProfileInformationEntry
2525
{
2626
Content = key.Trim(),
27-
CreatedDate = createdDate ?? DateTime.Now,
27+
SortOrder = sortOrder,
2828
};
2929
}
3030
}

0 commit comments

Comments
 (0)