btcpayserver/BTCPayServer/Controllers/GreenField/StoresController.cs

111 lines
4.3 KiB
C#
Raw Normal View History

2020-03-24 16:18:43 +01:00
using System.Collections.Generic;
using System.Linq;
2020-03-31 14:43:35 +02:00
using System.Threading.Tasks;
using BTCPayServer.Client;
2020-04-01 08:21:54 +02:00
using BTCPayServer.Client.Models;
2020-03-31 14:43:35 +02:00
using BTCPayServer.Data;
2020-03-24 16:18:43 +01:00
using BTCPayServer.Security;
2020-03-31 14:43:35 +02:00
using BTCPayServer.Services.Stores;
2020-03-24 16:18:43 +01:00
using Microsoft.AspNetCore.Authorization;
2020-03-31 14:43:35 +02:00
using Microsoft.AspNetCore.Identity;
2020-03-24 16:18:43 +01:00
using Microsoft.AspNetCore.Mvc;
2020-04-22 14:42:30 +02:00
using Microsoft.AspNetCore.Mvc.ModelBinding;
2020-03-24 16:18:43 +01:00
namespace BTCPayServer.Controllers.GreenField
2020-03-24 16:18:43 +01:00
{
[ApiController]
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public class GreenFieldController : ControllerBase
2020-03-24 16:18:43 +01:00
{
2020-03-31 14:43:35 +02:00
private readonly StoreRepository _storeRepository;
private readonly UserManager<ApplicationUser> _userManager;
public GreenFieldController(StoreRepository storeRepository, UserManager<ApplicationUser> userManager)
2020-03-31 14:43:35 +02:00
{
_storeRepository = storeRepository;
_userManager = userManager;
}
2020-03-24 16:18:43 +01:00
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores")]
public ActionResult<IEnumerable<Client.Models.StoreData>> GetStores()
{
var stores = HttpContext.GetStoresData();
2020-03-24 16:18:43 +01:00
return Ok(stores.Select(FromModel));
}
[Authorize(Policy = Policies.CanViewStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}")]
public ActionResult<Client.Models.StoreData> GetStore(string storeId)
{
var store = HttpContext.GetStoreData();
if (store == null)
{
return NotFound();
}
return Ok(FromModel(store));
}
2020-03-31 14:43:35 +02:00
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpDelete("~/api/v1/stores/{storeId}")]
public async Task<ActionResult> RemoveStore(string storeId)
{
var store = HttpContext.GetStoreData();
if (store == null)
{
return NotFound();
}
if (!_storeRepository.CanDeleteStores())
{
ModelState.AddModelError(string.Empty, "BTCPay Server is using a database server that does not allow you to remove stores.");
return BadRequest(new ValidationProblemDetails(ModelState));
}
await _storeRepository.RemoveStore(storeId, _userManager.GetUserId(User));
return Ok();
}
2020-04-01 08:21:54 +02:00
[HttpPost("~/api/v1/stores")]
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<ActionResult<Client.Models.StoreData>> CreateStore(CreateStoreRequest request)
{
2020-04-22 14:42:30 +02:00
if (request?.Name is null)
return BadRequest(CreateValidationProblem(nameof(request.Name), "Name is missing"));
2020-04-01 08:21:54 +02:00
var store = await _storeRepository.CreateStore(_userManager.GetUserId(User), request.Name);
return Ok(FromModel(store));
}
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPut("~/api/v1/stores/{storeId}")]
public async Task<ActionResult> UpdateStore(string storeId, UpdateStoreRequest request)
{
var store = HttpContext.GetStoreData();
if (store == null)
{
return NotFound();
}
if (request?.Name is null)
return BadRequest(CreateValidationProblem(nameof(request.Name), "Name is missing"));
store.StoreName = request.Name;
await _storeRepository.UpdateStore(store);
return Ok(FromModel(store));
}
2020-04-01 08:21:54 +02:00
2020-03-24 16:18:43 +01:00
private static Client.Models.StoreData FromModel(Data.StoreData data)
{
return new Client.Models.StoreData()
{
Id = data.Id,
Name = data.StoreName
};
}
2020-04-22 14:42:30 +02:00
private ValidationProblemDetails CreateValidationProblem(string propertyName, string errorMessage)
{
var modelState = new ModelStateDictionary();
modelState.AddModelError(propertyName, errorMessage);
return new ValidationProblemDetails(modelState);
}
2020-03-24 16:18:43 +01:00
}
}