diff --git a/zotan.pw-web/Pages/NowPlaying.cshtml b/zotan.pw-web/Pages/NowPlaying.cshtml index 6661a85..e58ef84 100644 --- a/zotan.pw-web/Pages/NowPlaying.cshtml +++ b/zotan.pw-web/Pages/NowPlaying.cshtml @@ -1,8 +1,11 @@ @page "/np" +@using zotanpw_web.database @model NowPlayingModel @{ ViewData["title"] = "now playing"; + var db = new Database.DbConn(); + var albums = db.AlbumHistory.OrderByDescending(p => p.EntryId).Take(10); }

Here you can see what kind of music I've been listening to lately. This table is updated every couple minutes from multiple data sources.

@@ -12,22 +15,26 @@

>> Albums

+ + @foreach (var album in albums) { + + + + + + + } - - - - - + - - - - - - +
Date Artist Album Link
@album.DateTime.ToString("yyyy-MM-dd")@album.Artist@album.Title + @album.Source +
RöyksoppProfound Mysteriesmusic.zotan.services
2022-11-04 t+pazolite Refactoring TravelApple Music
testtesttest + Apple Music +
diff --git a/zotan.pw-web/PlaybackHistory/LogPlayback.cs b/zotan.pw-web/PlaybackHistory/LogPlayback.cs new file mode 100644 index 0000000..59d2820 --- /dev/null +++ b/zotan.pw-web/PlaybackHistory/LogPlayback.cs @@ -0,0 +1,40 @@ +using LinqToDB; +using Microsoft.AspNetCore.Mvc; +using zotanpw_web.database; +using zotanpw_web.database.Tables; + +namespace zotanpw_web.PlaybackHistory; + +[ApiController] +[Route("/np/log")] +public class LogPlayback : Controller { + private static readonly string Token = System.IO.File.ReadAllLines(".bearer_token")[0]; + + [HttpPost] + public AlbumHistoryEntry Log([FromBody] LogPlaybackRequest rq) { + var token = Request.Headers.Authorization; + if (token == Token) { + var db = new Database.DbConn(); + if (!db.AlbumHistory.Any(p => p.Title == rq.Title && p.Artist == rq.Artist && p.DateTime.Between(DateTime.Now - TimeSpan.FromHours(8), DateTime.Now))) { + if (!db.AlbumHistory.Any() || !(db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Artist == rq.Artist && db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Title == rq.Title)) { + var entry = new AlbumHistoryEntry { + Artist = rq.Artist ?? throw new InvalidOperationException(), + Title = rq.Title ?? throw new InvalidOperationException(), + Link = rq.Link ?? throw new InvalidOperationException(), + Source = rq.Source ?? throw new InvalidOperationException(), + DateTime = DateTime.Now + }; + db.InsertWithIdentity(entry); + Response.StatusCode = 201; + return entry; + } + } + + Response.StatusCode = 202; + return null!; + } + + Response.StatusCode = 403; + return null!; + } +} diff --git a/zotan.pw-web/PlaybackHistory/LogPlaybackRequest.cs b/zotan.pw-web/PlaybackHistory/LogPlaybackRequest.cs new file mode 100644 index 0000000..b442416 --- /dev/null +++ b/zotan.pw-web/PlaybackHistory/LogPlaybackRequest.cs @@ -0,0 +1,8 @@ +namespace zotanpw_web.PlaybackHistory; + +public class LogPlaybackRequest { + public string? Artist { get; set; } + public string? Title { get; set; } + public string? Source { get; set; } + public string? Link { get; set; } +}