protected async Task <OperationResult <bool> > ToggleReleaseFavorite(Guid releaseId, User user, bool isFavorite) { var result = false; try { var release = DbContext.Releases .Include(x => x.Artist) .Include(x => x.Genres) .Include("Genres.Genre") .Include(x => x.Medias) .Include("Medias.Tracks") .Include("Medias.Tracks.TrackArtist") .FirstOrDefault(x => x.RoadieId == releaseId); if (release == null) { return(new OperationResult <bool>(true, $"Invalid Release Id [{releaseId}]")); } var userRelease = DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id); if (userRelease == null) { userRelease = new data.UserRelease { IsFavorite = isFavorite, UserId = user.Id, ReleaseId = release.Id }; DbContext.UserReleases.Add(userRelease); } else { userRelease.IsFavorite = isFavorite; userRelease.LastUpdated = DateTime.UtcNow; } await DbContext.SaveChangesAsync(); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(release.CacheRegion); CacheManager.ClearRegion(release.Artist.CacheRegion); result = true; } catch (DbException ex) { Logger.LogError($"ToggleReleaseFavorite DbException [{ ex }]"); } return(new OperationResult <bool> { IsSuccess = result, Data = result }); }
protected async Task <OperationResult <bool> > ToggleReleaseDisliked(Guid releaseId, ApplicationUser user, bool isDisliked) { var release = DbContext.Releases .Include(x => x.Artist) .Include(x => x.Genres) .Include("Genres.Genre") .Include(x => x.Medias) .Include("Medias.Tracks") .Include("Medias.Tracks.TrackArtist") .FirstOrDefault(x => x.RoadieId == releaseId); if (release == null) { return(new OperationResult <bool>(true, $"Invalid Release Id [{releaseId}]")); } var userRelease = DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id); if (userRelease == null) { userRelease = new data.UserRelease { IsDisliked = isDisliked, UserId = user.Id, ReleaseId = release.Id }; DbContext.UserReleases.Add(userRelease); } else { userRelease.IsDisliked = isDisliked; userRelease.LastUpdated = DateTime.UtcNow; } await DbContext.SaveChangesAsync(); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(release.CacheRegion); CacheManager.ClearRegion(release.Artist.CacheRegion); return(new OperationResult <bool> { IsSuccess = true, Data = true }); }
protected async Task <OperationResult <short> > SetReleaseRating(Guid releaseId, ApplicationUser user, short rating) { var release = DbContext.Releases .Include(x => x.Artist) .Include(x => x.Genres) .Include("Genres.Genre") .Include(x => x.Medias) .Include("Medias.Tracks") .Include("Medias.Tracks.TrackArtist") .FirstOrDefault(x => x.RoadieId == releaseId); if (release == null) { return(new OperationResult <short>(true, $"Invalid Release Id [{releaseId}]")); } var userRelease = DbContext.UserReleases.FirstOrDefault(x => x.ReleaseId == release.Id && x.UserId == user.Id); var now = DateTime.UtcNow; if (userRelease == null) { userRelease = new data.UserRelease { Rating = rating, UserId = user.Id, ReleaseId = release.Id }; DbContext.UserReleases.Add(userRelease); } else { userRelease.Rating = rating; userRelease.LastUpdated = now; } await DbContext.SaveChangesAsync(); var ratings = DbContext.UserReleases.Where(x => x.ReleaseId == release.Id && x.Rating > 0) .Select(x => x.Rating); if (ratings != null && ratings.Any()) { release.Rating = (short)ratings.Average(x => (decimal)x); } else { release.Rating = 0; } release.LastUpdated = now; await DbContext.SaveChangesAsync(); await UpdateReleaseRank(release.Id); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(release.CacheRegion); CacheManager.ClearRegion(release.Artist.CacheRegion); release = GetRelease(releaseId); return(new OperationResult <short> { IsSuccess = true, Data = release.Rating ?? 0 }); }