Skip to content
This repository was archived by the owner on Apr 20, 2024. It is now read-only.

Commit 343e42f

Browse files
author
Morten Turn Pedersen
authored
Merge pull request #28 from nodes-dotnet/feature/26-select-statement
Feature/26 select statement
2 parents fffc33c + 429ca0f commit 343e42f

File tree

6 files changed

+285
-3
lines changed

6 files changed

+285
-3
lines changed

NetCoreEntityFramework.Tests/EntityRepositoryTests.cs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Linq;
9+
using System.Linq.Expressions;
910
using System.Threading.Tasks;
1011
using TestContext = Nodes.NetCore.EntityFramework.Tests.Mocks.TestContext;
1112

@@ -93,7 +94,6 @@ public void AddThrowsExceptionIfEntityIsNull()
9394
#endregion
9495

9596
#region List
96-
9797
[Test]
9898
public async Task GetListReturnsAll()
9999
{
@@ -167,6 +167,80 @@ public async Task GetListPaginated()
167167
Assert.AreEqual(3, entitiesLastPage.Count());
168168
}
169169

170+
[Test]
171+
public async Task GetListWithSelectReturnsAll()
172+
{
173+
var entities = await _repository.GetListWithSelect<string>(e => e.Property);
174+
175+
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
176+
}
177+
178+
[Test]
179+
public async Task GetListWithSelectWhere()
180+
{
181+
const string propertyToLookFor = "b";
182+
IEnumerable<string> entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property == propertyToLookFor);
183+
184+
Assert.AreEqual(1, entities.Count());
185+
Assert.AreEqual(propertyToLookFor, entities.ElementAt(0));
186+
}
187+
188+
[Test]
189+
public async Task GetListWithSelectOrderBy()
190+
{
191+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property);
192+
193+
Assert.AreEqual(_listEntities.Count(), entities.Count());
194+
Assert.AreEqual("a", entities.ElementAt(0));
195+
Assert.AreEqual("b", entities.ElementAt(1));
196+
Assert.AreEqual("c", entities.ElementAt(2));
197+
Assert.AreEqual("d", entities.ElementAt(3));
198+
Assert.AreEqual("e", entities.ElementAt(4));
199+
Assert.AreEqual("f", entities.ElementAt(5));
200+
Assert.AreEqual("g", entities.ElementAt(6));
201+
Assert.AreEqual("h", entities.ElementAt(7));
202+
Assert.AreEqual("i", entities.ElementAt(8));
203+
Assert.AreEqual("j", entities.ElementAt(9));
204+
Assert.AreEqual("k", entities.ElementAt(10));
205+
Assert.AreEqual("l", entities.ElementAt(11));
206+
Assert.AreEqual("m", entities.ElementAt(12));
207+
Assert.AreEqual("n", entities.ElementAt(13));
208+
}
209+
210+
[Test]
211+
public async Task GetListWithSelectOrderByDescending()
212+
{
213+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property, OrderBy.Descending);
214+
215+
Assert.AreEqual(_listEntities.Count(), entities.Count());
216+
Assert.AreEqual("n", entities.ElementAt(0));
217+
Assert.AreEqual("m", entities.ElementAt(1));
218+
Assert.AreEqual("l", entities.ElementAt(2));
219+
Assert.AreEqual("k", entities.ElementAt(3));
220+
Assert.AreEqual("j", entities.ElementAt(4));
221+
Assert.AreEqual("i", entities.ElementAt(5));
222+
Assert.AreEqual("h", entities.ElementAt(6));
223+
Assert.AreEqual("g", entities.ElementAt(7));
224+
Assert.AreEqual("f", entities.ElementAt(8));
225+
Assert.AreEqual("e", entities.ElementAt(9));
226+
Assert.AreEqual("d", entities.ElementAt(10));
227+
Assert.AreEqual("c", entities.ElementAt(11));
228+
Assert.AreEqual("b", entities.ElementAt(12));
229+
Assert.AreEqual("a", entities.ElementAt(13));
230+
}
231+
232+
[Test]
233+
public async Task GetListWithSelectPaginated()
234+
{
235+
const int pageSize = 6;
236+
237+
var entities = await _repository.GetListWithSelect(x => x.Property, 1, pageSize);
238+
var entitiesLastPage = await _repository.GetListWithSelect(x => x.Property, 3, pageSize);
239+
240+
Assert.AreEqual(pageSize, entities.Count());
241+
Assert.AreEqual(3, entitiesLastPage.Count());
242+
}
243+
170244
private IEnumerable<TestEntity> GetTestList()
171245
{
172246
return new List<TestEntity>

NetCoreEntityFramework.Tests/EntitySoftDeleteRepositoryTests.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,94 @@ private TestSoftDeleteEntity GetTestEntity(string property)
227227
Property = property
228228
};
229229
}
230+
[Test]
231+
public async Task GetListWithReturnsReturnsAllNotDeleted()
232+
{
233+
var entities = await _repository.GetListWithSelect(x => x.Property);
234+
235+
Assert.AreEqual(_listEntities.Count() + 1, entities.Count());
236+
}
237+
238+
[Test]
239+
public async Task GetListWithSelectReturnsAll()
240+
{
241+
var entities = await _repository.GetListWithSelect(x => x.Property, null, null, OrderBy.Ascending, GetListMode.IncludeDeleted);
242+
243+
Assert.AreEqual(_listEntities.Count() + 2, entities.Count());
244+
}
245+
246+
[Test]
247+
public async Task GetListWithSelectReturnsAllDeleted()
248+
{
249+
var entities = await _repository.GetListWithSelect(x => x.Property, null, null, OrderBy.Ascending, GetListMode.OnlyDeleted);
250+
251+
Assert.AreEqual(1, entities.Count());
252+
}
253+
254+
[Test]
255+
public async Task GetListWithSelectWhere()
256+
{
257+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property == "b");
258+
259+
Assert.AreEqual(1, entities.Count());
260+
Assert.AreEqual("b", entities.ElementAt(0));
261+
}
262+
263+
[Test]
264+
public async Task GetListWithSelectOrderBy()
265+
{
266+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property);
267+
268+
Assert.AreEqual(_listEntities.Count(), entities.Count());
269+
Assert.AreEqual("a", entities.ElementAt(0));
270+
Assert.AreEqual("b", entities.ElementAt(1));
271+
Assert.AreEqual("c", entities.ElementAt(2));
272+
Assert.AreEqual("d", entities.ElementAt(3));
273+
Assert.AreEqual("e", entities.ElementAt(4));
274+
Assert.AreEqual("f", entities.ElementAt(5));
275+
Assert.AreEqual("g", entities.ElementAt(6));
276+
Assert.AreEqual("h", entities.ElementAt(7));
277+
Assert.AreEqual("i", entities.ElementAt(8));
278+
Assert.AreEqual("j", entities.ElementAt(9));
279+
Assert.AreEqual("k", entities.ElementAt(10));
280+
Assert.AreEqual("l", entities.ElementAt(11));
281+
Assert.AreEqual("m", entities.ElementAt(12));
282+
Assert.AreEqual("n", entities.ElementAt(13));
283+
}
284+
285+
[Test]
286+
public async Task GetListWithSelectOrderByDescending()
287+
{
288+
var entities = await _repository.GetListWithSelect(x => x.Property, x => x.Property.Length == 1, x => x.Property, OrderBy.Descending);
289+
290+
Assert.AreEqual(_listEntities.Count(), entities.Count());
291+
Assert.AreEqual("n", entities.ElementAt(0));
292+
Assert.AreEqual("m", entities.ElementAt(1));
293+
Assert.AreEqual("l", entities.ElementAt(2));
294+
Assert.AreEqual("k", entities.ElementAt(3));
295+
Assert.AreEqual("j", entities.ElementAt(4));
296+
Assert.AreEqual("i", entities.ElementAt(5));
297+
Assert.AreEqual("h", entities.ElementAt(6));
298+
Assert.AreEqual("g", entities.ElementAt(7));
299+
Assert.AreEqual("f", entities.ElementAt(8));
300+
Assert.AreEqual("e", entities.ElementAt(9));
301+
Assert.AreEqual("d", entities.ElementAt(10));
302+
Assert.AreEqual("c", entities.ElementAt(11));
303+
Assert.AreEqual("b", entities.ElementAt(12));
304+
Assert.AreEqual("a", entities.ElementAt(13));
305+
}
306+
307+
[Test]
308+
public async Task GetListWithSelectPaginated()
309+
{
310+
const int pageSize = 6;
311+
312+
var entities = await _repository.GetListWithSelect(x => x.Property, 1, pageSize);
313+
var entitiesLastPage = await _repository.GetListWithSelect(x => x.Property, 3, pageSize);
314+
315+
Assert.AreEqual(pageSize, entities.Count());
316+
Assert.AreEqual(3, entitiesLastPage.Count());
317+
}
230318
#endregion
231319

232320
#region Get

NetCoreEntityFramework/Repositories/EntityRepository.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ public async virtual Task<IEnumerable<TEntity>> GetList(
3737
return await query.ToListAsync();
3838
}
3939

40+
public async virtual Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
41+
Expression<Func<TEntity, TResult>> select,
42+
[Range(1, int.MaxValue)] int page,
43+
[Range(1, int.MaxValue)] int pageSize,
44+
Expression<Func<TEntity, bool>> where = null,
45+
Expression<Func<TEntity, object>> orderByExpression = null,
46+
OrderBy orderBy = OrderBy.Ascending)
47+
{
48+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy);
49+
50+
query = Paginate(query, page, pageSize);
51+
52+
return await query.Select(select).ToListAsync();
53+
}
54+
4055
public async virtual Task<IEnumerable<TEntity>> GetList(
4156
Expression<Func<TEntity, bool>> where = null,
4257
Expression<Func<TEntity, object>> orderByExpression = null,
@@ -47,6 +62,17 @@ public async virtual Task<IEnumerable<TEntity>> GetList(
4762
return await query.ToListAsync();
4863
}
4964

65+
public async virtual Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
66+
Expression<Func<TEntity, TResult>> select,
67+
Expression<Func<TEntity, bool>> where = null,
68+
Expression<Func<TEntity, object>> orderByExpression = null,
69+
OrderBy orderBy = OrderBy.Ascending)
70+
{
71+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy);
72+
73+
return await query.Select(select).ToListAsync();
74+
}
75+
5076
public virtual Task Add(TEntity entity)
5177
{
5278
if (entity == null)

NetCoreEntityFramework/Repositories/EntitySoftDeleteRepository.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,32 @@ public async virtual Task<IEnumerable<TEntity>> GetList(
4444
return await query.ToListAsync();
4545
}
4646

47+
public virtual async Task<IEnumerable<TResult>> GetListWithSelect<TResult>(Expression<Func<TEntity, TResult>> select,
48+
[Range(1, int.MaxValue)] int page,
49+
[Range(1, int.MaxValue)] int pageSize,
50+
Expression<Func<TEntity, bool>> where = null,
51+
Expression<Func<TEntity, object>> orderByExpression = null,
52+
OrderBy orderBy = OrderBy.Ascending,
53+
GetListMode mode = GetListMode.ExcludeDeleted)
54+
{
55+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy, mode);
56+
57+
query = Paginate(query, page, pageSize);
58+
59+
return await query.Select(select).ToListAsync();
60+
}
61+
62+
public async virtual Task<IEnumerable<TResult>> GetListWithSelect<TResult>(Expression<Func<TEntity, TResult>> select,
63+
Expression<Func<TEntity, bool>> where = null,
64+
Expression<Func<TEntity, object>> orderByExpression = null,
65+
OrderBy orderBy = OrderBy.Ascending,
66+
GetListMode mode = GetListMode.ExcludeDeleted)
67+
{
68+
IQueryable<TEntity> query = GetQueryable(where, orderByExpression, orderBy, mode);
69+
70+
return await query.Select(select).ToListAsync();
71+
}
72+
4773
public override Task<bool> Delete(TEntity entity)
4874
{
4975
if (entity == null)

NetCoreEntityFramework/Repositories/IEntityRepository.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,44 @@ Task<IEnumerable<TEntity>> GetList(
3838
/// <param name="where">The filter expression.</param>
3939
/// <param name="orderByExpression">The expression to order by.</param>
4040
/// <param name="orderBy">To order by ascending or descending.</param>
41-
/// <exception cref="ArgumentException"></exception>
4241
Task<IEnumerable<TEntity>> GetList(
4342
Expression<Func<TEntity, bool>> where = null,
4443
Expression<Func<TEntity, object>> orderByExpression = null,
4544
OrderBy orderBy = OrderBy.Ascending);
4645

46+
/// <summary>
47+
/// Get multiple entities paginated and translated.
48+
/// </summary>
49+
/// <typeparam name="TResult">The type to return.</typeparam>
50+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
51+
/// <param name="page">Which page to fetch (1 and above).</param>
52+
/// <param name="pageSize">The size of each page (1 and above).</param>
53+
/// <param name="where">The filter expression.</param>
54+
/// <param name="orderByExpression">The expression to order by.</param>
55+
/// <param name="orderBy">To order by ascending or descending.</param>
56+
/// <exception cref="ArgumentException"></exception>
57+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
58+
Expression<Func<TEntity, TResult>> select,
59+
[Range(1, int.MaxValue)] int page,
60+
[Range(1, int.MaxValue)] int pageSize,
61+
Expression<Func<TEntity, bool>> where = null,
62+
Expression<Func<TEntity, object>> orderByExpression = null,
63+
OrderBy orderBy = OrderBy.Ascending);
64+
65+
/// <summary>
66+
/// Get multiple entities translated.
67+
/// </summary>
68+
/// <typeparam name="TResult">The type to return.</typeparam>
69+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
70+
/// <param name="where">The filter expression.</param>
71+
/// <param name="orderByExpression">The expression to order by.</param>
72+
/// <param name="orderBy">To order by ascending or descending.</param>
73+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
74+
Expression<Func<TEntity, TResult>> select,
75+
Expression<Func<TEntity, bool>> where = null,
76+
Expression<Func<TEntity, object>> orderByExpression = null,
77+
OrderBy orderBy = OrderBy.Ascending);
78+
4779
/// <summary>
4880
/// Add the given <paramref name="entity"/> to the database.
4981
/// An ID will be generated if not provided.

NetCoreEntityFramework/Repositories/IEntitySoftDeleteRepository.cs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,49 @@ Task<IEnumerable<TEntity>> GetList(
4242
/// <param name="orderByExpression">The expression to order by.</param>
4343
/// <param name="orderBy">To order by ascending or descending.</param>
4444
/// <param name="mode">Whether to include deleted or not.</param>
45-
/// <exception cref="ArgumentException"></exception>
4645
Task<IEnumerable<TEntity>> GetList(
4746
Expression<Func<TEntity, bool>> where = null,
4847
Expression<Func<TEntity, object>> orderByExpression = null,
4948
OrderBy orderBy = OrderBy.Ascending,
5049
GetListMode mode = GetListMode.ExcludeDeleted);
5150

51+
/// <summary>
52+
/// Get multiple entities paginated and translated.
53+
/// </summary>
54+
/// <typeparam name="TResult">The type to return.</typeparam>
55+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
56+
/// <param name="page">Which page to fetch (1 and above).</param>
57+
/// <param name="pageSize">The size of each page (1 and above).</param>
58+
/// <param name="where">The filter expression.</param>
59+
/// <param name="orderByExpression">The expression to order by.</param>
60+
/// <param name="orderBy">To order by ascending or descending.</param>
61+
/// <param name="mode">Whether to include deleted or not.</param>
62+
/// <exception cref="ArgumentException"></exception>
63+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
64+
Expression<Func<TEntity, TResult>> select,
65+
[Range(1, int.MaxValue)] int page,
66+
[Range(1, int.MaxValue)] int pageSize,
67+
Expression<Func<TEntity, bool>> where = null,
68+
Expression<Func<TEntity, object>> orderByExpression = null,
69+
OrderBy orderBy = OrderBy.Ascending,
70+
GetListMode mode = GetListMode.ExcludeDeleted);
71+
72+
/// <summary>
73+
/// Get multiple entities translated.
74+
/// </summary>
75+
/// <typeparam name="TResult">The type to return.</typeparam>
76+
/// <param name="select">The select statement to get <typeparamref name="TResult"/>.</param>
77+
/// <param name="where">The filter expression.</param>
78+
/// <param name="orderByExpression">The expression to order by.</param>
79+
/// <param name="orderBy">To order by ascending or descending.</param>
80+
/// <param name="mode">Whether to include deleted or not.</param>
81+
Task<IEnumerable<TResult>> GetListWithSelect<TResult>(
82+
Expression<Func<TEntity, TResult>> select,
83+
Expression<Func<TEntity, bool>> where = null,
84+
Expression<Func<TEntity, object>> orderByExpression = null,
85+
OrderBy orderBy = OrderBy.Ascending,
86+
GetListMode mode = GetListMode.ExcludeDeleted);
87+
5288
/// <summary>
5389
/// Restore/undelete the entity with the given <paramref name="id"/>.
5490
/// </summary>

0 commit comments

Comments
 (0)