2018-12-11 04:41:15 +00:00
|
|
|
|
using Mapster;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-12-05 16:04:21 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
using Roadie.Library;
|
2018-11-07 04:33:22 +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-07 04:33:22 +00:00
|
|
|
|
using Roadie.Library.Encoding;
|
|
|
|
|
using Roadie.Library.Enums;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
using Roadie.Library.Extensions;
|
2019-01-26 20:55:58 +00:00
|
|
|
|
using Roadie.Library.Imaging;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using Roadie.Library.Models;
|
2018-11-07 04:33:22 +00:00
|
|
|
|
using Roadie.Library.Models.Collections;
|
|
|
|
|
using Roadie.Library.Models.Pagination;
|
2019-06-30 22:14:36 +00:00
|
|
|
|
using Roadie.Library.Models.Releases;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
using Roadie.Library.Models.Statistics;
|
2018-11-14 23:18:02 +00:00
|
|
|
|
using Roadie.Library.Models.Users;
|
2018-11-07 04:33:22 +00:00
|
|
|
|
using Roadie.Library.Utility;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2019-07-31 16:44:25 +00:00
|
|
|
|
using System.IO;
|
2018-11-07 04:33:22 +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 CollectionService : ServiceBase, ICollectionService
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
private IBookmarkService BookmarkService { get; }
|
2018-12-11 04:41:15 +00:00
|
|
|
|
|
2018-11-07 04:33:22 +00:00
|
|
|
|
public CollectionService(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<CollectionService> logger,
|
|
|
|
|
IBookmarkService bookmarkService)
|
2018-11-07 04:33:22 +00:00
|
|
|
|
: base(configuration, httpEncoder, dbContext, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
BookmarkService = bookmarkService;
|
2018-12-11 04:41:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
private async Task<OperationResult<Collection>> CollectionByIdActionAsync(Guid id, IEnumerable<string> includes = null)
|
|
|
|
|
{
|
|
|
|
|
var timings = new Dictionary<string, long>();
|
|
|
|
|
var tsw = new Stopwatch();
|
|
|
|
|
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var collection = await GetCollection(id).ConfigureAwait(false);
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("getCollection", tsw.ElapsedMilliseconds);
|
|
|
|
|
|
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<Collection>(true, $"Collection Not Found [{id}]");
|
|
|
|
|
}
|
|
|
|
|
var result = collection.Adapt<Collection>();
|
|
|
|
|
var maintainer = DbContext.Users.FirstOrDefault(x => x.Id == collection.MaintainerId);
|
|
|
|
|
result.Maintainer = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = maintainer.UserName,
|
|
|
|
|
Value = maintainer.RoadieId.ToString()
|
|
|
|
|
};
|
|
|
|
|
result.AlternateNames = collection.AlternateNames;
|
|
|
|
|
result.Tags = collection.Tags;
|
|
|
|
|
result.URLs = collection.URLs;
|
|
|
|
|
result.Thumbnail = ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, collection.RoadieId);
|
|
|
|
|
result.MediumThumbnail = ImageHelper.MakeThumbnailImage(Configuration, HttpContext, id, "collection", Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
|
|
|
|
|
result.CollectionFoundCount = (from crc in DbContext.CollectionReleases
|
|
|
|
|
where crc.CollectionId == collection.Id
|
|
|
|
|
select crc.Id).Count();
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("adapt", tsw.ElapsedMilliseconds);
|
|
|
|
|
if (includes != null && includes.Any())
|
|
|
|
|
{
|
|
|
|
|
if (includes.Contains("list"))
|
|
|
|
|
{
|
|
|
|
|
result.ListInCSVFormat = collection.ListInCSVFormat;
|
|
|
|
|
result.ListInCSV = collection.ListInCSV;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result.ListInCSV = null;
|
|
|
|
|
result.ListInCSVFormat = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("releases"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
result.Releases = (from crc in DbContext.CollectionReleases
|
|
|
|
|
join r in DbContext.Releases.Include(x => x.Artist) on crc.ReleaseId equals r.Id
|
|
|
|
|
where crc.CollectionId == collection.Id
|
|
|
|
|
orderby crc.ListNumber
|
|
|
|
|
select new CollectionRelease
|
|
|
|
|
{
|
|
|
|
|
ListNumber = crc.ListNumber,
|
|
|
|
|
Release = ReleaseList.FromDataRelease(r, r.Artist, HttpContext.BaseUrl, ImageHelper.MakeArtistThumbnailImage(Configuration, HttpContext, r.Artist.RoadieId), ImageHelper.MakeReleaseThumbnailImage(Configuration, HttpContext, r.RoadieId))
|
|
|
|
|
}).ToArray();
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("releases", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("stats"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var collectionReleases = from crc in DbContext.CollectionReleases
|
|
|
|
|
join r in DbContext.Releases on crc.ReleaseId equals r.Id
|
|
|
|
|
where crc.CollectionId == collection.Id
|
|
|
|
|
select r;
|
|
|
|
|
|
|
|
|
|
var collectionTracks = from crc in DbContext.CollectionReleases
|
|
|
|
|
join r in DbContext.Releases on crc.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 crc.CollectionId == collection.Id
|
|
|
|
|
select t;
|
|
|
|
|
|
|
|
|
|
result.Statistics = new CollectionStatistics
|
|
|
|
|
{
|
|
|
|
|
ArtistCount = collectionReleases.Select(x => x.ArtistId).Distinct().Count(),
|
|
|
|
|
FileSize = collectionTracks.Sum(x => (long?)x.FileSize).ToFileSize(),
|
|
|
|
|
MissingTrackCount = collectionTracks.Count(x => x.Hash != null),
|
|
|
|
|
ReleaseCount = collectionReleases.Count(),
|
|
|
|
|
ReleaseMediaCount = collectionReleases.Sum(x => x.MediaCount),
|
|
|
|
|
Duration = collectionReleases.Sum(x => (long?)x.Duration),
|
|
|
|
|
TrackCount = collectionReleases.Sum(x => x.TrackCount),
|
|
|
|
|
TrackPlayedCount = collectionReleases.Sum(x => x.PlayedCount)
|
|
|
|
|
};
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("stats", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (includes.Contains("comments"))
|
|
|
|
|
{
|
|
|
|
|
tsw.Restart();
|
|
|
|
|
var collectionComments = DbContext.Comments.Include(x => x.User)
|
|
|
|
|
.Where(x => x.CollectionId == collection.Id)
|
|
|
|
|
.OrderByDescending(x => x.CreatedDate)
|
|
|
|
|
.ToArray();
|
|
|
|
|
if (collectionComments.Any())
|
|
|
|
|
{
|
|
|
|
|
var comments = new List<Comment>();
|
|
|
|
|
var commentIds = collectionComments.Select(x => x.Id).ToArray();
|
|
|
|
|
var userCommentReactions = (from cr in DbContext.CommentReactions
|
|
|
|
|
where commentIds.Contains(cr.CommentId)
|
|
|
|
|
select cr).ToArray();
|
|
|
|
|
foreach (var collectionComment in collectionComments)
|
|
|
|
|
{
|
|
|
|
|
var comment = collectionComment.Adapt<Comment>();
|
|
|
|
|
comment.DatabaseId = collectionComment.Id;
|
|
|
|
|
comment.User = UserList.FromDataUser(collectionComment.User, ImageHelper.MakeUserThumbnailImage(Configuration, HttpContext, collectionComment.User.RoadieId));
|
|
|
|
|
comment.DislikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Dislike);
|
|
|
|
|
comment.LikedCount = userCommentReactions.Count(x => x.CommentId == collectionComment.Id && x.ReactionValue == CommentReaction.Like);
|
|
|
|
|
comments.Add(comment);
|
|
|
|
|
}
|
|
|
|
|
result.Comments = comments;
|
|
|
|
|
}
|
|
|
|
|
tsw.Stop();
|
|
|
|
|
timings.Add("comments", tsw.ElapsedMilliseconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Logger.LogInformation($"ByIdAction: Collection `{ collection }`: includes [{includes.ToCSV()}], timings: [{ timings.ToTimings() }]");
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new OperationResult<Collection>
|
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = result != null,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-26 20:55:58 +00:00
|
|
|
|
/// <summary>
|
2019-06-30 22:14:36 +00:00
|
|
|
|
/// Get blank Collection to add
|
2019-01-26 20:55:58 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roadieUser"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public OperationResult<Collection> Add(User roadieUser)
|
|
|
|
|
{
|
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
sw.Start();
|
|
|
|
|
|
|
|
|
|
var id = Guid.Empty;
|
|
|
|
|
var collection = new data.Collection
|
|
|
|
|
{
|
|
|
|
|
Status = Statuses.New
|
|
|
|
|
};
|
|
|
|
|
var result = collection.Adapt<Collection>();
|
|
|
|
|
result.Id = id;
|
2020-06-07 22:46:24 +00:00
|
|
|
|
result.Thumbnail = ImageHelper.MakeNewImage(HttpContext, "collection");
|
|
|
|
|
result.MediumThumbnail = ImageHelper.MakeNewImage(HttpContext, "collection");
|
2019-06-30 22:14:36 +00:00
|
|
|
|
result.Maintainer = new DataToken
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
|
|
|
|
Value = roadieUser.UserId.ToString(),
|
|
|
|
|
Text = roadieUser.UserName
|
|
|
|
|
};
|
|
|
|
|
sw.Stop();
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
return new OperationResult<Collection>
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
|
|
|
|
Data = result,
|
|
|
|
|
IsSuccess = true,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<Collection>> ByIdAsync(User roadieUser, Guid id, IEnumerable<string> includes = null)
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
2019-05-29 22:25:40 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
2019-01-26 20:55:58 +00:00
|
|
|
|
sw.Start();
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var cacheKey = $"urn:collection_by_id_operation:{id}:{(includes == null ? "0" : string.Join("|", includes))}";
|
2019-08-22 20:33:07 +00:00
|
|
|
|
var result = await CacheManager.GetAsync(cacheKey, async () =>
|
2019-08-04 04:36:36 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return await CollectionByIdActionAsync(id, includes).ConfigureAwait(false);
|
|
|
|
|
}, data.Artist.CacheRegionUrn(id)).ConfigureAwait(false);
|
2019-05-29 22:25:40 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
if (result?.Data != null && roadieUser != null)
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var userBookmarkResult = await BookmarkService.ListAsync(roadieUser, new PagedRequest(), false, BookmarkType.Collection).ConfigureAwait(false);
|
2019-05-29 22:25:40 +00:00
|
|
|
|
if (userBookmarkResult.IsSuccess)
|
2019-08-04 04:36:36 +00:00
|
|
|
|
{
|
2019-11-28 17:38:26 +00:00
|
|
|
|
result.Data.UserBookmarked = userBookmarkResult?.Rows?.FirstOrDefault(x => x?.Bookmark?.Value == result?.Data?.Id?.ToString()) != null;
|
2019-08-04 04:36:36 +00:00
|
|
|
|
}
|
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-06-30 22:14:36 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-26 20:55:58 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
return new OperationResult<Collection>(result.Messages)
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
2019-05-29 22:25:40 +00:00
|
|
|
|
Data = result?.Data,
|
|
|
|
|
IsNotFoundResult = result?.IsNotFoundResult ?? false,
|
|
|
|
|
Errors = result?.Errors,
|
|
|
|
|
IsSuccess = result?.IsSuccess ?? false,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds
|
2019-01-26 20:55:58 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> DeleteCollectionAsync(User user, Guid id)
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var collection = DbContext.Collections.FirstOrDefault(x => x.RoadieId == id);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
if (collection == null)
|
|
|
|
|
{
|
|
|
|
|
return new OperationResult<bool>(true, $"Collection Not Found [{id}]");
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
if (!user.IsEditor)
|
2019-01-26 20:55:58 +00:00
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogWarning($"DeleteCollection: Access Denied: `{collection}`, By User `{user}`");
|
2019-08-03 15:23:46 +00:00
|
|
|
|
var r = new OperationResult<bool>("Access Denied");
|
|
|
|
|
r.IsAccessDeniedResult = true;
|
|
|
|
|
return r;
|
2019-01-26 20:55:58 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-01-26 20:55:58 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
DbContext.Collections.Remove(collection);
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
await BookmarkService.RemoveAllBookmarksForItemAsync(BookmarkType.Collection, collection.Id).ConfigureAwait(false);
|
2019-07-31 16:44:25 +00:00
|
|
|
|
var collectionImageFilename = collection.PathToImage(Configuration);
|
|
|
|
|
if (File.Exists(collectionImageFilename))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(collectionImageFilename);
|
|
|
|
|
}
|
2019-01-26 20:55:58 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
Logger.LogError(ex);
|
2019-01-26 20:55:58 +00:00
|
|
|
|
errors.Add(ex);
|
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-01-26 20:55:58 +00:00
|
|
|
|
sw.Stop();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
Logger.LogWarning($"DeleteCollection `{collection}`, By User `{user}`");
|
2019-01-26 20:55:58 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = !errors.Any(),
|
|
|
|
|
Data = true,
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public Task<Library.Models.Pagination.PagedResult<CollectionList>> ListAsync(User roadieUser, PagedRequest request,
|
2019-06-30 22:14:36 +00:00
|
|
|
|
bool? doRandomize = false, Guid? releaseId = null, Guid? artistId = null)
|
2019-01-08 22:40:26 +00:00
|
|
|
|
{
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
IQueryable<data.Collection> collections = null;
|
|
|
|
|
if (artistId.HasValue)
|
|
|
|
|
{
|
2019-07-04 15:50:31 +00:00
|
|
|
|
collections = (from cr in DbContext.CollectionReleases
|
|
|
|
|
join c in DbContext.Collections on cr.CollectionId equals c.Id
|
|
|
|
|
join r in DbContext.Releases on cr.ReleaseId equals r.Id
|
|
|
|
|
join a in DbContext.Artists on r.ArtistId equals a.Id
|
|
|
|
|
where a.RoadieId == artistId
|
|
|
|
|
select c).Distinct();
|
2019-01-08 22:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
else if (releaseId.HasValue)
|
|
|
|
|
{
|
2019-07-04 15:50:31 +00:00
|
|
|
|
collections = (from cr in DbContext.CollectionReleases
|
|
|
|
|
join c in DbContext.Collections on cr.CollectionId equals c.Id
|
|
|
|
|
join r in DbContext.Releases on cr.ReleaseId equals r.Id
|
|
|
|
|
where r.RoadieId == releaseId
|
|
|
|
|
select c).Distinct();
|
2019-01-08 22:40:26 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
collections = DbContext.Collections;
|
2019-01-08 22:40:26 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2020-06-05 22:49:12 +00:00
|
|
|
|
var normalizedFilterValue = !string.IsNullOrEmpty(request.FilterValue)
|
|
|
|
|
? request.FilterValue.ToAlphanumericName()
|
|
|
|
|
: null;
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var result = from c in collections
|
2019-08-03 22:59:20 +00:00
|
|
|
|
let collectionCount = (from crc in DbContext.CollectionReleases
|
|
|
|
|
where crc.CollectionId == c.Id
|
|
|
|
|
select crc.Id).Count()
|
2020-06-05 22:49:12 +00:00
|
|
|
|
where string.IsNullOrEmpty(normalizedFilterValue) || (
|
|
|
|
|
c.Name.ToLower().Contains(normalizedFilterValue) ||
|
|
|
|
|
c.SortName.ToLower().Contains(normalizedFilterValue) ||
|
|
|
|
|
c.AlternateNames.ToLower().Contains(normalizedFilterValue)
|
|
|
|
|
)
|
2019-06-30 22:14:36 +00:00
|
|
|
|
where request.FilterToStatusValue == Statuses.Ok || c.Status == request.FilterToStatusValue
|
2019-08-03 22:59:20 +00:00
|
|
|
|
select new CollectionList
|
|
|
|
|
{
|
|
|
|
|
DatabaseId = c.Id,
|
|
|
|
|
Collection = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = c.Name,
|
|
|
|
|
Value = c.RoadieId.ToString()
|
|
|
|
|
},
|
|
|
|
|
Id = c.RoadieId,
|
|
|
|
|
CollectionCount = c.CollectionCount,
|
|
|
|
|
CollectionType = (c.CollectionType ?? CollectionType.Unknown).ToString(),
|
|
|
|
|
CollectionFoundCount = collectionCount,
|
|
|
|
|
CreatedDate = c.CreatedDate,
|
|
|
|
|
IsLocked = c.IsLocked,
|
|
|
|
|
LastUpdated = c.LastUpdated,
|
2019-11-24 21:58:38 +00:00
|
|
|
|
Thumbnail = ImageHelper.MakeCollectionThumbnailImage(Configuration, HttpContext, c.RoadieId)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
};
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var sortBy = string.IsNullOrEmpty(request.Sort)
|
|
|
|
|
? request.OrderValue(new Dictionary<string, string> { { "Collection.Text", "ASC" } })
|
|
|
|
|
: request.OrderValue();
|
2019-01-08 22:40:26 +00:00
|
|
|
|
var rowCount = result.Count();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
var rows = result
|
|
|
|
|
.OrderBy(sortBy)
|
|
|
|
|
.Skip(request.SkipValue)
|
|
|
|
|
.Take(request.LimitValue)
|
|
|
|
|
.ToArray();
|
2019-06-23 15:45:45 +00:00
|
|
|
|
if (request.FilterToStatusValue == Statuses.Incomplete)
|
2019-08-03 22:59:20 +00:00
|
|
|
|
{
|
2019-06-23 15:45:45 +00:00
|
|
|
|
rows = rows.OrderByDescending(x => x.PercentComplete).ThenBy(x => x.SortName).ToArray();
|
2019-08-03 22:59:20 +00:00
|
|
|
|
}
|
2019-01-08 22:40:26 +00:00
|
|
|
|
sw.Stop();
|
|
|
|
|
return Task.FromResult(new Library.Models.Pagination.PagedResult<CollectionList>
|
|
|
|
|
{
|
|
|
|
|
TotalCount = rowCount,
|
|
|
|
|
CurrentPage = request.PageValue,
|
|
|
|
|
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Rows = rows
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
/// <summary>
|
2019-06-30 22:14:36 +00:00
|
|
|
|
/// Updates (or Adds) Collection
|
2019-05-29 22:25:40 +00:00
|
|
|
|
/// </summary>
|
2020-06-07 22:46:24 +00:00
|
|
|
|
public async Task<OperationResult<bool>> UpdateCollectionAsync(User user, Collection model)
|
2019-05-29 22:25:40 +00:00
|
|
|
|
{
|
|
|
|
|
var isNew = model.Id == Guid.Empty;
|
|
|
|
|
var now = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var errors = new List<Exception>();
|
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
var collection = new data.Collection();
|
2019-05-29 22:25:40 +00:00
|
|
|
|
|
|
|
|
|
if (!isNew)
|
|
|
|
|
{
|
2019-06-30 22:14:36 +00:00
|
|
|
|
collection = DbContext.Collections.FirstOrDefault(x => x.RoadieId == model.Id);
|
2019-05-29 22:25:40 +00:00
|
|
|
|
if (collection == null)
|
2019-07-31 16:44:25 +00:00
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
return new OperationResult<bool>(true, $"Collection Not Found [{model.Id}]");
|
2019-07-31 16:44:25 +00:00
|
|
|
|
}
|
2019-08-03 15:23:46 +00:00
|
|
|
|
// If collection is being renamed, see if collection already exists with new model supplied name
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var collectionName = collection.SortNameValue;
|
|
|
|
|
var collectionModelName = model.SortNameValue;
|
|
|
|
|
if (collectionName.ToAlphanumericName() != collectionModelName.ToAlphanumericName())
|
2019-08-03 15:23:46 +00:00
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
var existingCollection = DbContext.Collections.FirstOrDefault(x => x.Name == model.Name || x.SortName == model.SortName);
|
2019-08-03 15:23:46 +00:00
|
|
|
|
if (existingCollection != null)
|
|
|
|
|
{
|
2019-11-17 20:02:19 +00:00
|
|
|
|
return new OperationResult<bool>($"Collection already exists `{ collection }` with name [{ collectionModelName }].");
|
2019-08-03 15:23:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-29 22:25:40 +00:00
|
|
|
|
}
|
|
|
|
|
collection.IsLocked = model.IsLocked;
|
2019-07-31 17:02:15 +00:00
|
|
|
|
var oldPathToImage = collection.PathToImage(Configuration);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
var didChangeName = collection.Name != model.Name && !isNew;
|
2019-05-29 22:25:40 +00:00
|
|
|
|
collection.Name = model.Name;
|
|
|
|
|
collection.SortName = model.SortName;
|
|
|
|
|
collection.Edition = model.Edition;
|
|
|
|
|
collection.ListInCSVFormat = model.ListInCSVFormat;
|
|
|
|
|
collection.ListInCSV = model.ListInCSV;
|
|
|
|
|
collection.Description = model.Description;
|
|
|
|
|
collection.URLs = model.URLs;
|
|
|
|
|
collection.Status = SafeParser.ToEnum<Statuses>(model.Status);
|
|
|
|
|
collection.CollectionType = SafeParser.ToEnum<CollectionType>(model.CollectionType);
|
|
|
|
|
collection.Tags = model.TagsList.ToDelimitedList();
|
|
|
|
|
collection.URLs = model.URLsList.ToDelimitedList();
|
|
|
|
|
collection.AlternateNames = model.AlternateNamesList.ToDelimitedList();
|
|
|
|
|
collection.CollectionCount = model.CollectionCount;
|
|
|
|
|
|
2019-11-28 17:38:26 +00:00
|
|
|
|
if (model.Maintainer?.Value != null)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
var maintainer = await DbContext.Users.FirstOrDefaultAsync(x => x.RoadieId == SafeParser.ToGuid(model.Maintainer.Value)).ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
if (maintainer != null)
|
|
|
|
|
{
|
|
|
|
|
collection.MaintainerId = maintainer.Id;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isNew)
|
|
|
|
|
{
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await DbContext.Collections.AddAsync(collection).ConfigureAwait(false);
|
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 17:02:15 +00:00
|
|
|
|
if (didChangeName)
|
|
|
|
|
{
|
|
|
|
|
if (File.Exists(oldPathToImage))
|
|
|
|
|
{
|
|
|
|
|
File.Move(oldPathToImage, collection.PathToImage(Configuration));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
|
|
|
|
|
if (collectionImage != null)
|
|
|
|
|
{
|
2019-08-22 20:33:07 +00:00
|
|
|
|
// Save unaltered collection image
|
2019-11-10 14:48:07 +00:00
|
|
|
|
File.WriteAllBytes(collection.PathToImage(Configuration, true), ImageHelper.ConvertToJpegFormat(collectionImage));
|
2019-05-29 22:25:40 +00:00
|
|
|
|
}
|
2019-06-30 22:14:36 +00:00
|
|
|
|
|
2019-05-29 22:25:40 +00:00
|
|
|
|
collection.LastUpdated = now;
|
2020-06-07 22:46:24 +00:00
|
|
|
|
await DbContext.SaveChangesAsync().ConfigureAwait(false);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
|
2019-06-30 22:14:36 +00:00
|
|
|
|
CacheManager.ClearRegion(collection.CacheRegion);
|
2019-11-28 17:38:26 +00:00
|
|
|
|
Logger.LogInformation($"UpdateCollection `{collection}` By User `{user}`");
|
2019-05-29 22:25:40 +00:00
|
|
|
|
return new OperationResult<bool>
|
|
|
|
|
{
|
|
|
|
|
IsSuccess = !errors.Any(),
|
|
|
|
|
Data = !errors.Any(),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Errors = errors
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-11-07 04:33:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|