2022-02-24 00:27:34 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
|
|
|
|
namespace Selector.Model
|
|
|
|
|
{
|
|
|
|
|
public class ScrobbleRepository : IScrobbleRepository
|
|
|
|
|
{
|
|
|
|
|
private readonly ApplicationDbContext db;
|
|
|
|
|
|
|
|
|
|
public ScrobbleRepository(ApplicationDbContext context)
|
|
|
|
|
{
|
|
|
|
|
db = context;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Add(UserScrobble item)
|
|
|
|
|
{
|
|
|
|
|
db.Scrobble.Add(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void AddRange(IEnumerable<UserScrobble> item)
|
|
|
|
|
{
|
|
|
|
|
db.Scrobble.AddRange(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public UserScrobble Find(int key, string include = null)
|
|
|
|
|
{
|
|
|
|
|
var scrobbles = db.Scrobble.Where(s => s.Id == key);
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(include))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Include(include);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return scrobbles.FirstOrDefault();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 23:15:17 +00:00
|
|
|
|
public IQueryable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
|
2022-02-24 00:27:34 +00:00
|
|
|
|
{
|
|
|
|
|
var scrobbles = db.Scrobble.AsQueryable();
|
|
|
|
|
|
2022-11-05 22:39:07 +00:00
|
|
|
|
if (!tracking)
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.AsNoTracking();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-24 00:27:34 +00:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(include))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Include(include);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(s => s.UserId == userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(username))
|
|
|
|
|
{
|
2022-06-22 08:01:59 +01:00
|
|
|
|
var normalUsername = username.ToUpperInvariant();
|
|
|
|
|
var user = db.Users.AsNoTracking().Where(u => u.NormalizedUserName == normalUsername).FirstOrDefault();
|
2022-02-24 00:27:34 +00:00
|
|
|
|
if (user is not null)
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(s => s.UserId == user.Id);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
scrobbles = Enumerable.Empty<UserScrobble>().AsQueryable();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(trackName))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(s => s.TrackName == trackName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(albumName))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(s => s.AlbumName == albumName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(artistName))
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(s => s.ArtistName == artistName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (from is not null)
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(u => u.Timestamp >= from.Value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (to is not null)
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.Where(u => u.Timestamp < to.Value);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 22:39:07 +00:00
|
|
|
|
if (orderTime)
|
|
|
|
|
{
|
|
|
|
|
scrobbles = scrobbles.OrderBy(x => x.Timestamp);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-22 08:01:59 +01:00
|
|
|
|
return scrobbles;
|
2022-02-24 00:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-03 23:15:17 +00:00
|
|
|
|
// public IEnumerable<IListen> GetAll(string include = null, string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null, bool tracking = true, bool orderTime = false)
|
|
|
|
|
// => GetAllQueryable(include: include, userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: tracking, orderTime: orderTime).AsEnumerable();
|
2022-06-22 08:01:59 +01:00
|
|
|
|
|
2022-02-24 00:27:34 +00:00
|
|
|
|
public void Remove(int key)
|
|
|
|
|
{
|
|
|
|
|
Remove(Find(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(UserScrobble scrobble)
|
|
|
|
|
{
|
|
|
|
|
db.Scrobble.Remove(scrobble);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemoveRange(IEnumerable<UserScrobble> scrobbles)
|
|
|
|
|
{
|
|
|
|
|
db.Scrobble.RemoveRange(scrobbles);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Update(UserScrobble item)
|
|
|
|
|
{
|
|
|
|
|
db.Scrobble.Update(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<int> Save()
|
|
|
|
|
{
|
|
|
|
|
return db.SaveChangesAsync();
|
|
|
|
|
}
|
2022-06-22 08:01:59 +01:00
|
|
|
|
|
2022-10-07 23:33:54 +01:00
|
|
|
|
public int Count(string userId = null, string username = null, string trackName = null, string albumName = null, string artistName = null, DateTime? from = null, DateTime? to = null)
|
2023-01-03 23:15:17 +00:00
|
|
|
|
=> GetAll(userId: userId, username: username, trackName: trackName, albumName: albumName, artistName: artistName, from: from, to: to, tracking: false).Count();
|
2022-02-24 00:27:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|