add request name validation

This commit is contained in:
Kukks 2020-04-22 14:42:30 +02:00
parent b7c0e049b5
commit 297b84a18b

View File

@ -9,6 +9,7 @@ using BTCPayServer.Services.Stores;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
namespace BTCPayServer.Controllers.GreenField namespace BTCPayServer.Controllers.GreenField
{ {
@ -67,8 +68,8 @@ namespace BTCPayServer.Controllers.GreenField
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)] [Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<ActionResult<Client.Models.StoreData>> CreateStore(CreateStoreRequest request) public async Task<ActionResult<Client.Models.StoreData>> CreateStore(CreateStoreRequest request)
{ {
if (request is null) if (request?.Name is null)
return BadRequest(); return BadRequest(CreateValidationProblem(nameof(request.Name), "Name is missing"));
var store = await _storeRepository.CreateStore(_userManager.GetUserId(User), request.Name); var store = await _storeRepository.CreateStore(_userManager.GetUserId(User), request.Name);
return Ok(FromModel(store)); return Ok(FromModel(store));
} }
@ -81,5 +82,12 @@ namespace BTCPayServer.Controllers.GreenField
Name = data.StoreName Name = data.StoreName
}; };
} }
private ValidationProblemDetails CreateValidationProblem(string propertyName, string errorMessage)
{
var modelState = new ModelStateDictionary();
modelState.AddModelError(propertyName, errorMessage);
return new ValidationProblemDetails(modelState);
}
} }
} }