From 37c02d2539e648ec2a0a2a548eaa9b573f0ac705 Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Mon, 23 Oct 2017 22:55:46 +0900 Subject: [PATCH] Can delete a store --- BTCPayServer/Controllers/StoresController.cs | 28 +++++++++++++++++++ BTCPayServer/Models/ConfirmModel.cs | 23 +++++++++++++++ .../Services/Stores/StoreRepository.cs | 12 ++++++++ BTCPayServer/Views/Shared/Confirm.cshtml | 23 +++++++++++++++ BTCPayServer/Views/Stores/ListStores.cshtml | 2 +- 5 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 BTCPayServer/Models/ConfirmModel.cs create mode 100644 BTCPayServer/Views/Shared/Confirm.cshtml diff --git a/BTCPayServer/Controllers/StoresController.cs b/BTCPayServer/Controllers/StoresController.cs index 353e190f4..4254f0e7b 100644 --- a/BTCPayServer/Controllers/StoresController.cs +++ b/BTCPayServer/Controllers/StoresController.cs @@ -108,6 +108,34 @@ namespace BTCPayServer.Controllers return View(result); } + [HttpGet] + [Route("{storeId}/delete")] + public async Task DeleteStore(string storeId) + { + var store = await _Repo.FindStore(storeId, GetUserId()); + if(store == null) + return NotFound(); + return View("Confirm", new ConfirmModel() + { + Title = "Delete store " + store.StoreName, + Description = "This store will still be accessible to users sharing it", + Action = "Delete" + }); + } + + [HttpPost] + [Route("{storeId}/delete")] + public async Task DeleteStorePost(string storeId) + { + var userId = GetUserId(); + var store = await _Repo.FindStore(storeId, GetUserId()); + if(store == null) + return NotFound(); + await _Repo.RemoveStore(storeId, userId); + StatusMessage = "Store removed successfully"; + return RedirectToAction(nameof(ListStores)); + } + [HttpGet] [Route("{storeId}")] public async Task UpdateStore(string storeId) diff --git a/BTCPayServer/Models/ConfirmModel.cs b/BTCPayServer/Models/ConfirmModel.cs new file mode 100644 index 000000000..5a112ca31 --- /dev/null +++ b/BTCPayServer/Models/ConfirmModel.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BTCPayServer.Models +{ + public class ConfirmModel + { + public string Title + { + get; set; + } + public string Description + { + get; set; + } + public string Action + { + get; set; + } + } +} diff --git a/BTCPayServer/Services/Stores/StoreRepository.cs b/BTCPayServer/Services/Stores/StoreRepository.cs index 479c3dc35..2713dda3c 100644 --- a/BTCPayServer/Services/Stores/StoreRepository.cs +++ b/BTCPayServer/Services/Stores/StoreRepository.cs @@ -83,6 +83,18 @@ namespace BTCPayServer.Services.Stores } } + 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(); + } + } + public async Task UpdateStore(StoreData store) { using(var ctx = _ContextFactory.CreateContext()) diff --git a/BTCPayServer/Views/Shared/Confirm.cshtml b/BTCPayServer/Views/Shared/Confirm.cshtml new file mode 100644 index 000000000..77c18ceb4 --- /dev/null +++ b/BTCPayServer/Views/Shared/Confirm.cshtml @@ -0,0 +1,23 @@ +@model ConfirmModel +@{ + Layout = "_Layout.cshtml"; +} + +
+
+
+
+

@Model.Title

+
+

@Model.Description

+
+
+
+
+
+ +
+
+
+
+
\ No newline at end of file diff --git a/BTCPayServer/Views/Stores/ListStores.cshtml b/BTCPayServer/Views/Stores/ListStores.cshtml index 59ae08a8a..befe8706c 100644 --- a/BTCPayServer/Views/Stores/ListStores.cshtml +++ b/BTCPayServer/Views/Stores/ListStores.cshtml @@ -42,7 +42,7 @@ @store.WebSite } @store.Balance - Settings + Settings - Remove }