From fa945ac2c0ee8414234dd22ceb996404f46c4c31 Mon Sep 17 00:00:00 2001 From: Hyun Seungmin Date: Tue, 21 May 2024 15:37:27 +0900 Subject: [PATCH] apply AddressValidator from controllers --- Mimir/Controllers/AgentController.cs | 25 ++++++++++++----------- Mimir/Controllers/AvatarController.cs | 28 ++++++++++++-------------- Mimir/Controllers/BalanceController.cs | 15 +++++++------- 3 files changed, 34 insertions(+), 34 deletions(-) diff --git a/Mimir/Controllers/AgentController.cs b/Mimir/Controllers/AgentController.cs index 4d7e7221..6fdc1ae4 100644 --- a/Mimir/Controllers/AgentController.cs +++ b/Mimir/Controllers/AgentController.cs @@ -6,6 +6,7 @@ using Mimir.Models.Assets; using Mimir.Services; using Mimir.Util; +using Mimir.Validators; namespace Mimir.Controllers; @@ -26,14 +27,14 @@ public class AgentController : ControllerBase string address, IStateService stateService) { - Address agentAddress; - try - { - agentAddress = new Address(address); - } - catch (ArgumentException) + if (!AddressValidator.TryValidate( + address, + out var agentAddress, + out var errorMessage)) { Response.StatusCode = StatusCodes.Status400BadRequest; + Response.ContentType = "application/json"; + await Response.WriteAsJsonAsync(new { message = errorMessage }); return null; } @@ -53,14 +54,14 @@ public async Task GetAvatars( string address, IStateService stateService) { - Address agentAddress; - try - { - agentAddress = new Address(address); - } - catch (ArgumentException) + if (!AddressValidator.TryValidate( + address, + out var agentAddress, + out var errorMessage)) { Response.StatusCode = StatusCodes.Status400BadRequest; + Response.ContentType = "application/json"; + await Response.WriteAsJsonAsync(new { message = errorMessage }); return new AvatarsResponse([]); } diff --git a/Mimir/Controllers/AvatarController.cs b/Mimir/Controllers/AvatarController.cs index 57017994..d6179b8f 100644 --- a/Mimir/Controllers/AvatarController.cs +++ b/Mimir/Controllers/AvatarController.cs @@ -1,16 +1,14 @@ using System.Numerics; using Bencodex; -using Lib9c; using Libplanet.Common; using Libplanet.Crypto; -using Libplanet.Types.Assets; using Microsoft.AspNetCore.Mvc; using Mimir.Models.Agent; -using Mimir.Models.Assets; using Mimir.Models.Avatar; using Mimir.Repositories; using Mimir.Services; using Mimir.Util; +using Mimir.Validators; using Nekoyume.Model.State; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -114,14 +112,14 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer return avatar; } - Address avatarAddress; - try - { - avatarAddress = new Address(address); - } - catch (ArgumentException) + if (!AddressValidator.TryValidate( + address, + out var avatarAddress, + out var errorMessage)) { Response.StatusCode = StatusCodes.Status400BadRequest; + Response.ContentType = "application/json"; + await Response.WriteAsJsonAsync(new { message = errorMessage }); return null; } @@ -148,14 +146,14 @@ public override void WriteJson(JsonWriter writer, object? value, JsonSerializer return inventory; } - Address inventoryAddress; - try - { - inventoryAddress = new Address(address); - } - catch (ArgumentException) + if (!AddressValidator.TryValidate( + address, + out var inventoryAddress, + out var errorMessage)) { Response.StatusCode = StatusCodes.Status400BadRequest; + Response.ContentType = "application/json"; + await Response.WriteAsJsonAsync(new { message = errorMessage }); return null; } diff --git a/Mimir/Controllers/BalanceController.cs b/Mimir/Controllers/BalanceController.cs index 26db0bf6..bdcb43df 100644 --- a/Mimir/Controllers/BalanceController.cs +++ b/Mimir/Controllers/BalanceController.cs @@ -5,6 +5,7 @@ using Mimir.Models.Assets; using Mimir.Services; using Mimir.Util; +using Mimir.Validators; namespace Mimir.Controllers; @@ -26,14 +27,14 @@ public class BalanceController : ControllerBase string currencyTicker, IStateService stateService) { - Address agentAddress; - try - { - agentAddress = new Address(address); - } - catch (ArgumentException) + if (!AddressValidator.TryValidate( + address, + out var balanceAddress, + out var errorMessage)) { Response.StatusCode = StatusCodes.Status400BadRequest; + Response.ContentType = "application/json"; + await Response.WriteAsJsonAsync(new { message = errorMessage }); return null; } @@ -62,7 +63,7 @@ public class BalanceController : ControllerBase } var stateGetter = new StateGetter(stateService); - var balance = await stateGetter.GetBalanceAsync(agentAddress, c.Value); + var balance = await stateGetter.GetBalanceAsync(balanceAddress, c.Value); return new Balance(c.Value, balance); } }