roadie/RoadieApi/Services/BookmarkService.cs

146 lines
6.7 KiB
C#
Raw Normal View History

2018-11-16 03:37:00 +00:00
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;
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,
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)
{
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();
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-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-05 16:04:21 +00:00
var release = this.DbContext.Releases.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-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-05 16:04:21 +00:00
var track = this.DbContext.Tracks.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()
};
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-05 16:04:21 +00:00
var playlist = this.DbContext.Playlists.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-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-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-05 16:04:21 +00:00
row.Thumbnail = this.MakeLabelThumbnailImage(label.RoadieId);
row.SortName = label.SortName ?? label.Name;
break;
}
};
sw.Stop();
return 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-11-16 03:37:00 +00:00
}