41 lines
1.4 KiB
C#
41 lines
1.4 KiB
C#
|
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!;
|
||
|
}
|
||
|
}
|