Skip to content

Commit

Permalink
Merge pull request #2 from PhucNghi176/main
Browse files Browse the repository at this point in the history
123
  • Loading branch information
PhucNghi176 authored Jul 8, 2024
2 parents d57f334 + f6e9a13 commit 3ca5c17
Show file tree
Hide file tree
Showing 166 changed files with 1,331 additions and 1,030 deletions.
12 changes: 12 additions & 0 deletions DeerCoffeeShop.API/.config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "8.0.6",
"commands": [
"dotnet-ef"
]
}
}
}
1 change: 0 additions & 1 deletion DeerCoffeeShop.API/.env

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public class ApiVersionSwaggerGenOptions(IApiVersionDescriptionProvider provider

public void Configure(SwaggerGenOptions options)
{
foreach (var description in _provider.ApiVersionDescriptions.OrderByDescending(o => o.ApiVersion))
foreach (ApiVersionDescription? description in _provider.ApiVersionDescriptions.OrderByDescending(o => o.ApiVersion))
{
options.SwaggerDoc(description.GroupName, CreateInfoForApiVersion(description));
}
}

private static OpenApiInfo CreateInfoForApiVersion(ApiVersionDescription description)
{
var info = new OpenApiInfo()
OpenApiInfo info = new()
{
Title = "Deer Coffee Shop API",
Version = description.ApiVersion.ToString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class ApiVersioningConfiguration
{
public static IServiceCollection ConfigureApiVersioning(this IServiceCollection services)
{
services.AddApiVersioning(options =>
_ = services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public static IServiceCollection ConfigureApplicationSecurity(
this IServiceCollection services,
IConfiguration configuration)
{
services.AddTransient<ICurrentUserService, CurrentUserService>();
services.AddTransient<JwtService>();
_ = services.AddTransient<ICurrentUserService, CurrentUserService>();
_ = services.AddTransient<JwtService>();
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
services.AddHttpContextAccessor();
_ = services.AddHttpContextAccessor();

//services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

Expand All @@ -30,7 +30,7 @@ public static IServiceCollection ConfigureApplicationSecurity(
// options.TokenValidationParameters.RoleClaimType = "role";
// options.SaveToken = true;
// });
services.AddAuthentication(options =>
_ = services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
Expand All @@ -49,7 +49,7 @@ public static IServiceCollection ConfigureApplicationSecurity(
};
});

services.AddAuthorization(ConfigureAuthorization);
_ = services.AddAuthorization(ConfigureAuthorization);

return services;
}
Expand Down
2 changes: 1 addition & 1 deletion DeerCoffeeShop.API/Configuration/CorsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class CorsConfiguration
{
public static IServiceCollection ConfigurationCors(this IServiceCollection services)
{
services.AddCors(o =>
_ = services.AddCors(o =>
{
o.AddPolicy("CorsPolicy",
builder => builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public static class ProblemDetailsConfiguration
{
public static IServiceCollection ConfigureProblemDetails(this IServiceCollection services)
{
services.AddProblemDetails(conf => conf.CustomizeProblemDetails = context =>
_ = services.AddProblemDetails(conf => conf.CustomizeProblemDetails = context =>
{
context.ProblemDetails.Type = $"https://httpstatuses.io/{context.ProblemDetails.Status}";

if (context.ProblemDetails.Status != 500) { return; }
context.ProblemDetails.Title = "Internal Server Error";
context.ProblemDetails.Extensions.TryAdd("traceId", Activity.Current?.Id ?? context.HttpContext.TraceIdentifier);
_ = context.ProblemDetails.Extensions.TryAdd("traceId", Activity.Current?.Id ?? context.HttpContext.TraceIdentifier);

var env = context.HttpContext.RequestServices.GetService<IWebHostEnvironment>()!;
IWebHostEnvironment env = context.HttpContext.RequestServices.GetService<IWebHostEnvironment>()!;
if (!env.IsDevelopment()) { return; }

var exceptionFeature = context.HttpContext.Features.Get<IExceptionHandlerFeature>();
IExceptionHandlerFeature? exceptionFeature = context.HttpContext.Features.Get<IExceptionHandlerFeature>();
if (exceptionFeature is null) { return; }
context.ProblemDetails.Detail = exceptionFeature.Error.ToString();
});
Expand Down
26 changes: 13 additions & 13 deletions DeerCoffeeShop.API/Configuration/SwashbuckleConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ public static class SwashbuckleConfiguration
{
public static IServiceCollection ConfigureSwagger(this IServiceCollection services)
{
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ApiVersionSwaggerGenOptions>();
services.AddSwaggerGen(
_ = services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ApiVersionSwaggerGenOptions>();
_ = services.AddSwaggerGen(
options =>
{
options.SchemaFilter<RequireNonNullablePropertiesSchemaFilter>();
options.SupportNonNullableReferenceTypes();
options.CustomSchemaIds(x => x.FullName);

var apiXmlFile = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
string apiXmlFile = Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
if (File.Exists(apiXmlFile))
{
options.IncludeXmlComments(apiXmlFile);
}

var applicationXmlFile = Path.Combine(AppContext.BaseDirectory, $"{typeof(DependencyInjection).Assembly.GetName().Name}.xml");
string applicationXmlFile = Path.Combine(AppContext.BaseDirectory, $"{typeof(DependencyInjection).Assembly.GetName().Name}.xml");
if (File.Exists(applicationXmlFile))
{
options.IncludeXmlComments(applicationXmlFile);
}
options.OperationFilter<AuthorizeCheckOperationFilter>();

var securityScheme = new OpenApiSecurityScheme()
OpenApiSecurityScheme securityScheme = new()
{
Name = "Authorization",
Description = "Enter a Bearer Token into the `Value` field to have it automatically prefixed with `Bearer ` and used as an `Authorization` header value for requests.",
Expand All @@ -57,14 +57,14 @@ public static IServiceCollection ConfigureSwagger(this IServiceCollection servic
{ securityScheme, Array.Empty<string>() }
});
});
services.AddRouting(options => options.LowercaseUrls = true);
_ = services.AddRouting(options => options.LowercaseUrls = true);
return services;
}

public static void UseSwashbuckle(this IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI(
_ = app.UseSwagger();
_ = app.UseSwaggerUI(
options =>
{
options.RoutePrefix = "swagger";
Expand All @@ -80,9 +80,9 @@ public static void UseSwashbuckle(this IApplicationBuilder app)

private static void AddSwaggerEndpoints(IApplicationBuilder app, SwaggerUIOptions options)
{
var provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();
IApiVersionDescriptionProvider provider = app.ApplicationServices.GetRequiredService<IApiVersionDescriptionProvider>();

foreach (var description in provider.ApiVersionDescriptions.OrderByDescending(o => o.ApiVersion))
foreach (ApiVersionDescription? description in provider.ApiVersionDescriptions.OrderByDescending(o => o.ApiVersion))
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", $"{options.OAuthConfigObject.AppName} {description.GroupName}");
}
Expand All @@ -93,13 +93,13 @@ internal class RequireNonNullablePropertiesSchemaFilter : ISchemaFilter
{
public void Apply(OpenApiSchema model, SchemaFilterContext context)
{
var additionalRequiredProps = model.Properties
IEnumerable<string> additionalRequiredProps = model.Properties
.Where(x => !x.Value.Nullable && !model.Required.Contains(x.Key))
.Select(x => x.Key);

foreach (var propKey in additionalRequiredProps)
foreach (string? propKey in additionalRequiredProps)
{
model.Required.Add(propKey);
_ = model.Required.Add(propKey);
}
}
}
Expand Down
28 changes: 19 additions & 9 deletions DeerCoffeeShop.API/Controllers/EmployeeController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using DeerCoffeeShop.API.Controllers.ResponseTypes;
using DeerCoffeeShop.API.Services;
using DeerCoffeeShop.Application.Authentication.Login;
using DeerCoffeeShop.Application.Common.Security;
using DeerCoffeeShop.Application.Employees;
using DeerCoffeeShop.Application.Employees.AddDeviceToken;
using DeerCoffeeShop.Application.Employees.CreateEmployee;
using DeerCoffeeShop.Application.Employees.DeleteEmployee;
using DeerCoffeeShop.Application.Employees.GetAllEmployee;
using DeerCoffeeShop.Application.Employees.GetEmployee;
using DeerCoffeeShop.Application.Employees.UpdateEmployee;
using DeerCoffeeShop.Application.Employees.GetEmployeeInfo;
using DeerCoffeeShop.Application.Employees.UpdateEmployee;
using MediatR;
using Microsoft.AspNetCore.Mvc;

Expand All @@ -20,7 +20,7 @@ public class EmployeeController(ISender sender, JwtService _jwtService) : BaseCo
[HttpPut()]
public async Task<ActionResult<string>> UpdateEmployee(UpdateEmployeeCommand command, CancellationToken cancellationToken = default)
{
var result = await _sender.Send(new UpdateEmployeeCommand(command.EmployeeID, command.Email, command.PhoneNumber, command.Address, command.RoleId, command.FullName, command.DateOfBirth, command.IsActive), cancellationToken);
string result = await _sender.Send(new UpdateEmployeeCommand(command.EmployeeID, command.Email, command.PhoneNumber, command.Address, command.RoleId, command.FullName, command.DateOfBirth, command.IsActive), cancellationToken);
var response = new
{
Message = result,
Expand All @@ -31,8 +31,8 @@ public async Task<ActionResult<string>> UpdateEmployee(UpdateEmployeeCommand com
[HttpPost("login")]
public async Task<IActionResult> Login(LoginQuery loginQuery, CancellationToken cancellationToken = default)
{
var loginDTO = await _sender.Send(new LoginQuery(loginQuery.EmployeeID, loginQuery.Password), cancellationToken);
var token = _jwtService.CreateToken(loginDTO.Id, loginDTO.RoleName, loginDTO.RefreshToken,loginDTO.RestaurantID);
Application.Authentication.LoginDTO loginDTO = await _sender.Send(new LoginQuery(loginQuery.EmployeeID, loginQuery.Password), cancellationToken);
JwtService.Token token = _jwtService.CreateToken(loginDTO.Id, loginDTO.RoleName, loginDTO.RefreshToken, loginDTO.RestaurantID);
token.EmployeeDto = await _sender.Send(new GetEmployeeInfoQuery(loginDTO.Id), cancellationToken);
var response = new
{
Expand All @@ -45,14 +45,14 @@ public async Task<IActionResult> Login(LoginQuery loginQuery, CancellationToken
[HttpPost("apply")]
public async Task<ActionResult<string>> CreateEmployeeAplication([FromBody] CreateEmployeeCommand command, CancellationToken cancellationToken)
{
var result = await _sender.Send(command, cancellationToken);
string result = await _sender.Send(command, cancellationToken);
return Ok(result);
}

[HttpGet("{employee_id}")]
public async Task<ActionResult<EmployeeDto>> GetAll([FromRoute] string employee_id, CancellationToken cancellationToken)
{
var result = await _sender.Send(new GetEmployeeQuery(employee_id), cancellationToken);
EmployeeDto result = await _sender.Send(new GetEmployeeQuery(employee_id), cancellationToken);
var response = new
{
Message = "Get Employee Information Successfully !",
Expand All @@ -64,7 +64,7 @@ public async Task<ActionResult<EmployeeDto>> GetAll([FromRoute] string employee_
[HttpDelete("")]
public async Task<ActionResult<string>> Delete(DeleteEmployeeCommand command, CancellationToken cancellationToken = default)
{
var result = await _sender.Send(new DeleteEmployeeCommand(command.EmployeeID), cancellationToken);
string result = await _sender.Send(new DeleteEmployeeCommand(command.EmployeeID), cancellationToken);
var response = new
{
Message = result,
Expand All @@ -76,13 +76,23 @@ public async Task<ActionResult<string>> Delete(DeleteEmployeeCommand command, Ca
[HttpGet("")]
public async Task<ActionResult<EmployeeDto>> GetAll([FromQuery] GetAllEmployeeQuery query, CancellationToken cancellationToken)
{
var result = await _sender.Send(query, cancellationToken);
Application.Common.Pagination.PagedResult<EmployeeDto> result = await _sender.Send(query, cancellationToken);
var response = new
{
Message = "Get All Successfully",
Data = result
};
return Ok(response);
}
[HttpPost("add-token")]
public async Task<ActionResult<string>> AddDeviceToken(AddDeviceTokenCommand command, CancellationToken cancellationToken)
{
string result = await _sender.Send(command, cancellationToken);
var response = new
{
Message = result,
};
return Ok(response);
}

}
Loading

0 comments on commit 3ca5c17

Please sign in to comment.