public async Task <IActionResult> DeleteFavorite(string id) { try { if (!Guid.TryParse(id, out Guid favoriteId)) { throw new GuidException("Invalid id", this.GetType().Name, "DeleteFavorite", "400"); } await _favoriteRepository.DeleteFavorite(id); return(NoContent()); } catch (MovieMindException e) { if (e.MovieMindError.Status.Equals("404")) { return(NotFound(e.MovieMindError)); } else { return(BadRequest(e.MovieMindError)); } } }
/// <summary> /// Process returns a JSON response list of folders /// </summary> public void Process(UriWrapper uri, IHttpProcessor processor, User user) { // Shortcut to favorites repository IFavoriteRepository favoriteRepository = Injection.Kernel.Get <IFavoriteRepository>(); // Lists of favorites and items associated IList <IItem> items = new List <IItem>(); IList <Favorite> favorites = new List <Favorite>(); // If no action specified, read favorites if (uri.Action == null || uri.Action == "read") { // Get this users's favorites favorites = favoriteRepository.FavoritesForUserId((int)user.UserId); // Get the items associated with their favorites items = favoriteRepository.ItemsForFavorites(favorites); // Send response processor.WriteJson(new FavoritesResponse(null, items, favorites)); return; } // Verify ID present for remaining actions if (uri.Id == null) { processor.WriteJson(new FavoritesResponse("ID required for modifying favorites", null, null)); return; } // create - add favorites if (uri.Action == "create") { favoriteRepository.AddFavorite((int)user.UserId, (int)uri.Id, null); processor.WriteJson(new FavoritesResponse(null, items, favorites)); return; } // delete - remove favorites if (uri.Action == "delete") { // Grab favorite to delete, verify its ownership Favorite fav = favoriteRepository.FavoriteForId((int)uri.Id); if (fav.FavoriteUserId != user.UserId) { processor.WriteJson(new FavoritesResponse("Cannot delete another user's favorite", null, null)); return; } // Remove favorite favoriteRepository.DeleteFavorite((int)uri.Id); processor.WriteJson(new FavoritesResponse(null, items, favorites)); return; } // Invalid action processor.WriteJson(new FavoritesResponse("Invalid action specified: " + uri.Action, null, null)); return; }
public IActionResult DeleteFavoriteForUser(Guid userId, Guid carparkId) { if (!_userRepository.UserExists(userId)) { return(NotFound(new { message = $"User {userId} does not exist" })); } var favoriteForUserFromRepo = _favoriteRepository.GetFavoriteForUser(userId, carparkId); if (favoriteForUserFromRepo == null) { return(NotFound(new { message = $"Favorite carpark {carparkId} for User {userId} does not exist" })); } _favoriteRepository.DeleteFavorite(favoriteForUserFromRepo); if (!_favoriteRepository.Save()) { throw new Exception($"Deleting favorite carpark {carparkId} for user {userId} failed on save"); } return(NoContent()); }
public async Task DeleteFavorite(Favorite favorite) { await _favoriteRepository.DeleteFavorite(favorite); }