Use Comment.Text instead of ValueObjects.Name

This commit is contained in:
Biarity 2019-03-18 08:23:37 +10:00
parent d4b85b6bbc
commit a582c6be06
6 changed files with 32 additions and 76 deletions

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework> <TargetFramework>netstandard2.0</TargetFramework>
<Version>2.3.1</Version> <Version>2.3.2</Version>
<Description>Sieve is a simple, clean, and extensible framework for .NET Core that adds sorting, filtering, and pagination functionality out of the box. Most common use case would be for serving ASP.NET Core GET queries. Documentation available on GitHub: https://github.com/Biarity/Sieve/ <Description>Sieve is a simple, clean, and extensible framework for .NET Core that adds sorting, filtering, and pagination functionality out of the box. Most common use case would be for serving ASP.NET Core GET queries. Documentation available on GitHub: https://github.com/Biarity/Sieve/
</Description> </Description>
<Copyright>Copyright 2018</Copyright> <Copyright>Copyright 2018</Copyright>
@ -10,7 +10,7 @@
<PackageProjectUrl>https://github.com/Biarity/Sieve</PackageProjectUrl> <PackageProjectUrl>https://github.com/Biarity/Sieve</PackageProjectUrl>
<PackageIconUrl>https://emojipedia-us.s3.amazonaws.com/thumbs/240/twitter/120/alembic_2697.png</PackageIconUrl> <PackageIconUrl>https://emojipedia-us.s3.amazonaws.com/thumbs/240/twitter/120/alembic_2697.png</PackageIconUrl>
<RepositoryUrl></RepositoryUrl> <RepositoryUrl></RepositoryUrl>
<PackageReleaseNotes>Filtering and sorting for nested objects (#51)</PackageReleaseNotes> <PackageReleaseNotes>Allow subvalues with identical names (#54)</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Authors>Biarity</Authors> <Authors>Biarity</Authors>

View File

@ -1,6 +1,5 @@
using System; using System;
using Sieve.Attributes; using Sieve.Attributes;
using SieveUnitTests.ValueObjects;
namespace SieveUnitTests.Entities namespace SieveUnitTests.Entities
{ {
@ -8,10 +7,6 @@ namespace SieveUnitTests.Entities
{ {
public int Id { get; set; } public int Id { get; set; }
public Name AuthorFirstName { get; set; }
public Name AuthorLastName { get; set; }
[Sieve(CanFilter = true, CanSort = true)] [Sieve(CanFilter = true, CanSort = true)]
public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow; public DateTimeOffset DateCreated { get; set; } = DateTimeOffset.UtcNow;

View File

@ -32,5 +32,6 @@ namespace SieveUnitTests.Entities
public int OnlySortableViaFluentApi { get; set; } public int OnlySortableViaFluentApi { get; set; }
public Comment TopComment { get; set; } public Comment TopComment { get; set; }
public Comment FeaturedComment { get; set; }
} }
} }

View File

@ -7,7 +7,6 @@ using Sieve.Models;
using Sieve.Services; using Sieve.Services;
using SieveUnitTests.Entities; using SieveUnitTests.Entities;
using SieveUnitTests.Services; using SieveUnitTests.Services;
using SieveUnitTests.ValueObjects;
namespace SieveUnitTests namespace SieveUnitTests
{ {
@ -32,7 +31,8 @@ namespace SieveUnitTests
LikeCount = 100, LikeCount = 100,
IsDraft = true, IsDraft = true,
CategoryId = null, CategoryId = null,
TopComment = new Comment { Id = 0, Text = "A" } TopComment = new Comment { Id = 0, Text = "A1" },
FeaturedComment = new Comment { Id = 4, Text = "A2" }
}, },
new Post() { new Post() {
Id = 1, Id = 1,
@ -40,14 +40,16 @@ namespace SieveUnitTests
LikeCount = 50, LikeCount = 50,
IsDraft = false, IsDraft = false,
CategoryId = 1, CategoryId = 1,
TopComment = new Comment { Id = 3, Text = "A" } TopComment = new Comment { Id = 3, Text = "B1" },
FeaturedComment = new Comment { Id = 5, Text = "B2" }
}, },
new Post() { new Post() {
Id = 2, Id = 2,
Title = "C", Title = "C",
LikeCount = 0, LikeCount = 0,
CategoryId = 1, CategoryId = 1,
TopComment = new Comment { Id = 2, Text = "Z" } TopComment = new Comment { Id = 2, Text = "C1" },
FeaturedComment = new Comment { Id = 6, Text = "C2" }
}, },
new Post() { new Post() {
Id = 3, Id = 3,
@ -55,7 +57,8 @@ namespace SieveUnitTests
LikeCount = 3, LikeCount = 3,
IsDraft = true, IsDraft = true,
CategoryId = 2, CategoryId = 2,
TopComment = new Comment { Id = 1, Text = "Z" } TopComment = new Comment { Id = 1, Text = "D1" },
FeaturedComment = new Comment { Id = 7, Text = "D2" }
}, },
}.AsQueryable(); }.AsQueryable();
@ -64,23 +67,17 @@ namespace SieveUnitTests
new Comment() { new Comment() {
Id = 0, Id = 0,
DateCreated = DateTimeOffset.UtcNow.AddDays(-20), DateCreated = DateTimeOffset.UtcNow.AddDays(-20),
Text = "This is an old comment.", Text = "This is an old comment."
AuthorFirstName = new Name("FirstName1"),
AuthorLastName = new Name("LastName1")
}, },
new Comment() { new Comment() {
Id = 1, Id = 1,
DateCreated = DateTimeOffset.UtcNow.AddDays(-1), DateCreated = DateTimeOffset.UtcNow.AddDays(-1),
Text = "This is a fairly new comment.", Text = "This is a fairly new comment."
AuthorFirstName = new Name("FirstName2"),
AuthorLastName = new Name("LastName2")
}, },
new Comment() { new Comment() {
Id = 2, Id = 2,
DateCreated = DateTimeOffset.UtcNow, DateCreated = DateTimeOffset.UtcNow,
Text = "This is a brand new comment. ()", Text = "This is a brand new comment. (Text in braces)"
AuthorFirstName = new Name("FirstName3"),
AuthorLastName = new Name("LastName3")
}, },
}.AsQueryable(); }.AsQueryable();
} }
@ -350,10 +347,11 @@ namespace SieveUnitTests
}; };
var result = _processor.Apply(model, _posts); var result = _processor.Apply(model, _posts);
Assert.AreEqual(2, result.Count()); Assert.AreEqual(3, result.Count());
var posts = result.ToList(); var posts = result.ToList();
Assert.IsTrue(posts[0].TopComment.Text.Contains("Z")); Assert.IsTrue(posts[0].TopComment.Text.Contains("B"));
Assert.IsTrue(posts[1].TopComment.Text.Contains("Z")); Assert.IsTrue(posts[1].TopComment.Text.Contains("C"));
Assert.IsTrue(posts[2].TopComment.Text.Contains("D"));
} }
[TestMethod] [TestMethod]
@ -378,14 +376,19 @@ namespace SieveUnitTests
{ {
var model = new SieveModel() var model = new SieveModel()
{ {
Filters = "(firstName|lastName)@=*2", Filters = "(topc|featc)@=*2",
}; };
var result = _processor.Apply(model, _comments); var result = _processor.Apply(model, _posts);
Assert.AreEqual(1, result.Count()); Assert.AreEqual(4, result.Count());
var comment = result.First(); model = new SieveModel()
Assert.AreEqual(comment.Id, 1); {
Filters = "(topc|featc)@=*B",
};
result = _processor.Apply(model, _posts);
Assert.AreEqual(1, result.Count());
} }
} }
} }

View File

@ -31,13 +31,13 @@ namespace SieveUnitTests.Services
mapper.Property<Post>(p => p.OnlySortableViaFluentApi) mapper.Property<Post>(p => p.OnlySortableViaFluentApi)
.CanSort(); .CanSort();
mapper.Property<Comment>(c => c.AuthorFirstName.Value) mapper.Property<Post>(p => p.TopComment.Text)
.CanFilter() .CanFilter()
.HasName("firstName"); .HasName("topc");
mapper.Property<Comment>(c => c.AuthorLastName.Value) mapper.Property<Post>(p => p.FeaturedComment.Text)
.CanFilter() .CanFilter()
.HasName("lastName"); .HasName("featc");
return mapper; return mapper;
} }

View File

@ -1,43 +0,0 @@
using System;
namespace SieveUnitTests.ValueObjects
{
public sealed class Name : IEquatable<Name>
{
public Name(string value)
{
if (string.IsNullOrEmpty(value))
{
throw new InvalidOperationException("Invalid string!");
}
if (value.Length > 50)
{
throw new InvalidOperationException("String exceeds maximum name length!");
}
Value = value;
}
public string Value { get; private set; }
public bool Equals(Name other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is Name && Equals((Name) obj);
}
public override int GetHashCode()
{
return (Value != null ? Value.GetHashCode() : 0);
}
}
}