2019-08-02 20:59:24 +00:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2019-08-02 20:59:24 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Roadie.Library;
|
2018-11-15 04:25:40 +00:00
|
|
|
|
using Roadie.Library.Caching;
|
|
|
|
|
using Roadie.Library.Configuration;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
using Roadie.Library.Data.Context;
|
2018-11-15 04:25:40 +00:00
|
|
|
|
using Roadie.Library.Encoding;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
using Roadie.Library.Enums;
|
2019-08-22 20:33:07 +00:00
|
|
|
|
using Roadie.Library.Extensions;
|
2019-08-02 20:59:24 +00:00
|
|
|
|
using Roadie.Library.Identity;
|
|
|
|
|
using Roadie.Library.Imaging;
|
2018-11-15 04:25:40 +00:00
|
|
|
|
using Roadie.Library.Models;
|
|
|
|
|
using Roadie.Library.Models.Pagination;
|
|
|
|
|
using Roadie.Library.Models.Users;
|
|
|
|
|
using Roadie.Library.Utility;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2019-08-02 20:59:24 +00:00
|
|
|
|
using System.IO;
|
2018-11-15 04:25:40 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Dynamic.Core;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using data = Roadie.Library.Data;
|
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Services
|
|
|
|
|
{
|
|
|
|
|
public class GenreService : ServiceBase, IGenreService
|
|
|
|
|
{
|
|
|
|
|
public GenreService(IRoadieSettings configuration,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
IHttpEncoder httpEncoder,
|
|
|
|
|
IHttpContext httpContext,
|
2019-11-17 14:10:17 +00:00
|
|
|
|
IRoadieDbContext dbContext,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
ILogger<GenreService> logger)
|
2018-11-15 04:25:40 +00:00
|
|
|
|
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-02 20:59:24 +00:00
|
|
|
|
public async Task<OperationResult<Genre>> ById(User roadieUser, Guid id, IEnumerable<string> includes = null)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var cacheKey = string.Format("urn:genre_by_id_operation:{0}:{1}", id, includes == null ? "0" : string.Join("|", includes));
|
2019-08-22 20:33:07 +00:00
|
|
|
|
var result = await CacheManager.GetAsync(cacheKey, async () =>
|
2019-08-02 20:59:24 +00:00
|
|
|
|
{
|
|
|
|
|
return await GenreByIdAction(id, includes);
|
|
|
|
|
}, data.Genre.CacheRegionUrn(id));
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<Genre>(result.Messages)
|
|
|
|
|
{
|
|
|
|
|
Data = result?.Data,
|
|
|
|
|
IsNotFoundResult = result?.IsNotFoundResult ?? false,
|
|
|
|
|
Errors = result?.Errors,
|
|
|
|
|
IsSuccess = result?.IsSuccess ?? false,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OperationResult<bool>> Delete(ApplicationUser user, Guid id)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var genre = DbContext.Genres.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
if (genre == null) return new OperationResult<bool>(true, string.Format("Genre Not Found [{0}]", id));
|
|
|
|
|
DbContext.Genres.Remove(genre);
|
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var genreImageFilename = genre.PathToImage(Configuration);
|
|
|
|
|
if (File.Exists(genreImageFilename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(genreImageFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 22:59:20 +00:00
|
|
|
|
Logger.LogWarning("User `{0}` deleted Genre `{1}]`", user, genre);
|
2019-08-02 20:59:24 +00:00
|
|
|
|
CacheManager.ClearRegion(genre.CacheRegion);
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
Data = true,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 20:02:19 +00:00
|
|
|
|
public async Task<Library.Models.Pagination.PagedResult<GenreList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false)
|
2019-08-22 20:33:07 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
int? rowCount = null;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(request.Sort))
|
|
|
|
|
{
|
|
|
|
|
request.Sort = request.Sort.Replace("createdDate", "createdDateTime");
|
|
|
|
|
request.Sort = request.Sort.Replace("lastUpdated", "lastUpdatedDateTime");
|
|
|
|
|
}
|
2019-08-02 20:59:24 +00:00
|
|
|
|
|
2019-08-22 20:33:07 +00:00
|
|
|
|
int[] randomGenreIds = null;
|
2019-11-17 00:08:44 +00:00
|
|
|
|
SortedDictionary<int, int> randomGenreData = null;
|
2019-08-22 20:33:07 +00:00
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
|
|
|
|
var randomLimit = request.Limit ?? roadieUser?.RandomReleaseLimit ?? request.LimitValue;
|
2019-11-17 20:02:19 +00:00
|
|
|
|
randomGenreData = await DbContext.RandomGenreIds(roadieUser?.Id ?? -1, randomLimit, request.FilterFavoriteOnly, request.FilterRatedOnly);
|
2019-11-17 00:08:44 +00:00
|
|
|
|
randomGenreIds = randomGenreData.Select(x => x.Value).ToArray();
|
2019-08-22 20:33:07 +00:00
|
|
|
|
rowCount = DbContext.Genres.Count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var result = from g in DbContext.Genres
|
|
|
|
|
where randomGenreIds == null || randomGenreIds.Contains(g.Id)
|
|
|
|
|
let releaseCount = (from rg in DbContext.ReleaseGenres
|
|
|
|
|
where rg.GenreId == g.Id
|
|
|
|
|
select rg.Id).Count()
|
|
|
|
|
let artistCount = (from rg in DbContext.ArtistGenres
|
|
|
|
|
where rg.GenreId == g.Id
|
|
|
|
|
select rg.Id).Count()
|
|
|
|
|
where request.FilterValue.Length == 0 || g.Name.Contains(request.FilterValue)
|
|
|
|
|
select new GenreList
|
|
|
|
|
{
|
|
|
|
|
DatabaseId = g.Id,
|
|
|
|
|
Id = g.RoadieId,
|
|
|
|
|
Genre = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = g.Name,
|
|
|
|
|
Value = g.RoadieId.ToString()
|
|
|
|
|
},
|
|
|
|
|
ReleaseCount = releaseCount,
|
|
|
|
|
ArtistCount = artistCount,
|
|
|
|
|
CreatedDate = g.CreatedDate,
|
|
|
|
|
LastUpdated = g.LastUpdated,
|
2019-11-24 21:58:38 +00:00
|
|
|
|
Thumbnail = ImageHelper.MakeGenreThumbnailImage(Configuration, HttpContext, g.RoadieId)
|
2019-08-22 20:33:07 +00:00
|
|
|
|
};
|
|
|
|
|
GenreList[] rows;
|
|
|
|
|
rowCount = rowCount ?? result.Count();
|
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
2019-11-17 00:08:44 +00:00
|
|
|
|
var resultData = result.ToArray();
|
|
|
|
|
rows = (from r in resultData
|
|
|
|
|
join ra in randomGenreData on r.DatabaseId equals ra.Value
|
|
|
|
|
orderby ra.Key
|
|
|
|
|
select r
|
|
|
|
|
).ToArray();
|
2019-08-22 20:33:07 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var sortBy = string.IsNullOrEmpty(request.Sort)
|
|
|
|
|
? request.OrderValue(new Dictionary<string, string> { { "Genre.Text", "ASC" } })
|
|
|
|
|
: request.OrderValue();
|
|
|
|
|
rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return (new Library.Models.Pagination.PagedResult<GenreList>
|
2019-08-22 20:33:07 +00:00
|
|
|
|
{
|
|
|
|
|
TotalCount = rowCount.Value,
|
|
|
|
|
CurrentPage = request.PageValue,
|
|
|
|
|
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Rows = rows
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
public async Task<OperationResult<Library.Models.Image>> SetGenreImageByUrl(User user, Guid id, string imageUrl)
|
2019-08-22 20:33:07 +00:00
|
|
|
|
{
|
|
|
|
|
return await SaveImageBytes(user, id, WebHelper.BytesForImageUrl(imageUrl));
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-17 14:10:17 +00:00
|
|
|
|
public async Task<OperationResult<bool>> UpdateGenre(User user, Genre model)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
var genre = DbContext.Genres.FirstOrDefault(x => x.RoadieId == model.Id);
|
|
|
|
|
if (genre == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, string.Format("Genre Not Found [{0}]", model.Id));
|
|
|
|
|
}
|
|
|
|
|
// If genre is being renamed, see if genre already exists with new model supplied name
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var genreName = genre.SortNameValue;
|
|
|
|
|
var genreModelName = genre.SortNameValue;
|
|
|
|
|
var didChangeName = !genreName.ToAlphanumericName().Equals(genreModelName.ToAlphanumericName(), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
if (didChangeName)
|
2019-11-17 14:10:17 +00:00
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var existingGenre = DbContext.Genres.FirstOrDefault(x => x.Name == model.Name || x.SortName == model.SortName);
|
2019-11-17 14:10:17 +00:00
|
|
|
|
if (existingGenre != null)
|
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new OperationResult<bool>($"Genre already exists `{ existingGenre }` with name [{ genreModelName }].");
|
2019-11-17 14:10:17 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var specialGenreName = model.Name.ToAlphanumericName();
|
|
|
|
|
var alt = new List<string>(model.AlternateNamesList);
|
|
|
|
|
if (!model.AlternateNamesList.Contains(specialGenreName, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
alt.Add(specialGenreName);
|
|
|
|
|
}
|
|
|
|
|
genre.AlternateNames = alt.ToDelimitedList();
|
2019-11-17 20:02:19 +00:00
|
|
|
|
genre.Description = model.Description;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
genre.IsLocked = model.IsLocked;
|
|
|
|
|
var oldPathToImage = genre.PathToImage(Configuration);
|
|
|
|
|
genre.Name = model.Name;
|
2019-11-17 20:02:19 +00:00
|
|
|
|
genre.NormalizedName = model.NormalizedName;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
genre.SortName = model.SortName;
|
|
|
|
|
genre.Status = SafeParser.ToEnum<Statuses>(model.Status);
|
|
|
|
|
genre.Tags = model.TagsList.ToDelimitedList();
|
|
|
|
|
|
|
|
|
|
if (didChangeName)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(oldPathToImage))
|
|
|
|
|
{
|
|
|
|
|
File.Move(oldPathToImage, genre.PathToImage(Configuration));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var genreImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
|
|
|
|
|
if (genreImage != null)
|
|
|
|
|
{
|
|
|
|
|
// Save unaltered genre image
|
|
|
|
|
File.WriteAllBytes(genre.PathToImage(Configuration), ImageHelper.ConvertToJpegFormat(genreImage));
|
|
|
|
|
}
|
|
|
|
|
genre.LastUpdated = now;
|
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
CacheManager.ClearRegion(genre.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"UpdateGenre `{genre}` By User `{user}`");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(ex);
|
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
|
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = !errors.Any(),
|
|
|
|
|
Data = !errors.Any(),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
public async Task<OperationResult<Library.Models.Image>> UploadGenreImage(User user, Guid id, IFormFile file)
|
2019-08-22 20:33:07 +00:00
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[0];
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
file.CopyTo(ms);
|
|
|
|
|
bytes = ms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await SaveImageBytes(user, id, bytes);
|
|
|
|
|
}
|
2019-08-02 20:59:24 +00:00
|
|
|
|
|
|
|
|
|
private Task<OperationResult<Genre>> GenreByIdAction(Guid id, IEnumerable<string> includes = null)
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
var timings = new Dictionary<string, long>();
|
|
|
|
|
var tsw = new Stopwatch();
|
|
|
|
|
|
2019-08-02 20:59:24 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
var genre = DbContext.Genres.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
if (genre == null)
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(new OperationResult<Genre>(true, string.Format("Genre Not Found [{0}]", id)));
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("getGenre", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
tsw.Restart();
|
2019-08-02 20:59:24 +00:00
|
|
|
|
var result = genre.Adapt<Genre>();
|
|
|
|
|
result.AlternateNames = genre.AlternateNames;
|
|
|
|
|
result.Tags = genre.Tags;
|
2019-11-24 21:58:38 +00:00
|
|
|
|
result.Thumbnail = ImageHelper.MakeGenreThumbnailImage(Configuration, HttpContext, genre.RoadieId);
|
|
|
|
|
result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "genre", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("adapt", tsw.ElapsedMilliseconds);
|
2019-08-02 20:59:24 +00:00
|
|
|
|
if (includes != null && includes.Any())
|
|
|
|
|
{
|
|
|
|
|
if (includes.Contains("stats"))
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2019-08-02 20:59:24 +00:00
|
|
|
|
var releaseCount = (from rg in DbContext.ReleaseGenres
|
|
|
|
|
where rg.GenreId == genre.Id
|
|
|
|
|
select rg.Id).Count();
|
|
|
|
|
var artistCount = (from rg in DbContext.ArtistGenres
|
|
|
|
|
where rg.GenreId == genre.Id
|
|
|
|
|
select rg.Id).Count();
|
|
|
|
|
result.Statistics = new Library.Models.Statistics.ReleaseGroupingStatistics
|
|
|
|
|
{
|
|
|
|
|
ArtistCount = artistCount,
|
|
|
|
|
ReleaseCount = releaseCount
|
|
|
|
|
};
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("stats", tsw.ElapsedMilliseconds);
|
2019-08-02 20:59:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sw.Stop();
|
2019-08-04 16:30:19 +00:00
|
|
|
|
Logger.LogInformation($"ByIdAction: Genre `{ genre }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
|
2019-08-02 20:59:24 +00:00
|
|
|
|
return Task.FromResult(new OperationResult<Genre>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result != null,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
private async Task<OperationResult<Library.Models.Image>> SaveImageBytes(User user, Guid id, byte[] imageBytes)
|
2019-08-02 20:59:24 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
var genre = DbContext.Genres.FirstOrDefault(x => x.RoadieId == id);
|
2019-11-10 14:48:07 +00:00
|
|
|
|
if (genre == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<Library.Models.Image>(true, string.Format("Genre Not Found [{0}]", id));
|
|
|
|
|
}
|
2019-08-02 20:59:24 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-11-04 03:19:04 +00:00
|
|
|
|
if (imageBytes != null)
|
2019-08-02 20:59:24 +00:00
|
|
|
|
{
|
2019-11-10 14:48:07 +00:00
|
|
|
|
// Save unaltered genre image
|
|
|
|
|
File.WriteAllBytes(genre.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(imageBytes));
|
2019-08-02 20:59:24 +00:00
|
|
|
|
}
|
|
|
|
|
genre.LastUpdated = now;
|
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
CacheManager.ClearRegion(genre.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"UploadGenreImage `{genre}` By User `{user}`");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Logger.LogError(ex);
|
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
return new OperationResult<Library.Models.Image>
|
2019-08-02 20:59:24 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = !errors.Any(),
|
2019-11-24 21:58:38 +00:00
|
|
|
|
Data = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "genre", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height, true),
|
2019-08-02 20:59:24 +00:00
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-11-15 04:25:40 +00:00
|
|
|
|
}
|
2018-11-16 03:37:00 +00:00
|
|
|
|
}
|