/// <summary> /// Adds or removes label from anime /// </summary> /// <returns>true if label was added, false if label was removed</returns> private bool LabelAnime(int animeId, AnimeLabel label) { var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <ApplicationDbContext>(); AppUser currentUserFromManager = _userManager.GetUserAsync(User).GetAwaiter().GetResult(); var currentUser = dbContext.Users.FirstOrDefault(x => x.Id == currentUserFromManager.Id) as AppUser; var anime = _jikanService.GetAnime(animeId); dbContext.Animes.AddIfNotExists(anime); var foundLabel = dbContext.UserAnimeLabels.FirstOrDefault(x => x.UserId == currentUser.Id && x.AnimeId == animeId && x.LabelId == label.Id); bool flag = foundLabel == null; if (flag) { dbContext.UserAnimeLabels.Add(new UserAnimeLabel { User = currentUser, AnimeId = animeId, LabelId = label.Id }); } else { dbContext.UserAnimeLabels.Remove(foundLabel); } dbContext.SaveChanges(); return(flag); }
public JsonResult DislikeAnime(int animeId) { var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetRequiredService <ApplicationDbContext>(); try { AnimeLabel likeLabel = dbContext.AnimeLabels.First(x => x.Name == "Disliked"); bool added = LabelAnime(animeId, likeLabel); return(new JsonResult(new { success = true, added = added })); } catch (Exception e) { return(new JsonResult(new { success = false, error = e.Message })); } }