2018-11-16 03:37:00 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
using Roadie.Library.Caching;
|
|
|
|
|
using Roadie.Library.Configuration;
|
|
|
|
|
using Roadie.Library.Encoding;
|
|
|
|
|
using Roadie.Library.Enums;
|
|
|
|
|
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;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Dynamic.Core;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using data = Roadie.Library.Data;
|
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Services
|
|
|
|
|
{
|
|
|
|
|
public class BookmarkService : ServiceBase, IBookmarkService
|
|
|
|
|
{
|
|
|
|
|
public BookmarkService(IRoadieSettings configuration,
|
|
|
|
|
IHttpEncoder httpEncoder,
|
|
|
|
|
IHttpContext httpContext,
|
|
|
|
|
data.IRoadieDbContext context,
|
|
|
|
|
ICacheManager cacheManager,
|
2018-11-17 02:14:32 +00:00
|
|
|
|
ILogger<BookmarkService> logger,
|
2018-11-15 04:11:25 +00:00
|
|
|
|
ICollectionService collectionService,
|
|
|
|
|
IPlaylistService playlistService)
|
|
|
|
|
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Library.Models.Pagination.PagedResult<BookmarkList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, BookmarkType? filterType = null)
|
|
|
|
|
{
|
2018-11-24 17:52:15 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
var sw = new Stopwatch();
|
|
|
|
|
sw.Start();
|
|
|
|
|
var result = (from b in this.DbContext.Bookmarks
|
2018-11-24 17:52:15 +00:00
|
|
|
|
join u in this.DbContext.Users on b.UserId equals u.Id
|
2018-11-15 04:11:25 +00:00
|
|
|
|
where b.UserId == roadieUser.Id
|
|
|
|
|
where (filterType == null || b.BookmarkType == filterType)
|
|
|
|
|
select new BookmarkList
|
|
|
|
|
{
|
2018-11-24 17:52:15 +00:00
|
|
|
|
Comment = b.Comment,
|
|
|
|
|
Position = b.Position,
|
|
|
|
|
User = new DataToken
|
|
|
|
|
{
|
|
|
|
|
Text = u.UserName,
|
|
|
|
|
Value = u.RoadieId.ToString()
|
|
|
|
|
},
|
2018-11-15 04:11:25 +00:00
|
|
|
|
DatabaseId = b.Id,
|
|
|
|
|
Id = b.RoadieId,
|
|
|
|
|
CreatedDate = b.CreatedDate,
|
|
|
|
|
LastUpdated = b.LastUpdated,
|
|
|
|
|
Type = b.BookmarkType,
|
2018-11-24 17:52:15 +00:00
|
|
|
|
BookmarkTargetId = b.BookmarkTargetId
|
2018-11-15 04:11:25 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "CreatedDate", "DESC" } }) : request.OrderValue(null);
|
|
|
|
|
var rowCount = result.Count();
|
2018-11-24 17:52:15 +00:00
|
|
|
|
BookmarkList[] rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
|
2018-11-15 04:11:25 +00:00
|
|
|
|
|
|
|
|
|
foreach (var row in rows)
|
|
|
|
|
{
|
|
|
|
|
switch (row.Type)
|
|
|
|
|
{
|
|
|
|
|
case BookmarkType.Artist:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var artist = this.DbContext.Artists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = artist.Name,
|
|
|
|
|
Value = artist.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakeArtistThumbnailImage(artist.RoadieId);
|
|
|
|
|
row.SortName = artist.SortName ?? artist.Name;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
2018-11-16 03:37:00 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
case BookmarkType.Release:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var release = this.DbContext.Releases.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = release.Title,
|
|
|
|
|
Value = release.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakeReleaseThumbnailImage(release.RoadieId);
|
|
|
|
|
row.SortName = release.Title;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
2018-11-16 03:37:00 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
case BookmarkType.Track:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var track = this.DbContext.Tracks.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = track.Title,
|
|
|
|
|
Value = track.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakeTrackThumbnailImage(track.RoadieId);
|
|
|
|
|
row.SortName = track.Title;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
2018-11-16 03:37:00 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
case BookmarkType.Playlist:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var playlist = this.DbContext.Playlists.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = playlist.Name,
|
|
|
|
|
Value = playlist.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakePlaylistThumbnailImage(playlist.RoadieId);
|
|
|
|
|
row.SortName = playlist.Name;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
2018-11-16 03:37:00 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
case BookmarkType.Collection:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var collection = this.DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = collection.Name,
|
|
|
|
|
Value = collection.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakeCollectionThumbnailImage(collection.RoadieId);
|
|
|
|
|
row.SortName = collection.SortName ?? collection.Name;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
2018-11-16 03:37:00 +00:00
|
|
|
|
|
2018-11-15 04:11:25 +00:00
|
|
|
|
case BookmarkType.Label:
|
2018-12-05 16:04:21 +00:00
|
|
|
|
var label = this.DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
|
2018-11-15 04:11:25 +00:00
|
|
|
|
row.Bookmark = new DataToken
|
|
|
|
|
{
|
2018-12-05 16:04:21 +00:00
|
|
|
|
Text = label.Name,
|
|
|
|
|
Value = label.RoadieId.ToString()
|
2018-11-15 04:11:25 +00:00
|
|
|
|
};
|
2018-12-05 16:04:21 +00:00
|
|
|
|
row.Thumbnail = this.MakeLabelThumbnailImage(label.RoadieId);
|
|
|
|
|
row.SortName = label.SortName ?? label.Name;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
sw.Stop();
|
|
|
|
|
return new Library.Models.Pagination.PagedResult<BookmarkList>
|
2018-11-24 17:52:15 +00:00
|
|
|
|
{
|
2018-11-15 04:11:25 +00:00
|
|
|
|
TotalCount = rowCount,
|
|
|
|
|
CurrentPage = request.PageValue,
|
|
|
|
|
TotalPages = (int)Math.Ceiling((double)rowCount / request.LimitValue),
|
|
|
|
|
OperationTime = sw.ElapsedMilliseconds,
|
|
|
|
|
Rows = rows
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-16 03:37:00 +00:00
|
|
|
|
}
|