จัดระเบียบ Code ใหม่ และเพิ่ม Extension Method
implement GenericRepository Class ใหม่
This commit is contained in:
parent
e49c6a4aca
commit
89de09d213
21 changed files with 1439 additions and 63 deletions
114
BMA.EHR.Domain/Common/BaseController.cs
Normal file
114
BMA.EHR.Domain/Common/BaseController.cs
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
using BMA.EHR.Domain.Shared;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace BMA.EHR.Domain.Common
|
||||
{
|
||||
public class BaseController : ControllerBase
|
||||
{
|
||||
#region " Fields "
|
||||
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Constructor and Destructor "
|
||||
|
||||
public BaseController(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region " Properties "
|
||||
|
||||
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||
|
||||
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
|
||||
|
||||
#endregion
|
||||
|
||||
#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, string result, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return StatusCode((int)statusCode, new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = message,
|
||||
Result = result
|
||||
});
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(string message, int statusCode = StatusCodes.Status500InternalServerError)
|
||||
{
|
||||
return Error(message, message, statusCode);
|
||||
}
|
||||
|
||||
protected virtual ActionResult<ResponseObject> Error(Exception exception, string message, 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(message, msg, statusCode);
|
||||
}
|
||||
|
||||
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, msg, statusCode);
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue