This commit is contained in:
parent
7d431d7882
commit
0e74ad730c
2 changed files with 52 additions and 7 deletions
|
|
@ -223,6 +223,38 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
var response = context.Response;
|
var response = context.Response;
|
||||||
var statusCode = response.StatusCode;
|
var statusCode = response.StatusCode;
|
||||||
|
|
||||||
|
|
||||||
|
string? message = null;
|
||||||
|
string? responseBodyJson = null;
|
||||||
|
if (memoryStream.Length > 0)
|
||||||
|
{
|
||||||
|
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||||
|
var responseBody = new StreamReader(memoryStream).ReadToEnd();
|
||||||
|
|
||||||
|
var jsonOptions = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||||
|
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||||
|
WriteIndented = true,
|
||||||
|
Converters = { new DateTimeFixConverter() }
|
||||||
|
};
|
||||||
|
responseBodyJson =
|
||||||
|
JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBody), jsonOptions);
|
||||||
|
|
||||||
|
var json = JsonSerializer.Deserialize<JsonElement>(responseBody);
|
||||||
|
if (json.ValueKind == JsonValueKind.Array)
|
||||||
|
{
|
||||||
|
message = "success";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (json.TryGetProperty("message", out var messageElement))
|
||||||
|
{
|
||||||
|
message = messageElement.GetString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Console.WriteLine($"FormatResponse: StatusCode={statusCode}, HasStarted={response.HasStarted}");
|
Console.WriteLine($"FormatResponse: StatusCode={statusCode}, HasStarted={response.HasStarted}");
|
||||||
|
|
||||||
// จัดการ response format แม้กับ status code จาก Authentication middleware
|
// จัดการ response format แม้กับ status code จาก Authentication middleware
|
||||||
|
|
@ -230,7 +262,7 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Formatting response for status: {statusCode}");
|
Console.WriteLine($"Formatting response for status: {statusCode}");
|
||||||
|
|
||||||
var responseModel = CreateResponseModel(statusCode);
|
var responseModel = CreateResponseModel(statusCode,message);
|
||||||
|
|
||||||
// Clear memory stream และเขียน response ใหม่
|
// Clear memory stream และเขียน response ใหม่
|
||||||
memoryStream.SetLength(0);
|
memoryStream.SetLength(0);
|
||||||
|
|
@ -246,6 +278,10 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
};
|
};
|
||||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||||
|
|
||||||
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
||||||
|
response.ContentLength = bytes.Length;
|
||||||
|
|
||||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
Console.WriteLine($"Response formatted successfully: {jsonResponse}");
|
Console.WriteLine($"Response formatted successfully: {jsonResponse}");
|
||||||
|
|
@ -255,7 +291,7 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Creating response body for {statusCode} status");
|
Console.WriteLine($"Creating response body for {statusCode} status");
|
||||||
|
|
||||||
var responseModel = CreateResponseModel(statusCode);
|
var responseModel = CreateResponseModel(statusCode,message);
|
||||||
response.ContentType = "application/json; charset=utf-8";
|
response.ContentType = "application/json; charset=utf-8";
|
||||||
|
|
||||||
var jsonOptions = new JsonSerializerOptions
|
var jsonOptions = new JsonSerializerOptions
|
||||||
|
|
@ -265,6 +301,10 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
};
|
};
|
||||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||||
|
|
||||||
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
||||||
|
response.ContentLength = bytes.Length;
|
||||||
|
|
||||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
Console.WriteLine($"Response body created: {jsonResponse}");
|
Console.WriteLine($"Response body created: {jsonResponse}");
|
||||||
|
|
@ -304,8 +344,8 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
var responseModel = new ResponseObject
|
var responseModel = new ResponseObject
|
||||||
{
|
{
|
||||||
Status = response.StatusCode,
|
Status = response.StatusCode,
|
||||||
Message = GlobalMessages.ExceptionOccured,
|
Message = GetErrorMessage(error),
|
||||||
Result = GetErrorMessage(error)
|
Result = null
|
||||||
};
|
};
|
||||||
|
|
||||||
var jsonOptions = new JsonSerializerOptions
|
var jsonOptions = new JsonSerializerOptions
|
||||||
|
|
@ -315,6 +355,11 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
};
|
};
|
||||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||||
|
|
||||||
|
// กำหนด Content-Length ให้ตรงกับขนาดจริง
|
||||||
|
response.ContentLength = bytes.Length;
|
||||||
|
|
||||||
|
|
||||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||||
|
|
||||||
Console.WriteLine($"Exception response formatted: {jsonResponse}");
|
Console.WriteLine($"Exception response formatted: {jsonResponse}");
|
||||||
|
|
@ -456,7 +501,7 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
statusCode == (int)HttpStatusCode.InternalServerError;
|
statusCode == (int)HttpStatusCode.InternalServerError;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ResponseObject CreateResponseModel(int statusCode)
|
private static ResponseObject CreateResponseModel(int statusCode,string? error)
|
||||||
{
|
{
|
||||||
var message = statusCode switch
|
var message = statusCode switch
|
||||||
{
|
{
|
||||||
|
|
@ -473,7 +518,7 @@ namespace BMA.EHR.Domain.Middlewares
|
||||||
return new ResponseObject
|
return new ResponseObject
|
||||||
{
|
{
|
||||||
Status = statusCode,
|
Status = statusCode,
|
||||||
Message = message
|
Message = error ?? message
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -303,7 +303,7 @@ namespace BMA.EHR.Leave.Service.Controllers
|
||||||
|
|
||||||
if (oldData is not null)
|
if (oldData is not null)
|
||||||
{
|
{
|
||||||
return Error("ไม่สามารถบันทึกข้อมูล เนื่องจากมีข้อมูลในระบบแล้ว",StatusCodes.Status400BadRequest);
|
return Error("ไม่สามารถบันทึกข้อมูล เนื่องจากมีข้อมูลในระบบแล้ว");
|
||||||
}
|
}
|
||||||
|
|
||||||
var leaveBeginning = new LeaveBeginning();
|
var leaveBeginning = new LeaveBeginning();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue