roadie/RoadieApi/Services/ServiceBase.cs

537 lines
20 KiB
C#
Raw Normal View History

2018-12-04 23:26:27 +00:00
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;
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
{
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;
}
2018-12-04 23:26:27 +00:00
public Image MakeThumbnailImage(Guid id, string type, int? width = null, int? height = null)
{
return this.MakeImage(id, type, width ?? this.Configuration.ThumbnailImageSize.Width, height ?? this.Configuration.ThumbnailImageSize.Height);
}
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);
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")
.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-12-09 17:58:31 +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;
if (Guid.TryParse(id, out trackId))
2018-11-17 02:14:32 +00:00
{
return this.GetTrack(trackId);
}
return null;
}
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)
.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
.FirstOrDefault(x => x.UserName == username);
2018-11-20 14:36:07 +00:00
}, null);
return this.GetUser(userByUsername?.RoadieId);
}
protected ApplicationUser GetUser(Guid? id)
2018-11-11 20:10:10 +00:00
{
if (!id.HasValue)
{
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);
}, 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
{
2018-12-11 23:09:52 +00:00
if(!id.HasValue)
{
return null;
}
return MakeThumbnailImage(id.Value, "artist");
2018-12-04 23:26:27 +00:00
}
protected Image MakeCollectionThumbnailImage(Guid id)
{
return MakeThumbnailImage(id, "collection");
}
protected Image MakeImage(Guid id, int width = 200, int height = 200, string caption = null)
{
return new Image($"{this.HttpContext.ImageBaseUrl }/{id}/{ width }/{ height }", caption, $"{this.HttpContext.ImageBaseUrl }/{id}/{ this.Configuration.SmallImageSize.Width }/{ this.Configuration.SmallImageSize.Height }");
}
protected Image MakeFullsizeImage(Guid id, string caption = null)
{
return new Image($"{this.HttpContext.ImageBaseUrl }/{id}", caption, $"{this.HttpContext.ImageBaseUrl }/{id}/{ this.Configuration.SmallImageSize.Width }/{ this.Configuration.SmallImageSize.Height }");
}
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");
}
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
{
var artist = this.GetArtist(artistId);
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
}
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;
userArtist.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
var sql = "UPDATE `artist` set lastUpdated = UTC_DATE(), rating = (SELECT cast(avg(ur.rating) as signed) " +
"FROM `userartist` ur " +
"where artistId = {0}) " +
"WHERE id = {0};";
await this.DbContext.Database.ExecuteSqlCommandAsync(sql, artist.Id);
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
{
var release = this.GetRelease(releaseId);
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();
var sql = "UPDATE `release` set lastUpdated = UTC_DATE(), rating = (SELECT cast(avg(ur.rating) as signed) " +
"FROM `userrelease` ur " +
"where releaseId = {0}) " +
"WHERE id = {0};";
await this.DbContext.Database.ExecuteSqlCommandAsync(sql, release.Id);
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
{
var track = this.GetTrack(trackId);
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
}
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;
userTrack.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
var sql = "UPDATE `track` set lastUpdated = UTC_DATE(), rating = (SELECT cast(avg(ur.rating) as signed) " +
"FROM `usertrack` ur " +
"where trackId = {0}) " +
"WHERE id = {0};";
await this.DbContext.Database.ExecuteSqlCommandAsync(sql, track.Id);
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
};
}
2018-12-01 18:05:24 +00:00
protected async Task<OperationResult<bool>> ToggleArtistFavorite(Guid artistId, ApplicationUser user, bool isFavorite)
{
var artist = this.GetArtist(artistId);
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
{
IsFavorite = true,
UserId = user.Id,
ArtistId = artist.Id
};
this.DbContext.UserArtists.Add(userArtist);
}
else
{
userArtist.IsFavorite = isFavorite;
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>> ToggleReleaseFavorite(Guid releaseId, ApplicationUser user, bool isFavorite)
{
var release = this.GetRelease(releaseId);
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
{
IsFavorite = true,
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
};
}
protected async Task<OperationResult<bool>> ToggleTrackFavorite(Guid trackId, ApplicationUser user, bool isFavorite)
{
var track = this.GetTrack(trackId);
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
{
IsFavorite = true,
UserId = user.Id,
TrackId = track.Id
};
this.DbContext.UserTracks.Add(userTrack);
}
else
{
userTrack.IsFavorite = isFavorite;
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);
return new OperationResult<bool>
{
IsSuccess = true,
Data = true
};
}
2018-12-04 23:26:27 +00:00
private Image MakeImage(Guid id, string type, int? width, int? height, string caption = null)
2018-11-06 21:55:31 +00:00
{
2018-12-04 23:26:27 +00:00
if (width.HasValue && height.HasValue && (width.Value != this.Configuration.ThumbnailImageSize.Width || height.Value != this.Configuration.ThumbnailImageSize.Height))
{
2018-12-04 23:26:27 +00:00
return new Image($"{this.HttpContext.ImageBaseUrl }/{type}/{id}/{width}/{height}", caption, $"{this.HttpContext.ImageBaseUrl }/{type}/{id}/{ this.Configuration.ThumbnailImageSize.Width }/{ this.Configuration.ThumbnailImageSize.Height }");
}
2018-12-04 23:26:27 +00:00
return new Image($"{this.HttpContext.ImageBaseUrl }/{type}/{id}", caption, null);
2018-11-22 13:48:32 +00:00
}
2018-11-06 04:31:25 +00:00
}
2018-11-11 20:10:10 +00:00
}