Skip to content

Commit 5b11d1c

Browse files
committed
Performer created and *Endpoints.cs refact.
1 parent fab342d commit 5b11d1c

File tree

7 files changed

+731
-646
lines changed

7 files changed

+731
-646
lines changed
Lines changed: 61 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
3+
using Microsoft.Extensions.Logging;
34
using RoyalCode.SmartProblems;
45
using RoyalCode.SmartSearch.AspNetCore.HttpResults;
5-
using RoyalCode.SmartSearch.Exceptions;
66

77
namespace RoyalCode.SmartSearch.AspNetCore.Internals;
88

@@ -31,45 +31,25 @@ public FirstEntityEndpoint(Action<ICriteria<TEntity>>? searchAction = null)
3131
/// </summary>
3232
/// <param name="filtro">Filter object with search criteria.</param>
3333
/// <param name="orderby">Sorting criteria.</param>
34-
/// <param name="searchable">Search service for the entity.</param>
34+
/// <param name="criteria">Search service for the entity.</param>
35+
/// <param name="logger">Logger for logging errors.</param>
3536
/// <param name="ct">Cancellation token.</param>
3637
/// <returns>HTTP 200 result with the item, 204 if not found, or 400 in case of sorting error.</returns>
3738
[ProduceProblems(ProblemCategory.InvalidParameter, ProblemCategory.InternalServerError)]
38-
public async Task<MatchFirst<TEntity>> First(
39+
public Task<MatchFirst<TEntity>> First(
3940
[AsParameters] TFilter filtro,
4041
[FromQuery] Sorting[]? orderby,
41-
[FromServices] ICriteria<TEntity> searchable,
42+
[FromServices] ICriteria<TEntity> criteria,
43+
[FromServices] ILogger<ICriteria<TEntity>> logger,
4244
CancellationToken ct)
4345
{
44-
try
45-
{
46-
var search = searchable
47-
.OrderBy(orderby)
48-
.FilterBy(filtro);
49-
50-
if (searchAction is not null)
51-
searchAction(search);
52-
53-
var entity = await search.FirstOrDefaultAsync(ct);
54-
55-
if (entity is null)
56-
{
57-
return TypedResults.NoContent();
58-
}
59-
60-
return entity;
61-
}
62-
catch (OrderByException obex)
63-
{
64-
return Problems.InvalidParameter(obex.Message, nameof(orderby))
65-
.With("propertyName", obex.PropertyName)
66-
.With("typeName", obex.TypeName)
67-
.With("orderby", orderby);
68-
}
69-
catch (Exception ex)
70-
{
71-
return Problems.InternalError(ex);
72-
}
46+
return Performer.FirstAsync(
47+
filtro,
48+
orderby,
49+
criteria,
50+
searchAction,
51+
logger,
52+
ct);
7353
}
7454
}
7555

@@ -100,46 +80,30 @@ public FirstEntityEndpoint(Action<TId, ICriteria<TEntity>>? searchAction = null)
10080
/// <param name="id">Additional identifier for the search.</param>
10181
/// <param name="filtro">Filter object with search criteria.</param>
10282
/// <param name="orderby">Sorting criteria.</param>
103-
/// <param name="searchable">Search service for the entity.</param>
83+
/// <param name="criteria">Search service for the entity.</param>
84+
/// <param name="logger">Logger for logging errors.</param>
10485
/// <param name="ct">Cancellation token.</param>
10586
/// <returns>HTTP 200 result with the item, 204 if not found, or 400 in case of sorting error.</returns>
10687
[ProduceProblems(ProblemCategory.InvalidParameter, ProblemCategory.InternalServerError)]
107-
public async Task<MatchFirst<TEntity>> First(
88+
public Task<MatchFirst<TEntity>> First(
10889
[FromRoute] TId id,
10990
[AsParameters] TFilter filtro,
11091
[FromQuery] Sorting[]? orderby,
111-
[FromServices] ICriteria<TEntity> searchable,
92+
[FromServices] ICriteria<TEntity> criteria,
93+
[FromServices] ILogger<ICriteria<TEntity>> logger,
11294
CancellationToken ct)
11395
{
114-
try
115-
{
116-
var search = searchable
117-
.OrderBy(orderby)
118-
.FilterBy(filtro);
119-
120-
if (searchAction is not null)
121-
searchAction(id, search);
122-
123-
var entity = await search.FirstOrDefaultAsync(ct);
124-
125-
if (entity is null)
126-
{
127-
return TypedResults.NoContent();
128-
}
129-
130-
return entity;
131-
}
132-
catch (OrderByException obex)
133-
{
134-
return Problems.InvalidParameter(obex.Message, nameof(orderby))
135-
.With("propertyName", obex.PropertyName)
136-
.With("typeName", obex.TypeName)
137-
.With("orderby", orderby);
138-
}
139-
catch (Exception ex)
140-
{
141-
return Problems.InternalError(ex);
142-
}
96+
Action<ICriteria<TEntity>>? searchAction = null;
97+
if (this.searchAction is not null)
98+
searchAction = c => this.searchAction(id, c);
99+
100+
return Performer.FirstAsync(
101+
filtro,
102+
orderby,
103+
criteria,
104+
searchAction,
105+
logger,
106+
ct);
143107
}
144108
}
145109

@@ -172,47 +136,31 @@ public FirstEntityEndpoint(Action<TId1, TId2, ICriteria<TEntity>>? searchAction
172136
/// <param name="relatedId">Additional identifier, related to the first, for the search.</param>
173137
/// <param name="filtro">Filter object with search criteria.</param>
174138
/// <param name="orderby">Sorting criteria.</param>
175-
/// <param name="searchable">Search service for the entity.</param>
139+
/// <param name="criteria">Search service for the entity.</param>
140+
/// <param name="logger">Logger for logging errors.</param>
176141
/// <param name="ct">Cancellation token.</param>
177142
/// <returns>HTTP 200 result with the item, 204 if not found, or 400 in case of sorting error.</returns>
178143
[ProduceProblems(ProblemCategory.InvalidParameter, ProblemCategory.InternalServerError)]
179-
public async Task<MatchFirst<TEntity>> First(
144+
public Task<MatchFirst<TEntity>> First(
180145
[FromRoute] TId1 id,
181146
[FromRoute] TId2 relatedId,
182147
[AsParameters] TFilter filtro,
183148
[FromQuery] Sorting[]? orderby,
184-
[FromServices] ICriteria<TEntity> searchable,
149+
[FromServices] ICriteria<TEntity> criteria,
150+
[FromServices] ILogger<ICriteria<TEntity>> logger,
185151
CancellationToken ct)
186152
{
187-
try
188-
{
189-
var search = searchable
190-
.OrderBy(orderby)
191-
.FilterBy(filtro);
192-
193-
if (searchAction is not null)
194-
searchAction(id, relatedId, search);
195-
196-
var entity = await search.FirstOrDefaultAsync(ct);
197-
198-
if (entity is null)
199-
{
200-
return TypedResults.NoContent();
201-
}
202-
203-
return entity;
204-
}
205-
catch (OrderByException obex)
206-
{
207-
return Problems.InvalidParameter(obex.Message, nameof(orderby))
208-
.With("propertyName", obex.PropertyName)
209-
.With("typeName", obex.TypeName)
210-
.With("orderby", orderby);
211-
}
212-
catch (Exception ex)
213-
{
214-
return Problems.InternalError(ex);
215-
}
153+
Action<ICriteria<TEntity>>? searchAction = null;
154+
if (this.searchAction is not null)
155+
searchAction = c => this.searchAction(id, relatedId, c);
156+
157+
return Performer.FirstAsync(
158+
filtro,
159+
orderby,
160+
criteria,
161+
searchAction,
162+
logger,
163+
ct);
216164
}
217165
}
218166

@@ -247,47 +195,31 @@ public FirstEntityEndpoint(Action<TId1, TId2, TId3, ICriteria<TEntity>>? searchA
247195
/// <param name="subRelatedId">Third additional identifier for the search.</param>
248196
/// <param name="filtro">Filter object with search criteria.</param>
249197
/// <param name="orderby">Sorting criteria.</param>
250-
/// <param name="searchable">Search service for the entity.</param>
198+
/// <param name="criteria">Search service for the entity.</param>
199+
/// <param name="logger">Logger for logging errors.</param>
251200
/// <param name="ct">Cancellation token.</param>
252201
/// <returns>HTTP 200 result with the item, 204 if not found, or 400 in case of sorting error.</returns>
253202
[ProduceProblems(ProblemCategory.InvalidParameter, ProblemCategory.InternalServerError)]
254-
public async Task<MatchFirst<TEntity>> First(
203+
public Task<MatchFirst<TEntity>> First(
255204
[FromRoute] TId1 id,
256205
[FromRoute] TId2 relatedId,
257206
[FromRoute] TId3 subRelatedId,
258207
[AsParameters] TFilter filtro,
259208
[FromQuery] Sorting[]? orderby,
260-
[FromServices] ICriteria<TEntity> searchable,
209+
[FromServices] ICriteria<TEntity> criteria,
210+
[FromServices] ILogger<ICriteria<TEntity>> logger,
261211
CancellationToken ct)
262212
{
263-
try
264-
{
265-
var search = searchable
266-
.OrderBy(orderby)
267-
.FilterBy(filtro);
268-
269-
if (searchAction is not null)
270-
searchAction(id, relatedId, subRelatedId, search);
271-
272-
var entity = await search.FirstOrDefaultAsync(ct);
273-
274-
if (entity is null)
275-
{
276-
return TypedResults.NoContent();
277-
}
278-
279-
return entity;
280-
}
281-
catch (OrderByException obex)
282-
{
283-
return Problems.InvalidParameter(obex.Message, nameof(orderby))
284-
.With("propertyName", obex.PropertyName)
285-
.With("typeName", obex.TypeName)
286-
.With("orderby", orderby);
287-
}
288-
catch (Exception ex)
289-
{
290-
return Problems.InternalError(ex);
291-
}
213+
Action<ICriteria<TEntity>>? searchAction = null;
214+
if (this.searchAction is not null)
215+
searchAction = c => this.searchAction(id, relatedId, subRelatedId, c);
216+
217+
return Performer.FirstAsync(
218+
filtro,
219+
orderby,
220+
criteria,
221+
searchAction,
222+
logger,
223+
ct);
292224
}
293225
}

0 commit comments

Comments
 (0)