Skip to content

Commit

Permalink
apply AddressValidator from controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
boscohyun committed May 21, 2024
1 parent 38f2564 commit fa945ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
25 changes: 13 additions & 12 deletions Mimir/Controllers/AgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Mimir.Models.Assets;
using Mimir.Services;
using Mimir.Util;
using Mimir.Validators;

namespace Mimir.Controllers;

Expand All @@ -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;
}

Expand All @@ -53,14 +54,14 @@ public async Task<AvatarsResponse> 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([]);
}

Expand Down
28 changes: 13 additions & 15 deletions Mimir/Controllers/AvatarController.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
15 changes: 8 additions & 7 deletions Mimir/Controllers/BalanceController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Mimir.Models.Assets;
using Mimir.Services;
using Mimir.Util;
using Mimir.Validators;

namespace Mimir.Controllers;

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit fa945ac

Please sign in to comment.