add lastmodified and lastmodifiedid to table also add code for modified and create

This commit is contained in:
Kham
2021-09-01 16:03:05 +10:00
parent c5ab17115d
commit cc276d4a17
25 changed files with 255 additions and 115 deletions
@@ -1,4 +1,7 @@
using CarParkValidationAPI.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
@@ -10,30 +13,35 @@ namespace CarParkValidationAPI.Helpers
public class ErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly ILogger<ErrorHandlerMiddleware> _logger;
private readonly IHostEnvironment _env;
public ErrorHandlerMiddleware(RequestDelegate next)
public ErrorHandlerMiddleware(RequestDelegate next,
ILogger<ErrorHandlerMiddleware> logger, IHostEnvironment env)
{
_next = next;
_logger = logger;
_env = env;
}
public async Task Invoke(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception error)
catch (Exception ex)
{
var response = context.Response;
response.ContentType = "application/json";
switch(error)
_logger.LogError(ex, ex.Message);
/*
switch (ex)
{
// case Exception e:
// case Exception e:
// // custom application error
// response.StatusCode = (int)HttpStatusCode.BadRequest;
// break;
case KeyNotFoundException e:
case KeyNotFoundException:
// not found error
response.StatusCode = (int)HttpStatusCode.NotFound;
break;
@@ -41,10 +49,24 @@ namespace CarParkValidationAPI.Helpers
// unhandled error
response.StatusCode = (int)HttpStatusCode.InternalServerError;
break;
}
} */
response.StatusCode = ex switch
{
// case Exception e:
// // custom application error
// response.StatusCode = (int)HttpStatusCode.BadRequest;
// break;
KeyNotFoundException => (int)HttpStatusCode.NotFound,// not found error
_ => (int)HttpStatusCode.InternalServerError,// unhandled error
};
//response.StatusCode = (int)HttpStatusCode.InternalServerError;
var res = _env.IsDevelopment() ?
new ApiException(response.StatusCode, ex.Message, ex.StackTrace?.ToString())
: new ApiException(response.StatusCode, "Internal Server Error");
var result = JsonSerializer.Serialize(new { message = error?.Message });
await response.WriteAsync(result);
var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
var json = JsonSerializer.Serialize(res, options);
await response.WriteAsync(json);
}
}
}