public string RemoveFavRest(int userid, int restid) //only from header of api since the user should only press a button to 'unsubscribe' { try { var Restaurant = db.Restaurant.Find(restid); // If restaurant does not exist if (Restaurant == null) { return("could not find the restaurant"); } FavoritesRestaurants favoritesRestaurant = db.FavoritesRestaurants.Where(x => x.Restaurant_id == restid && x.User_id == userid).FirstOrDefault(); db.FavoritesRestaurants.Attach(favoritesRestaurant); //telling db what kind of object to delete db.Entry(favoritesRestaurant).State = EntityState.Deleted; //db deletes it db.SaveChanges(); //save db return("Success"); } catch (Exception e) { return("exception " + e.Message); } }
public string AddFavRest(int userid, int restid) //Ad, with GET, Favourite restaurant to db. Since user sho uld only press an icon to add. { try { User user = db.User.Find(userid); //find user with logged in userid sent through header of api var Restaurant = db.Restaurant.Find(restid); //find restaurant with restaurantID sent through header of api FavoritesRestaurants favoritesRestaurant = new FavoritesRestaurants(); //specify the new favouriterestaurant with an object to send to dbo.FavouriteRestaurant favoritesRestaurant.Restaurant_id = Restaurant.id; favoritesRestaurant.User_id = user.id; db.FavoritesRestaurants.Add(favoritesRestaurant); //add to db db.SaveChanges(); return("Success"); } catch (Exception e) { return("exception " + e.Message); } }