Api Work for roadie-vuejs

This commit is contained in:
Steven Hildreth 2018-12-01 12:05:24 -06:00
parent df6cfe6fe4
commit 6c3d92d49d
12 changed files with 204 additions and 132 deletions

View file

@ -55,10 +55,11 @@ namespace Roadie.Api.Controllers
[HttpGet]
[ProducesResponseType(200)]
public async Task<IActionResult> List([FromQuery]PagedRequest request)
public async Task<IActionResult> List([FromQuery]PagedRequest request, string inc, bool? doRandomize = false)
{
var result = await this.ArtistService.List(roadieUser: await this.CurrentUserModel(),
request: request);
request: request,
doRandomize: doRandomize ?? false);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);

View file

@ -67,18 +67,5 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost("random")]
[ProducesResponseType(200)]
public async Task<IActionResult> RandomList(PagedRequest request)
{
var result = await this.ReleaseService.List(user: await this.CurrentUserModel(),
request: request,
doRandomize: true);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
}
}

View file

@ -94,6 +94,29 @@ namespace Roadie.Api.Controllers
return Ok(result);
}
[HttpPost("setArtistFavorite/{artistId}/{isFavorite}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetArtistFavorite(Guid artistId, bool isFavorite)
{
var result = await this.UserService.SetArtistFavorite(artistId, await this.CurrentUserModel(), isFavorite);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpPost("setReleaseFavorite/{releaseId}/{isFavorite}")]
[ProducesResponseType(200)]
public async Task<IActionResult> SetReleaseFavorite(Guid releaseId, bool isFavorite)
{
var result = await this.UserService.SetReleaseFavorite(releaseId, await this.CurrentUserModel(), isFavorite);
if (!result.IsSuccess)
{
return StatusCode((int)HttpStatusCode.InternalServerError);
}
return Ok(result);
}
[HttpGet]

View file

@ -308,7 +308,7 @@ namespace Roadie.Api.Services
};
}
public async Task<Library.Models.Pagination.PagedResult<ArtistList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, bool? onlyIncludeWithReleases = true, bool? doArtistCounts = true)
public async Task<Library.Models.Pagination.PagedResult<ArtistList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, bool? onlyIncludeWithReleases = true)
{
var sw = new Stopwatch();
sw.Start();
@ -319,6 +319,7 @@ namespace Roadie.Api.Services
favoriteArtistIds = (from a in this.DbContext.Artists
join ua in this.DbContext.UserArtists on a.Id equals ua.ArtistId
where ua.IsFavorite ?? false
where (roadieUser == null || ua.UserId == roadieUser.Id)
select a.Id
).ToArray();
}
@ -342,9 +343,10 @@ namespace Roadie.Api.Services
Rating = a.Rating,
CreatedDate = a.CreatedDate,
LastUpdated = a.LastUpdated,
ArtistPlayedCount = 0,
ArtistReleaseCount = 0,
ArtistTrackCount = 0,
LastPlayed = a.LastPlayed,
PlayedCount = a.PlayedCount,
ReleaseCount = a.ReleaseCount,
TrackCount = a.TrackCount,
SortName = a.SortName
}).Distinct();
@ -352,7 +354,9 @@ namespace Roadie.Api.Services
var rowCount = result.Count();
if (doRandomize ?? false)
{
request.Limit = request.LimitValue > roadieUser.RandomReleaseLimit ? roadieUser.RandomReleaseLimit : request.LimitValue;
var randomLimit = roadieUser?.RandomReleaseLimit ?? 100;
request.Limit = request.LimitValue > randomLimit ? randomLimit : request.LimitValue;
rows = result.OrderBy(x => Guid.NewGuid()).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
else
@ -368,27 +372,6 @@ namespace Roadie.Api.Services
}
rows = result.OrderBy(sortBy).Skip(request.SkipValue).Take(request.LimitValue).ToArray();
}
if(rows.Any() && (doArtistCounts ?? true))
{
var rowArtistIds = rows.Select(x => x.DatabaseId).ToArray();
var artistReleases = (from a in this.DbContext.Artists
join r in this.DbContext.Releases on a.Id equals r.ArtistId
where rowArtistIds.Contains(a.Id)
select new
{
artistId = a.Id,
releaseId = r.Id,
r.TrackCount,
r.PlayedCount
}).ToArray();
foreach(var row in rows)
{
var rowArtistReleases = artistReleases.Where(r => r.artistId == row.DatabaseId);
row.ArtistReleaseCount = rowArtistReleases.Select(r => r.releaseId).Count();
row.ArtistTrackCount = rowArtistReleases.Sum(r => r.TrackCount);
row.ArtistPlayedCount = rowArtistReleases.Sum(r => r.PlayedCount);
}
}
if (rows.Any() && roadieUser != null)
{
foreach (var userArtistRating in this.GetUser(roadieUser.UserId).ArtistRatings.Where(x => rows.Select(r => r.DatabaseId).Contains(x.ArtistId)))

View file

@ -12,6 +12,6 @@ namespace Roadie.Api.Services
{
Task<OperationResult<Artist>> ById(User roadieUser, Guid id, IEnumerable<string> includes);
Task<PagedResult<ArtistList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, bool? onlyIncludeWithReleases = true, bool? doArtistCounts = true);
Task<PagedResult<ArtistList>> List(User roadieUser, PagedRequest request, bool? doRandomize = false, bool? onlyIncludeWithReleases = true);
}
}

View file

@ -13,5 +13,7 @@ namespace Roadie.Api.Services
Task<OperationResult<short>> SetReleaseRating(Guid releaseId, User roadieUser, short rating);
Task<OperationResult<short>> SetArtistRating(Guid artistId, User roadieUser, short rating);
Task<OperationResult<short>> SetTrackRating(Guid trackId, User roadieUser, short rating);
Task<OperationResult<bool>> SetArtistFavorite(Guid artistId, User roadieUser, bool isFavorite);
Task<OperationResult<bool>> SetReleaseFavorite(Guid releaseId, User roadieUser, bool isFavorite);
}
}

View file

@ -153,6 +153,10 @@ namespace Roadie.Api.Services
release.LastPlayed = now;
release.PlayedCount++;
var artist = this.GetArtist(release.Artist.RoadieId);
artist.LastPlayed = now;
artist.PlayedCount++;
this.CacheManager.ClearRegion(track.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);

View file

@ -227,7 +227,7 @@ namespace Roadie.Api.Services
{
return new OperationResult<short>(true, $"Invalid Artist Id [{ artistId }]");
}
var userArtist = user.ArtistRatings.FirstOrDefault(x => x.ArtistId == artist.Id);
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id);
if (userArtist == null)
{
userArtist = new data.UserArtist
@ -270,7 +270,7 @@ namespace Roadie.Api.Services
{
return new OperationResult<short>(true, $"Invalid Release Id [{ releaseId }]");
}
var userRelease = user.ReleaseRatings.FirstOrDefault(x => x.ReleaseId == release.Id);
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id);
var now = DateTime.UtcNow;
if (userRelease == null)
{
@ -315,7 +315,7 @@ namespace Roadie.Api.Services
{
return new OperationResult<short>(true, $"Invalid Track Id [{ trackId }]");
}
var userTrack = user.TrackRatings.FirstOrDefault(x => x.TrackId == track.Id);
var userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.TrackId == track.Id && x.UserId == user.Id);
if (userTrack == null)
{
userTrack = new data.UserTrack
@ -353,6 +353,115 @@ namespace Roadie.Api.Services
};
}
protected async Task<OperationResult<bool>> ToggleArtistFavorite(Guid artistId, ApplicationUser user, bool isFavorite)
{
var artist = this.GetArtist(artistId);
if (artist == null)
{
return new OperationResult<bool>(true, $"Invalid Artist Id [{ artistId }]");
}
var userArtist = this.DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id);
if (userArtist == null)
{
userArtist = new data.UserArtist
{
IsFavorite = true,
UserId = user.Id,
ArtistId = artist.Id
};
this.DbContext.UserArtists.Add(userArtist);
}
else
{
userArtist.IsFavorite = isFavorite;
userArtist.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(artist.CacheRegion);
return new OperationResult<bool>
{
IsSuccess = true,
Data = true
};
}
protected async Task<OperationResult<bool>> ToggleReleaseFavorite(Guid releaseId, ApplicationUser user, bool isFavorite)
{
var release = this.GetRelease(releaseId);
if (release == null)
{
return new OperationResult<bool>(true, $"Invalid Release Id [{ releaseId }]");
}
var userRelease = this.DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id);
if (userRelease == null)
{
userRelease = new data.UserRelease
{
IsFavorite = true,
UserId = user.Id,
ReleaseId = release.Id
};
this.DbContext.UserReleases.Add(userRelease);
}
else
{
userRelease.IsFavorite = isFavorite;
userRelease.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(release.CacheRegion);
this.CacheManager.ClearRegion(release.Artist.CacheRegion);
return new OperationResult<bool>
{
IsSuccess = true,
Data = true
};
}
protected async Task<OperationResult<bool>> ToggleTrackFavorite(Guid trackId, ApplicationUser user, bool isFavorite)
{
var track = this.GetTrack(trackId);
if (track == null)
{
return new OperationResult<bool>(true, $"Invalid Track Id [{ trackId }]");
}
var userTrack = this.DbContext.UserTracks.FirstOrDefault(x => x.TrackId == track.Id && x.UserId == user.Id);
if (userTrack == null)
{
userTrack = new data.UserTrack
{
IsFavorite = true,
UserId = user.Id,
TrackId = track.Id
};
this.DbContext.UserTracks.Add(userTrack);
}
else
{
userTrack.IsFavorite = isFavorite;
userTrack.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(track.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
return new OperationResult<bool>
{
IsSuccess = true,
Data = true
};
}
protected Image MakeArtistThumbnailImage(Guid id)

View file

@ -1818,8 +1818,7 @@ namespace Roadie.Api.Services
var artistList = await this.ArtistService.List(roadieUser: roadieUser,
request: pagedRequest,
doRandomize: false,
onlyIncludeWithReleases: true,
doArtistCounts: true);
onlyIncludeWithReleases: true);
foreach (var artistGroup in artistList.Rows.GroupBy(x => x.Artist.Text.Substring(0, 1)))
{
indexes.Add(new subsonic.IndexID3
@ -1900,8 +1899,7 @@ namespace Roadie.Api.Services
var artistList = await this.ArtistService.List(roadieUser: roadieUser,
request: pagedRequest,
doRandomize: false,
onlyIncludeWithReleases: true,
doArtistCounts: false);
onlyIncludeWithReleases: true);
foreach (var artistGroup in artistList.Rows.GroupBy(x => x.Artist.Text.Substring(0, 1)))
{
indexes.Add(new subsonic.Index
@ -2038,7 +2036,7 @@ namespace Roadie.Api.Services
{
id = subsonic.Request.ArtistIdIdentifier + artist.Artist.Value.ToString(),
name = artist.Artist.Text,
albumCount = artist.ArtistReleaseCount ?? 0,
albumCount = artist.ReleaseCount ?? 0,
coverArt = artistImageUrl,
artistImageUrl = artistImageUrl,
starred = artist.UserRating?.RatedDate ?? DateTime.UtcNow,
@ -2296,109 +2294,43 @@ namespace Roadie.Api.Services
private async Task<subsonic.SubsonicOperationResult<bool>> ToggleArtistStar(Guid artistId, ApplicationUser user, bool starred)
{
var artist = this.GetArtist(artistId);
if (artist == null)
var r = await base.ToggleArtistFavorite(artistId, user, starred);
if (r.IsNotFoundResult)
{
return new subsonic.SubsonicOperationResult<bool>(subsonic.ErrorCodes.TheRequestedDataWasNotFound, $"Invalid Artist Id [{ artistId }]");
}
var userArtist = user.ArtistRatings.FirstOrDefault(x => x.ArtistId == artist.Id);
if (userArtist == null)
{
userArtist = new data.UserArtist
{
IsFavorite = true,
UserId = user.Id,
ArtistId = artist.Id
};
this.DbContext.UserArtists.Add(userArtist);
}
else
{
userArtist.IsFavorite = starred;
userArtist.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(artist.CacheRegion);
return new subsonic.SubsonicOperationResult<bool>
{
IsSuccess = true,
Data = true
IsSuccess = r.IsSuccess,
Data = r.IsSuccess
};
}
private async Task<subsonic.SubsonicOperationResult<bool>> ToggleReleaseStar(Guid releaseId, ApplicationUser user, bool starred)
{
var release = this.GetRelease(releaseId);
if (release == null)
var r = await base.ToggleReleaseFavorite(releaseId, user, starred);
if (r.IsNotFoundResult)
{
return new subsonic.SubsonicOperationResult<bool>(subsonic.ErrorCodes.TheRequestedDataWasNotFound, $"Invalid Release Id [{ releaseId }]");
}
var userRelease = user.ReleaseRatings.FirstOrDefault(x => x.ReleaseId == release.Id);
if (userRelease == null)
{
userRelease = new data.UserRelease
{
IsFavorite = true,
UserId = user.Id,
ReleaseId = release.Id
};
this.DbContext.UserReleases.Add(userRelease);
}
else
{
userRelease.IsFavorite = starred;
userRelease.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(release.CacheRegion);
this.CacheManager.ClearRegion(release.Artist.CacheRegion);
return new subsonic.SubsonicOperationResult<bool>
{
IsSuccess = true,
Data = true
IsSuccess = r.IsSuccess,
Data = r.IsSuccess
};
}
private async Task<subsonic.SubsonicOperationResult<bool>> ToggleTrackStar(Guid trackId, ApplicationUser user, bool starred)
{
var track = this.GetTrack(trackId);
if (track == null)
var r = await base.ToggleTrackFavorite(trackId, user, starred);
if (r.IsNotFoundResult)
{
return new subsonic.SubsonicOperationResult<bool>(subsonic.ErrorCodes.TheRequestedDataWasNotFound, $"Invalid Track Id [{ trackId }]");
}
var userTrack = user.TrackRatings.FirstOrDefault(x => x.TrackId == track.Id);
if (userTrack == null)
{
userTrack = new data.UserTrack
{
IsFavorite = true,
UserId = user.Id,
TrackId = track.Id
};
this.DbContext.UserTracks.Add(userTrack);
}
else
{
userTrack.IsFavorite = starred;
userTrack.LastUpdated = DateTime.UtcNow;
}
await this.DbContext.SaveChangesAsync();
this.CacheManager.ClearRegion(user.CacheRegion);
this.CacheManager.ClearRegion(track.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.CacheRegion);
this.CacheManager.ClearRegion(track.ReleaseMedia.Release.Artist.CacheRegion);
return new subsonic.SubsonicOperationResult<bool>
{
IsSuccess = true,
Data = true
IsSuccess = r.IsSuccess,
Data = r.IsSuccess
};
}

View file

@ -182,5 +182,27 @@ namespace Roadie.Api.Services
}
return await base.SetTrackRating(trackId, user, rating);
}
public async Task<OperationResult<bool>> SetArtistFavorite(Guid artistId, User roadieUser, bool isFavorite)
{
var user = this.GetUser(roadieUser.UserId);
if (user == null)
{
return new OperationResult<bool>(true, $"Invalid User [{ roadieUser }]");
}
return await base.ToggleArtistFavorite(artistId, user, isFavorite);
}
public async Task<OperationResult<bool>> SetReleaseFavorite(Guid releaseId, User roadieUser, bool isFavorite)
{
var user = this.GetUser(roadieUser.UserId);
if (user == null)
{
return new OperationResult<bool>(true, $"Invalid User [{ roadieUser }]");
}
return await base.ToggleReleaseFavorite(releaseId, user, isFavorite);
}
}
}

View file

@ -45,6 +45,9 @@ namespace Roadie.Library.Data
[MaxLength(100)]
public string ITunesId { get; set; }
[Column("lastPlayed")]
public DateTime? LastPlayed { get; set; }
[Column("musicBrainzId")]
[MaxLength(100)]
public string MusicBrainzId { get; set; }
@ -53,6 +56,9 @@ namespace Roadie.Library.Data
[MaxLength(65535)]
public string Profile { get; set; }
[Column("playedCount")]
public int? PlayedCount { get; set; }
[Column("rating")]
public short? Rating { get; set; }
@ -69,5 +75,8 @@ namespace Roadie.Library.Data
[Column("releaseCount")]
public int? ReleaseCount { get; set; } // TODO update this on artist folder scan
[Column("trackCount")]
public int? TrackCount { get; set; } // TODO update this on artist folder scane
}
}

View file

@ -11,10 +11,10 @@ namespace Roadie.Library.Models
public DataToken Artist { get; set; }
public short? Rating { get; set; }
public UserArtist UserRating { get; set; }
public int? ArtistReleaseCount { get; set; }
public int? ArtistTrackCount { get; set; }
public int? ArtistPlayedCount { get; set; }
public int? ReleaseCount { get; set; }
public int? TrackCount { get; set; }
public int? PlayedCount { get; set; }
public Image Thumbnail { get; set; }
public DateTime? LastPlayed { get; set; }
}
}