Skip to content

Commit 714d2df

Browse files
authored
Merge pull request #17 from coderstrong/develop
Update Repository
2 parents c7a0b4a + 7bbb706 commit 714d2df

25 files changed

+272
-381
lines changed
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using MakeSimple.SharedKernel.Infrastructure.DTO;
1+
using MakeSimple.SharedKernel.Contract;
22
using System.Net;
33
using Xunit;
44

@@ -9,14 +9,11 @@ public class ValidationExceptionTest
99
[Fact]
1010
public void CreateResponseException_Success()
1111
{
12-
var result = new Response<bool>
13-
(
14-
HttpStatusCode.InternalServerError,
15-
new ErrorBase("InternalServerError", "Internal Server Error")
16-
);
12+
var result = Error.Create("InternalServerError", "Internal Server Error", HttpStatusCode.InternalServerError);
1713

18-
Assert.NotNull(result.Error);
19-
Assert.NotNull(result.Error.TraceId);
14+
Assert.NotNull(result.Code);
15+
Assert.NotNull(result.Message);
16+
Assert.NotNull(result.TraceId);
2017
}
2118
}
2219
}

src/MakeSimple.SharedKernel.Infrastructure.Test/Repository/EfRepositoryGenericTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Found_Success()
202202
await _repositoryGeneric.InsertRangeAsync(Studentes);
203203
await _repositoryGeneric.UnitOfWork.SaveEntitiesAsync();
204204

205-
var result = await _repositoryGeneric.FirstOrDefaultAsync<StudentDto>(saveIds[2]);
205+
var result = await _repositoryGeneric.FindAsync<StudentDto>(saveIds[2]);
206206

207207
Assert.NotNull(result);
208208
Assert.Equal(result.Item.Id, saveIds[2]);
@@ -346,7 +346,7 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Linq_Found_Success()
346346
await _repositoryGeneric.InsertRangeAsync(Studentes);
347347
await _repositoryGeneric.UnitOfWork.SaveEntitiesAsync();
348348

349-
var result = await _repositoryGeneric.FirstOrDefaultAsync<StudentDto>(e => e.Id == saveIds[2], i => i.Class);
349+
var result = await _repositoryGeneric.FindAsync<StudentDto>(e => e.Id == saveIds[2], i => i.Class);
350350

351351
Assert.NotNull(result.Item);
352352
Assert.NotNull(result.Item.Class);

src/MakeSimple.SharedKernel.Infrastructure/DTO/DataNotFoundError.cs

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

src/MakeSimple.SharedKernel.Infrastructure/DTO/ErrorBase.cs

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

src/MakeSimple.SharedKernel.Infrastructure/DTO/Response.cs

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

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/BaseException.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using MakeSimple.SharedKernel.Contract;
22
using System;
3+
using System.Runtime.Serialization;
34

45
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
56
{
67
public class ConflictException : BaseException
78
{
8-
public ConflictException(IDataResult errorResult)
9+
public ConflictException(Error errorResult)
910
: base(errorResult)
1011
{ }
1112

12-
public ConflictException(IDataResult errorResult, Exception innerException)
13+
public ConflictException(Error errorResult, Exception innerException)
1314
: base(errorResult, innerException)
1415
{ }
16+
17+
protected ConflictException(SerializationInfo info, StreamingContext context)
18+
: base(info, context)
19+
{ }
1520
}
1621
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using MakeSimple.SharedKernel.Contract;
22
using System;
3+
using System.Runtime.Serialization;
34

45
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
56
{
67
public class ForbiddenException : BaseException
78
{
8-
public ForbiddenException(IDataResult errorResult)
9+
public ForbiddenException(Error errorResult)
910
: base(errorResult)
1011
{ }
1112

12-
public ForbiddenException(IDataResult errorResult, Exception innerException)
13+
public ForbiddenException(Error errorResult, Exception innerException)
1314
: base(errorResult, innerException)
1415
{ }
16+
17+
protected ForbiddenException(SerializationInfo info, StreamingContext context)
18+
: base(info, context)
19+
{ }
1520
}
1621
}

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/TryGetKeyNotFoundException.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
using MakeSimple.SharedKernel.Contract;
22
using System;
3+
using System.Runtime.Serialization;
34

45
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
56
{
67
public class UnhandledException : BaseException
78
{
8-
public UnhandledException(IDataResult errorResult)
9+
public UnhandledException(Error errorResult)
910
: base(errorResult)
1011
{ }
1112

12-
public UnhandledException(IDataResult errorResult, Exception innerException)
13+
public UnhandledException(Error errorResult, Exception innerException)
1314
: base(errorResult, innerException)
1415
{ }
16+
17+
protected UnhandledException(SerializationInfo info, StreamingContext context)
18+
: base(info, context)
19+
{ }
1520
}
1621
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using MakeSimple.SharedKernel.Contract;
22
using System;
3+
using System.Runtime.Serialization;
34

45
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
56
{
6-
/// <summary>
7-
/// No log when throw this exception
8-
/// </summary>
9-
#pragma warning disable S3925 // "ISerializable" should be implemented correctly
10-
117
public class ValidationException : BaseException
12-
#pragma warning restore S3925 // "ISerializable" should be implemented correctly
138
{
14-
public ValidationException(IDataResult errorResult)
9+
public ValidationException(Error errorResult)
1510
: base(errorResult)
1611
{ }
1712

18-
public ValidationException(IDataResult errorResult, Exception innerException)
13+
public ValidationException(Error errorResult, Exception innerException)
1914
: base(errorResult, innerException)
2015
{ }
16+
17+
protected ValidationException(SerializationInfo info, StreamingContext context)
18+
: base(info, context)
19+
{ }
2120
}
2221
}

src/MakeSimple.SharedKernel.Infrastructure/Extensions/MediaRExtensions.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
namespace MakeSimple.SharedKernel.Infrastructure.Extensions
1212
{
1313
using FluentValidation.Results;
14+
using MakeSimple.SharedKernel.Contract;
1415
using MakeSimple.SharedKernel.Extensions;
15-
using MakeSimple.SharedKernel.Infrastructure.DTO;
1616
using MakeSimple.SharedKernel.Infrastructure.Exceptions;
1717
using System.Net;
1818
using System.Reflection;
@@ -109,11 +109,8 @@ public async Task<TResponse> Handle(TRequest request, CancellationToken cancella
109109
{
110110
_logger.LogWarning("Validation errors - {typeName} - Command: {@request} - Errors: {@failures}", typeName, request, failures);
111111

112-
throw new ValidationException(new Response<bool>
113-
(
114-
HttpStatusCode.BadRequest,
115-
new ErrorBase("ValidationError", string.Join(", ", failures.Select(err => err.ErrorMessage).ToArray()))
116-
));
112+
throw new ValidationException(Error.Create("ValidationError", string.Join(", ", failures.Select(err => err.ErrorMessage).ToArray())
113+
, HttpStatusCode.BadRequest));
117114
}
118115

119116
return await next();

src/MakeSimple.SharedKernel.Infrastructure/MakeSimple.SharedKernel.Infrastructure.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
55
<SonarQubeExclude>True</SonarQubeExclude>
66
<SonarQubeTestProject>False</SonarQubeTestProject>
7-
<Version>1.0.15</Version>
7+
<Version>1.0.16</Version>
88
<ApplicationIcon>logo.ico</ApplicationIcon>
99
<Authors>JohnnyTran</Authors>
1010
<Company>MakeSimple</Company>

src/MakeSimple.SharedKernel.Infrastructure/Repository/EfRepositoryGeneric.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using AutoMapper;
44
using AutoMapper.QueryableExtensions;
55
using MakeSimple.SharedKernel.Contract;
6-
using MakeSimple.SharedKernel.Infrastructure.DTO;
76
using MakeSimple.SharedKernel.Utils;
87
using MakeSimple.SharedKernel.Wrappers;
98
using Microsoft.EntityFrameworkCore;
@@ -13,7 +12,6 @@
1312
using System.Collections.Generic;
1413
using System.Linq;
1514
using System.Linq.Expressions;
16-
using System.Net;
1715
using System.Threading.Tasks;
1816

1917
public class EfRepositoryGeneric<TContext, TEntity> : Disposable, IRepository<TContext, TEntity>
@@ -89,7 +87,7 @@ public async Task<List<TEntity>> ToListAsync(
8987
return await query.AsNoTracking().ToListAsync();
9088
}
9189

92-
public async Task<IPaginatedList<DTO>> ToListAsync<DTO>(
90+
public async Task<PaginatedList<DTO>> ToListAsync<DTO>(
9391
PaginationQuery paging
9492
, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null
9593
, string expandSorts = null
@@ -156,10 +154,13 @@ await query.AsNoTracking().ProjectTo<DTO>(_mapper.ConfigurationProvider).ToListA
156154
, paging.PageSize
157155
);
158156
}
159-
else
160-
{
161-
return new PaginatedList<DTO>(HttpStatusCode.NotFound);
162-
}
157+
158+
return new PaginatedList<DTO>(
159+
new List<DTO>()
160+
, totalItems
161+
, paging.PageNumber
162+
, paging.PageSize
163+
);
163164
}
164165

165166
public async Task<TEntity> FirstOrDefaultAsync(object key)
@@ -181,10 +182,11 @@ public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> f
181182
query = query.Include(include);
182183
}
183184
}
185+
184186
return await query.FirstOrDefaultAsync().ConfigureAwait(false);
185187
}
186188

187-
public async Task<IResponse<DTO>> FirstOrDefaultAsync<DTO>(object key)
189+
public async Task<Response<DTO>> FindAsync<DTO>(object key)
188190
{
189191
Guard.NotNull(key, nameof(key));
190192

@@ -196,11 +198,11 @@ public async Task<IResponse<DTO>> FirstOrDefaultAsync<DTO>(object key)
196198
}
197199
else
198200
{
199-
return new Response<DTO>(HttpStatusCode.NotFound, new DataNotFoundError("key"));
201+
throw new KeyNotFoundException();
200202
}
201203
}
202204

203-
public async Task<IResponse<DTO>> FirstOrDefaultAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
205+
public async Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
204206
{
205207
Guard.NotNull(filter, nameof(filter));
206208

@@ -220,7 +222,7 @@ public async Task<IResponse<DTO>> FirstOrDefaultAsync<DTO>(Expression<Func<TEnti
220222
}
221223
else
222224
{
223-
return new Response<DTO>(HttpStatusCode.NotFound);
225+
throw new KeyNotFoundException();
224226
}
225227
}
226228

0 commit comments

Comments
 (0)