public async Task <IActionResult> PutItem(int id, Item item) { if (id != item.ItemId) { return(BadRequest()); } _context.Entry(item).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ItemExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IHttpActionResult> CreateRating(Rating model) { // Check to make sure model isn't empty if (model == null) { return(BadRequest("Your request cannot be empty.")); } // Check to see if the model is NOT valid if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Find the targeted restaurant // Checking to see if restaurant exists to create rating var restaurant = await _context.Restaurants.FindAsync(model.RestaurantId); if (restaurant == null) { return(BadRequest($"The target restaurant with the ID of {model.RestaurantId} does not exist.")); } // The restaurant isn't null, so we can successfully rate it _context.Ratings.Add(model); if (await _context.SaveChangesAsync() == 1) { return(Ok($"You rated {restaurant.Name} successfully!")); } // Means something went wrong with the saving, don't think it's the users fault return(InternalServerError()); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.CustomerId) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IHttpActionResult> CreateRating(Rating model) { if (model == null) { return(BadRequest("Your request cannot be empty.")); } // Check to see if the model is NOT valid if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Find the targeted restaurant var restaurant = await _context.Restaurants.FindAsync(model.RestaurantId); if (restaurant == null) { return(BadRequest($"The target restaurant with the ID of {model.RestaurantId} does not exist")); } //The resaurant isn't null, so we can successfully rate it _context.Ratings.Add(model); if (await _context.SaveChangesAsync() == 1) { return(Ok($"You rated {restaurant.Name} successfully!")); } return(InternalServerError()); }
public async Task <IHttpActionResult> PostRestaurant(Restaurant model) { if (model is null) { return(BadRequest("your request body cannot be empty")); } if (ModelState.IsValid) { _context.Restaurants.Add(model); await _context.SaveChangesAsync(); return(Ok("You did it")); } return(BadRequest(ModelState)); }
public async Task <IActionResult> Register(RegisterModel model) { ViewBag.email = Email(); ViewBag.role = Role(); if (ModelState.IsValid) { User user = await restaurantService.Users.FirstOrDefaultAsync(u => u.Email == model.Email); if (user == null) { user = new User { Email = model.Email, Password = model.Password }; Role userRole = await restaurantService.Roles.FirstOrDefaultAsync(r => r.Name == "user"); if (userRole != null) { user.Role = userRole; } restaurantService.Users.Add(user); await restaurantService.SaveChangesAsync(); await Authenticate(user); return(RedirectToAction("Index", "Home")); } else { ModelState.AddModelError("", "Данный емейл уже используется"); } } return(View(model)); }
public async Task <IHttpActionResult> PostRestaurant(Restaurant model) { if (model == null) { return(BadRequest("Your request body cannot be empty.")); } if (ModelState.IsValid) { _context.Restaurants.Add(model); await _context.SaveChangesAsync(); return(Ok("You created a restaurant and it was saved!")); } return(BadRequest(ModelState)); }
// CREATE (POST) public async Task <IHttpActionResult> PostRestaurant(Restaurant model) { if (model == null) { return(BadRequest("Your request body cannot be empty.")); } // check to see if valid model based on criteria in restaurant class if (ModelState.IsValid) { // save it to DBContext _context.Restaurants.Add(model); // save all changes to DB await _context.SaveChangesAsync(); // return 200 response return(Ok("You created a restaurant and it was saved!")); } // if model is not valid, return bad request return(BadRequest(ModelState)); }