Skip to content

Commit 124f359

Browse files
committed
Update Wrapper class
Change wrapper class Remove Response class, Move Exception, Add HttpCode in BaseException
1 parent 78b0421 commit 124f359

File tree

16 files changed

+118
-86
lines changed

16 files changed

+118
-86
lines changed

src/MakeSimple.SharedKernel.Infrastructure.Test/Exceptions/ValidationExceptionTest.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ public class ValidationExceptionTest
99
[Fact]
1010
public void CreateResponseException_Success()
1111
{
12-
var result = Error.Create("InternalServerError", "Internal Server Error", HttpStatusCode.InternalServerError);
12+
var code = "InternalServerError";
13+
var mess = "Internal Server Error";
14+
var result = Error.Create(code, mess);
1315

1416
Assert.NotNull(result.Code);
17+
Assert.Equal(result.Code, code);
1518
Assert.NotNull(result.Message);
19+
Assert.Equal(result.Message, mess);
1620
Assert.NotNull(result.TraceId);
1721
}
1822
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Found_Success()
205205
var result = await _repositoryGeneric.FindAsync<StudentDto>(saveIds[2]);
206206

207207
Assert.NotNull(result);
208-
Assert.Equal(result.Item.Id, saveIds[2]);
208+
Assert.Equal(result.Id, saveIds[2]);
209209
}
210210

211211
[Fact]
@@ -348,9 +348,9 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Linq_Found_Success()
348348

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

351-
Assert.NotNull(result.Item);
352-
Assert.NotNull(result.Item.Class);
353-
Assert.Equal(result.Item.Id, saveIds[2]);
351+
Assert.NotNull(result);
352+
Assert.NotNull(result.Class);
353+
Assert.Equal(result.Id, saveIds[2]);
354354
}
355355

356356
[Fact]

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace MakeSimple.SharedKernel.Infrastructure.Extensions
1313
using FluentValidation.Results;
1414
using MakeSimple.SharedKernel.Contract;
1515
using MakeSimple.SharedKernel.Extensions;
16-
using MakeSimple.SharedKernel.Infrastructure.Exceptions;
16+
using MakeSimple.SharedKernel.Exceptions;
1717
using System.Net;
1818
using System.Reflection;
1919

@@ -109,8 +109,7 @@ 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(Error.Create("ValidationError", string.Join(", ", failures.Select(err => err.ErrorMessage).ToArray())
113-
, HttpStatusCode.BadRequest));
112+
throw new ValidationException(Error.Create("vd#001", $"Validation errors - {typeName} - Command: {@request}", failures.ToDictionary(e => e.PropertyName, e => e.ErrorMessage)));
114113
}
115114

116115
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.17</Version>
7+
<Version>1.1.0</Version>
88
<ApplicationIcon>logo.ico</ApplicationIcon>
99
<Authors>JohnnyTran</Authors>
1010
<Company>MakeSimple</Company>

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using AutoMapper;
44
using AutoMapper.QueryableExtensions;
55
using MakeSimple.SharedKernel.Contract;
6+
using MakeSimple.SharedKernel.Exceptions;
67
using MakeSimple.SharedKernel.Utils;
78
using MakeSimple.SharedKernel.Wrappers;
89
using Microsoft.EntityFrameworkCore;
@@ -186,23 +187,23 @@ public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> f
186187
return await query.FirstOrDefaultAsync().ConfigureAwait(false);
187188
}
188189

189-
public async Task<Response<DTO>> FindAsync<DTO>(object key)
190+
public async Task<DTO> FindAsync<DTO>(object key)
190191
{
191192
Guard.NotNull(key, nameof(key));
192193

193194
var item = await _context.Set<TEntity>().FindAsync(key).ConfigureAwait(false);
194195

195196
if (item != null)
196197
{
197-
return new Response<DTO>(_mapper.Map<DTO>(item));
198+
return _mapper.Map<DTO>(item);
198199
}
199200
else
200201
{
201-
throw new KeyNotFoundException();
202+
throw new NotFoundException(Error.Create("db#001", $"FindAsync not found item with key {key}"));
202203
}
203204
}
204205

205-
public async Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
206+
public async Task<DTO> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
206207
{
207208
Guard.NotNull(filter, nameof(filter));
208209

@@ -218,11 +219,11 @@ public async Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>>
218219
var item = await query.ProjectTo<DTO>(_mapper.ConfigurationProvider).FirstOrDefaultAsync().ConfigureAwait(false);
219220
if (item != null)
220221
{
221-
return new Response<DTO>(item);
222+
return item;
222223
}
223224
else
224225
{
225-
throw new KeyNotFoundException();
226+
throw new NotFoundException(Error.Create("db#002", $"FindAsync<DTO> not found item with filter"));
226227
}
227228
}
228229

src/MakeSimple.SharedKernel/Contract/BaseException.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
1-
using System;
2-
using System.Runtime.Serialization;
3-
4-
namespace MakeSimple.SharedKernel.Contract
1+
namespace MakeSimple.SharedKernel.Contract
52
{
3+
using System;
4+
using System.Net;
5+
using System.Runtime.Serialization;
6+
67
public abstract class BaseException : Exception
78
{
89
public Error Errors { get; private set; }
910

10-
protected BaseException(Error errors)
11+
public HttpStatusCode Code { get; private set; }
12+
13+
protected BaseException(Error errors, HttpStatusCode code)
1114
: base(errors.Message)
1215
{
1316
Errors = errors;
17+
Code = code;
1418
}
1519

16-
protected BaseException(Error errors, Exception innerException)
20+
protected BaseException(Error errors, HttpStatusCode code, Exception innerException)
1721
: base(errors.Message, innerException)
1822
{
1923
Errors = errors;
24+
Code = code;
2025
}
2126

22-
protected BaseException(SerializationInfo info, StreamingContext context)
27+
protected BaseException(SerializationInfo info, StreamingContext context)
2328
: base(info, context)
2429
{ }
2530
}
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
using MakeSimple.SharedKernel.Helpers;
2-
using System.Net;
3-
41
namespace MakeSimple.SharedKernel.Contract
52
{
3+
using MakeSimple.SharedKernel.Helpers;
4+
using System.Collections.Generic;
5+
66
public class Error : IDataResult
77
{
88
public string Code { get; }
99
public string Message { get; }
10-
public HttpStatusCode StatusCode { get; set; }
1110
public string TraceId { get; }
1211

13-
protected Error(string code, string message, HttpStatusCode statusCode)
12+
public Dictionary<string, string> Details { get; }
13+
14+
protected Error(string code, string message, Dictionary<string, string> details = null)
1415
{
1516
Code = code;
1617
Message = message;
17-
StatusCode = statusCode;
1818
TraceId = UuuidHelper.GenerateId();
19+
Details = details;
1920
}
2021

21-
public static Error Create(string code, string message, HttpStatusCode statusCode)
22+
public static Error Create(string code, string message, Dictionary<string, string> details = null)
2223
{
23-
return new Error(code, message, statusCode);
24+
return new Error(code, message, details);
2425
}
2526
}
2627
}

src/MakeSimple.SharedKernel/Contract/IRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface IRepository<TContext, TEntity> : IDisposable
4141
/// <exception cref="AutoMapperMappingException">Miss config Automapper</exception>
4242
/// <exception cref="KeyNotFoundException">Miss config Automapper</exception>
4343
/// <exception cref="NullReferenceException">Param Paging is required has value</exception>
44-
Task<Response<DTO>> FindAsync<DTO>(object key);
44+
Task<DTO> FindAsync<DTO>(object key);
4545

4646
/// <summary>
4747
/// Get row by filter and auto mapper to Model DTO
@@ -52,7 +52,7 @@ public interface IRepository<TContext, TEntity> : IDisposable
5252
/// <exception cref="AutoMapperMappingException">Miss config Automapper</exception>
5353
/// <exception cref="KeyNotFoundException">Miss config Automapper</exception>
5454
/// <exception cref="NullReferenceException">Param Paging is required has value</exception>
55-
Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes);
55+
Task<DTO> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes);
5656

5757
/// <summary>
5858
/// Get data from Database

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/ConflictException.cs renamed to src/MakeSimple.SharedKernel/Exceptions/ConflictException.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using MakeSimple.SharedKernel.Contract;
2-
using System;
3-
using System.Runtime.Serialization;
4-
5-
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
1+
namespace MakeSimple.SharedKernel.Exceptions
62
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
7+
78
public class ConflictException : BaseException
89
{
910
public ConflictException(Error errorResult)
10-
: base(errorResult)
11+
: base(errorResult, HttpStatusCode.Conflict)
1112
{ }
1213

1314
public ConflictException(Error errorResult, Exception innerException)
14-
: base(errorResult, innerException)
15+
: base(errorResult, HttpStatusCode.Conflict, innerException)
1516
{ }
1617

1718
protected ConflictException(SerializationInfo info, StreamingContext context)

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/ForbiddenException.cs renamed to src/MakeSimple.SharedKernel/Exceptions/ForbiddenException.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
using MakeSimple.SharedKernel.Contract;
2-
using System;
3-
using System.Runtime.Serialization;
4-
5-
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
1+
namespace MakeSimple.SharedKernel.Exceptions
62
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
77
public class ForbiddenException : BaseException
88
{
99
public ForbiddenException(Error errorResult)
10-
: base(errorResult)
10+
: base(errorResult, HttpStatusCode.Forbidden)
1111
{ }
1212

1313
public ForbiddenException(Error errorResult, Exception innerException)
14-
: base(errorResult, innerException)
14+
: base(errorResult, HttpStatusCode.Forbidden, innerException)
1515
{ }
1616

1717
protected ForbiddenException(SerializationInfo info, StreamingContext context)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace MakeSimple.SharedKernel.Exceptions
2+
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
7+
8+
public class NotFoundException : BaseException
9+
{
10+
public NotFoundException(Error errorResult)
11+
: base(errorResult, HttpStatusCode.NotFound)
12+
{ }
13+
14+
public NotFoundException(Error errorResult, Exception innerException)
15+
: base(errorResult, HttpStatusCode.NotFound, innerException)
16+
{ }
17+
18+
protected NotFoundException(SerializationInfo info, StreamingContext context)
19+
: base(info, context)
20+
{ }
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
namespace MakeSimple.SharedKernel.Exceptions
2+
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
7+
8+
public class TimeOutException : BaseException
9+
{
10+
public TimeOutException(Error errorResult)
11+
: base(errorResult, HttpStatusCode.RequestTimeout)
12+
{ }
13+
14+
public TimeOutException(Error errorResult, Exception innerException)
15+
: base(errorResult, HttpStatusCode.RequestTimeout, innerException)
16+
{ }
17+
18+
protected TimeOutException(SerializationInfo info, StreamingContext context)
19+
: base(info, context)
20+
{ }
21+
}
22+
}

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/UnhandledException.cs renamed to src/MakeSimple.SharedKernel/Exceptions/UnhandledException.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using MakeSimple.SharedKernel.Contract;
2-
using System;
3-
using System.Runtime.Serialization;
4-
5-
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
1+
namespace MakeSimple.SharedKernel.Exceptions
62
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
7+
78
public class UnhandledException : BaseException
89
{
910
public UnhandledException(Error errorResult)
10-
: base(errorResult)
11+
: base(errorResult, HttpStatusCode.InternalServerError)
1112
{ }
1213

1314
public UnhandledException(Error errorResult, Exception innerException)
14-
: base(errorResult, innerException)
15+
: base(errorResult, HttpStatusCode.InternalServerError, innerException)
1516
{ }
1617

1718
protected UnhandledException(SerializationInfo info, StreamingContext context)

src/MakeSimple.SharedKernel.Infrastructure/Exceptions/ValidationException.cs renamed to src/MakeSimple.SharedKernel/Exceptions/ValidationException.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1-
using MakeSimple.SharedKernel.Contract;
2-
using System;
3-
using System.Runtime.Serialization;
4-
5-
namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
1+
namespace MakeSimple.SharedKernel.Exceptions
62
{
3+
using MakeSimple.SharedKernel.Contract;
4+
using System;
5+
using System.Net;
6+
using System.Runtime.Serialization;
7+
78
public class ValidationException : BaseException
89
{
910
public ValidationException(Error errorResult)
10-
: base(errorResult)
11+
: base(errorResult, HttpStatusCode.BadRequest)
1112
{ }
1213

1314
public ValidationException(Error errorResult, Exception innerException)
14-
: base(errorResult, innerException)
15+
: base(errorResult, HttpStatusCode.BadRequest, innerException)
1516
{ }
1617

1718
protected ValidationException(SerializationInfo info, StreamingContext context)

src/MakeSimple.SharedKernel/MakeSimple.SharedKernel.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<SonarQubeExclude>True</SonarQubeExclude>
6-
<Version>1.0.17</Version>
6+
<Version>1.1.0</Version>
77
<PackageIcon>logo.png</PackageIcon>
88
<NeutralLanguage>en</NeutralLanguage>
99
<ApplicationIcon>logo.ico</ApplicationIcon>

src/MakeSimple.SharedKernel/Wrappers/Response.cs

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

0 commit comments

Comments
 (0)