-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: @CCT-21 Configure Swagger, Logging, Exception Handler Middlew…
…are (#9) Ticket: https://uit-project.atlassian.net/browse/CCT-21
- Loading branch information
1 parent
88e8a05
commit 7d7f57d
Showing
33 changed files
with
777 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,4 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ChitChat.Application.Mapping; | ||
using ChitChat.Application.Services; | ||
using FluentValidation; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
namespace ChitChat.Application | ||
{ | ||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddApplicationServices(this IServiceCollection services) | ||
{ | ||
services.AddAutoMapper(Assembly.GetExecutingAssembly()); | ||
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly()); | ||
services.AddMediatR(cfg => | ||
{ | ||
cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()); | ||
}); | ||
services.AddAutoMapper(typeof(IMappingProfileMarker)); | ||
services.AddService(); | ||
return services; | ||
} | ||
public static IServiceCollection AddService(this IServiceCollection services) { | ||
|
||
return services; | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/ChitChat.Application/Exceptions/ApplicationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using ChitChat.Application.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Exceptions | ||
{ | ||
[Serializable] | ||
public abstract class ApplicationException : Exception | ||
{ | ||
public ApiResultErrorCodes Code { get; protected set; } | ||
|
||
public bool TransactionRollback { get; protected set; } = true; | ||
|
||
public ApplicationException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using ChitChat.Application.Models; | ||
|
||
namespace ChitChat.Application.Exceptions; | ||
public class ConflictException : ApplicationException | ||
{ | ||
public ConflictException(string message, bool transactionRollback = true) : base(message) | ||
{ | ||
Code = ApiResultErrorCodes.Conflict; | ||
TransactionRollback = transactionRollback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using ChitChat.Application.Models; | ||
|
||
namespace ChitChat.Application.Exceptions | ||
{ | ||
public class ForbiddenException : ApplicationException | ||
{ | ||
public ForbiddenException(string message) : base(message) | ||
{ | ||
Code = ApiResultErrorCodes.Forbidden; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/ChitChat.Application/Exceptions/InvalidModelException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using ChitChat.Application.Models; | ||
|
||
namespace ChitChat.Application.Exceptions; | ||
|
||
[Serializable] | ||
public class InvalidModelException : ApplicationException | ||
{ | ||
public InvalidModelException(string message, bool transactionRollback = true) : base(message) | ||
{ | ||
Code = ApiResultErrorCodes.ModelValidation; | ||
TransactionRollback = transactionRollback; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using ChitChat.Application.Localization; | ||
using ChitChat.Application.Models; | ||
|
||
namespace ChitChat.Application.Exceptions | ||
{ | ||
public class NotFoundException : ApplicationException | ||
{ | ||
public NotFoundException(Guid id, Type type) : base(ValidationTexts.NotFound.Format(type.Name, id.ToString())) | ||
{ | ||
Code = ApiResultErrorCodes.NotFound; | ||
} | ||
|
||
public NotFoundException(string code, Type type) : base(ValidationTexts.NotFound.Format(type.Name, code)) | ||
{ | ||
Code = ApiResultErrorCodes.NotFound; | ||
} | ||
|
||
public NotFoundException(string message): base(message) | ||
{ | ||
Code = ApiResultErrorCodes.NotFound; | ||
} | ||
|
||
public NotFoundException(int count, Type type) : base(ValidationTexts.NotFound.Format(count, type.Name)) | ||
{ | ||
Code = ApiResultErrorCodes.NotFound; | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/ChitChat.Application/Exceptions/UnauthorizeException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace ChitChat.Application.Exceptions | ||
{ | ||
public class UnauthorizeException : ApplicationException | ||
{ | ||
public UnauthorizeException(string message) : base(message) | ||
{ | ||
Code = Models.ApiResultErrorCodes.Unauthorize; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ChitChat.Domain.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Localization | ||
{ | ||
public static class ValidationTexts | ||
{ | ||
public static readonly LocalizedText NotFound = LocalizedText.New("{0} {1} not found").AddDefaultText("{0} {1} không tồn tại."); | ||
public static readonly LocalizedText Conflict = LocalizedText.New("{0} {1} is already existed").AddDefaultText("{0} với {1} đã tồn tại."); | ||
public static readonly LocalizedText NotValidate = LocalizedText.New("{0} {1} is not validated").AddDefaultText("{0} với {1} không hợp lệ."); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Mapping | ||
{ | ||
public interface IMappingProfileMarker | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Models | ||
{ | ||
public class ApiResult<T> | ||
{ | ||
private ApiResult() { } | ||
|
||
private ApiResult(bool succeeded, T result, IEnumerable<ApiResultError> errors) | ||
{ | ||
Succeeded = succeeded; | ||
Result = result; | ||
Errors = errors; | ||
} | ||
|
||
public bool Succeeded { get; set; } | ||
|
||
public T Result { get; set; } | ||
|
||
public IEnumerable<ApiResultError> Errors { get; set; } | ||
|
||
public static ApiResult<T> Success(T result) | ||
{ | ||
return new ApiResult<T>(true, result, new List<ApiResultError>()); | ||
} | ||
|
||
public static ApiResult<T> Failure(IEnumerable<ApiResultError> errors) | ||
{ | ||
return new ApiResult<T>(false, default, errors); | ||
} | ||
} | ||
|
||
public class ApiResultError | ||
{ | ||
public string Code { get; set; } | ||
|
||
public string Message { get; set; } | ||
|
||
public ApiResultError(ApiResultErrorCodes code, string message) | ||
{ | ||
Code = code.ToString(); | ||
Message = message; | ||
} | ||
} | ||
|
||
public enum ApiResultErrorCodes | ||
{ | ||
InternalServerError, | ||
ModelValidation, | ||
PermissionValidation, | ||
NotFound, | ||
Unauthorize, | ||
Forbidden, | ||
Conflict | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Models.Dtos | ||
{ | ||
public class BaseResponseDto | ||
{ | ||
public Guid Id; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/ChitChat.Application/Services/Caching/CacheEntryOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Services.Caching | ||
{ | ||
public class CacheEntryOptions | ||
{ | ||
public TimeSpan SlidingExpiration { get; set; } | ||
public TimeSpan AbsoluteExpirationRelativeToNow { get; set; } | ||
|
||
public static CacheEntryOptions Default = new() | ||
{ | ||
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(1), | ||
SlidingExpiration = TimeSpan.FromHours(1) | ||
}; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/ChitChat.Application/Services/Caching/ICachingService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Application.Services.Caching | ||
{ | ||
public interface ICachingService | ||
{ | ||
Task Set<T>(string key, T value); | ||
Task<T> Get<T>(string key); | ||
Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> func); | ||
Task Remove(string key); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/ChitChat.Infrastructure/Caching/CachingRegistrations.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using ChitChat.Application.Services.Caching; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Infrastructure.Caching | ||
{ | ||
public static class CachingRegistrations | ||
{ | ||
public static WebApplicationBuilder AddCaching(this WebApplicationBuilder builder) | ||
{ | ||
builder.Services | ||
.AddMemoryCache() | ||
.AddSingleton<ICachingService, MemoryCacheService>(); | ||
return builder; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using ChitChat.Application.Services.Caching; | ||
using Microsoft.Extensions.Caching.Memory; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChitChat.Infrastructure.Caching | ||
{ | ||
public class MemoryCacheService : ICachingService | ||
{ | ||
private readonly IMemoryCache _memoryCache; | ||
|
||
private MemoryCacheEntryOptions MemoryCacheEntryOptions = new() | ||
{ | ||
AbsoluteExpirationRelativeToNow = CacheEntryOptions.Default.AbsoluteExpirationRelativeToNow, | ||
SlidingExpiration = CacheEntryOptions.Default.SlidingExpiration | ||
}; | ||
|
||
public MemoryCacheService(IMemoryCache memoryCache) | ||
{ | ||
this._memoryCache = memoryCache; | ||
} | ||
|
||
public async Task Set<T>(string key, T value) | ||
{ | ||
await Task.Yield(); | ||
this._memoryCache.Set(key, value, this.MemoryCacheEntryOptions); | ||
} | ||
|
||
public async Task<T> Get<T>(string key) | ||
{ | ||
await Task.Yield(); | ||
this._memoryCache.TryGetValue<T>(key, out var cacheEntry); | ||
return cacheEntry; | ||
} | ||
|
||
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> func) | ||
{ | ||
var cacheEntry = await this._memoryCache.GetOrCreateAsync(key, async entry => | ||
{ | ||
entry | ||
.SetSlidingExpiration(this.MemoryCacheEntryOptions.SlidingExpiration!.Value) | ||
.SetAbsoluteExpiration(this.MemoryCacheEntryOptions.AbsoluteExpirationRelativeToNow!.Value); | ||
|
||
return await func(); | ||
}); | ||
|
||
return cacheEntry; | ||
} | ||
|
||
public async Task Remove(string key) | ||
{ | ||
this._memoryCache.Remove(key); | ||
await Task.CompletedTask; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.