Skip to content

Update Wrapper class #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ public class ValidationExceptionTest
[Fact]
public void CreateResponseException_Success()
{
var result = Error.Create("InternalServerError", "Internal Server Error", HttpStatusCode.InternalServerError);
var code = "InternalServerError";
var mess = "Internal Server Error";
var result = Error.Create(code, mess);

Assert.NotNull(result.Code);
Assert.Equal(result.Code, code);
Assert.NotNull(result.Message);
Assert.Equal(result.Message, mess);
Assert.NotNull(result.TraceId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Found_Success()
var result = await _repositoryGeneric.FindAsync<StudentDto>(saveIds[2]);

Assert.NotNull(result);
Assert.Equal(result.Item.Id, saveIds[2]);
Assert.Equal(result.Id, saveIds[2]);
}

[Fact]
Expand Down Expand Up @@ -348,9 +348,9 @@ public async Task GetFirstOrDefaultAsync_WithMapper_Linq_Found_Success()

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

Assert.NotNull(result.Item);
Assert.NotNull(result.Item.Class);
Assert.Equal(result.Item.Id, saveIds[2]);
Assert.NotNull(result);
Assert.NotNull(result.Class);
Assert.Equal(result.Id, saveIds[2]);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace MakeSimple.SharedKernel.Infrastructure.Extensions
using FluentValidation.Results;
using MakeSimple.SharedKernel.Contract;
using MakeSimple.SharedKernel.Extensions;
using MakeSimple.SharedKernel.Infrastructure.Exceptions;
using MakeSimple.SharedKernel.Exceptions;
using System.Net;
using System.Reflection;

Expand Down Expand Up @@ -109,8 +109,7 @@ public async Task<TResponse> Handle(TRequest request, CancellationToken cancella
{
_logger.LogWarning("Validation errors - {typeName} - Command: {@request} - Errors: {@failures}", typeName, request, failures);

throw new ValidationException(Error.Create("ValidationError", string.Join(", ", failures.Select(err => err.ErrorMessage).ToArray())
, HttpStatusCode.BadRequest));
throw new ValidationException(Error.Create("vd#001", $"Validation errors - {typeName} - Command: {@request}", failures.ToDictionary(e => e.PropertyName, e => e.ErrorMessage)));
}

return await next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<SonarQubeExclude>True</SonarQubeExclude>
<SonarQubeTestProject>False</SonarQubeTestProject>
<Version>1.0.17</Version>
<Version>1.1.0</Version>
<ApplicationIcon>logo.ico</ApplicationIcon>
<Authors>JohnnyTran</Authors>
<Company>MakeSimple</Company>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using AutoMapper;
using AutoMapper.QueryableExtensions;
using MakeSimple.SharedKernel.Contract;
using MakeSimple.SharedKernel.Exceptions;
using MakeSimple.SharedKernel.Utils;
using MakeSimple.SharedKernel.Wrappers;
using Microsoft.EntityFrameworkCore;
Expand Down Expand Up @@ -186,23 +187,23 @@ public async Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> f
return await query.FirstOrDefaultAsync().ConfigureAwait(false);
}

public async Task<Response<DTO>> FindAsync<DTO>(object key)
public async Task<DTO> FindAsync<DTO>(object key)
{
Guard.NotNull(key, nameof(key));

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

if (item != null)
{
return new Response<DTO>(_mapper.Map<DTO>(item));
return _mapper.Map<DTO>(item);
}
else
{
throw new KeyNotFoundException();
throw new NotFoundException(Error.Create("db#001", $"FindAsync not found item with key {key}"));
}
}

public async Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
public async Task<DTO> FindAsync<DTO>(Expression<Func<TEntity, bool>> filter, params Expression<Func<TEntity, object>>[] includes)
{
Guard.NotNull(filter, nameof(filter));

Expand All @@ -218,11 +219,11 @@ public async Task<Response<DTO>> FindAsync<DTO>(Expression<Func<TEntity, bool>>
var item = await query.ProjectTo<DTO>(_mapper.ConfigurationProvider).FirstOrDefaultAsync().ConfigureAwait(false);
if (item != null)
{
return new Response<DTO>(item);
return item;
}
else
{
throw new KeyNotFoundException();
throw new NotFoundException(Error.Create("db#002", $"FindAsync<DTO> not found item with filter"));
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/MakeSimple.SharedKernel/Contract/BaseException.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@
using System;
using System.Runtime.Serialization;

namespace MakeSimple.SharedKernel.Contract
namespace MakeSimple.SharedKernel.Contract
{
using System;
using System.Net;
using System.Runtime.Serialization;

public abstract class BaseException : Exception
{
public Error Errors { get; private set; }

protected BaseException(Error errors)
public HttpStatusCode Code { get; private set; }

protected BaseException(Error errors, HttpStatusCode code)
: base(errors.Message)
{
Errors = errors;
Code = code;
}

protected BaseException(Error errors, Exception innerException)
protected BaseException(Error errors, HttpStatusCode code, Exception innerException)
: base(errors.Message, innerException)
{
Errors = errors;
Code = code;
}

protected BaseException(SerializationInfo info, StreamingContext context)
protected BaseException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
Expand Down
17 changes: 9 additions & 8 deletions src/MakeSimple.SharedKernel/Contract/Error.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
using MakeSimple.SharedKernel.Helpers;
using System.Net;

namespace MakeSimple.SharedKernel.Contract
{
using MakeSimple.SharedKernel.Helpers;
using System.Collections.Generic;

public class Error : IDataResult
{
public string Code { get; }
public string Message { get; }
public HttpStatusCode StatusCode { get; set; }
public string TraceId { get; }

protected Error(string code, string message, HttpStatusCode statusCode)
public Dictionary<string, string> Details { get; }

protected Error(string code, string message, Dictionary<string, string> details = null)
{
Code = code;
Message = message;
StatusCode = statusCode;
TraceId = UuuidHelper.GenerateId();
Details = details;
}

public static Error Create(string code, string message, HttpStatusCode statusCode)
public static Error Create(string code, string message, Dictionary<string, string> details = null)
{
return new Error(code, message, statusCode);
return new Error(code, message, details);
}
}
}
4 changes: 2 additions & 2 deletions src/MakeSimple.SharedKernel/Contract/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public interface IRepository<TContext, TEntity> : IDisposable
/// <exception cref="AutoMapperMappingException">Miss config Automapper</exception>
/// <exception cref="KeyNotFoundException">Miss config Automapper</exception>
/// <exception cref="NullReferenceException">Param Paging is required has value</exception>
Task<Response<DTO>> FindAsync<DTO>(object key);
Task<DTO> FindAsync<DTO>(object key);

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

/// <summary>
/// Get data from Database
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using MakeSimple.SharedKernel.Contract;
using System;
using System.Runtime.Serialization;

namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;

public class ConflictException : BaseException
{
public ConflictException(Error errorResult)
: base(errorResult)
: base(errorResult, HttpStatusCode.Conflict)
{ }

public ConflictException(Error errorResult, Exception innerException)
: base(errorResult, innerException)
: base(errorResult, HttpStatusCode.Conflict, innerException)
{ }

protected ConflictException(SerializationInfo info, StreamingContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using MakeSimple.SharedKernel.Contract;
using System;
using System.Runtime.Serialization;

namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;
public class ForbiddenException : BaseException
{
public ForbiddenException(Error errorResult)
: base(errorResult)
: base(errorResult, HttpStatusCode.Forbidden)
{ }

public ForbiddenException(Error errorResult, Exception innerException)
: base(errorResult, innerException)
: base(errorResult, HttpStatusCode.Forbidden, innerException)
{ }

protected ForbiddenException(SerializationInfo info, StreamingContext context)
Expand Down
22 changes: 22 additions & 0 deletions src/MakeSimple.SharedKernel/Exceptions/NotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;

public class NotFoundException : BaseException
{
public NotFoundException(Error errorResult)
: base(errorResult, HttpStatusCode.NotFound)
{ }

public NotFoundException(Error errorResult, Exception innerException)
: base(errorResult, HttpStatusCode.NotFound, innerException)
{ }

protected NotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}
22 changes: 22 additions & 0 deletions src/MakeSimple.SharedKernel/Exceptions/TimeOutException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;

public class TimeOutException : BaseException
{
public TimeOutException(Error errorResult)
: base(errorResult, HttpStatusCode.RequestTimeout)
{ }

public TimeOutException(Error errorResult, Exception innerException)
: base(errorResult, HttpStatusCode.RequestTimeout, innerException)
{ }

protected TimeOutException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using MakeSimple.SharedKernel.Contract;
using System;
using System.Runtime.Serialization;

namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;

public class UnhandledException : BaseException
{
public UnhandledException(Error errorResult)
: base(errorResult)
: base(errorResult, HttpStatusCode.InternalServerError)
{ }

public UnhandledException(Error errorResult, Exception innerException)
: base(errorResult, innerException)
: base(errorResult, HttpStatusCode.InternalServerError, innerException)
{ }

protected UnhandledException(SerializationInfo info, StreamingContext context)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
using MakeSimple.SharedKernel.Contract;
using System;
using System.Runtime.Serialization;

namespace MakeSimple.SharedKernel.Infrastructure.Exceptions
namespace MakeSimple.SharedKernel.Exceptions
{
using MakeSimple.SharedKernel.Contract;
using System;
using System.Net;
using System.Runtime.Serialization;

public class ValidationException : BaseException
{
public ValidationException(Error errorResult)
: base(errorResult)
: base(errorResult, HttpStatusCode.BadRequest)
{ }

public ValidationException(Error errorResult, Exception innerException)
: base(errorResult, innerException)
: base(errorResult, HttpStatusCode.BadRequest, innerException)
{ }

protected ValidationException(SerializationInfo info, StreamingContext context)
Expand Down
2 changes: 1 addition & 1 deletion src/MakeSimple.SharedKernel/MakeSimple.SharedKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<SonarQubeExclude>True</SonarQubeExclude>
<Version>1.0.17</Version>
<Version>1.1.0</Version>
<PackageIcon>logo.png</PackageIcon>
<NeutralLanguage>en</NeutralLanguage>
<ApplicationIcon>logo.ico</ApplicationIcon>
Expand Down
25 changes: 0 additions & 25 deletions src/MakeSimple.SharedKernel/Wrappers/Response.cs

This file was deleted.