2018-12-09 20:31:02 +00:00
|
|
|
|
using Mapster;
|
2019-02-03 17:50:17 +00:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-12-09 23:30:55 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-12-09 20:31:02 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Roadie.Library;
|
2018-11-14 23:18:02 +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-14 23:18:02 +00:00
|
|
|
|
using Roadie.Library.Encoding;
|
2018-12-09 20:31:02 +00:00
|
|
|
|
using Roadie.Library.Enums;
|
|
|
|
|
using Roadie.Library.Extensions;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
using Roadie.Library.Identity;
|
2019-02-03 17:50:17 +00:00
|
|
|
|
using Roadie.Library.Imaging;
|
2018-11-14 23:18:02 +00:00
|
|
|
|
using Roadie.Library.Models;
|
|
|
|
|
using Roadie.Library.Models.Pagination;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using Roadie.Library.Models.Statistics;
|
2018-11-14 23:18:02 +00:00
|
|
|
|
using Roadie.Library.Models.Users;
|
|
|
|
|
using Roadie.Library.Utility;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2019-02-03 17:50:17 +00:00
|
|
|
|
using System.IO;
|
2018-11-14 23:18:02 +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 LabelService : ServiceBase, ILabelService
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
private IBookmarkService BookmarkService { get; }
|
2018-12-09 20:31:02 +00:00
|
|
|
|
|
2018-11-14 23:18:02 +00:00
|
|
|
|
public LabelService(IRoadieSettings configuration,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
IHttpEncoder httpEncoder,
|
|
|
|
|
IHttpContext httpContext,
|
2019-11-17 14:10:17 +00:00
|
|
|
|
IRoadieDbContext context,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
ICacheManager cacheManager,
|
|
|
|
|
ILogger<LabelService> logger,
|
|
|
|
|
IBookmarkService bookmarkService)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
BookmarkService = bookmarkService;
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-09 20:31:02 +00:00
|
|
|
|
public async Task<OperationResult<Label>> ById(User roadieUser, Guid id, IEnumerable<string> includes = null)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var cacheKey = string.Format("urn:label_by_id_operation:{0}:{1}", id,
|
|
|
|
|
includes == null ? "0" : string.Join("|", includes));
|
|
|
|
|
var result = await CacheManager.GetAsync(cacheKey,
|
2019-08-02 20:59:24 +00:00
|
|
|
|
async () => { return await LabelByIdAction(id, includes); }, data.Label.CacheRegionUrn(id));
|
2018-12-09 20:31:02 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
if (result?.Data != null && roadieUser != null)
|
|
|
|
|
{
|
2019-08-11 19:11:29 +00:00
|
|
|
|
var userBookmarkResult = await BookmarkService.List(roadieUser, new PagedRequest(), false, BookmarkType.Label);
|
2018-12-09 20:31:02 +00:00
|
|
|
|
if (userBookmarkResult.IsSuccess)
|
2019-08-11 19:11:29 +00:00
|
|
|
|
{
|
|
|
|
|
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x.Bookmark.Text == result.Data.Id.ToString()) != null;
|
|
|
|
|
}
|
2019-06-28 21:24:32 +00:00
|
|
|
|
if (result.Data.Comments.Any())
|
|
|
|
|
{
|
|
|
|
|
var commentIds = result.Data.Comments.Select(x => x.DatabaseId).ToArray();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var userCommentReactions = (from cr in DbContext.CommentReactions
|
2019-06-28 21:24:32 +00:00
|
|
|
|
where commentIds.Contains(cr.CommentId)
|
|
|
|
|
where cr.UserId == roadieUser.Id
|
|
|
|
|
select cr).ToArray();
|
|
|
|
|
foreach (var comment in result.Data.Comments)
|
|
|
|
|
{
|
2019-08-11 19:11:29 +00:00
|
|
|
|
var userCommentReaction = userCommentReactions.FirstOrDefault(x => x.CommentId == comment.DatabaseId);
|
2019-06-28 21:24:32 +00:00
|
|
|
|
comment.IsDisliked = userCommentReaction?.ReactionValue == CommentReaction.Dislike;
|
|
|
|
|
comment.IsLiked = userCommentReaction?.ReactionValue == CommentReaction.Like;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-09 20:31:02 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-12-09 20:31:02 +00:00
|
|
|
|
return new OperationResult<Label>(result.Messages)
|
|
|
|
|
{
|
|
|
|
|
Data = result?.Data,
|
|
|
|
|
IsNotFoundResult = result?.IsNotFoundResult ?? false,
|
|
|
|
|
Errors = result?.Errors,
|
|
|
|
|
IsSuccess = result?.IsSuccess ?? false,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-22 20:33:07 +00:00
|
|
|
|
public async Task<OperationResult<bool>> Delete(ApplicationUser user, Guid id)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var label = DbContext.Labels.FirstOrDefault(x => x.RoadieId == id);
|
|
|
|
|
if (label == null) return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", id));
|
|
|
|
|
DbContext.Labels.Remove(label);
|
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
var labelImageFilename = label.PathToImage(Configuration);
|
|
|
|
|
if (File.Exists(labelImageFilename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(labelImageFilename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.LogWarning("User `{0}` deleted Label `{1}]`", user, label);
|
|
|
|
|
CacheManager.ClearRegion(label.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<LabelList>> List(User roadieUser, PagedRequest request,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
bool? doRandomize = false)
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
2019-08-03 22:59:20 +00:00
|
|
|
|
int? rowCount = null;
|
|
|
|
|
|
2018-11-14 23:18:02 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(request.Sort))
|
|
|
|
|
{
|
|
|
|
|
request.Sort = request.Sort.Replace("createdDate", "createdDateTime");
|
|
|
|
|
request.Sort = request.Sort.Replace("lastUpdated", "lastUpdatedDateTime");
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
|
|
|
|
var normalizedFilterValue = !string.IsNullOrEmpty(request.FilterValue)
|
|
|
|
|
? request.FilterValue.ToAlphanumericName()
|
|
|
|
|
: null;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
|
|
|
|
|
int[] randomLabelIds = null;
|
2019-11-17 00:08:44 +00:00
|
|
|
|
SortedDictionary<int, int> randomLabelData = null;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
|
|
|
|
var randomLimit = request.Limit ?? roadieUser?.RandomReleaseLimit ?? request.LimitValue;
|
2019-11-17 20:02:19 +00:00
|
|
|
|
randomLabelData = await DbContext.RandomLabelIds(roadieUser?.Id ?? -1, randomLimit, request.FilterFavoriteOnly, request.FilterRatedOnly);
|
2019-11-17 00:08:44 +00:00
|
|
|
|
randomLabelIds = randomLabelData.Select(x => x.Value).ToArray();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
rowCount = DbContext.Labels.Count();
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var result = from l in DbContext.Labels
|
2019-08-03 22:59:20 +00:00
|
|
|
|
where randomLabelIds == null || randomLabelIds.Contains(l.Id)
|
2019-08-22 20:33:07 +00:00
|
|
|
|
where request.FilterValue == "" || (
|
2019-07-31 16:44:25 +00:00
|
|
|
|
l.Name.Contains(request.FilterValue) ||
|
|
|
|
|
l.SortName.Contains(request.FilterValue) ||
|
|
|
|
|
l.AlternateNames.Contains(request.FilterValue) ||
|
|
|
|
|
l.AlternateNames.Contains(normalizedFilterValue)
|
2019-06-30 22:14:36 +00:00
|
|
|
|
)
|
|
|
|
|
select new LabelList
|
|
|
|
|
{
|
|
|
|
|
DatabaseId = l.Id,
|
|
|
|
|
Id = l.RoadieId,
|
|
|
|
|
Label = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = l.Name,
|
|
|
|
|
Value = l.RoadieId.ToString()
|
2019-07-31 16:44:25 +00:00
|
|
|
|
},
|
2019-06-30 22:14:36 +00:00
|
|
|
|
SortName = l.SortName,
|
|
|
|
|
CreatedDate = l.CreatedDate,
|
|
|
|
|
LastUpdated = l.LastUpdated,
|
|
|
|
|
ArtistCount = l.ArtistCount,
|
|
|
|
|
ReleaseCount = l.ReleaseCount,
|
|
|
|
|
TrackCount = l.TrackCount,
|
2019-10-23 13:45:36 +00:00
|
|
|
|
Thumbnail = MakeLabelThumbnailImage(Configuration, HttpContext, l.RoadieId)
|
2019-06-30 22:14:36 +00:00
|
|
|
|
};
|
2018-12-09 23:30:55 +00:00
|
|
|
|
LabelList[] rows = null;
|
2019-08-03 22:59:20 +00:00
|
|
|
|
rowCount = rowCount ?? result.Count();
|
2018-12-09 23:30:55 +00:00
|
|
|
|
if (doRandomize ?? false)
|
|
|
|
|
{
|
2019-11-17 00:08:44 +00:00
|
|
|
|
var resultData = result.ToArray();
|
|
|
|
|
rows = (from r in resultData
|
|
|
|
|
join ra in randomLabelData on r.DatabaseId equals ra.Value
|
|
|
|
|
orderby ra.Key
|
|
|
|
|
select r
|
|
|
|
|
).ToArray();
|
2018-12-09 23:30:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var sortBy = string.IsNullOrEmpty(request.Sort)
|
|
|
|
|
? request.OrderValue(new Dictionary<string, string> { { "SortName", "ASC" }, { "Label.Text", "ASC" } })
|
|
|
|
|
: request.OrderValue();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
rows = result
|
|
|
|
|
.OrderBy(sortBy)
|
|
|
|
|
.Skip(request.SkipValue)
|
|
|
|
|
.Take(request.LimitValue)
|
|
|
|
|
.ToArray();
|
2018-12-09 23:30:55 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2018-11-14 23:18:02 +00:00
|
|
|
|
sw.Stop();
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new Library.Models.Pagination.PagedResult<LabelList>
|
2018-11-14 23:18:02 +00:00
|
|
|
|
{
|
2019-08-03 22:59:20 +00:00
|
|
|
|
TotalCount = rowCount.Value,
|
2018-11-14 23:18:02 +00:00
|
|
|
|
CurrentPage = request.PageValue,
|
|
|
|
|
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Rows = rows
|
2019-11-17 20:02:19 +00:00
|
|
|
|
};
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
2019-01-08 22:40:26 +00:00
|
|
|
|
|
2019-07-31 16:44:25 +00:00
|
|
|
|
public async Task<OperationResult<bool>> MergeLabelsIntoLabel(ApplicationUser user, Guid intoLabelId, IEnumerable<Guid> labelIdsToMerge)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
var label = DbContext.Labels.FirstOrDefault(x => x.RoadieId == intoLabelId);
|
|
|
|
|
if (label == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, string.Format("Merge Into Label Not Found [{0}]", intoLabelId));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
var labelsToMerge = (from l in DbContext.Labels
|
|
|
|
|
join ltm in labelIdsToMerge on l.RoadieId equals ltm
|
|
|
|
|
select l);
|
|
|
|
|
foreach (var labelToMerge in labelsToMerge)
|
|
|
|
|
{
|
|
|
|
|
label.MusicBrainzId = label.MusicBrainzId ?? labelToMerge.MusicBrainzId;
|
|
|
|
|
label.SortName = label.SortName ?? labelToMerge.SortName;
|
|
|
|
|
label.Profile = label.Profile ?? labelToMerge.Profile;
|
|
|
|
|
label.BeginDate = label.BeginDate ?? labelToMerge.BeginDate;
|
|
|
|
|
label.EndDate = label.EndDate ?? labelToMerge.EndDate;
|
|
|
|
|
label.Profile = label.Profile ?? labelToMerge.Profile;
|
|
|
|
|
label.DiscogsId = label.DiscogsId ?? labelToMerge.DiscogsId;
|
|
|
|
|
label.ImageUrl = label.ImageUrl ?? labelToMerge.ImageUrl;
|
|
|
|
|
label.Tags = label.Tags.AddToDelimitedList(labelToMerge.Tags.ToListFromDelimited());
|
|
|
|
|
var altNames = labelToMerge.AlternateNames.ToListFromDelimited().ToList();
|
|
|
|
|
altNames.Add(labelToMerge.Name);
|
|
|
|
|
altNames.Add(labelToMerge.SortName);
|
|
|
|
|
altNames.Add(labelToMerge.Name.ToAlphanumericName());
|
|
|
|
|
label.AlternateNames = label.AlternateNames.AddToDelimitedList(altNames);
|
|
|
|
|
label.URLs = label.URLs.AddToDelimitedList(labelToMerge.URLs.ToListFromDelimited());
|
|
|
|
|
|
|
|
|
|
var labelToMergeReleases = (from rl in DbContext.ReleaseLabels
|
|
|
|
|
where rl.LabelId == labelToMerge.Id
|
|
|
|
|
select rl);
|
|
|
|
|
foreach (var labelToMergeRelease in labelToMergeReleases)
|
|
|
|
|
{
|
|
|
|
|
labelToMergeRelease.LabelId = label.Id;
|
|
|
|
|
labelToMergeRelease.LastUpdated = now;
|
|
|
|
|
}
|
|
|
|
|
label.LastUpdated = now;
|
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
await UpdateLabelCounts(label.Id, now);
|
|
|
|
|
|
|
|
|
|
CacheManager.ClearRegion(label.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"MergeLabelsIntoLabel `{label}`, Merged Label Ids [{ string.Join(",", labelIdsToMerge) }] By User `{user}`");
|
|
|
|
|
|
|
|
|
|
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>> SetLabelImageByUrl(User user, Guid id, string imageUrl)
|
2019-02-03 17:50:17 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
return await SaveImageBytes(user, id, WebHelper.BytesForImageUrl(imageUrl));
|
2019-02-03 17:50:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<OperationResult<bool>> UpdateLabel(User user, Label model)
|
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var label = DbContext.Labels.FirstOrDefault(x => x.RoadieId == model.Id);
|
2019-08-03 15:23:46 +00:00
|
|
|
|
if (label == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, string.Format("Label Not Found [{0}]", model.Id));
|
|
|
|
|
}
|
|
|
|
|
// If label is being renamed, see if label already exists with new model supplied name
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var labelName = label.SortNameValue;
|
|
|
|
|
var labelModelName = model.SortNameValue;
|
|
|
|
|
var oldPathToImage = label.PathToImage(Configuration);
|
|
|
|
|
var didChangeName = !labelName.ToAlphanumericName().Equals(labelModelName.ToAlphanumericName(), StringComparison.OrdinalIgnoreCase);
|
|
|
|
|
if (didChangeName)
|
2019-08-03 15:23:46 +00:00
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var existingLabel = DbContext.Labels.FirstOrDefault(x => x.Name == model.Name || x.SortName == model.SortName );
|
2019-08-03 15:23:46 +00:00
|
|
|
|
if (existingLabel != null)
|
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new OperationResult<bool>($"Label already exists `{ existingLabel }` with name [{ labelModelName }].");
|
2019-08-03 15:23:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-03 17:50:17 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-11-17 14:10:17 +00:00
|
|
|
|
var specialLabelName = model.Name.ToAlphanumericName();
|
2019-07-31 16:44:25 +00:00
|
|
|
|
var alt = new List<string>(model.AlternateNamesList);
|
2019-11-17 14:10:17 +00:00
|
|
|
|
if (!model.AlternateNamesList.Contains(specialLabelName, StringComparer.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
alt.Add(specialLabelName);
|
|
|
|
|
}
|
2019-07-31 16:44:25 +00:00
|
|
|
|
label.AlternateNames = alt.ToDelimitedList();
|
2019-02-03 17:50:17 +00:00
|
|
|
|
label.BeginDate = model.BeginDate;
|
|
|
|
|
label.DiscogsId = model.DiscogsId;
|
|
|
|
|
label.EndDate = model.EndDate;
|
|
|
|
|
label.IsLocked = model.IsLocked;
|
|
|
|
|
label.MusicBrainzId = model.MusicBrainzId;
|
|
|
|
|
label.Name = model.Name;
|
|
|
|
|
label.Profile = model.Profile;
|
|
|
|
|
label.SortName = model.SortName;
|
|
|
|
|
label.Status = SafeParser.ToEnum<Statuses>(model.Status);
|
|
|
|
|
label.Tags = model.TagsList.ToDelimitedList();
|
|
|
|
|
label.URLs = model.URLsList.ToDelimitedList();
|
|
|
|
|
|
2019-07-31 17:02:15 +00:00
|
|
|
|
if (didChangeName)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(oldPathToImage))
|
|
|
|
|
{
|
|
|
|
|
File.Move(oldPathToImage, label.PathToImage(Configuration));
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-03 17:50:17 +00:00
|
|
|
|
var labelImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
|
|
|
|
|
if (labelImage != null)
|
|
|
|
|
{
|
2019-08-22 20:33:07 +00:00
|
|
|
|
// Save unaltered label image
|
2019-07-31 16:44:25 +00:00
|
|
|
|
File.WriteAllBytes(label.PathToImage(Configuration), ImageHelper.ConvertToJpegFormat(labelImage));
|
2019-02-03 17:50:17 +00:00
|
|
|
|
}
|
|
|
|
|
label.LastUpdated = now;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
await DbContext.SaveChangesAsync();
|
2019-02-03 17:50:17 +00:00
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
CacheManager.ClearRegion(label.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"UpdateLabel `{label}` By User `{user}`");
|
2019-02-03 17:50:17 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex);
|
2019-02-03 17:50:17 +00:00
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-02-03 17:50:17 +00:00
|
|
|
|
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>> UploadLabelImage(User user, Guid id, IFormFile file)
|
2019-02-03 17:50:17 +00:00
|
|
|
|
{
|
|
|
|
|
var bytes = new byte[0];
|
|
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
file.CopyTo(ms);
|
|
|
|
|
bytes = ms.ToArray();
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
|
|
|
|
return await SaveImageBytes(user, id, bytes);
|
2019-02-03 17:50:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-08 22:40:26 +00:00
|
|
|
|
private Task<OperationResult<Label>> LabelByIdAction(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-01-08 22:40:26 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var label = GetLabel(id);
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("GetLabel", tsw.ElapsedMilliseconds);
|
2019-01-08 22:40:26 +00:00
|
|
|
|
if (label == null)
|
2019-08-04 16:30:19 +00:00
|
|
|
|
{
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return Task.FromResult(new OperationResult<Label>(true, string.Format("Label Not Found [{0}]", id)));
|
2019-08-04 16:30:19 +00:00
|
|
|
|
}
|
|
|
|
|
tsw.Restart();
|
2019-01-08 22:40:26 +00:00
|
|
|
|
var result = label.Adapt<Label>();
|
|
|
|
|
result.AlternateNames = label.AlternateNames;
|
|
|
|
|
result.Tags = label.Tags;
|
|
|
|
|
result.URLs = label.URLs;
|
2019-10-23 13:45:36 +00:00
|
|
|
|
result.Thumbnail = MakeLabelThumbnailImage(Configuration, HttpContext, label.RoadieId);
|
|
|
|
|
result.MediumThumbnail = MakeThumbnailImage(Configuration, HttpContext, id, "label", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("adapt", tsw.ElapsedMilliseconds);
|
2019-01-08 22:40:26 +00:00
|
|
|
|
if (includes != null && includes.Any())
|
|
|
|
|
{
|
|
|
|
|
if (includes.Contains("stats"))
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var labelTracks = from l in DbContext.Labels
|
|
|
|
|
join rl in DbContext.ReleaseLabels on l.Id equals rl.LabelId into rld
|
|
|
|
|
from rl in rld.DefaultIfEmpty()
|
|
|
|
|
join r in DbContext.Releases on rl.ReleaseId equals r.Id
|
|
|
|
|
join rm in DbContext.ReleaseMedias on r.Id equals rm.ReleaseId
|
|
|
|
|
join t in DbContext.Tracks on rm.Id equals t.ReleaseMediaId
|
|
|
|
|
where l.Id == label.Id
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
t.Duration,
|
|
|
|
|
t.FileSize
|
|
|
|
|
};
|
2019-01-08 22:40:26 +00:00
|
|
|
|
result.Duration = labelTracks.Sum(x => x.Duration);
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
result.Statistics = new ReleaseGroupingStatistics
|
2019-01-08 22:40:26 +00:00
|
|
|
|
{
|
|
|
|
|
TrackCount = label.TrackCount,
|
|
|
|
|
ArtistCount = label.ArtistCount,
|
|
|
|
|
ReleaseCount = label.ReleaseCount,
|
|
|
|
|
TrackSize = result.DurationTime,
|
|
|
|
|
FileSize = labelTracks.Sum(x => (long?)x.FileSize).ToFileSize()
|
|
|
|
|
};
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("stats", tsw.ElapsedMilliseconds);
|
2019-01-08 22:40:26 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-06-28 21:24:32 +00:00
|
|
|
|
if (includes.Contains("comments"))
|
|
|
|
|
{
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Restart();
|
|
|
|
|
var labelComments = DbContext.Comments.Include(x => x.User)
|
|
|
|
|
.Where(x => x.LabelId == label.Id)
|
|
|
|
|
.OrderByDescending(x => x.CreatedDate)
|
|
|
|
|
.ToArray();
|
2019-06-28 21:24:32 +00:00
|
|
|
|
if (labelComments.Any())
|
|
|
|
|
{
|
|
|
|
|
var comments = new List<Comment>();
|
|
|
|
|
var commentIds = labelComments.Select(x => x.Id).ToArray();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var userCommentReactions = (from cr in DbContext.CommentReactions
|
2019-06-28 21:24:32 +00:00
|
|
|
|
where commentIds.Contains(cr.CommentId)
|
|
|
|
|
select cr).ToArray();
|
|
|
|
|
foreach (var labelComment in labelComments)
|
|
|
|
|
{
|
|
|
|
|
var comment = labelComment.Adapt<Comment>();
|
|
|
|
|
comment.DatabaseId = labelComment.Id;
|
2019-10-23 13:45:36 +00:00
|
|
|
|
comment.User = UserList.FromDataUser(labelComment.User, MakeUserThumbnailImage(Configuration, HttpContext, labelComment.User.RoadieId));
|
2019-08-04 16:30:19 +00:00
|
|
|
|
comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == labelComment.Id && x.ReactionValue == CommentReaction.Dislike);
|
|
|
|
|
comment.LikedCount = userCommentReactions.Count(x => x.CommentId == labelComment.Id && x.ReactionValue == CommentReaction.Like);
|
2019-06-28 21:24:32 +00:00
|
|
|
|
comments.Add(comment);
|
|
|
|
|
}
|
|
|
|
|
result.Comments = comments;
|
|
|
|
|
}
|
2019-08-04 16:30:19 +00:00
|
|
|
|
tsw.Stop();
|
2019-08-11 19:11:29 +00:00
|
|
|
|
timings.Add("comments", tsw.ElapsedMilliseconds);
|
2019-06-28 21:24:32 +00:00
|
|
|
|
}
|
2019-01-08 22:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sw.Stop();
|
2019-08-04 16:30:19 +00:00
|
|
|
|
Logger.LogInformation($"ByIdAction: Label `{ label }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
|
2019-01-08 22:40:26 +00:00
|
|
|
|
return Task.FromResult(new OperationResult<Label>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result != null,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-05-29 22:25:40 +00:00
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
private async Task<OperationResult<Library.Models.Image>> SaveImageBytes(User user, Guid id, byte[] imageBytes)
|
2019-05-29 22:25:40 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var label = DbContext.Labels.FirstOrDefault(x => x.RoadieId == id);
|
2019-11-04 03:19:04 +00:00
|
|
|
|
if (label == null) return new OperationResult<Library.Models.Image>(true, string.Format("Label Not Found [{0}]", id));
|
2019-05-29 22:25:40 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var now = DateTime.UtcNow;
|
2019-11-04 03:19:04 +00:00
|
|
|
|
if (imageBytes != null)
|
2019-05-29 22:25:40 +00:00
|
|
|
|
{
|
2019-08-22 20:33:07 +00:00
|
|
|
|
// Save unaltered label image
|
2019-11-10 14:48:07 +00:00
|
|
|
|
File.WriteAllBytes(label.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(imageBytes));
|
2019-05-29 22:25:40 +00:00
|
|
|
|
}
|
|
|
|
|
label.LastUpdated = now;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
await DbContext.SaveChangesAsync();
|
|
|
|
|
CacheManager.ClearRegion(label.CacheRegion);
|
|
|
|
|
Logger.LogInformation($"UploadLabelImage `{label}` By User `{user}`");
|
2019-05-29 22:25:40 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex);
|
2019-05-29 22:25:40 +00:00
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2019-11-04 03:19:04 +00:00
|
|
|
|
return new OperationResult<Library.Models.Image>
|
2019-05-29 22:25:40 +00:00
|
|
|
|
{
|
|
|
|
|
IsSuccess = !errors.Any(),
|
2019-10-23 13:45:36 +00:00
|
|
|
|
Data = MakeThumbnailImage(Configuration, HttpContext, id, "label", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height, true),
|
2019-05-29 22:25:40 +00:00
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-11-14 23:18:02 +00:00
|
|
|
|
}
|
2018-11-16 03:37:00 +00:00
|
|
|
|
}
|