Restucture Project

This commit is contained in:
Suphonchai Phoonsawat 2023-03-17 14:24:43 +07:00
parent 5a14843e31
commit b2abfe9e87
177 changed files with 1231 additions and 869 deletions

View file

@ -7,6 +7,7 @@
<UserSecretsId>d45c95ce-6b9d-4aa7-aaaf-62fe8b792934</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<RootNamespace>BMA.EHR.Recruit.Service</RootNamespace>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

View file

@ -0,0 +1,73 @@
using BMA.EHR.Core;
using BMA.EHR.Recruit.Service.Responses;
using BMA.EHR.Recruit.Service.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
namespace BMA.EHR.Recruit.Service.Controllers
{
public class BaseController : ControllerBase
{
#region " Methods "
#region " Protected "
#region " IActionResult "
protected virtual ActionResult<ResponseObject> Success(string message, object? result = null)
{
if (result != null)
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message,
Result = result
});
}
else
{
return Ok(new ResponseObject
{
Status = StatusCodes.Status200OK,
Message = message
});
}
}
protected virtual ActionResult<ResponseObject> Success(object? result = null)
{
return Success(GlobalMessages.Success, result);
}
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
{
return StatusCode((int)statusCode, new ResponseObject
{
Status = statusCode,
Message = message
});
}
protected virtual ActionResult<ResponseObject> Error(Exception exception, int statusCode = StatusCodes.Status500InternalServerError)
{
var msg = exception.Message;
var inner = exception.InnerException;
while (inner != null)
{
msg += $" {inner.Message}\r\n";
inner = inner.InnerException;
}
return Error(msg, statusCode);
}
#endregion
#endregion
#endregion
}
}

View file

@ -0,0 +1,57 @@
using BMA.EHR.Recruit.Service.Responses;
using BMA.EHR.Recruit.Service.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;
using BMA.EHR.Recruit.Service.Data;
namespace BMA.EHR.Recruit.Service.Controllers
{
[Route("api/v{version:apiVersion}/recruit")]
[ApiVersion("1.0")]
[ApiController]
[Produces("application/json")]
[Authorize]
[SwaggerTag("จัดการข้อมูลการสอบแข่งขัน")]
public class RecruitController : BaseController
{
#region " Fields "
private readonly ApplicationDbContext _context;
private readonly DocumentService _documentService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly RecruitService _recruitService;
#endregion
#region " Constructor and Destructor "
public RecruitController(ApplicationDbContext context,
DocumentService documentService,
IWebHostEnvironment webHostEnvironment,
RecruitService recruitService)
{
_context = context;
_documentService = documentService;
_webHostEnvironment = webHostEnvironment;
_recruitService = recruitService;
}
#endregion
#region " Methods "
#region " จัดการรอบการสมัครสอบแข่งขัน "
#endregion
[HttpGet]
public async Task<ActionResult<ResponseObject>> GetsAsync()
{
return Success("OK");
}
#endregion
}
}

View file

@ -27,18 +27,18 @@ builder.Services.AddHttpContextAccessor();
builder.Services.AddApiVersioning(opt =>
{
opt.DefaultApiVersion = new ApiVersion(1, 0);
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.ReportApiVersions = true;
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
new HeaderApiVersionReader("x-api-version"),
new MediaTypeApiVersionReader("x-api-version"));
opt.DefaultApiVersion = new ApiVersion(1, 0);
opt.AssumeDefaultVersionWhenUnspecified = true;
opt.ReportApiVersions = true;
opt.ApiVersionReader = ApiVersionReader.Combine(new UrlSegmentApiVersionReader(),
new HeaderApiVersionReader("x-api-version"),
new MediaTypeApiVersionReader("x-api-version"));
});
builder.Services.AddVersionedApiExplorer(setup =>
{
setup.GroupNameFormat = "'v'VVV";
setup.SubstituteApiVersionInUrl = true;
setup.GroupNameFormat = "'v'VVV";
setup.SubstituteApiVersionInUrl = true;
});
builder.Services.AddEndpointsApiExplorer();
@ -46,20 +46,25 @@ builder.Services.AddEndpointsApiExplorer();
// Authorization
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt =>
{
opt.RequireHttpsMetadata = false; //false for dev
opt.Authority = issuer;
opt.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
opt.RequireHttpsMetadata = false; //false for dev
opt.Authority = issuer;
opt.TokenValidationParameters = new()
{
ValidateIssuer = true,
ValidateAudience = false,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(key))
};
});
builder.Services.AddAuthorization();
// Register Services
builder.Services.AddTransient<DocumentService>();
builder.Services.AddTransient<RecruitService>();
// use serilog
ConfigureLogs();
builder.Host.UseSerilog();
@ -70,23 +75,23 @@ BsonSerializer.RegisterSerializer(new DateTimeSerializer(BsonType.String));
// Register DbContext
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)));
options.UseMySql(defaultConnection, ServerVersion.AutoDetect(defaultConnection)));
// Add config CORS
builder.Services.AddCors(options => options.AddDefaultPolicy(builder =>
{
builder
.AllowAnyOrigin()
//.WithOrigins("http://localhost:8000")
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowedToAllowWildcardSubdomains();
builder
.AllowAnyOrigin()
//.WithOrigins("http://localhost:8000")
.AllowAnyMethod()
.AllowAnyHeader()
.SetIsOriginAllowedToAllowWildcardSubdomains();
}));
// Add services to the container.
builder.Services.AddControllers(options =>
{
options.SuppressAsyncSuffixInActionNames = false;
options.SuppressAsyncSuffixInActionNames = false;
})
.AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
@ -102,15 +107,15 @@ var apiVersionDescriptionProvider = app.Services.GetRequiredService<IApiVersionD
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
description.GroupName.ToUpperInvariant());
}
});
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in apiVersionDescriptionProvider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json",
description.GroupName.ToUpperInvariant());
}
});
}
app.MapHealthChecks("/health");
@ -134,32 +139,32 @@ app.Run();
void ConfigureLogs()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true)
.Build();
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile(
$"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json",
optional: true)
.Build();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
// .WriteTo.Debug()
.MinimumLevel.Error()
.WriteTo.Console()
.Enrich.WithExceptionDetails()
// .Enrich.WithEnvironmentUserName()
.WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environment ?? ""))
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
// .WriteTo.Debug()
.MinimumLevel.Error()
.WriteTo.Console()
.Enrich.WithExceptionDetails()
// .Enrich.WithEnvironmentUserName()
.WriteTo.Elasticsearch(ConfigureElasticSink(configuration, environment ?? ""))
.Enrich.WithProperty("Environment", environment)
.ReadFrom.Configuration(configuration)
.CreateLogger();
}
ElasticsearchSinkOptions ConfigureElasticSink(IConfigurationRoot configuration, string environment)
{
return new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"] ?? ""))
{
AutoRegisterTemplate = true,
IndexFormat = $"{Assembly.GetExecutingAssembly()?.GetName()?.Name?.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}"
};
return new ElasticsearchSinkOptions(new Uri(configuration["ElasticConfiguration:Uri"] ?? ""))
{
AutoRegisterTemplate = true,
IndexFormat = $"{Assembly.GetExecutingAssembly()?.GetName()?.Name?.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}"
};
}

View file

@ -0,0 +1,13 @@
using System.Net;
namespace BMA.EHR.Recruit.Service.Responses
{
public class ResponseObject
{
public int Status { get; set; }
public string? Message { get; set; }
public object? Result { get; set; }
}
}

View file

@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using BMA.EHR.Recruit.Service.Data;
using BMA.EHR.Recruit.Service.Models.Recruits;
namespace BMA.EHR.Recruit.Service.Services
{
public class RecruitService
{
private readonly ApplicationDbContext _context;
public RecruitService(ApplicationDbContext context)
{
_context = context;
}
public async Task<string> GetExamAttributeAsync(Guid period, Guid exam)
{
try
{
var payment = await _context.RecruitPayments.AsQueryable()
.Include(x => x.Recruit)
.ThenInclude(x => x.RecruitImport)
.Where(x => x.Recruit.Id == exam)
.Where(x => x.Recruit.RecruitImport.Id == period)
.FirstOrDefaultAsync();
return payment != null ? "มีคุณสมบัติ" : "ไม่มีคุณสมบัติ";
}
catch
{
throw;
}
}
public bool CheckValidCertificate(DateTime certDate, int nextYear = 5)
{
var valid = true;
if (DateTime.Now.Date > certDate.Date.AddYears(nextYear))
valid = false;
return valid;
}
}
}

0
bin/Debug/net7.0/AWSSDK.Core.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/AWSSDK.SecurityToken.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Azure.Core.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Azure.Identity.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/BMA.EHR.Core.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/BMA.EHR.Extensions.dll Normal file → Executable file
View file

View file

@ -809,22 +809,22 @@
"runtimes/win-arm/native/Microsoft.Data.SqlClient.SNI.dll": {
"rid": "win-arm",
"assetType": "native",
"fileVersion": "5.0.1.0"
"fileVersion": "0.0.0.0"
},
"runtimes/win-arm64/native/Microsoft.Data.SqlClient.SNI.dll": {
"rid": "win-arm64",
"assetType": "native",
"fileVersion": "5.0.1.0"
"fileVersion": "0.0.0.0"
},
"runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.dll": {
"rid": "win-x64",
"assetType": "native",
"fileVersion": "5.0.1.0"
"fileVersion": "0.0.0.0"
},
"runtimes/win-x86/native/Microsoft.Data.SqlClient.SNI.dll": {
"rid": "win-x86",
"assetType": "native",
"fileVersion": "5.0.1.0"
"fileVersion": "0.0.0.0"
}
}
},
@ -1642,7 +1642,7 @@
"runtimes/win-arm64/native/sni.dll": {
"rid": "win-arm64",
"assetType": "native",
"fileVersion": "4.6.25512.1"
"fileVersion": "0.0.0.0"
}
}
},
@ -1651,7 +1651,7 @@
"runtimes/win-x64/native/sni.dll": {
"rid": "win-x64",
"assetType": "native",
"fileVersion": "4.6.25512.1"
"fileVersion": "0.0.0.0"
}
}
},
@ -1660,7 +1660,7 @@
"runtimes/win-x86/native/sni.dll": {
"rid": "win-x86",
"assetType": "native",
"fileVersion": "4.6.25512.1"
"fileVersion": "0.0.0.0"
}
}
},

File diff suppressed because one or more lines are too long

0
bin/Debug/net7.0/BouncyCastle.Crypto.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Dapper.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/DnsClient.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/DotNetEd.CoreAdmin.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/EPPlus.Interfaces.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/EPPlus.System.Drawing.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/EPPlus.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Elasticsearch.Net.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Google.Protobuf.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Humanizer.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/K4os.Compression.LZ4.Streams.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/K4os.Compression.LZ4.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/K4os.Hash.xxHash.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/LiteDB.dll Normal file → Executable file
View file

View file

0
bin/Debug/net7.0/Microsoft.AspNetCore.JsonPatch.dll Normal file → Executable file
View file

View file

View file

View file

View file

0
bin/Debug/net7.0/Microsoft.AspNetCore.OpenApi.dll Normal file → Executable file
View file

View file

0
bin/Debug/net7.0/Microsoft.Bcl.AsyncInterfaces.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.CodeAnalysis.CSharp.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.CodeAnalysis.Razor.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.CodeAnalysis.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.Data.SqlClient.dll Normal file → Executable file
View file

View file

View file

View file

View file

View file

0
bin/Debug/net7.0/Microsoft.EntityFrameworkCore.dll Normal file → Executable file
View file

View file

View file

View file

0
bin/Debug/net7.0/Microsoft.Identity.Client.dll Normal file → Executable file
View file

View file

View file

0
bin/Debug/net7.0/Microsoft.IdentityModel.Logging.dll Normal file → Executable file
View file

View file

0
bin/Debug/net7.0/Microsoft.IdentityModel.Protocols.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.IdentityModel.Tokens.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.OpenApi.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.SqlServer.Server.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Microsoft.Win32.SystemEvents.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MongoDB.Bson.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MongoDB.Driver.Core.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MongoDB.Driver.GridFS.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MongoDB.Driver.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MongoDB.Libmongocrypt.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Mono.TextTemplating.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Mvc.Grid.Core.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MySql.Data.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/MySqlConnector.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Newtonsoft.Json.Bson.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Newtonsoft.Json.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Npgsql.dll Normal file → Executable file
View file

View file

0
bin/Debug/net7.0/Pomelo.EntityFrameworkCore.MySql.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Sentry.AspNetCore.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Sentry.Extensions.Logging.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Sentry.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.AspNetCore.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Enrichers.Environment.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Exceptions.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Extensions.Hosting.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Extensions.Logging.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Formatting.Compact.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Formatting.Elasticsearch.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Settings.Configuration.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Sinks.Console.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Sinks.Debug.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Sinks.Elasticsearch.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Sinks.File.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.Sinks.PeriodicBatching.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Serilog.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/SharpCompress.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Snappier.dll Normal file → Executable file
View file

View file

0
bin/Debug/net7.0/Swashbuckle.AspNetCore.Swagger.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Swashbuckle.AspNetCore.SwaggerGen.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/Swashbuckle.AspNetCore.SwaggerUI.dll Normal file → Executable file
View file

0
bin/Debug/net7.0/System.CodeDom.dll Normal file → Executable file
View file

Some files were not shown because too many files have changed in this diff Show more