btcpayserver/BTCPayServer/Services/Stores/StoreRepository.cs

201 lines
7.1 KiB
C#
Raw Normal View History

2017-09-13 15:47:34 +09:00
using BTCPayServer.Data;
using BTCPayServer.Models;
using NBitcoin;
using NBitcoin.DataEncoders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
namespace BTCPayServer.Services.Stores
2017-09-13 15:47:34 +09:00
{
public class StoreRepository
{
private ApplicationDbContextFactory _ContextFactory;
public StoreRepository(ApplicationDbContextFactory contextFactory)
{
_ContextFactory = contextFactory ?? throw new ArgumentNullException(nameof(contextFactory));
}
2017-09-13 15:47:34 +09:00
public async Task<StoreData> FindStore(string storeId)
{
if (storeId == null)
return null;
using (var ctx = _ContextFactory.CreateContext())
{
return await ctx.FindAsync<StoreData>(storeId).ConfigureAwait(false);
}
}
2017-09-13 15:47:34 +09:00
public async Task<StoreData> FindStore(string storeId, string userId)
{
if (userId == null)
throw new ArgumentNullException(nameof(userId));
using (var ctx = _ContextFactory.CreateContext())
{
return (await ctx
.UserStore
.Where(us => us.ApplicationUserId == userId && us.StoreDataId == storeId)
.Select(us => new
{
Store = us.StoreData,
Role = us.Role
}).ToArrayAsync())
.Select(us =>
{
2018-04-30 02:33:42 +09:00
#pragma warning disable CS0612 // Type or member is obsolete
us.Store.Role = us.Role;
2018-04-30 02:33:42 +09:00
#pragma warning restore CS0612 // Type or member is obsolete
return us.Store;
}).FirstOrDefault();
}
}
2017-09-13 23:50:36 +09:00
2018-03-23 16:24:57 +09:00
public class StoreUser
{
public string Id { get; set; }
public string Email { get; set; }
public string Role { get; set; }
}
public async Task<StoreUser[]> GetStoreUsers(string storeId)
{
if (storeId == null)
throw new ArgumentNullException(nameof(storeId));
using (var ctx = _ContextFactory.CreateContext())
{
return await ctx
.UserStore
.Where(u => u.StoreDataId == storeId)
.Select(u => new StoreUser()
{
Id = u.ApplicationUserId,
Email = u.ApplicationUser.Email,
Role = u.Role
}).ToArrayAsync();
}
}
public async Task<StoreData[]> GetStoresByUserId(string userId)
{
using (var ctx = _ContextFactory.CreateContext())
{
2018-03-23 16:24:57 +09:00
return (await ctx.UserStore
.Where(u => u.ApplicationUserId == userId)
2018-03-23 16:24:57 +09:00
.Select(u => new { u.StoreData, u.Role })
.ToArrayAsync())
.Select(u =>
{
2018-04-30 02:33:42 +09:00
#pragma warning disable CS0612 // Type or member is obsolete
2018-03-23 16:24:57 +09:00
u.StoreData.Role = u.Role;
2018-04-30 02:33:42 +09:00
#pragma warning restore CS0612 // Type or member is obsolete
2018-03-23 16:24:57 +09:00
return u.StoreData;
}).ToArray();
}
}
public async Task<bool> AddStoreUser(string storeId, string userId, string role)
{
using (var ctx = _ContextFactory.CreateContext())
{
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId, Role = role };
ctx.UserStore.Add(userStore);
try
{
await ctx.SaveChangesAsync();
return true;
}
catch (DbUpdateException)
{
return false;
}
}
}
public async Task RemoveStoreUser(string storeId, string userId)
{
2018-07-19 21:38:55 +09:00
bool delete = false;
2018-03-23 16:24:57 +09:00
using (var ctx = _ContextFactory.CreateContext())
{
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId };
ctx.UserStore.Add(userStore);
ctx.Entry<UserStore>(userStore).State = EntityState.Deleted;
await ctx.SaveChangesAsync();
2018-07-19 21:38:55 +09:00
delete = await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0;
}
2018-07-19 21:38:55 +09:00
if (delete)
await DeleteStore(storeId);
}
2017-09-13 23:50:36 +09:00
public async Task<StoreData> CreateStore(string ownerId, string name)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException("name should not be empty", nameof(name));
using (var ctx = _ContextFactory.CreateContext())
{
StoreData store = new StoreData
{
Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(32)),
2018-01-19 00:37:00 +09:00
StoreName = name,
SpeedPolicy = Invoices.SpeedPolicy.MediumSpeed
};
var userStore = new UserStore
{
StoreDataId = store.Id,
ApplicationUserId = ownerId,
Role = "Owner"
};
2017-11-12 23:37:21 +09:00
ctx.Add(store);
ctx.Add(userStore);
await ctx.SaveChangesAsync().ConfigureAwait(false);
return store;
}
}
2017-09-13 15:47:34 +09:00
public async Task RemoveStore(string storeId, string userId)
{
using (var ctx = _ContextFactory.CreateContext())
{
var storeUser = await ctx.UserStore.FirstOrDefaultAsync(o => o.StoreDataId == storeId && o.ApplicationUserId == userId);
if (storeUser == null)
return;
ctx.UserStore.Remove(storeUser);
await ctx.SaveChangesAsync();
}
}
2017-10-23 22:55:46 +09:00
public async Task UpdateStore(StoreData store)
{
using (var ctx = _ContextFactory.CreateContext())
{
var existing = await ctx.FindAsync<StoreData>(store.Id);
ctx.Entry(existing).CurrentValues.SetValues(store);
await ctx.SaveChangesAsync().ConfigureAwait(false);
}
}
2018-07-19 19:31:17 +09:00
public async Task<bool> DeleteStore(string storeId)
{
using (var ctx = _ContextFactory.CreateContext())
{
if (!ctx.Database.SupportDropForeignKey())
return false;
2018-07-19 19:31:17 +09:00
var store = await ctx.Stores.FindAsync(storeId);
if (store == null)
return false;
ctx.Stores.Remove(store);
await ctx.SaveChangesAsync();
return true;
}
}
public bool CanDeleteStores()
{
using (var ctx = _ContextFactory.CreateContext())
{
return ctx.Database.SupportDropForeignKey();
}
}
}
2017-09-13 15:47:34 +09:00
}