zotan.pw-web/Controllers/PlaybackHistory/LogPlayback.cs

42 lines
1.5 KiB
C#
Raw Normal View History

2022-11-20 16:44:40 +01:00
using LinqToDB;
using Microsoft.AspNetCore.Mvc;
2022-11-26 13:23:58 +01:00
using zotanpw.Backend.database;
using zotanpw.Backend.database.Tables;
2022-11-20 16:44:40 +01:00
2022-11-26 13:23:58 +01:00
namespace zotanpw.Controllers.PlaybackHistory;
2022-11-20 16:44:40 +01:00
2022-11-20 17:00:51 +01:00
[ApiController, Route("/np/log")]
2022-11-20 16:44:40 +01:00
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();
2022-11-20 17:00:51 +01:00
if (!db.AlbumHistory.Any(p => p.Title == rq.Title && p.Artist == rq.Artist && p.DateTime.Between(DateTime.Now - TimeSpan.FromHours(8), DateTime.Now)))
2022-11-20 16:57:45 +01:00
if (!db.AlbumHistory.Any()
|| !(db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Artist == rq.Artist
&& db.AlbumHistory.OrderByDescending(p => p.EntryId).First().Title == rq.Title)) {
2022-11-20 16:44:40 +01:00
var entry = new AlbumHistoryEntry {
2022-11-20 16:57:45 +01:00
Artist = rq.Artist ?? throw new InvalidOperationException(),
Title = rq.Title ?? throw new InvalidOperationException(),
Link = rq.Link ?? throw new InvalidOperationException(),
Source = rq.Source ?? throw new InvalidOperationException(),
2022-11-20 16:44:40 +01:00
DateTime = DateTime.Now
};
2022-11-20 16:57:45 +01:00
entry.Link = entry.Link.StartsWith("http:") ? entry.Link = "https:" + entry.Link[5..] : entry.Link;
2022-11-20 16:44:40 +01:00
db.InsertWithIdentity(entry);
Response.StatusCode = 201;
return entry;
}
Response.StatusCode = 202;
return null!;
}
Response.StatusCode = 403;
return null!;
}
2022-11-26 13:23:58 +01:00
}