// GET: Catalouges/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Catalouge catalouge = await db.Catalouges.FindAsync(id);

            if (catalouge == null)
            {
                return(HttpNotFound());
            }
            //return View(supplier);
            CatalougeViewModel catalougeViewModel = new CatalougeViewModel();

            catalougeViewModel.Catalouge    = db.Catalouges.Include(c => c.Supplier).First(c => c.FoodId == id);;
            catalougeViewModel.AllSuppliers = db.Suppliers.Select(s => new SelectListItem
            {
                Text  = s.SupplierName,
                Value = s.SupplierId.ToString()
            }
                                                                  );
            catalougeViewModel.SelectedSuppliers = catalougeViewModel.Catalouge.Supplier.Select(s => s.SupplierId).ToList();
            return(View(catalougeViewModel));
        }
        public async Task <IHttpActionResult> PutCatalouge(int id, Catalouge catalouge)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != catalouge.FoodId)
            {
                return(BadRequest());
            }

            db.Entry(catalouge).State = EntityState.Modified;

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

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            Catalouge catalouge = await db.Catalouges.FindAsync(id);

            db.Catalouges.Remove(catalouge);
            await db.SaveChangesAsync();

            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> Create([Bind(Include = "FoodId,FoodName,FoodType,CommericalGood")] Catalouge catalouge)
        {
            if (ModelState.IsValid)
            {
                db.Catalouges.Add(catalouge);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(catalouge));
        }
        public async Task <IHttpActionResult> PostCatalouge(Catalouge catalouge)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Catalouges.Add(catalouge);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = catalouge.FoodId }, catalouge));
        }
        // GET: Catalouges/Delete/5
        public async Task <ActionResult> Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Catalouge catalouge = await db.Catalouges.FindAsync(id);

            if (catalouge == null)
            {
                return(HttpNotFound());
            }
            return(View(catalouge));
        }
        public async Task <IHttpActionResult> DeleteCatalouge(int id)
        {
            Catalouge catalouge = await db.Catalouges.FindAsync(id);

            if (catalouge == null)
            {
                return(NotFound());
            }

            db.Catalouges.Remove(catalouge);
            await db.SaveChangesAsync();

            return(Ok(catalouge));
        }
 public async Task <ActionResult> Edit(CatalougeViewModel catalougeViewModel)
 {
     if (ModelState.IsValid)
     {
         Catalouge     catalouge         = db.Catalouges.Include(c => c.Supplier).First(c => c.FoodId == catalougeViewModel.Catalouge.FoodId);
         HashSet <int> selectedSuppliers = new HashSet <int>(catalougeViewModel.SelectedSuppliers);
         foreach (Supplier s in db.Suppliers)
         {
             if (!selectedSuppliers.Contains(s.SupplierId))
             {
                 catalouge.Supplier.Remove(s);
             }
             else
             {
                 catalouge.Supplier.Add(s);
             }
         }
         db.Entry(catalouge).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(catalougeViewModel));
 }
        public async Task <IHttpActionResult> GetCatalouge(int id)
        {
            Catalouge c = await db.Catalouges.FindAsync(id);

            if (c == null)
            {
                return(NotFound());
            }
            CatalougeDTO catalouge = new CatalougeDTO
            {
                FoodId         = c.FoodId,
                FoodName       = c.FoodName,
                FoodType       = c.FoodType,
                CommericalGood = c.CommericalGood,
                Suppliers      = c.Supplier.Select(s => new SupplierDTO()
                {
                    SupplierId   = s.SupplierId,
                    SupplierName = s.SupplierName,
                    SupplierType = s.SupplierType
                }).ToList()
            };

            return(Ok(catalouge));
        }