protected async Task <OperationResult <short> > SetArtistRating(Guid artistId, ApplicationUser user, short rating) { var artist = DbContext.Artists .Include(x => x.Genres) .Include("Genres.Genre") .FirstOrDefault(x => x.RoadieId == artistId); if (artist == null) { return(new OperationResult <short>(true, $"Invalid Artist Id [{artistId}]")); } var now = DateTime.UtcNow; var userArtist = DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id); if (userArtist == null) { userArtist = new data.UserArtist { Rating = rating, UserId = user.Id, ArtistId = artist.Id }; DbContext.UserArtists.Add(userArtist); } else { userArtist.Rating = rating; userArtist.LastUpdated = now; } await DbContext.SaveChangesAsync(); var ratings = DbContext.UserArtists.Where(x => x.ArtistId == artist.Id && x.Rating > 0) .Select(x => x.Rating); if (ratings != null && ratings.Any()) { artist.Rating = (short)ratings.Average(x => (decimal)x); } else { artist.Rating = 0; } artist.LastUpdated = now; await DbContext.SaveChangesAsync(); await UpdateArtistRank(artist.Id); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(artist.CacheRegion); artist = GetArtist(artistId); return(new OperationResult <short> { IsSuccess = true, Data = artist.Rating ?? 0 }); }
protected async Task <OperationResult <bool> > ToggleArtistFavorite(Guid artistId, User user, bool isFavorite) { var result = false; try { var artist = DbContext.Artists .Include(x => x.Genres) .Include("Genres.Genre") .FirstOrDefault(x => x.RoadieId == artistId); if (artist == null) { return(new OperationResult <bool>(true, $"Invalid Artist Id [{artistId}]")); } var userArtist = DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id); if (userArtist == null) { userArtist = new data.UserArtist { IsFavorite = isFavorite, UserId = user.Id, ArtistId = artist.Id }; DbContext.UserArtists.Add(userArtist); } else { userArtist.IsFavorite = isFavorite; userArtist.LastUpdated = DateTime.UtcNow; } await DbContext.SaveChangesAsync(); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(artist.CacheRegion); result = true; } catch (DbException ex) { Logger.LogError($"ToggleArtistFavorite DbException [{ ex }]"); } return(new OperationResult <bool> { IsSuccess = result, Data = result }); }
protected async Task <OperationResult <bool> > ToggleArtistDisliked(Guid artistId, ApplicationUser user, bool isDisliked) { var artist = DbContext.Artists .Include(x => x.Genres) .Include("Genres.Genre") .FirstOrDefault(x => x.RoadieId == artistId); if (artist == null) { return(new OperationResult <bool>(true, $"Invalid Artist Id [{artistId}]")); } var userArtist = DbContext.UserArtists.FirstOrDefault(x => x.ArtistId == artist.Id && x.UserId == user.Id); if (userArtist == null) { userArtist = new data.UserArtist { IsDisliked = isDisliked, UserId = user.Id, ArtistId = artist.Id }; DbContext.UserArtists.Add(userArtist); } else { userArtist.IsDisliked = isDisliked; userArtist.LastUpdated = DateTime.UtcNow; } await DbContext.SaveChangesAsync(); CacheManager.ClearRegion(user.CacheRegion); CacheManager.ClearRegion(artist.CacheRegion); return(new OperationResult <bool> { IsSuccess = true, Data = true }); }