Skip to content

Commit

Permalink
Merge pull request #21 from bobbahbrown/v1.2.0
Browse files Browse the repository at this point in the history
Version 1.2.0
  • Loading branch information
bobbah authored Oct 19, 2020
2 parents af9c55d + d590e5b commit c589a9a
Show file tree
Hide file tree
Showing 23 changed files with 570 additions and 31 deletions.
6 changes: 3 additions & 3 deletions CentCom.API/CentCom.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.1.0</Version>
<AssemblyVersion>1.1.0.0</AssemblyVersion>
<FileVersion>1.1.0.0</FileVersion>
<Version>1.2.0</Version>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
60 changes: 60 additions & 0 deletions CentCom.API/Controllers/ViewerController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using CentCom.API.Models;
using CentCom.API.Services;
using CentCom.Common;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CentCom.API.Controllers
{
public class ViewerController : Controller
{
private readonly ILogger<ViewerController> _logger;
private readonly IBanService _banService;
private readonly IBanSourceService _banSourceService;

public ViewerController(ILogger<ViewerController> logger, IBanService banService, IBanSourceService banSourceService)
{
_logger = logger;
_banService = banService;
_banSourceService = banSourceService;
}

[HttpGet]
public async Task<IActionResult> Index()
{
return View();
}

[HttpGet("viewer/search/{key}")]
public async Task<IActionResult> SearchBans(string key)
{
var ckey = KeyUtilities.GetCanonicalKey(key);
if (string.IsNullOrWhiteSpace(ckey) || ckey.Length < 3)
{
return View("badsearch", new BanSearchViewModel() { CKey = ckey });
}

var searchResults = await _banService.SearchSummariesForKeyAsync(key);

// If there is only one result, just view it
if (searchResults.Count() == 1)
{
return RedirectToAction("ViewBans", new { key = searchResults.First().CKey });
}

return View(new BanSearchViewModel() { CKey = ckey, Data = searchResults });
}

[HttpGet("viewer/view/{key}")]
public async Task<IActionResult> ViewBans(string key, bool onlyActive = false)
{
var bans = await _banService.GetBansForKeyAsync(key, null, onlyActive);

return View(new BanViewViewModel() { CKey = KeyUtilities.GetCanonicalKey(key), Bans = bans, OnlyActive = onlyActive });
}
}
}
14 changes: 14 additions & 0 deletions CentCom.API/Models/BanSearchViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CentCom.Common.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CentCom.API.Models
{
public class BanSearchViewModel
{
public string CKey { get; set; }
public IEnumerable<KeySummary> Data { get; set; }
}
}
14 changes: 14 additions & 0 deletions CentCom.API/Models/BanViewViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CentCom.API.Models
{
public class BanViewViewModel
{
public string CKey { get; set; }
public bool OnlyActive { get; set; }
public IEnumerable<BanData> Bans { get; set; }
}
}
2 changes: 2 additions & 0 deletions CentCom.API/Services/IBanService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CentCom.API.Models;
using CentCom.Common.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

Expand All @@ -9,5 +10,6 @@ public interface IBanService
public Task<IEnumerable<BanData>> GetBansForSourceAsync(int source, bool onlyActive = false);
public Task<IEnumerable<BanData>> GetBansForKeyAsync(string key, int? source, bool onlyActive = false);
public Task<BanData> GetBanAsync(int ban);
public Task<IEnumerable<KeySummary>> SearchSummariesForKeyAsync(string key);
}
}
15 changes: 13 additions & 2 deletions CentCom.API/Services/Implemented/BanService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CentCom.API.Models;
using CentCom.Common;
using CentCom.Common.Data;
using CentCom.Common.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
Expand All @@ -12,7 +14,6 @@ namespace CentCom.API.Services.Implemented
public class BanService : IBanService
{
private DatabaseContext _dbContext;
private static Regex _canonicalKeyInvalidChars = new Regex(@"[^a-z0-9]");

public BanService(DatabaseContext dbContext)
{
Expand All @@ -29,7 +30,7 @@ public async Task<BanData> GetBanAsync(int ban)

public async Task<IEnumerable<BanData>> GetBansForKeyAsync(string key, int? source, bool onlyActive = false)
{
var ckey = _canonicalKeyInvalidChars.Replace(key.ToLower(), "");
var ckey = KeyUtilities.GetCanonicalKey(key);
var query = _dbContext.Bans
.Include(x => x.JobBans)
.Include(x => x.SourceNavigation)
Expand Down Expand Up @@ -61,5 +62,15 @@ public async Task<IEnumerable<BanData>> GetBansForSourceAsync(int source, bool o
.Select(x => BanData.FromBan(x))
.ToListAsync();
}

public async Task<IEnumerable<KeySummary>> SearchSummariesForKeyAsync(string key)
{
key = KeyUtilities.GetCanonicalKey(key);
var query = _dbContext.KeySummaries
.Where(x => x.CKey.ToLower().Contains(key));

return await query.OrderByDescending(x => x.LatestBan)
.ToListAsync();
}
}
}
7 changes: 4 additions & 3 deletions CentCom.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(x =>
services.AddControllersWithViews().AddJsonOptions(x =>
{
x.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
x.JsonSerializerOptions.IgnoreNullValues = true;
Expand Down Expand Up @@ -80,11 +80,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "";
c.RoutePrefix = "swagger";
c.SwaggerEndpoint("/swagger/v1/swagger.json", "CentCom V1");
});

Expand All @@ -94,7 +95,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapControllerRoute("default", "{controller=Viewer}/{action=Index}/{id?}");
});
}
}
Expand Down
98 changes: 98 additions & 0 deletions CentCom.API/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-TX8t27EcRE3e/ihU7zmQxVncDAy5uIKz4rEkgIXeMed4M0jlfIDPvg6uqKI2xXr2" crossorigin="anonymous">
<environment include="Development">
<link rel="stylesheet" href="~/css/site.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />
</environment>

<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
@if (IsSectionDefined("AddToHead"))
{
@RenderSection("AddToHead", required: false)
}
</head>
<body>
<nav class="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<a class="navbar-brand col-md-3 col-lg-2 mr-0 px-3" href="/">CentCom</a>
<button class="navbar-toggler position-absolute d-md-none collapsed" type="button" data-toggle="collapse"
data-target="#sidebarMenu" aria-controls="sidebarMenu" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<input id="ckey-search" class="form-control form-control-dark w-100" type="text" placeholder="Search ckey" aria-label="Search">
</nav>
<div class="container-fluid">
<div class="row">
<nav id="sidebarMenu" class="col-md-3 col-lg-2 d-md-block bg-light sidebar collapse">
<div class="sidebar-sticky pt-3">
<ul class="nav flex-column">
<li class="nav-item">
<a asp-controller="Viewer" asp-action="Index" class="nav-link active">
<i data-feather="home"></i>
Viewer
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/swagger">
<span data-feather="file"></span>
API Documentation
</a>
</li>
</ul>
</div>
</nav>

<main role="main" class="col-md-9 ml-sm-auto col-lg-10 px-md-4 pt-3">
<div class="container body-content">
@RenderBody()
</div>
<hr />
<footer>
<center>
<p>
<small>
All content is provided without any guarantee, explicit or implied, of accuracy or permanent access.
</small>
</p>
</center>
</footer>
</main>
</div>
</div>
<script>
feather.replace()
function submitSearch() {
var text = $('#ckey-search').val();
if ($.trim(text) != '') {
window.location.href = window.location.origin + "/viewer/search/" + text;
}
}
$('#ckey-search').submit(function (e) {
submitSearch();
e.preventDefault();
});
$('#ckey-search').keypress(function (e) {
if (e.which == '13') {
submitSearch();
}
});
</script>
@RenderSection("Scripts", required: false)
</body>
</html>
13 changes: 13 additions & 0 deletions CentCom.API/Views/Viewer/badsearch.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@model BanSearchViewModel
@{
ViewBag.Title = "CentCom | Search Bans";
}
<h2 class="mb-3">Search Results for '@Model.CKey'</h2>

<div class="pl-1 pr-1">
<div>
<p>
Uh oh! Looks like the search request you tried was too short, or an invalid string. Try again with something longer!
</p>
</div>
</div>
9 changes: 9 additions & 0 deletions CentCom.API/Views/Viewer/index.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@{
ViewBag.Title = "CentCom";
}
<h2 class="mb-3">CentCom Viewer</h2>
<p>
You can use this simple application to search through bans that are stored within CentCom. Type a ckey into the search box above to begin a query.
<br /><br />
To view information on the API for CentCom, and how to use it, go to the <a href="/swagger">API Documentation</a>.
</p>
44 changes: 44 additions & 0 deletions CentCom.API/Views/Viewer/searchbans.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@model BanSearchViewModel
@{
ViewBag.Title = "CentCom | Search Bans";
}
<h2 class="mb-3">Search Results for '@Model.CKey'</h2>

<div class="pl-1 pr-1">
@if (Model.Data.Count() == 0)
{
<div>
<p>
Found no bans for the searched key, perhaps that's a good thing!
</p>
</div>
}
else
{
<table class="table table-striped">
<thead>
<tr>
<th scope="col">CKey</th>
<th scope="col">Server Bans</th>
<th scope="col">Job Bans</th>
<th scope="col">Latest Ban</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@{
foreach (var b in Model.Data)
{
<tr>
<td>@b.CKey</td>
<td>@b.ServerBans</td>
<td>@b.JobBans</td>
<td>@b.LatestBan</td>
<td><a asp-controller="Viewer" asp-action="ViewBans" asp-route-key="@b.CKey" class="btn btn-outline-primary">View</a></td>
</tr>
}
}
</tbody>
</table>
}
</div>
Loading

0 comments on commit c589a9a

Please sign in to comment.