From 1d9712294f64ff164b090bda59905037d01135b6 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Wed, 9 Feb 2022 20:51:35 +0100 Subject: [PATCH] Code cleanup, 2022 edition --- DataModels/Tables/States.cs | 14 +-- Migrations.cs | 2 +- Pages/Conference.cshtml | 4 +- Pages/Conference.cshtml.cs | 54 ++++++------ Pages/Error.cshtml | 18 ++-- Pages/Error.cshtml.cs | 20 ++--- Pages/Index.cshtml.cs | 12 +-- Pages/Info.cshtml | 164 ++++++++++++++++++------------------ Pages/Info.cshtml.cs | 12 +-- Pages/Privacy.cshtml.cs | 12 +-- Pages/Shared/_Layout.cshtml | 50 +++++------ Pages/Watchlist.cshtml | 6 +- Pages/Watchlist.cshtml.cs | 54 ++++++------ Pages/_ViewStart.cshtml | 2 +- README.md | 3 +- Startup.cs | 40 ++++----- 16 files changed, 230 insertions(+), 237 deletions(-) diff --git a/DataModels/Tables/States.cs b/DataModels/Tables/States.cs index 048b41a..43f3c79 100644 --- a/DataModels/Tables/States.cs +++ b/DataModels/Tables/States.cs @@ -1,10 +1,10 @@ using LinqToDB.Mapping; -namespace c3stream.DataModels.Tables { - [Table(Name = "States")] - public class States { - [Column(Name = "TalkId"), PrimaryKey, NotNull] public string TalkId { get; set; } - [Column(Name = "UserId"), PrimaryKey, NotNull] public string UserId { get; set; } - [Column(Name = "State"), NotNull] public string State { get; set; } - } +namespace c3stream.DataModels.Tables; + +[Table(Name = "States")] +public class States { + [Column(Name = "TalkId"), PrimaryKey, NotNull] public string TalkId { get; set; } + [Column(Name = "UserId"), PrimaryKey, NotNull] public string UserId { get; set; } + [Column(Name = "State"), NotNull] public string State { get; set; } } \ No newline at end of file diff --git a/Migrations.cs b/Migrations.cs index 794f2b5..c434e08 100644 --- a/Migrations.cs +++ b/Migrations.cs @@ -11,7 +11,7 @@ namespace c3stream; public static class Migrations { private const int DbVer = 0; - private static readonly List _migrations = new() { }; + private static readonly List _migrations = new(); public static void RunMigrations() { using var db = new Database.DbConn(); diff --git a/Pages/Conference.cshtml b/Pages/Conference.cshtml index 7cf2adb..5578d74 100644 --- a/Pages/Conference.cshtml +++ b/Pages/Conference.cshtml @@ -1,8 +1,6 @@ @page -@model ConferenceModel -@using System.Net @using global::c3stream.DataModels -@using static ConferenceModel +@model ConferenceModel @{ if (c3stream.Conferences.All(c => c.Acronym != Request.Query["c"])) { Response.Redirect("/"); diff --git a/Pages/Conference.cshtml.cs b/Pages/Conference.cshtml.cs index 7cb7be7..bd68fb9 100644 --- a/Pages/Conference.cshtml.cs +++ b/Pages/Conference.cshtml.cs @@ -1,39 +1,37 @@ -using System.Collections.Generic; -using System.Linq; +using System.Linq; using c3stream.DataModels; using c3stream.DataModels.Tables; using LinqToDB; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -namespace c3stream.Pages { - public class ConferenceModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public ConferenceModel(ILogger logger) => _logger = logger; +public class ConferenceModel : PageModel { + private readonly ILogger _logger; - public void OnGet() { - var guid = Request.Query["guid"].ToString(); - var state = Request.Query["state"].ToString(); - var userid = Request.Cookies["bookmark"]; - if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark")) - return; + public ConferenceModel(ILogger logger) => _logger = logger; - using var db = new Database.DbConn(); - var existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid); - if (existing != null) - if (state == "unwatched") - db.States.Delete(p => p == existing); - else { - existing.State = state; - db.Update(existing); - } - else - db.Insert(new States { - State = state, TalkId = guid, UserId = userid - }); - Response.Redirect("/"); - } + public void OnGet() { + var guid = Request.Query["guid"].ToString(); + var state = Request.Query["state"].ToString(); + var userid = Request.Cookies["bookmark"]; + if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark")) + return; + + using var db = new Database.DbConn(); + var existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid); + if (existing != null) + if (state == "unwatched") { + db.States.Delete(p => p == existing); + } + else { + existing.State = state; + db.Update(existing); + } + else + db.Insert(new States { State = state, TalkId = guid, UserId = userid }); + + Response.Redirect("/"); } } \ No newline at end of file diff --git a/Pages/Error.cshtml b/Pages/Error.cshtml index 89a1b7f..ef1bc8e 100644 --- a/Pages/Error.cshtml +++ b/Pages/Error.cshtml @@ -1,25 +1,25 @@ @page @model ErrorModel @{ - ViewData["Title"] = "Error"; + ViewData["Title"] = "Error"; }

Error.

An error occurred while processing your request.

@if (Model.ShowRequestId) { -

- Request ID: @Model.RequestId -

+

+ Request ID: @Model.RequestId +

}

Development Mode

- Swapping to the Development environment displays detailed information about the error that occurred. + Swapping to the Development environment displays detailed information about the error that occurred.

- The Development environment shouldn't be enabled for deployed applications. - It can result in displaying sensitive information from exceptions to end users. - For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development - and restarting the app. + The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app.

\ No newline at end of file diff --git a/Pages/Error.cshtml.cs b/Pages/Error.cshtml.cs index 7e9b21b..330f216 100644 --- a/Pages/Error.cshtml.cs +++ b/Pages/Error.cshtml.cs @@ -3,19 +3,19 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -namespace c3stream.Pages { - [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] - public class ErrorModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public ErrorModel(ILogger logger) => _logger = logger; +[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] +public class ErrorModel : PageModel { + private readonly ILogger _logger; - public string RequestId { get; set; } + public ErrorModel(ILogger logger) => _logger = logger; - public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + public string RequestId { get; set; } - public void OnGet() { - RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; - } + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + public void OnGet() { + RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; } } \ No newline at end of file diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs index b237605..70cec97 100644 --- a/Pages/Index.cshtml.cs +++ b/Pages/Index.cshtml.cs @@ -1,12 +1,12 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -namespace c3stream.Pages { - public class IndexModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public IndexModel(ILogger logger) => _logger = logger; +public class IndexModel : PageModel { + private readonly ILogger _logger; - public void OnGet() { } - } + public IndexModel(ILogger logger) => _logger = logger; + + public void OnGet() { } } \ No newline at end of file diff --git a/Pages/Info.cshtml b/Pages/Info.cshtml index 0a429f6..d4bb34a 100644 --- a/Pages/Info.cshtml +++ b/Pages/Info.cshtml @@ -2,115 +2,115 @@ @using global::c3stream.DataModels @model InfoModel @{ - ViewData["Title"] = "Info"; + ViewData["Title"] = "Info"; } @{ - if (string.IsNullOrWhiteSpace(Request.Query["guid"]) || c3stream.GetEventByGuid(Request.Query["guid"]) == null) { - Response.Redirect("/"); - return; - } + if (string.IsNullOrWhiteSpace(Request.Query["guid"]) || c3stream.GetEventByGuid(Request.Query["guid"]) == null) { + Response.Redirect("/"); + return; + } - var cookie = c3stream.UpdateCookie(Request, Response, $"/Info?guid={Request.Query["guid"]}"); - - await using var db = new Database.DbConn(); + var cookie = c3stream.UpdateCookie(Request, Response, $"/Info?guid={Request.Query["guid"]}"); - var talk = c3stream.GetEventByGuid(Request.Query["guid"]); - var state = db.States.FirstOrDefault(p => p.TalkId == Request.Query["guid"].ToString() && p.UserId == cookie)?.State; - if (talk == null) { - Response.Redirect("/"); - return; - } - if (state == null) { - state = "unwatched"; - } + await using var db = new Database.DbConn(); - var title = talk.Title; - var speakers = talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : ""; - var description = talk.Description; - if (string.IsNullOrEmpty(description)) { - description = "<missing description>"; - } + var talk = c3stream.GetEventByGuid(Request.Query["guid"]); + var state = db.States.FirstOrDefault(p => p.TalkId == Request.Query["guid"].ToString() && p.UserId == cookie)?.State; + if (talk == null) { + Response.Redirect("/"); + return; + } + if (state == null) { + state = "unwatched"; + } - var isWatched = state == "watched"; - var isMarked = state == "marked"; - var file = $"{talk.Slug}.mp4"; - var conference = c3stream.GetConferenceByEventGuid(talk.Guid); + var title = talk.Title; + var speakers = talk.Persons.Any() ? talk.Persons.Aggregate((s, s1) => $"{s}, {s1}") : ""; + var description = talk.Description; + if (string.IsNullOrEmpty(description)) { + description = "<missing description>"; + } - var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0]; - var logoPath = System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, "logo.png"); + var isWatched = state == "watched"; + var isMarked = state == "marked"; + var file = $"{talk.Slug}.mp4"; + var conference = c3stream.GetConferenceByEventGuid(talk.Guid); - var category = talk.Tags.Count switch { - 0 => "", - 1 => talk.Tags[0], - 2 => "", - 3 => talk.Tags[2], - 4 => talk.Tags[3], - 5 => talk.Tags[3], - 6 => talk.Tags[3], // rc3: is this correct? - _ => "" - }; + var eventName = talk.Tags.Count <= 1 ? conference.Acronym : talk.Tags[0]; + var logoPath = System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, "logo.png"); + + var category = talk.Tags.Count switch { + 0 => "", + 1 => talk.Tags[0], + 2 => "", + 3 => talk.Tags[2], + 4 => talk.Tags[3], + 5 => talk.Tags[3], + 6 => talk.Tags[3], // rc3: is this correct? + _ => "" + }; } @if (System.IO.File.Exists(logoPath)) { - Conference logo + Conference logo } else { - Conference logo + Conference logo } @if (isWatched) { -

@title - @speakers

+

@title - @speakers

} else if (isMarked) { -

@title - @speakers

+

@title - @speakers

} else { -

@title - @speakers

+

@title - @speakers

}
@eventName - @category - @talk.Date?.Date.ToShortDateString()
- - - - @if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) { - - - - } - else { - - - - } - @if (isWatched) { - - - } - else if (isMarked) { - - - } - else { - - - } + + + + @if (System.IO.File.Exists(System.IO.Path.Combine(c3stream.CachePath, conference.Acronym, file))) { + + + + } + else { + + + + } + @if (isWatched) { + + + } + else if (isMarked) { + + + } + else { + + + }

- @Html.Raw(description.Replace("\n", "
").Replace("

", "").Replace("

", "")) + @Html.Raw(description.Replace("\n", "
").Replace("

", "").Replace("

", ""))

Share this talk:
diff --git a/Pages/Info.cshtml.cs b/Pages/Info.cshtml.cs index 3b8a73f..3abf0a9 100644 --- a/Pages/Info.cshtml.cs +++ b/Pages/Info.cshtml.cs @@ -1,12 +1,12 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -namespace c3stream.Pages { - public class InfoModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public InfoModel(ILogger logger) => _logger = logger; +public class InfoModel : PageModel { + private readonly ILogger _logger; - public void OnGet() { } - } + public InfoModel(ILogger logger) => _logger = logger; + + public void OnGet() { } } \ No newline at end of file diff --git a/Pages/Privacy.cshtml.cs b/Pages/Privacy.cshtml.cs index fecb13c..7dec4ce 100644 --- a/Pages/Privacy.cshtml.cs +++ b/Pages/Privacy.cshtml.cs @@ -1,12 +1,12 @@ using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -namespace c3stream.Pages { - public class PrivacyModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public PrivacyModel(ILogger logger) => _logger = logger; +public class PrivacyModel : PageModel { + private readonly ILogger _logger; - public void OnGet() { } - } + public PrivacyModel(ILogger logger) => _logger = logger; + + public void OnGet() { } } \ No newline at end of file diff --git a/Pages/Shared/_Layout.cshtml b/Pages/Shared/_Layout.cshtml index bc2cc97..5f3c8f5 100644 --- a/Pages/Shared/_Layout.cshtml +++ b/Pages/Shared/_Layout.cshtml @@ -1,38 +1,38 @@  - - - @ViewData["Title"] - c3stream - - - + + + @ViewData["Title"] - c3stream + + +
- +
-
- @RenderBody() -
+
+ @RenderBody() +
-
- Privacy - - Contact - - Source Code - - c3stream is not affiliated with media.ccc.de in any way. Mirrored video files display their license in the video, no rights reserved. -
+
+ Privacy - + Contact - + Source Code - + c3stream is not affiliated with media.ccc.de in any way. Mirrored video files display their license in the video, no rights reserved. +
@@ -41,4 +41,4 @@ @RenderSection("Scripts", false) - + \ No newline at end of file diff --git a/Pages/Watchlist.cshtml b/Pages/Watchlist.cshtml index 504ac9a..5396637 100644 --- a/Pages/Watchlist.cshtml +++ b/Pages/Watchlist.cshtml @@ -1,10 +1,8 @@ @page -@model WatchlistModel -@using System.Net @using global::c3stream.DataModels -@using static WatchlistModel +@model WatchlistModel @{ - var cookie = c3stream.UpdateCookie(Request, Response, $"/Watchlist"); + var cookie = c3stream.UpdateCookie(Request, Response, "/Watchlist"); ViewData["Title"] = "Watchlist"; await using var db = new Database.DbConn(); var states = db.States.ToList(); diff --git a/Pages/Watchlist.cshtml.cs b/Pages/Watchlist.cshtml.cs index 8a9b367..7c774a3 100644 --- a/Pages/Watchlist.cshtml.cs +++ b/Pages/Watchlist.cshtml.cs @@ -1,39 +1,37 @@ -using System.Collections.Generic; -using System.Linq; +using System.Linq; using c3stream.DataModels; using c3stream.DataModels.Tables; using LinqToDB; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Logging; -using Newtonsoft.Json; -namespace c3stream.Pages { - public class WatchlistModel : PageModel { - private readonly ILogger _logger; +namespace c3stream.Pages; - public WatchlistModel(ILogger logger) => _logger = logger; +public class WatchlistModel : PageModel { + private readonly ILogger _logger; - public void OnGet() { - var guid = Request.Query["guid"].ToString(); - var state = Request.Query["state"].ToString(); - var userid = Request.Cookies["bookmark"]; - if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark")) - return; + public WatchlistModel(ILogger logger) => _logger = logger; - using var db = new Database.DbConn(); - var existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid); - if (existing != null) - if (state == "unwatched") - db.States.Delete(p => p == existing); - else { - existing.State = state; - db.Update(existing); - } - else - db.Insert(new States { - State = state, TalkId = guid, UserId = userid - }); - Response.Redirect("/"); - } + public void OnGet() { + var guid = Request.Query["guid"].ToString(); + var state = Request.Query["state"].ToString(); + var userid = Request.Cookies["bookmark"]; + if (string.IsNullOrWhiteSpace(guid) || string.IsNullOrWhiteSpace(state) || !Request.Cookies.ContainsKey("bookmark")) + return; + + using var db = new Database.DbConn(); + var existing = db.States.FirstOrDefault(p => p.TalkId == guid && p.UserId == userid); + if (existing != null) + if (state == "unwatched") { + db.States.Delete(p => p == existing); + } + else { + existing.State = state; + db.Update(existing); + } + else + db.Insert(new States { State = state, TalkId = guid, UserId = userid }); + + Response.Redirect("/"); } } \ No newline at end of file diff --git a/Pages/_ViewStart.cshtml b/Pages/_ViewStart.cshtml index 1af6e49..817a913 100644 --- a/Pages/_ViewStart.cshtml +++ b/Pages/_ViewStart.cshtml @@ -1,3 +1,3 @@ @{ - Layout = "_Layout"; + Layout = "_Layout"; } \ No newline at end of file diff --git a/README.md b/README.md index e642002..235b334 100644 --- a/README.md +++ b/README.md @@ -1 +1,2 @@ -c3stream is a small proxy site meant for saving watched status & watch-later-lists for media.ccc.de talks. Test in production at https://c3stream.de \ No newline at end of file +c3stream is a small proxy site meant for saving watched status & watch-later-lists for media.ccc.de talks. Test in +production at https://c3stream.de \ No newline at end of file diff --git a/Startup.cs b/Startup.cs index 2e92ba1..5b9556e 100644 --- a/Startup.cs +++ b/Startup.cs @@ -4,32 +4,32 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -namespace c3stream { - public class Startup { - public Startup(IConfiguration configuration) => Configuration = configuration; +namespace c3stream; - public IConfiguration Configuration { get; } +public class Startup { + public Startup(IConfiguration configuration) => Configuration = configuration; - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) { - services.AddRazorPages(); - } + public IConfiguration Configuration { get; } - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - if (env.IsDevelopment()) - app.UseDeveloperExceptionPage(); - else - app.UseExceptionHandler("/Error"); - // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. + // This method gets called by the runtime. Use this method to add services to the container. + public void ConfigureServices(IServiceCollection services) { + services.AddRazorPages(); + } - app.UseStaticFiles(); + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + if (env.IsDevelopment()) + app.UseDeveloperExceptionPage(); + else + app.UseExceptionHandler("/Error"); + // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. - app.UseRouting(); + app.UseStaticFiles(); - app.UseAuthorization(); + app.UseRouting(); - app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); - } + app.UseAuthorization(); + + app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); } } \ No newline at end of file