Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EstateManagement/Bootstrapper/MiddlewareRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public MiddlewareRegistry(){

this.AddControllers(options => {
// Add filter for all controllers.
options.Filters.Add<TranslateResultToActionResultAttribute>();
//options.Filters.Add<TranslateResultToActionResultAttribute>();
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
Expand Down
95 changes: 58 additions & 37 deletions EstateManagement/Controllers/ContractController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<IActionResult> GetContract([FromRoute] Guid estateId,
[FromRoute] Guid contractId,
CancellationToken cancellationToken) {
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result<ContractResponse>> result = await this.V2ContractController.GetContract(estateId, contractId, cancellationToken);
var result = await this.V2ContractController.GetContract(estateId, contractId, cancellationToken);

return ActionResultHelpers.HandleResult(result, $"Contract not found with estate Id {estateId} and contract Id {contractId}");
}
Expand All @@ -99,7 +99,7 @@ public async Task<IActionResult> GetContracts([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result<List<ContractResponse>>> result = await this.V2ContractController.GetContracts(estateId, cancellationToken);
var result = await this.V2ContractController.GetContracts(estateId, cancellationToken);

return ActionResultHelpers.HandleResult(result, String.Empty);

Expand All @@ -123,7 +123,7 @@ public async Task<IActionResult> AddProductToContract([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2ContractController.AddProductToContract(estateId, contractId, addProductToContractRequest, cancellationToken);
var result = await this.V2ContractController.AddProductToContract(estateId, contractId, addProductToContractRequest, cancellationToken);

return ActionResultHelpers.HandleResult(result, String.Empty);
}
Expand All @@ -148,7 +148,7 @@ public async Task<IActionResult> AddTransactionFeeForProductToContract([FromRout
CancellationToken cancellationToken)
{
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2ContractController.AddTransactionFeeForProductToContract(
var result = await this.V2ContractController.AddTransactionFeeForProductToContract(
estateId, contractId, productId, addTransactionFeeForProductToContractRequest, cancellationToken);

return ActionResultHelpers.HandleResult(result, String.Empty);
Expand All @@ -172,7 +172,7 @@ public async Task<IActionResult> DisableTransactionFeeForProduct([FromRoute] Gui
CancellationToken cancellationToken)
{
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2ContractController.DisableTransactionFeeForProduct(estateId, contractId, productId, transactionFeeId, cancellationToken);
var result = await this.V2ContractController.DisableTransactionFeeForProduct(estateId, contractId, productId, transactionFeeId, cancellationToken);

return ActionResultHelpers.HandleResult(result, String.Empty);
}
Expand All @@ -192,7 +192,7 @@ public async Task<IActionResult> CreateContract([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2ContractController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2ContractController.CreateContract(estateId, createContractRequest, cancellationToken);
var result = await this.V2ContractController.CreateContract(estateId, createContractRequest, cancellationToken);

return ActionResultHelpers.HandleResult(result, String.Empty);
}
Expand All @@ -215,45 +215,66 @@ public async Task<IActionResult> CreateContract([FromRoute] Guid estateId,
}

public static class ActionResultHelpers{
public static IActionResult HandleResult<T>(ActionResult<Result<T>> result, String notFoundMessage)
{
if (result.Result.IsSuccess())
{
OkObjectResult ok = result.Result as OkObjectResult;
Result<T> x = ok.Value as Result<T>;
//public static IActionResult HandleResult<T>(ActionResult<Result<T>> result, String notFoundMessage)
//{
// if (result.Result.IsSuccess())
// {
// OkObjectResult ok = result.Result as OkObjectResult;
// Result<T> x = ok.Value as Result<T>;

return new OkObjectResult(x.Data);
}
// return new OkObjectResult(x.Data);
// }

if (result.Result is NotFoundObjectResult)
{
throw new NotFoundException(notFoundMessage);
}
// if (result.Result is NotFoundObjectResult)
// {
// throw new NotFoundException(notFoundMessage);
// }

ObjectResult r = result.Result as ObjectResult;
if (r.StatusCode == 403)
return new ForbidResult();
// ObjectResult r = result.Result as ObjectResult;
// if (r.StatusCode == 403)
// return new ForbidResult();

return new BadRequestResult();
}
// return new BadRequestResult();
//}

public static IActionResult HandleResult(ActionResult<Result> result, String notFoundMessage)
{
if (result.Result.IsSuccess())
{
return new OkResult();
}
public static IActionResult HandleResult(IActionResult result, String notFoundMessage) {

if (result.Result is NotFoundObjectResult)
{
throw new NotFoundException(notFoundMessage);
}
if (result.GetType().Name == nameof(OkObjectResult)) {
OkObjectResult ok = result as OkObjectResult;
Type type = ok.Value.GetType();
dynamic convertedObj = Convert.ChangeType(ok.Value, ok.Value.GetType());

ObjectResult r = result.Result as ObjectResult;
if (r.StatusCode == 403)
return new ForbidResult();
//Result x = ok.Value as Result;
return new OkObjectResult(convertedObj.Data);
}

return new BadRequestResult();
IActionResult x = result.GetType().Name switch {
nameof(BadRequestObjectResult) => new BadRequestResult(),
nameof(NotFoundObjectResult) => throw new NotFoundException(notFoundMessage),
nameof(UnauthorizedObjectResult) => new UnauthorizedResult(),
nameof(ConflictObjectResult) => new ConflictResult(),
nameof(ForbidResult) => new ForbidResult(),
//nameof(OkObjectResult) => new OkObjectResult(result.)
_ => result
};
return x;


//if (result.Result.IsSuccess())
//{
// return new OkResult();
//}

//if (result.Result is NotFoundObjectResult)
//{
// throw new NotFoundException(notFoundMessage);
//}

//ObjectResult r = result.Result as ObjectResult;
//if (r.StatusCode == 403)
// return new ForbidResult();

//return new BadRequestResult();
}
}
}
12 changes: 6 additions & 6 deletions EstateManagement/Controllers/EstateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task<IActionResult> CreateEstate([FromBody] CreateEstateRequest cre
CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2EstateController.CreateEstate(createEstateRequest, cancellationToken);
var result = await this.V2EstateController.CreateEstate(createEstateRequest, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand All @@ -85,7 +85,7 @@ public async Task<IActionResult> GetEstate([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result<EstateResponse>> result = await this.V2EstateController.GetEstate(estateId, cancellationToken);
var result = await this.V2EstateController.GetEstate(estateId, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand All @@ -97,7 +97,7 @@ public async Task<IActionResult> GetEstates([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result<List<EstateResponse>>> result = await this.V2EstateController.GetEstates(estateId, cancellationToken);
var result = await this.V2EstateController.GetEstates(estateId, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand All @@ -116,7 +116,7 @@ public async Task<IActionResult> CreateEstateUser([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2EstateController.CreateEstateUser(estateId, createEstateUserRequest, cancellationToken);
var result = await this.V2EstateController.CreateEstateUser(estateId, createEstateUserRequest, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand All @@ -125,7 +125,7 @@ public async Task<IActionResult> CreateEstateUser([FromRoute] Guid estateId,
public async Task<IActionResult> AssignOperator([FromRoute] Guid estateId, [FromBody] AssignOperatorRequest assignOperatorRequest, CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2EstateController.AssignOperator(estateId, assignOperatorRequest, cancellationToken);
var result = await this.V2EstateController.AssignOperator(estateId, assignOperatorRequest, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand All @@ -136,7 +136,7 @@ public async Task<IActionResult> RemoveOperator([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2EstateController.SetContextOverride(this.HttpContext);
ActionResult<Result> result = await this.V2EstateController.RemoveOperator(estateId, operatorId, cancellationToken);
var result = await this.V2EstateController.RemoveOperator(estateId, operatorId, cancellationToken);
return ActionResultHelpers.HandleResult(result, String.Empty);
}

Expand Down
2 changes: 1 addition & 1 deletion EstateManagement/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task<IActionResult> GetFile([FromRoute] Guid estateId,
CancellationToken cancellationToken)
{
this.V2FileController.SetContextOverride(this.HttpContext);
ActionResult<Result<FileDetailsResponse>> result = await this.V2FileController.GetFile(estateId, fileId, cancellationToken);
var result = await this.V2FileController.GetFile(estateId, fileId, cancellationToken);

return ActionResultHelpers.HandleResult(result, "");
}
Expand Down
Loading