Refactoring code to extract claim parsing

This commit is contained in:
rockstardev
2020-06-11 00:06:53 -05:00
parent 7e9bf9598d
commit b6e3ad9325

View File

@@ -32,9 +32,7 @@ namespace BTCPayServer.Controllers
[HttpGet] [HttpGet]
public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0) public async Task<IActionResult> Index(int skip = 0, int count = 50, int timezoneOffset = 0)
{ {
// TODO: Refactor if (!validUserClaim(out var claimWithId))
var claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
if (claimWithId == null)
return RedirectToAction("Index", "Home"); return RedirectToAction("Index", "Home");
var userId = claimWithId.Value; var userId = claimWithId.Value;
@@ -63,14 +61,12 @@ namespace BTCPayServer.Controllers
[HttpPost] [HttpPost]
public async Task<IActionResult> FlipRead(string id) public async Task<IActionResult> FlipRead(string id)
{ {
// TODO: Refactor if (validUserClaim(out var claimWithId))
var claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier); {
if (claimWithId == null) var notif = _db.Notifications.Single(a => a.Id == id && a.ApplicationUserId == claimWithId.Value);
return RedirectToAction("Index", "Home"); notif.Seen = !notif.Seen;
await _db.SaveChangesAsync();
var notif = _db.Notifications.SingleOrDefault(a => a.Id == id && a.ApplicationUserId == claimWithId.Value); }
notif.Seen = !notif.Seen;
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
@@ -87,13 +83,8 @@ namespace BTCPayServer.Controllers
if (parsedGuids != null) if (parsedGuids != null)
{ {
if (command == "delete") if (command == "delete" && validUserClaim(out var claimWithId))
{ {
// TODO: Refactor
var claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
if (claimWithId == null)
return RedirectToAction("Index", "Home");
var toRemove = _db.Notifications.Where(a => a.ApplicationUserId == claimWithId.Value && parsedGuids.Contains(a.Id)); var toRemove = _db.Notifications.Where(a => a.ApplicationUserId == claimWithId.Value && parsedGuids.Contains(a.Id));
_db.Notifications.RemoveRange(toRemove); _db.Notifications.RemoveRange(toRemove);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync();
@@ -104,5 +95,11 @@ namespace BTCPayServer.Controllers
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
private bool validUserClaim(out Claim claimWithId)
{
claimWithId = User.Claims.SingleOrDefault(a => a.Type == ClaimTypes.NameIdentifier);
return claimWithId != null;
}
} }
} }