42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using LinqToDB;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using zotanpw.Backend.database;
|
|
using zotanpw.Backend.database.Tables;
|
|
|
|
namespace zotanpw.Controllers.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
|
|
};
|
|
entry.Link = entry.Link.StartsWith("http:") ? entry.Link = "https:" + entry.Link[5..] : entry.Link;
|
|
db.InsertWithIdentity(entry);
|
|
Response.StatusCode = 201;
|
|
return entry;
|
|
}
|
|
|
|
Response.StatusCode = 202;
|
|
return null!;
|
|
}
|
|
|
|
Response.StatusCode = 403;
|
|
return null!;
|
|
}
|
|
}
|