Пример #1
0
        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());
        }
Пример #2
0
        public async Task MakeOrder(List <OrderDto> order, int session)
        {
            float totalFee = 0;

            foreach (var item in order)
            {
                totalFee += (item.Price * item.Quantity);
            }
            var Ses       = _context.Session.Where(x => x.SessionId == session).FirstOrDefault();
            var oldOrders = _context.Order.Where(x => x.SessionId == session).ToList();

            foreach (var item in oldOrders)
            {
                totalFee += (item.Price * item.Quantity);
            }

            foreach (var item in order)
            {
                Order orderItem = new Order
                {
                    Price       = item.Price,
                    ProductName = item.ProductName,
                    Quantity    = item.Quantity,
                    SessionId   = session,
                    Session     = Ses
                };
                _context.Order.Add(orderItem);
            }
            Ses.TotalFee = totalFee;
            _context.Session.Update(Ses);
            await _context.SaveChangesAsync();
        }
        public async Task <IHttpActionResult> CreateRating(Rating model)
        {
            if (model == null)
            {
                return(BadRequest("Your request body cannot be empty."));
            }
            // Check to see if the model is NOT valid, bang makes it "If it is not modelState.IsValid, so checks if false
            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 restaurant isn't null, so we can successfully rate it
            _context.Ratings.Add(model);
            // Check to make sure it updated
            if (await _context.SaveChangesAsync() == 1)
            {
                return(Ok($"You rated {restaurant.Name} successfully!"));
            }
            // Backup error message in case the above fails
            return(InternalServerError());
        }
Пример #4
0
 public async Task DeleteAsync(int id)
 {
     _context.Restaurant.Remove(new Restaurant {
         ID = id
     });
     await _context.SaveChangesAsync();
 }
Пример #5
0
        public async System.Threading.Tasks.Task <T> Add(T entity)
        {
            _ctx.Set <T>().Add(entity);
            await _ctx.SaveChangesAsync();

            return(entity);
        }
Пример #6
0
        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 target restaurant
            var restaurant = await _context.Restaurants.FindAsync(model.RestaurantId);

            if (restaurant == null)
            {
                return(BadRequest($"The target restaurant with ID {model.RestaurantId} does not exist."));
            }

            // The restaurant isn't null so we can rate it
            _context.Ratings.Add(model);
            if (await _context.SaveChangesAsync() == 1)
            {
                return(Ok($"You rated {restaurant.Name} successfully!"));
            }

            return(InternalServerError());
        }
        public async Task<IActionResult> PutRestaurant(int id, Restaurant restaurant)
        {
            if (id != restaurant.Id)
            {
                return BadRequest();
            }

            _context.Entry(restaurant).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RestaurantExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }
Пример #8
0
        public async Task <IActionResult> PutCommandeM(long id, CommandeM commandeM)
        {
            if (id != commandeM.CommandeMId)
            {
                return(BadRequest());
            }

            _context.Entry(commandeM).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CommandeMExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutFoodItem(int id, FoodItem foodItem)
        {
            if (id != foodItem.FoodItemId)
            {
                return(BadRequest());
            }

            _context.Entry(foodItem).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FoodItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutRestaurant([FromRoute] int id, [FromBody] Restaurant restaurant)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != restaurant.Id)
            {
                return(BadRequest());
            }

            _context.Entry(restaurant).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RestaurantExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #11
0
        public async Task <IActionResult> PutOrderMaster(long id, OrderMaster orderMaster)
        {
            if (id != orderMaster.OrderMasterId)
            {
                return(BadRequest());
            }

            _context.Entry(orderMaster).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderMasterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #12
0
        public async void AddTable(Table masa, string userName)
        {
            var user = await _userManager.FindByNameAsync(userName);

            masa.Restaurant   = user;
            masa.RestaurantId = user.Id;
            _context.Set <Table>().Add(masa);
            var result = await _context.SaveChangesAsync();
        }
Пример #13
0
 public async Task<IHttpActionResult> PostRestaurant(Restaurant model)
 {
     if (ModelState.IsValid)
     {
         _context.Restaurants.Add(model);
         await _context.SaveChangesAsync();
         return Ok();
     }
     return BadRequest(ModelState);
 }
Пример #14
0
        public async Task DeleteAsync(int id)
        {
            Restaurant restaurant = await _context.Restaurants.FindAsync(id);

            if (restaurant != null)
            {
                _context.Remove(restaurant);
                await _context.SaveChangesAsync();
            }
        }
Пример #15
0
        public async Task <IActionResult> Create([Bind("Id,Name")] Chef chef)
        {
            if (ModelState.IsValid)
            {
                _context.Add(chef);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(chef));
        }
Пример #16
0
        public async Task <IActionResult> Create([Bind("TableCategoryId,TableCapacity")] TableCategory tableCategory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tableCategory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tableCategory));
        }
Пример #17
0
        //POST
        public async Task <IHttpActionResult> PostRestaurant(Restaurant restaurant)
        {
            if (ModelState.IsValid && restaurant != null) //ModelState is property from APIController
            {
                _context.Restaurants.Add(restaurant);     //restaurants is the name of the database table we're adding the local _context to, passing in THE restaurant to add it
                await _context.SaveChangesAsync();        //returns int of how many items were changed

                return(Ok());                             //returning http protocol response, assuming it's 200 level
            }
            return(BadRequest(ModelState));
        }
        public async Task <IHttpActionResult> PostRestaurant(Restaurant restaurant)
        {
            if (ModelState.IsValid && restaurant != null)
            {
                _context.Restaurants.Add(restaurant);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            return(BadRequest(ModelState));
        }
Пример #19
0
        public async Task <IActionResult> Create([Bind("Id,Name,Comments")] Meal meal)
        {
            if (ModelState.IsValid)
            {
                _context.Add(meal);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(meal));
        }
        public async Task <IHttpActionResult> PostRestaurant(Restaurant restaurant)
        {
            if (ModelState.IsValid && restaurant != null)
            {
                _context.Restaurants.Add(restaurant); //_context was initialized above - .Restaurants references the dbcontext we set in RestaurantDbContext
                await _context.SaveChangesAsync();    //line above added to the dbContext (dbset Restaurants) (snapshot), now we need to save that snapshot to the actual database

                return(Ok());
            }
            return(BadRequest(ModelState));
        }//as soon as we call this method for the first time, our database will be scaffolded out, up until then, all we have is a dbcontext(stagin area)
        public async Task <IActionResult> Create([Bind("TableId,TableCategoryId,IsBooked")] Table table)
        {
            if (ModelState.IsValid)
            {
                _context.Add(table);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TableCategoryId"] = new SelectList(_context.TableCategories, "TableCategoryId", "TableCategoryId", table.TableCategoryId);
            return(View(table));
        }
Пример #22
0
        public async Task <IHttpActionResult> PostRestaurant(Restaurant model)
        {
            //server side or back end validation
            if (ModelState.IsValid)
            {
                _context.Restaurants.Add(model);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            return(BadRequest(ModelState));
        }
Пример #23
0
        public async Task <IActionResult> Create([Bind("CustomerId,CustomerName,Address,PhoneNumber,EmailAddress,GroupCount")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                //return RedirectToAction(nameof(Index));
                return(RedirectToAction("IndexAvailable", "Tables"));
            }
            return(View(customer));
        }
Пример #24
0
        public async Task <IActionResult> Create([Bind("BookingId,BookingDate,CustomerId,TableId")] Booking booking)
        {
            if (ModelState.IsValid)
            {
                _context.Add(booking);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CustomerId"] = new SelectList(_context.Customers, "CustomerId", "CustomerId", booking.CustomerId);
            ViewData["TableId"]    = new SelectList(_context.Tables, "TableId", "TableId", booking.TableId);
            return(View(booking));
        }
Пример #25
0
        public async Task <bool> Add(T entity)
        {
            if (entity == null)
            {
                return(false);
            }

            await _entities.AddAsync(entity);

            var created = await RestaurantDbContext.SaveChangesAsync();

            return(created > 0);
        }
Пример #26
0
        [HttpPost]   //Create in CRUD Example
        public async Task <IHttpActionResult> PostRestaurant(Restaurant model)
        {
            //because of the type of method, you can see if the model being passed in is valid
            //valid if the objects have the required fields (in Restaurant Class: name, address, rating)
            if (ModelState.IsValid)
            {
                //database  //table we want  //.Adding to table //object being added
                _context.Restaurants.Add(model);
                await _context.SaveChangesAsync();

                return(Ok());
            }
            return(BadRequest(ModelState));
        }
Пример #27
0
        [HttpPost] //We have to say what kind it is. Post is a way for us to create
        public async Task <IHttpActionResult> PostRestaurant(Restaurant model)
        {
            if (ModelState.IsValid)                //Because of the type of method this is, we can see that if the object being passed in, is valid. It's going to look at the class's properties and compare it to what's coming in.
            {
                _context.Restaurants.Add(model);   //We're looking at the database, then the specific table and then we're adding this model to it. But now we have to tell it to save.
                await _context.SaveChangesAsync(); //Saving the changes/addition.

                return(Ok());
            }

            return(BadRequest(ModelState));

            //ANY TIME YOU DO ANYTHING TO A DATABASE YOU NEED TO SAVE. THIS GOES FOR ADDING, DELETING, UPDATING, ETC.
        }
        public async Task <IActionResult> PutOrderMaster(long id, OrderMaster orderMaster)
        {
            if (id != orderMaster.OrderMasterId)
            {
                return(BadRequest());
            }

            _context.Entry(orderMaster).State = EntityState.Modified;

            // existing food items  & newly added food items
            foreach (OrderDetail item in orderMaster.OrderDetails)
            {
                if (item.OrderDetailId == 0)
                {
                    _context.OrderDetails.Add(item);
                }
                else
                {
                    _context.Entry(item).State = EntityState.Modified;
                }
            }
            // deleted food item
            if (orderMaster.DeletedOrderItemIds != null)
            {
                foreach (var i in orderMaster.DeletedOrderItemIds.Split(',').Where(x => x != ""))
                {
                    OrderDetail y = _context.OrderDetails.Find(Convert.ToInt32(i));
                    _context.OrderDetails.Remove(y);
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OrderMasterExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IHttpActionResult> PostRestaurant(Restaurant model)
        {
            if (model == null)
            {
                return(BadRequest("Your request cannot be empty"));
            }
            if (ModelState.IsValid)
            {
                _context.Restaurant.Add(model);
                await _context.SaveChangesAsync();

                return(Ok("You created a restraunt with a rating and it was saved"));
            }
            return(BadRequest(ModelState));
        }
        public async Task <IHttpActionResult> PostRestaurant(Restaurant model)
        {
            if (model is null)
            {
                return(BadRequest("Request body cant be empty"));
            }
            if (ModelState.IsValid)
            {
                _context.Restaurants.Add(model);
                await _context.SaveChangesAsync();

                return(Ok(model));
            }
            return(BadRequest(ModelState));
        }