Skip to content

Commit 4e9f191

Browse files
committed
add crud model
1 parent 01b5de9 commit 4e9f191

File tree

25 files changed

+317
-114
lines changed

25 files changed

+317
-114
lines changed

restclient.http

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
1-
###
21
@host = http://localhost:5002
3-
GET {{host}}/api/products HTTP/1.1
2+
3+
###
4+
POST {{host}}/api/products-query HTTP/1.1
45
content-type: {{contentType}}
56

67
{
8+
"includes": ["Returns", "Code"],
79
"filters": [
8-
{
9-
"fieldName": "Quantity",
10-
"comparision": ">",
11-
"fieldValue": "1000"
12-
},
1310
{
1411
"fieldName": "Name",
1512
"comparision": "Contains",
16-
"fieldValue": "adipiscing"
13+
"fieldValue": "test product"
1714
}
1815
],
1916
"sorts": ["nameDesc"],
2017
"page": 1,
2118
"pageSize": 20
2219
}
20+
21+
###
22+
@id = 23537dac-303f-446f-be2a-1dbea22b3eba
23+
GET {{host}}/api/products/{{id}} HTTP/1.1
24+
content-type: {{contentType}}
25+
26+
###
27+
POST {{host}}/api/products HTTP/1.1
28+
content-type: {{contentType}}
29+
30+
{
31+
"model": {
32+
"name": "test product 1",
33+
"quantity": 1,
34+
"cost": 1000,
35+
"productCodeName": "pcode"
36+
}
37+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MediatR;
2+
3+
namespace N8T.Core.Domain
4+
{
5+
public interface ICommand<out T> : IRequest<T>
6+
{
7+
}
8+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#nullable enable
2+
using System.Collections.Generic;
3+
4+
namespace N8T.Core.Domain
5+
{
6+
public interface ICrudModel
7+
{
8+
}
9+
10+
public interface ICreateModel<T> : ICrudModel
11+
{
12+
public T Model { get; init; }
13+
}
14+
15+
public interface IUpdateModel<T> : ICrudModel
16+
{
17+
public T Model { get; init; }
18+
}
19+
20+
public interface IDeleteModel<TId> : ICrudModel where TId : struct
21+
{
22+
public TId Id { get; init; }
23+
}
24+
25+
public interface IListQueryModel : ICrudModel
26+
{
27+
public List<string> Includes { get; init; }
28+
public List<FilterModel> Filters { get; init; }
29+
public List<string> Sorts { get; init; }
30+
public int Page { get; init; }
31+
public int PageSize { get; init; }
32+
}
33+
34+
public interface IItemQueryModel<TId> : ICrudModel where TId : struct
35+
{
36+
public List<string> Includes { get; init; }
37+
public TId Id { get; init; }
38+
}
39+
40+
public record FilterModel(string FieldName, string Comparision, string FieldValue);
41+
42+
public record ResultModel<T>(T Data, bool IsError = false, string? ErrorMessage = default);
43+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using MediatR;
2+
3+
namespace N8T.Core.Domain
4+
{
5+
public interface IQuery<out T> : IRequest<T>
6+
{
7+
}
8+
}

src/BuildingBlocks/N8T.Infrastructure.App/Dtos/ProductDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace N8T.Infrastructure.App.Dtos
44
{
5-
public class ProductDto
5+
public record ProductDto
66
{
77
public Guid Id { get; init; }
88
public string Name { get; init; } = default!;

src/BuildingBlocks/N8T.Infrastructure.EfCore/ITxRequest.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/BuildingBlocks/N8T.Infrastructure.EfCore/Repository.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ public async Task<TEntity> FindOneAsync(ISpecification<TEntity> spec)
3232
{
3333
await using var dbContext = _dbContextFactory.CreateDbContext();
3434

35-
return await dbContext.Set<TEntity>().Where(spec.Criteria).SingleOrDefaultAsync();
35+
var specificationResult = GetQuery(dbContext.Set<TEntity>(), spec);
36+
37+
return await specificationResult.FirstOrDefaultAsync();
3638
}
3739

3840
public async Task<List<TEntity>> FindAsync(ISpecification<TEntity> spec)
@@ -112,7 +114,7 @@ private static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery,
112114

113115
if (specification.IsPagingEnabled)
114116
{
115-
query = query.Skip(specification.Skip)
117+
query = query.Skip(specification.Skip - 1)
116118
.Take(specification.Take);
117119
}
118120

@@ -155,7 +157,7 @@ private static IQueryable<TEntity> GetQuery(IQueryable<TEntity> inputQuery,
155157

156158
if (specification.IsPagingEnabled)
157159
{
158-
query = query.Skip(specification.Skip)
160+
query = query.Skip(specification.Skip - 1)
159161
.Take(specification.Take);
160162
}
161163

src/BuildingBlocks/N8T.Infrastructure.EfCore/TxBehavior.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Data;
3+
using System.Diagnostics;
34
using System.Linq;
45
using System.Text.Json;
56
using System.Threading;
@@ -11,6 +12,9 @@
1112

1213
namespace N8T.Infrastructure.EfCore
1314
{
15+
public interface ITxRequest { }
16+
17+
//[DebuggerStepThrough]
1418
public class TxBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
1519
where TRequest : notnull, IRequest<TResponse>
1620
where TResponse : notnull

src/BuildingBlocks/N8T.Infrastructure/Validator/RequestValidationBehavior.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics;
23
using System.Text.Json;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -8,6 +9,7 @@
89

910
namespace N8T.Infrastructure.Validator
1011
{
12+
[DebuggerStepThrough]
1113
public class RequestValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
1214
where TRequest : notnull, IRequest<TResponse>
1315
where TResponse : notnull
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Threading;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Mvc;
4+
using N8T.Infrastructure.Endpoint;
5+
using ProductService.Application.Commands;
6+
7+
namespace ProductService.Api.Endpoints
8+
{
9+
public class CreateProduct : BaseAsyncEndpoint
10+
{
11+
[HttpPost("/api/products")]
12+
public async Task<ActionResult> HandleAsync([FromBody] CreateProductRequest model,
13+
CancellationToken cancellationToken = new())
14+
{
15+
return Ok(await Mediator.Send(model, cancellationToken));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)