Add Create Store

This commit is contained in:
Kukks 2020-04-01 08:21:54 +02:00
parent 4f1ae4733c
commit 34c1a304a9
4 changed files with 40 additions and 7 deletions

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Net.Http; using System.Net.Http;
using System.Threading; using System.Threading;
@ -27,5 +28,14 @@ namespace BTCPayServer.Client
CreateHttpRequest($"api/v1/stores/{storeId}", method: HttpMethod.Delete), token); CreateHttpRequest($"api/v1/stores/{storeId}", method: HttpMethod.Delete), token);
HandleResponse(response); HandleResponse(response);
} }
public virtual async Task<StoreData> CreateStore(CreateStoreRequest request, CancellationToken token = default)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var response = await _httpClient.SendAsync(CreateHttpRequest("api/v1/stores", bodyPayload: request, method: HttpMethod.Post), token);
return await HandleResponse<StoreData>(response);
}
} }
} }

View file

@ -0,0 +1,7 @@
namespace BTCPayServer.Client.Models
{
public class CreateStoreRequest
{
public string Name { get; set; }
}
}

View file

@ -169,25 +169,30 @@ namespace BTCPayServer.Tests
await user.MakeAdmin(); await user.MakeAdmin();
var client = await user.CreateClient(Policies.Unrestricted); var client = await user.CreateClient(Policies.Unrestricted);
//create store
var newStore = await client.CreateStore(new CreateStoreRequest() {Name = "A"});
//list stores //list stores
var stores = await client.GetStores(); var stores = await client.GetStores();
var storeIds = stores.Select(data => data.Id);
var storeNames = stores.Select(data => data.Name);
Assert.NotNull(stores); Assert.NotNull(stores);
Assert.Single(stores); Assert.Equal(2, stores.Count());
Assert.Equal(user.StoreId,stores.First().Id); Assert.Contains(newStore.Id, storeIds);
Assert.Contains(user.StoreId, storeIds);
//get store //get store
var store = await client.GetStore(user.StoreId); var store = await client.GetStore(user.StoreId);
Assert.Equal(user.StoreId,store.Id); Assert.Equal(user.StoreId,store.Id);
Assert.Equal(store.Name,stores.First().Name); Assert.Contains(store.Name,storeNames);
//remove store //remove store
await client.RemoveStore(user.StoreId); await client.RemoveStore(newStore.Id);
await AssertHttpError(403, async () => await AssertHttpError(403, async () =>
{ {
await client.GetStore(user.StoreId); await client.GetStore(newStore.Id);
}); });
//remove it from the tester state as it will not be happy we removed it ourselves Assert.Single(await client.GetStores());
tester.Stores.Remove(user.StoreId);
} }
} }

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Client; using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Security; using BTCPayServer.Security;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
@ -62,6 +63,16 @@ namespace BTCPayServer.Controllers.GreenField
return Ok(); return Ok();
} }
[HttpPost("~/api/v1/stores")]
[Authorize(Policy = Policies.CanModifyStoreSettings, AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
public async Task<ActionResult<Client.Models.StoreData>> CreateStore(CreateStoreRequest request)
{
if (request is null)
return BadRequest();
var store = await _storeRepository.CreateStore(_userManager.GetUserId(User), request.Name);
return Ok(FromModel(store));
}
private static Client.Models.StoreData FromModel(Data.StoreData data) private static Client.Models.StoreData FromModel(Data.StoreData data)
{ {
return new Client.Models.StoreData() return new Client.Models.StoreData()