2019-01-02 02:03:17 +00:00
|
|
|
|
using HashidsNet;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
using Roadie.Library;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
using Roadie.Library.Caching;
|
|
|
|
|
using Roadie.Library.Configuration;
|
|
|
|
|
using Roadie.Library.Encoding;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
using Roadie.Library.Identity;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
using Roadie.Library.Models;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
using Roadie.Library.Utility;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
using System;
|
2018-12-30 20:57:06 +00:00
|
|
|
|
using System.Collections.Generic;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
using System.Linq;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
using data = Roadie.Library.Data;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Services
|
|
|
|
|
{
|
|
|
|
|
public abstract class ServiceBase
|
|
|
|
|
{
|
2019-01-02 02:03:17 +00:00
|
|
|
|
public static string TrackTokenSalt = "B0246908-FBD6-4E12-A96C-AF5B086115B3";
|
|
|
|
|
|
2018-11-06 04:31:25 +00:00
|
|
|
|
protected readonly ICacheManager _cacheManager = null;
|
|
|
|
|
protected readonly IRoadieSettings _configuration = null;
|
|
|
|
|
protected readonly data.IRoadieDbContext _dbContext = null;
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected readonly IHttpContext _httpContext = null;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
protected readonly IHttpEncoder _httpEncoder = null;
|
|
|
|
|
protected readonly ILogger _logger = null;
|
|
|
|
|
|
|
|
|
|
protected ICacheManager CacheManager
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this._cacheManager;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected IRoadieSettings Configuration
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this._configuration;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected data.IRoadieDbContext DbContext
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return this._dbContext;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected IHttpContext HttpContext
|
2018-11-06 04:31:25 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this._httpContext;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected IHttpEncoder HttpEncoder
|
2018-11-06 04:31:25 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this._httpEncoder;
|
2018-11-06 04:31:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected ILogger Logger
|
2018-11-06 21:55:31 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this._logger;
|
|
|
|
|
}
|
2018-11-06 21:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-06 04:31:25 +00:00
|
|
|
|
public ServiceBase(IRoadieSettings configuration, IHttpEncoder httpEncoder, data.IRoadieDbContext context,
|
2018-11-06 21:55:31 +00:00
|
|
|
|
ICacheManager cacheManager, ILogger logger, IHttpContext httpContext)
|
|
|
|
|
{
|
|
|
|
|
this._configuration = configuration;
|
|
|
|
|
this._httpEncoder = httpEncoder;
|
|
|
|
|
this._dbContext = context;
|
|
|
|
|
this._cacheManager = cacheManager;
|
|
|
|
|
this._logger = logger;
|
|
|
|
|
this._httpContext = httpContext;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
public static bool ConfirmTrackPlayToken(ApplicationUser user, Guid trackRoadieId, string token)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(token))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return ServiceBase.TrackPlayToken(user, trackRoadieId).Equals(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string TrackPlayToken(ApplicationUser user, Guid trackId)
|
|
|
|
|
{
|
|
|
|
|
var hashids = new Hashids(ServiceBase.TrackTokenSalt);
|
|
|
|
|
var trackIdPart = BitConverter.ToInt32(trackId.ToByteArray(), 6);
|
|
|
|
|
if (trackIdPart < 0)
|
|
|
|
|
{
|
|
|
|
|
trackIdPart = trackIdPart * -1;
|
|
|
|
|
}
|
|
|
|
|
var token = hashids.Encode(user.Id, SafeParser.ToNumber<int>(user.CreatedDate.Value.ToString("DDHHmmss")), trackIdPart);
|
|
|
|
|
return token;
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-27 21:56:40 +00:00
|
|
|
|
public Image MakeThumbnailImage(Guid id, string type, int? width = null, int? height = null, bool includeCachebuster = false)
|
2018-12-04 23:26:27 +00:00
|
|
|
|
{
|
2018-12-27 21:56:40 +00:00
|
|
|
|
return this.MakeImage(id, type, width ?? this.Configuration.ThumbnailImageSize.Width, height ?? this.Configuration.ThumbnailImageSize.Height, null, includeCachebuster);
|
2018-12-04 23:26:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected IEnumerable<int> ArtistIdsForRelease(int releaseId)
|
|
|
|
|
{
|
|
|
|
|
var trackArtistIds = (from r in this.DbContext.Releases
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join tr in this.DbContext.Tracks on rm.Id equals tr.ReleaseMediaId
|
|
|
|
|
where r.Id == releaseId
|
|
|
|
|
where tr.ArtistId != null
|
|
|
|
|
select tr.ArtistId.Value).ToList();
|
|
|
|
|
trackArtistIds.Add(this.DbContext.Releases.FirstOrDefault(x => x.Id == releaseId).ArtistId);
|
|
|
|
|
return trackArtistIds.Distinct().ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-22 23:12:57 +00:00
|
|
|
|
protected data.Artist GetArtist(string artistName)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(artistName))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var artistByName = this.CacheManager.Get(data.Artist.CacheUrnByName(artistName), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Artists
|
|
|
|
|
.FirstOrDefault(x => x.Name == artistName);
|
|
|
|
|
}, null);
|
2018-11-24 17:52:15 +00:00
|
|
|
|
if (artistByName == null)
|
2018-11-22 23:12:57 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return this.GetArtist(artistByName.RoadieId);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected data.Artist GetArtist(Guid id)
|
2018-11-06 21:55:31 +00:00
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this.CacheManager.Get(data.Artist.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Artists
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Artist.CacheRegionUrn(id));
|
2018-11-06 21:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected data.Collection GetCollection(Guid id)
|
2018-11-07 04:33:22 +00:00
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this.CacheManager.Get(data.Collection.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Collections
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Collection.CacheRegionUrn(id));
|
2018-11-07 04:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected data.Label GetLabel(Guid id)
|
2018-11-07 04:33:22 +00:00
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this.CacheManager.Get(data.Label.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Labels
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Label.CacheRegionUrn(id));
|
2018-11-07 04:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected data.Playlist GetPlaylist(Guid id)
|
2018-11-06 21:55:31 +00:00
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this.CacheManager.Get(data.Playlist.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Playlists
|
2018-11-22 17:31:59 +00:00
|
|
|
|
.Include(x => x.User)
|
2018-11-11 20:10:10 +00:00
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Playlist.CacheRegionUrn(id));
|
2018-11-06 21:55:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-11 20:10:10 +00:00
|
|
|
|
protected data.Release GetRelease(Guid id)
|
2018-11-06 21:55:31 +00:00
|
|
|
|
{
|
2018-11-11 20:10:10 +00:00
|
|
|
|
return this.CacheManager.Get(data.Release.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Releases
|
2018-11-14 03:19:28 +00:00
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
2018-11-18 14:42:02 +00:00
|
|
|
|
.Include(x => x.Medias)
|
|
|
|
|
.Include("Medias.Tracks")
|
|
|
|
|
.Include("Medias.Tracks.TrackArtist")
|
2018-11-11 20:10:10 +00:00
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Release.CacheRegionUrn(id));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 17:52:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get Track by Subsonic Id ("T:guid")
|
|
|
|
|
/// </summary>
|
2018-11-17 02:14:32 +00:00
|
|
|
|
protected data.Track GetTrack(string id)
|
|
|
|
|
{
|
|
|
|
|
Guid trackId = Guid.Empty;
|
2018-11-24 17:52:15 +00:00
|
|
|
|
if (Guid.TryParse(id, out trackId))
|
2018-11-17 02:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
return this.GetTrack(trackId);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-12 21:10:00 +00:00
|
|
|
|
// Only read operations
|
2018-11-14 23:18:02 +00:00
|
|
|
|
protected data.Track GetTrack(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return this.CacheManager.Get(data.Track.CacheUrn(id), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Tracks
|
2018-11-17 02:14:32 +00:00
|
|
|
|
.Include(x => x.ReleaseMedia)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release.Artist)
|
|
|
|
|
.Include(x => x.TrackArtist)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
}, data.Track.CacheRegionUrn(id));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 14:36:07 +00:00
|
|
|
|
protected ApplicationUser GetUser(string username)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(username))
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var userByUsername = this.CacheManager.Get(ApplicationUser.CacheUrnByUsername(username), () =>
|
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Users
|
2018-11-24 17:52:15 +00:00
|
|
|
|
.FirstOrDefault(x => x.UserName == username);
|
2018-11-20 14:36:07 +00:00
|
|
|
|
}, null);
|
|
|
|
|
return this.GetUser(userByUsername?.RoadieId);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-18 14:42:02 +00:00
|
|
|
|
protected ApplicationUser GetUser(Guid? id)
|
2018-11-11 20:10:10 +00:00
|
|
|
|
{
|
2018-11-24 17:52:15 +00:00
|
|
|
|
if (!id.HasValue)
|
2018-11-18 14:42:02 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return this.CacheManager.Get(ApplicationUser.CacheUrn(id.Value), () =>
|
2018-11-11 20:10:10 +00:00
|
|
|
|
{
|
|
|
|
|
return this.DbContext.Users
|
2018-11-14 14:00:08 +00:00
|
|
|
|
.Include(x => x.ArtistRatings)
|
|
|
|
|
.Include(x => x.ReleaseRatings)
|
|
|
|
|
.Include(x => x.TrackRatings)
|
2018-11-23 04:18:48 +00:00
|
|
|
|
.Include(x => x.UserRoles)
|
|
|
|
|
.Include("UserRoles.Role")
|
|
|
|
|
.Include("UserRoles.Role.RoleClaims")
|
|
|
|
|
.Include(x => x.Claims)
|
2018-11-25 23:34:17 +00:00
|
|
|
|
.Include(x => x.UserQues)
|
|
|
|
|
.Include("UserQues.Track")
|
2018-11-11 20:10:10 +00:00
|
|
|
|
.FirstOrDefault(x => x.RoadieId == id);
|
2018-11-18 14:42:02 +00:00
|
|
|
|
}, ApplicationUser.CacheRegionUrn(id.Value));
|
2018-11-11 20:10:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-11 23:09:52 +00:00
|
|
|
|
protected Image MakeArtistThumbnailImage(Guid? id)
|
2018-12-04 23:26:27 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
if (!id.HasValue)
|
2018-12-11 23:09:52 +00:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return MakeThumbnailImage(id.Value, "artist");
|
2018-12-04 23:26:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakeCollectionThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "collection");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected Image MakeFullsizeImage(Guid id, string caption = null)
|
2018-12-04 23:26:27 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return new Image($"{this.HttpContext.ImageBaseUrl }/{id}", caption, $"{this.HttpContext.ImageBaseUrl }/{id}/{ this.Configuration.SmallImageSize.Width }/{ this.Configuration.SmallImageSize.Height }");
|
2018-12-04 23:26:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected Image MakeImage(Guid id, int width = 200, int height = 200, string caption = null, bool includeCachebuster = false)
|
2018-12-04 23:26:27 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return new Image($"{this.HttpContext.ImageBaseUrl }/{id}/{ width }/{ height }/{ (includeCachebuster ? DateTime.UtcNow.Ticks.ToString() : string.Empty) }", caption, $"{this.HttpContext.ImageBaseUrl }/{id}/{ this.Configuration.SmallImageSize.Width }/{ this.Configuration.SmallImageSize.Height }");
|
2018-12-04 23:26:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakeImage(Guid id, string type, ImageSize imageSize)
|
|
|
|
|
{
|
|
|
|
|
return this.MakeImage(id, type, imageSize.Width, imageSize.Height);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakeLabelThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "label");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected string MakeLastFmUrl(string artistName, string releaseTitle)
|
|
|
|
|
{
|
|
|
|
|
return "http://www.last.fm/music/" + this.HttpEncoder.UrlEncode($"{ artistName }/{ releaseTitle }");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakePlaylistThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "playlist");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakeReleaseThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "release");
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected string MakeTrackPlayUrl(ApplicationUser user, int trackId, Guid trackRoadieId)
|
|
|
|
|
{
|
|
|
|
|
return $"{ this.HttpContext.BaseUrl }/play/track/{user.Id}/{ ServiceBase.TrackPlayToken(user, trackRoadieId)}/{ trackRoadieId }.mp3";
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-04 23:26:27 +00:00
|
|
|
|
protected Image MakeTrackThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "track");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected Image MakeUserThumbnailImage(Guid id)
|
|
|
|
|
{
|
|
|
|
|
return MakeThumbnailImage(id, "user");
|
|
|
|
|
}
|
2018-12-01 03:22:35 +00:00
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
protected async Task<OperationResult<short>> SetArtistRating(Guid artistId, ApplicationUser user, short rating)
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var artist = this.DbContext.Artists
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == artistId);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
if (artist == null)
|
|
|
|
|
{
|
2018-12-01 14:57:27 +00:00
|
|
|
|
return new OperationResult<short>(true, $"Invalid Artist Id [{ artistId }]");
|
2018-12-01 03:22:35 +00:00
|
|
|
|
}
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
2018-12-01 18:05:24 +00:00
|
|
|
|
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
if (userArtist == null)
|
|
|
|
|
{
|
|
|
|
|
userArtist = new data.UserArtist
|
|
|
|
|
{
|
|
|
|
|
Rating = rating,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ArtistId = artist.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserArtists.Add(userArtist);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userArtist.Rating = rating;
|
2019-01-12 21:10:00 +00:00
|
|
|
|
userArtist.LastUpdated = now;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
2019-01-13 03:12:09 +00:00
|
|
|
|
var ratings = this.DbContext.UserArtists.Where(x => x.ArtistId == artist.Id && x.Rating > 0).Select(x => x.Rating);
|
|
|
|
|
if (ratings != null && ratings.Any())
|
|
|
|
|
{
|
|
|
|
|
artist.Rating = (short)ratings.Average(x => (decimal)x);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
artist.Rating = 0;
|
|
|
|
|
}
|
2019-01-12 21:10:00 +00:00
|
|
|
|
artist.LastUpdated = now;
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
2019-01-23 16:50:36 +00:00
|
|
|
|
await this.UpdateArtistRank(artist.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(artist.CacheRegion);
|
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
artist = this.GetArtist(artistId);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<short>
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
2018-12-01 14:57:27 +00:00
|
|
|
|
Data = artist.Rating ?? 0
|
2018-12-01 03:22:35 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
protected async Task<OperationResult<short>> SetReleaseRating(Guid releaseId, ApplicationUser user, short rating)
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var release = this.DbContext.Releases
|
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.Include(x => x.Medias)
|
|
|
|
|
.Include("Medias.Tracks")
|
|
|
|
|
.Include("Medias.Tracks.TrackArtist")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == releaseId);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
2018-12-01 14:57:27 +00:00
|
|
|
|
return new OperationResult<short>(true, $"Invalid Release Id [{ releaseId }]");
|
2018-12-01 03:22:35 +00:00
|
|
|
|
}
|
2018-12-01 18:05:24 +00:00
|
|
|
|
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
if (userRelease == null)
|
|
|
|
|
{
|
|
|
|
|
userRelease = new data.UserRelease
|
|
|
|
|
{
|
|
|
|
|
Rating = rating,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ReleaseId = release.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserReleases.Add(userRelease);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userRelease.Rating = rating;
|
|
|
|
|
userRelease.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
2019-01-13 03:12:09 +00:00
|
|
|
|
var ratings = this.DbContext.UserReleases.Where(x => x.ReleaseId == release.Id && x.Rating > 0).Select(x => x.Rating);
|
|
|
|
|
if (ratings != null && ratings.Any())
|
|
|
|
|
{
|
|
|
|
|
release.Rating = (short)ratings.Average(x => (decimal)x);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
release.Rating = 0;
|
|
|
|
|
}
|
2019-01-12 21:10:00 +00:00
|
|
|
|
release.LastUpdated = now;
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
2019-01-22 00:04:58 +00:00
|
|
|
|
await this.UpdateReleaseRank(release.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
release = this.GetRelease(releaseId);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<short>
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
2018-12-01 14:57:27 +00:00
|
|
|
|
Data = release.Rating ?? 0
|
2018-12-01 03:22:35 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
protected async Task<OperationResult<short>> SetTrackRating(Guid trackId, ApplicationUser user, short rating)
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var track = this.DbContext.Tracks
|
|
|
|
|
.Include(x => x.ReleaseMedia)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release.Artist)
|
|
|
|
|
.Include(x => x.TrackArtist)
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == trackId);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
if (track == null)
|
|
|
|
|
{
|
2018-12-01 14:57:27 +00:00
|
|
|
|
return new OperationResult<short>(true, $"Invalid Track Id [{ trackId }]");
|
2018-12-01 03:22:35 +00:00
|
|
|
|
}
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var now = DateTime.UtcNow;
|
2018-12-01 18:05:24 +00:00
|
|
|
|
var userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.TrackId == track.Id && x.UserId == user.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
if (userTrack == null)
|
|
|
|
|
{
|
|
|
|
|
userTrack = new data.UserTrack
|
|
|
|
|
{
|
|
|
|
|
Rating = rating,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
TrackId = track.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserTracks.Add(userTrack);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userTrack.Rating = rating;
|
2019-01-12 21:10:00 +00:00
|
|
|
|
userTrack.LastUpdated = now;
|
2018-12-01 03:22:35 +00:00
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
2019-01-13 03:12:09 +00:00
|
|
|
|
var ratings = this.DbContext.UserTracks.Where(x => x.TrackId == track.Id && x.Rating > 0).Select(x => x.Rating);
|
|
|
|
|
if (ratings != null && ratings.Any())
|
|
|
|
|
{
|
|
|
|
|
track.Rating = (short)ratings.Average(x => (decimal)x);
|
|
|
|
|
} else
|
|
|
|
|
{
|
|
|
|
|
track.Rating = 0;
|
|
|
|
|
}
|
2019-01-12 21:10:00 +00:00
|
|
|
|
track.LastUpdated = now;
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
2019-01-22 00:04:58 +00:00
|
|
|
|
await this.UpdateReleaseRank(track.ReleaseMedia.Release.Id);
|
2018-12-01 03:22:35 +00:00
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
|
|
|
|
|
|
2018-12-01 14:57:27 +00:00
|
|
|
|
track = this.GetTrack(trackId);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<short>
|
2018-12-01 03:22:35 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
2018-12-01 14:57:27 +00:00
|
|
|
|
Data = track.Rating
|
2018-12-01 03:22:35 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected async Task<OperationResult<bool>> ToggleArtistDisliked(Guid artistId, ApplicationUser user, bool isDisliked)
|
2018-12-01 18:05:24 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var artist = this.DbContext.Artists
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == artistId);
|
2018-12-01 18:05:24 +00:00
|
|
|
|
if (artist == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Artist Id [{ artistId }]");
|
|
|
|
|
}
|
|
|
|
|
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id);
|
|
|
|
|
if (userArtist == null)
|
|
|
|
|
{
|
|
|
|
|
userArtist = new data.UserArtist
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
IsDisliked = isDisliked,
|
2018-12-01 18:05:24 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ArtistId = artist.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserArtists.Add(userArtist);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
userArtist.IsDisliked = isDisliked;
|
2018-12-01 18:05:24 +00:00
|
|
|
|
userArtist.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(artist.CacheRegion);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected async Task<OperationResult<bool>> ToggleArtistFavorite(Guid artistId, ApplicationUser user, bool isFavorite)
|
2018-12-24 22:35:59 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var artist = this.DbContext.Artists
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == artistId);
|
2018-12-24 22:35:59 +00:00
|
|
|
|
if (artist == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Artist Id [{ artistId }]");
|
|
|
|
|
}
|
|
|
|
|
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id);
|
|
|
|
|
if (userArtist == null)
|
|
|
|
|
{
|
|
|
|
|
userArtist = new data.UserArtist
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
IsFavorite = isFavorite,
|
2018-12-24 22:35:59 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ArtistId = artist.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserArtists.Add(userArtist);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
userArtist.IsFavorite = isFavorite;
|
2018-12-24 22:35:59 +00:00
|
|
|
|
userArtist.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(artist.CacheRegion);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task<OperationResult<bool>> ToggleReleaseDisliked(Guid releaseId, ApplicationUser user, bool isDisliked)
|
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var release = this.DbContext.Releases
|
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.Include(x => x.Medias)
|
|
|
|
|
.Include("Medias.Tracks")
|
|
|
|
|
.Include("Medias.Tracks.TrackArtist")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == releaseId);
|
2018-12-24 22:35:59 +00:00
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Release Id [{ releaseId }]");
|
|
|
|
|
}
|
|
|
|
|
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id);
|
|
|
|
|
if (userRelease == null)
|
|
|
|
|
{
|
|
|
|
|
userRelease = new data.UserRelease
|
|
|
|
|
{
|
|
|
|
|
IsDisliked = isDisliked,
|
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ReleaseId = release.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserReleases.Add(userRelease);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userRelease.IsDisliked = isDisliked;
|
|
|
|
|
userRelease.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-01 18:05:24 +00:00
|
|
|
|
protected async Task<OperationResult<bool>> ToggleReleaseFavorite(Guid releaseId, ApplicationUser user, bool isFavorite)
|
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var release = this.DbContext.Releases
|
|
|
|
|
.Include(x => x.Artist)
|
|
|
|
|
.Include(x => x.Genres)
|
|
|
|
|
.Include("Genres.Genre")
|
|
|
|
|
.Include(x => x.Medias)
|
|
|
|
|
.Include("Medias.Tracks")
|
|
|
|
|
.Include("Medias.Tracks.TrackArtist")
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == releaseId);
|
2018-12-01 18:05:24 +00:00
|
|
|
|
if (release == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Release Id [{ releaseId }]");
|
|
|
|
|
}
|
|
|
|
|
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id);
|
|
|
|
|
if (userRelease == null)
|
|
|
|
|
{
|
|
|
|
|
userRelease = new data.UserRelease
|
|
|
|
|
{
|
2018-12-24 22:35:59 +00:00
|
|
|
|
IsFavorite = isFavorite,
|
2018-12-01 18:05:24 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
ReleaseId = release.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserReleases.Add(userRelease);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
userRelease.IsFavorite = isFavorite;
|
|
|
|
|
userRelease.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(release.Artist.CacheRegion);
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected async Task<OperationResult<bool>> ToggleTrackDisliked(Guid trackId, ApplicationUser user, bool isDisliked)
|
2018-12-01 18:05:24 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var track = this.DbContext.Tracks
|
|
|
|
|
.Include(x => x.ReleaseMedia)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release.Artist)
|
|
|
|
|
.Include(x => x.TrackArtist)
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == trackId);
|
2018-12-01 18:05:24 +00:00
|
|
|
|
if (track == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Track Id [{ trackId }]");
|
|
|
|
|
}
|
|
|
|
|
var userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.TrackId == track.Id && x.UserId == user.Id);
|
|
|
|
|
if (userTrack == null)
|
|
|
|
|
{
|
|
|
|
|
userTrack = new data.UserTrack
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
IsDisliked = isDisliked,
|
2018-12-01 18:05:24 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
TrackId = track.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserTracks.Add(userTrack);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
userTrack.IsDisliked = isDisliked;
|
2018-12-01 18:05:24 +00:00
|
|
|
|
userTrack.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
|
|
|
|
|
|
2019-01-05 22:40:33 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected async Task<OperationResult<bool>> ToggleTrackFavorite(Guid trackId, ApplicationUser user, bool isFavorite)
|
2019-01-05 22:40:33 +00:00
|
|
|
|
{
|
2019-01-12 21:10:00 +00:00
|
|
|
|
var track = this.DbContext.Tracks
|
|
|
|
|
.Include(x => x.ReleaseMedia)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release.Artist)
|
|
|
|
|
.Include(x => x.TrackArtist)
|
|
|
|
|
.FirstOrDefault(x => x.RoadieId == trackId);
|
2019-01-05 22:40:33 +00:00
|
|
|
|
if (track == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Invalid Track Id [{ trackId }]");
|
|
|
|
|
}
|
|
|
|
|
var userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.TrackId == track.Id && x.UserId == user.Id);
|
|
|
|
|
if (userTrack == null)
|
|
|
|
|
{
|
|
|
|
|
userTrack = new data.UserTrack
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
IsFavorite = isFavorite,
|
2019-01-05 22:40:33 +00:00
|
|
|
|
UserId = user.Id,
|
|
|
|
|
TrackId = track.Id
|
|
|
|
|
};
|
|
|
|
|
this.DbContext.UserTracks.Add(userTrack);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
userTrack.IsFavorite = isFavorite;
|
2019-01-05 22:40:33 +00:00
|
|
|
|
userTrack.LastUpdated = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
this.CacheManager.ClearRegion(user.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
|
|
|
|
|
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
|
|
|
|
|
|
2018-12-01 18:05:24 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 20:57:06 +00:00
|
|
|
|
protected async Task UpdateArtistCounts(int artistId, DateTime now)
|
|
|
|
|
{
|
|
|
|
|
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == artistId);
|
|
|
|
|
if (artist != null)
|
|
|
|
|
{
|
|
|
|
|
artist.ReleaseCount = this.DbContext.Releases.Where(x => x.ArtistId == artistId).Count();
|
|
|
|
|
artist.TrackCount = (from r in this.DbContext.Releases
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join tr in this.DbContext.Tracks on rm.Id equals tr.ReleaseMediaId
|
|
|
|
|
where (tr.ArtistId == artistId || r.ArtistId == artistId)
|
|
|
|
|
select tr).Count();
|
|
|
|
|
|
|
|
|
|
artist.LastUpdated = now;
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(artist.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update the counts for all artists on a release (both track and release artists)
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected async Task UpdateArtistCountsForRelease(int releaseId, DateTime now)
|
2018-12-30 20:57:06 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
foreach (var artistId in this.ArtistIdsForRelease(releaseId))
|
2018-12-30 20:57:06 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
await this.UpdateArtistCounts(artistId, now);
|
2018-12-30 20:57:06 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async Task UpdateLabelCounts(int labelId, DateTime now)
|
|
|
|
|
{
|
|
|
|
|
var label = this.DbContext.Labels.FirstOrDefault(x => x.Id == labelId);
|
|
|
|
|
if (label != null)
|
|
|
|
|
{
|
|
|
|
|
label.ReleaseCount = this.DbContext.ReleaseLabels.Where(x => x.LabelId == label.Id).Count();
|
|
|
|
|
label.ArtistCount = (from r in this.DbContext.Releases
|
|
|
|
|
join rl in this.DbContext.ReleaseLabels on r.Id equals rl.ReleaseId
|
|
|
|
|
join a in this.DbContext.Artists on r.ArtistId equals a.Id
|
|
|
|
|
where rl.LabelId == label.Id
|
|
|
|
|
group a by a.Id into artists
|
|
|
|
|
select artists).Select(x => x.Key).Count();
|
|
|
|
|
label.TrackCount = (from r in this.DbContext.Releases
|
|
|
|
|
join rl in this.DbContext.ReleaseLabels on r.Id equals rl.ReleaseId
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join t in this.DbContext.Tracks on rm.Id equals t.ReleaseMediaId
|
|
|
|
|
where rl.LabelId == label.Id
|
|
|
|
|
select t).Count();
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(label.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
protected async Task UpdatePlaylistCounts(int playlistId, DateTime now)
|
|
|
|
|
{
|
|
|
|
|
var playlist = this.DbContext.Playlists.FirstOrDefault(x => x.Id == playlistId);
|
|
|
|
|
if (playlist != null)
|
|
|
|
|
{
|
|
|
|
|
var playlistTracks = this.DbContext.PlaylistTracks
|
|
|
|
|
.Include(x => x.Track)
|
|
|
|
|
.Include("Track.ReleaseMedia")
|
|
|
|
|
.Where(x => x.PlayListId == playlist.Id).ToArray();
|
|
|
|
|
playlist.TrackCount = (short)playlistTracks.Count();
|
|
|
|
|
playlist.Duration = playlistTracks.Sum(x => x.Track.Duration);
|
|
|
|
|
playlist.ReleaseCount = (short)playlistTracks.Select(x => x.Track.ReleaseMedia.ReleaseId).Distinct().Count();
|
|
|
|
|
playlist.LastUpdated = now;
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(playlist.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 20:57:06 +00:00
|
|
|
|
protected async Task UpdateReleaseCounts(int releaseId, DateTime now)
|
|
|
|
|
{
|
|
|
|
|
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == releaseId);
|
|
|
|
|
if (release != null)
|
|
|
|
|
{
|
2019-01-22 00:04:58 +00:00
|
|
|
|
release.PlayedCount = (from t in this.DbContext.Tracks
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == releaseId
|
|
|
|
|
where t.PlayedCount.HasValue
|
|
|
|
|
select t).Sum(x => x.PlayedCount);
|
2018-12-30 20:57:06 +00:00
|
|
|
|
release.Duration = (from t in this.DbContext.Tracks
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == releaseId
|
|
|
|
|
select t).Sum(x => x.Duration);
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(release.CacheRegion);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-02 02:03:17 +00:00
|
|
|
|
|
2019-01-23 16:50:36 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Find all artists involved with release and update their rank
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected async Task UpdateArtistsRankForRelease(data.Release release)
|
|
|
|
|
{
|
|
|
|
|
if(release != null)
|
|
|
|
|
{
|
|
|
|
|
var artistsForRelease = new List<int>
|
|
|
|
|
{
|
|
|
|
|
release.ArtistId
|
|
|
|
|
};
|
|
|
|
|
var trackArtistsForRelease = (from t in this.DbContext.Tracks
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
where rm.ReleaseId == release.Id
|
|
|
|
|
where t.ArtistId.HasValue
|
|
|
|
|
select t.ArtistId.Value).ToArray();
|
|
|
|
|
artistsForRelease.AddRange(trackArtistsForRelease);
|
|
|
|
|
foreach(var artistId in artistsForRelease.Distinct())
|
|
|
|
|
{
|
|
|
|
|
await this.UpdateArtistRank(artistId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update Artist Rank
|
|
|
|
|
/// Artist Rank is a sum of the artists release ranks + artist tracks rating + artist user rating
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected async Task UpdateArtistRank(int artistId, bool updateReleaseRanks = false)
|
|
|
|
|
{
|
|
|
|
|
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == artistId);
|
|
|
|
|
if (artist != null)
|
|
|
|
|
{
|
|
|
|
|
if(updateReleaseRanks)
|
|
|
|
|
{
|
|
|
|
|
var artistReleaseIds = this.DbContext.Releases.Where(x => x.ArtistId == artistId).Select(x => x.Id).ToArray();
|
|
|
|
|
foreach(var artistReleaseId in artistReleaseIds)
|
|
|
|
|
{
|
|
|
|
|
await this.UpdateReleaseRank(artistReleaseId, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var artistTrackAverage = (from t in this.DbContext.Tracks
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
|
|
|
|
|
where t.ArtistId == artist.Id
|
|
|
|
|
select (double?)ut.Rating).ToArray().Average(x => x) ?? 0;
|
|
|
|
|
|
2019-01-26 04:44:07 +00:00
|
|
|
|
var artistReleaseRatingRating = (from r in this.DbContext.Releases
|
2019-01-23 16:50:36 +00:00
|
|
|
|
join ur in this.DbContext.UserReleases on r.Id equals ur.ReleaseId
|
|
|
|
|
where r.ArtistId == artist.Id
|
|
|
|
|
select (double?)ur.Rating).ToArray().Average(x => x) ?? 0;
|
|
|
|
|
|
|
|
|
|
var artistReleaseRankSum = (from r in this.DbContext.Releases
|
|
|
|
|
where r.ArtistId == artist.Id
|
2019-01-26 04:44:07 +00:00
|
|
|
|
select r.Rank).ToArray().Sum(x => x) ?? 0;
|
2019-01-23 16:50:36 +00:00
|
|
|
|
|
2019-01-26 04:44:07 +00:00
|
|
|
|
artist.Rank = SafeParser.ToNumber<decimal>(artistTrackAverage + artistReleaseRatingRating) + artistReleaseRankSum + artist.Rating;
|
2019-01-23 16:50:36 +00:00
|
|
|
|
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(artist.CacheRegion);
|
|
|
|
|
this.Logger.LogInformation("UpdatedArtistRank For Artist `{0}`", artist);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 00:04:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Update Relase Rank
|
|
|
|
|
/// Release Rank Calculation = Average of Track User Ratings + (User Rating of Release / Release Track Count) + Collection Rank Value
|
|
|
|
|
/// </summary>
|
2019-01-23 16:50:36 +00:00
|
|
|
|
protected async Task UpdateReleaseRank(int releaseId, bool updateArtistRank = true)
|
2019-01-22 00:04:58 +00:00
|
|
|
|
{
|
|
|
|
|
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == releaseId);
|
|
|
|
|
if (release != null)
|
|
|
|
|
{
|
|
|
|
|
var releaseTrackAverage = (from t in this.DbContext.Tracks
|
|
|
|
|
join rm in this.DbContext.ReleaseMedias on t.ReleaseMediaId equals rm.Id
|
|
|
|
|
join ut in this.DbContext.UserTracks on t.Id equals ut.TrackId
|
|
|
|
|
where rm.ReleaseId == releaseId
|
|
|
|
|
select (double?)ut.Rating).ToArray().Average(x => x);
|
|
|
|
|
|
|
|
|
|
var releaseUserRatingRank = release.Rating > 0 ? (decimal?)release.Rating / (decimal?)release.TrackCount : 0;
|
|
|
|
|
|
|
|
|
|
var collectionsWithRelease = (from c in this.DbContext.Collections
|
|
|
|
|
join cr in this.DbContext.CollectionReleases on c.Id equals cr.CollectionId
|
2019-01-22 02:23:38 +00:00
|
|
|
|
where c.CollectionType != Library.Enums.CollectionType.Chart
|
2019-01-22 00:04:58 +00:00
|
|
|
|
where cr.ReleaseId == release.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
c.CollectionCount,
|
|
|
|
|
cr.ListNumber
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
decimal releaseCollectionRank = 0;
|
|
|
|
|
foreach (var collectionWithRelease in collectionsWithRelease)
|
|
|
|
|
{
|
|
|
|
|
var rank = (decimal)((collectionWithRelease.CollectionCount * .01) - ((collectionWithRelease.ListNumber - 1) * .01));
|
|
|
|
|
releaseCollectionRank += rank;
|
|
|
|
|
}
|
|
|
|
|
release.Rank = SafeParser.ToNumber<decimal>(releaseTrackAverage) + releaseUserRatingRank + releaseCollectionRank;
|
|
|
|
|
|
|
|
|
|
await this.DbContext.SaveChangesAsync();
|
|
|
|
|
this.CacheManager.ClearRegion(release.CacheRegion);
|
2019-01-23 16:50:36 +00:00
|
|
|
|
this.Logger.LogInformation("UpdateReleaseRank For Release `{0}`", release);
|
|
|
|
|
if (updateArtistRank)
|
|
|
|
|
{
|
|
|
|
|
await this.UpdateArtistsRankForRelease(release);
|
|
|
|
|
}
|
2019-01-22 00:04:58 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:55:58 +00:00
|
|
|
|
protected Image MakeNewImage(string type)
|
|
|
|
|
{
|
|
|
|
|
return new Image($"{this.HttpContext.ImageBaseUrl }/{type}.jpg", null, null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-22 00:04:58 +00:00
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
private Image MakeImage(Guid id, string type, int? width, int? height, string caption = null, bool includeCachebuster = false)
|
2019-01-02 02:03:17 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
if (width.HasValue && height.HasValue && (width.Value != this.Configuration.ThumbnailImageSize.Width || height.Value != this.Configuration.ThumbnailImageSize.Height))
|
2019-01-02 02:03:17 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return new Image($"{this.HttpContext.ImageBaseUrl }/{type}/{id}/{width}/{height}/{ (includeCachebuster ? DateTime.UtcNow.Ticks.ToString() : string.Empty) }", caption, $"{this.HttpContext.ImageBaseUrl }/{type}/{id}/{ this.Configuration.ThumbnailImageSize.Width }/{ this.Configuration.ThumbnailImageSize.Height }");
|
2019-01-02 02:03:17 +00:00
|
|
|
|
}
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return new Image($"{this.HttpContext.ImageBaseUrl }/{type}/{id}", caption, null);
|
2019-01-02 02:03:17 +00:00
|
|
|
|
}
|
2018-11-06 04:31:25 +00:00
|
|
|
|
}
|
2018-11-11 20:10:10 +00:00
|
|
|
|
}
|