roadie/Roadie.Api.Services/BookmarkService.cs

174 lines
9.6 KiB
C#
Raw Normal View History

2018-12-11 23:09:52 +00:00
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
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;
namespace Roadie.Api.Services
{
public class BookmarkService : ServiceBase, IBookmarkService
{
public BookmarkService(IRoadieSettings configuration,
IHttpEncoder httpEncoder,
IHttpContext httpContext,
data.IRoadieDbContext context,
ICacheManager cacheManager,
ILogger<BookmarkService> logger)
: 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)
{
var sw = new Stopwatch();
sw.Start();
var result = (from b in this.DbContext.Bookmarks
join u in this.DbContext.Users on b.UserId equals u.Id
where b.UserId == roadieUser.Id
where (filterType == null || b.BookmarkType == filterType)
select new BookmarkList
{
Comment = b.Comment,
Position = b.Position,
User = new DataToken
{
Text = u.UserName,
Value = u.RoadieId.ToString()
},
DatabaseId = b.Id,
Id = b.RoadieId,
CreatedDate = b.CreatedDate,
LastUpdated = b.LastUpdated,
Type = b.BookmarkType,
BookmarkTargetId = b.BookmarkTargetId
});
var sortBy = string.IsNullOrEmpty(request.Sort) ? request.OrderValue(new Dictionary<string, string> { { "CreatedDate", "DESC" } }) : request.OrderValue(null);
var rowCount = result.Count();
BookmarkList[] rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
2019-01-02 02:03:17 +00:00
var user = this.GetUser(roadieUser.UserId);
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);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = artist.Name,
Value = artist.RoadieId.ToString()
};
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;
break;
2018-11-16 03:37:00 +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);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = release.Title,
Value = release.RoadieId.ToString()
};
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;
break;
2018-11-16 03:37:00 +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);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = track.Title,
Value = track.RoadieId.ToString()
};
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;
break;
2018-11-16 03:37:00 +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);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = playlist.Name,
Value = playlist.RoadieId.ToString()
};
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;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Collection:
2018-12-05 16:04:21 +00:00
var collection = this.DbContext.Collections.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = collection.Name,
Value = collection.RoadieId.ToString()
};
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;
break;
2018-11-16 03:37:00 +00:00
case BookmarkType.Label:
2018-12-05 16:04:21 +00:00
var label = this.DbContext.Labels.FirstOrDefault(x => x.Id == row.BookmarkTargetId);
row.Bookmark = new DataToken
{
2018-12-05 16:04:21 +00:00
Text = label.Name,
Value = label.RoadieId.ToString()
};
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;
break;
}
};
sw.Stop();
2018-12-24 19:40:49 +00:00
return Task.FromResult(new Library.Models.Pagination.PagedResult<BookmarkList>
{
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-16 03:37:00 +00:00
}