Add admin controls to slot page

Closes #77
This commit is contained in:
jvyden 2021-12-02 18:40:36 -05:00
commit 6fb294a5b5
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
2 changed files with 120 additions and 6 deletions

View file

@ -0,0 +1,73 @@
#nullable enable
using System;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Types;
using LBPUnion.ProjectLighthouse.Types.Levels;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Controllers.Website.Admin
{
[ApiController]
[Route("admin/slot/{id:int}")]
public class AdminSlotController : ControllerBase
{
private readonly Database database;
public AdminSlotController(Database database)
{
this.database = database;
}
[Route("teamPick")]
public async Task<IActionResult> TeamPick([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsAdmin) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
slot.TeamPick = true;
await this.database.SaveChangesAsync();
return this.Ok();
}
[Route("removeTeamPick")]
public async Task<IActionResult> RemoveTeamPick([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsAdmin) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.NotFound();
slot.TeamPick = false;
await this.database.SaveChangesAsync();
return this.Ok();
}
[Route("delete")]
public async Task<IActionResult> DeleteLevel([FromRoute] int id)
{
User? user = this.database.UserFromWebRequest(this.Request);
if (user == null || !user.IsAdmin) return this.StatusCode(403, "");
Slot? slot = await this.database.Slots.FirstOrDefaultAsync(s => s.SlotId == id);
if (slot == null) return this.Ok();
if (slot.Location == null) throw new ArgumentNullException();
this.database.Locations.Remove(slot.Location);
this.database.Slots.Remove(slot);
await this.database.SaveChangesAsync();
return this.Ok();
}
}
}

View file

@ -20,21 +20,62 @@
},
})
<div class="ui grid">
<div class="eight wide column">
<div class="ui blue segment">
<h2>Description</h2>
<p>@Model.Slot.Description</p>
<p>@(string.IsNullOrEmpty(Model.Slot.Description) ? "This level has no description." : Model.Slot.Description)</p>
</div>
</div>
<div class="eight wide column">
<div class="ui red segment">
<h2>Tags</h2>
@foreach (string label in Model.Slot.AuthorLabels.Split(",").Where(label => !string.IsNullOrEmpty(label)))
{
<div class="ui blue label">@label.Replace("LABEL_", "")</div>
@{
string[] authorLabels = Model.Slot.AuthorLabels.Split(",");
if (authorLabels.Length == 1) // ..?? ok c#
{
<p>This level has no tags.</p>
}
else
{
foreach (string label in authorLabels.Where(label => !string.IsNullOrEmpty(label)))
{
<div class="ui blue label">@label.Replace("LABEL_", "")</div>
}
}
}
</div>
</div>
</div>
</div>
@if (Model.User != null && Model.User.IsAdmin)
{
<div class="ui yellow segment">
<h2>Admin Settings</h2>
@if (Model.Slot.TeamPick)
{
<a href="/admin/slot/@Model.Slot.SlotId/removeTeamPick">
<div class="ui pink button">
<i class="ribbon icon"></i>
<span>Remove Team Pick</span>
</div>
</a>
}
else
{
<a href="/admin/slot/@Model.Slot.SlotId/teamPick">
<div class="ui pink button">
<i class="ribbon icon"></i>
<span>Team Pick</span>
</div>
</a>
}
<a href="/admin/slot/@Model.Slot.SlotId/delete">
<div class="ui red button">
<i class="trash icon"></i>
<span>Delete</span>
</div>
</a>
</div>
}