2018-12-11 23:09:52 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
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;
|
2018-12-11 23:09:52 +00:00
|
|
|
|
using models = Roadie.Library.Models;
|
2018-11-15 04:11:25 +00:00
|
|
|
|
|
|
|
|
|
namespace Roadie.Api.Services
|
|
|
|
|
{
|
|
|
|
|
public class BookmarkService : ServiceBase, IBookmarkService
|
|
|
|
|
{
|
|
|
|
|
public BookmarkService(IRoadieSettings configuration,
|
|
|
|
|
IHttpEncoder httpEncoder,
|
|
|
|
|
IHttpContext httpContext,
|
|
|
|
|
data.IRoadieDbContext context,
|
|
|
|
|
ICacheManager cacheManager,
|
2018-12-11 04:41:15 +00:00
|
|
|
|
ILogger<BookmarkService> logger)
|
2018-11-15 04:11:25 +00:00
|
|
|
|
: base(configuration, httpEncoder, context, cacheManager, logger, httpContext)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-24 19:40:49 +00:00
|
|
|
|
public Task<Library.Models.Pagination.PagedResult<BookmarkList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, BookmarkType? filterType = null)
|
2018-11-15 04:11:25 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
2019-01-02 02:03:17 +00:00
|
|
|
|
var user = this.GetUser(roadieUser.UserId);
|
|
|
|
|
|
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-11 23:09:52 +00:00
|
|
|
|
row.Artist = models.ArtistList.FromDataArtist(artist, this.MakeArtistThumbnailImage(artist.RoadieId));
|
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-11 23:09:52 +00:00
|
|
|
|
var release = this.DbContext.Releases.Include(x => x.Artist).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-11 23:09:52 +00:00
|
|
|
|
row.Release = models.Releases.ReleaseList.FromDataRelease(release, release.Artist, this.HttpContext.BaseUrl, this.MakeArtistThumbnailImage(release.Artist.RoadieId), this.MakeReleaseThumbnailImage(release.RoadieId));
|
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-11 23:09:52 +00:00
|
|
|
|
var track = this.DbContext.Tracks
|
|
|
|
|
.Include(x => x.ReleaseMedia)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release)
|
|
|
|
|
.Include(x => x.ReleaseMedia.Release.Artist)
|
|
|
|
|
.Include(x => x.TrackArtist)
|
|
|
|
|
.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
|
|
|
|
};
|
2019-01-02 02:03:17 +00:00
|
|
|
|
row.Track = TrackList.FromDataTrack(this.MakeTrackPlayUrl(user, track.Id, track.RoadieId),
|
|
|
|
|
track,
|
2018-12-11 23:09:52 +00:00
|
|
|
|
track.ReleaseMedia.MediaNumber,
|
|
|
|
|
track.ReleaseMedia.Release,
|
|
|
|
|
track.ReleaseMedia.Release.Artist,
|
|
|
|
|
track.TrackArtist,
|
|
|
|
|
this.HttpContext.BaseUrl,
|
|
|
|
|
this.MakeTrackThumbnailImage(track.RoadieId),
|
|
|
|
|
this.MakeReleaseThumbnailImage(track.ReleaseMedia.Release.RoadieId),
|
|
|
|
|
this.MakeArtistThumbnailImage(track.ReleaseMedia.Release.Artist.RoadieId),
|
|
|
|
|
this.MakeArtistThumbnailImage(track.TrackArtist == null ? null : (Guid?)track.TrackArtist.RoadieId));
|
2019-01-02 02:03:17 +00:00
|
|
|
|
row.Track.TrackPlayUrl = this.MakeTrackPlayUrl(user, track.Id, track.RoadieId);
|
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-11 23:09:52 +00:00
|
|
|
|
var playlist = this.DbContext.Playlists
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.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-11 23:09:52 +00:00
|
|
|
|
row.Playlist = models.Playlists.PlaylistList.FromDataPlaylist(playlist, playlist.User, this.MakePlaylistThumbnailImage(playlist.RoadieId), this.MakeUserThumbnailImage(playlist.User.RoadieId));
|
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-11 23:09:52 +00:00
|
|
|
|
row.Collection = models.Collections.CollectionList.FromDataCollection(collection, (from crc in this.DbContext.CollectionReleases
|
|
|
|
|
where crc.CollectionId == collection.Id
|
|
|
|
|
select crc.Id).Count(), this.MakeCollectionThumbnailImage(collection.RoadieId));
|
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-11 23:09:52 +00:00
|
|
|
|
row.Label = models.LabelList.FromDataLabel(label, this.MakeLabelThumbnailImage(label.RoadieId));
|
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();
|
2018-12-24 19:40:49 +00:00
|
|
|
|
return Task.FromResult(new Library.Models.Pagination.PagedResult<BookmarkList>
|
|
|
|
|
{
|
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-12-24 19:40:49 +00:00
|
|
|
|
});
|
2018-11-15 04:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-16 03:37:00 +00:00
|
|
|
|
}
|