Esempio n. 1
0
        public async Task <ActionResult <BrandSuppliersViewModel> > PostSuppliers(BrandSuppliersViewModel suppliersVM)
        {
            Suppliers suppliers = new Suppliers()
            {
                SupplierId    = suppliersVM.SupplierId,
                SupplierName  = suppliersVM.SupplierName,
                Address       = suppliersVM.Address,
                ContactNumber = suppliersVM.ContactNumber,
                RecordDate    = suppliersVM.RecordDate,
                UniqueId      = suppliersVM.UniqueId
            };

            _context.Suppliers.Add(suppliers);
            await _context.SaveChangesAsync();



            foreach (int item in suppliersVM.Brands)
            {
                BrandSuppliers BS = new BrandSuppliers();
                BS.SupplierId = suppliers.SupplierId;
                BS.BrandId    = item;
                _context.BrandSuppliers.Add(BS);
                _context.SaveChanges();
            }



            return(CreatedAtAction("GetSuppliers", new { id = suppliersVM.SupplierId }, suppliersVM));
        }
Esempio n. 2
0
        public async Task <IActionResult> PutSuppliers(int id, BrandSuppliersViewModel suppliersVM)
        {
            if (id != suppliersVM.SupplierId)
            {
                return(BadRequest());
            }
            try
            {
                Suppliers suppliers = _context.Suppliers.Find(suppliersVM.SupplierId);

                suppliers.SupplierId    = suppliersVM.SupplierId;
                suppliers.SupplierName  = suppliersVM.SupplierName;
                suppliers.Address       = suppliersVM.Address;
                suppliers.ContactNumber = suppliersVM.ContactNumber;
                suppliers.RecordDate    = suppliersVM.RecordDate;


                _context.Entry(suppliers).State = EntityState.Modified;
                await _context.SaveChangesAsync();


                _context.BrandSuppliers.RemoveRange(_context.BrandSuppliers.Where(p => p.SupplierId == suppliers.SupplierId));
                await _context.SaveChangesAsync();

                foreach (int item in suppliersVM.Brands)
                {
                    BrandSuppliers BS = new BrandSuppliers();
                    BS.SupplierId = suppliers.SupplierId;
                    BS.BrandId    = item;
                    _context.BrandSuppliers.Add(BS);
                    await _context.SaveChangesAsync();
                }
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SuppliersExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Esempio n. 3
0
        public async Task <ActionResult <BrandSuppliersViewModel> > GetSuppliers(int id)
        {
            var suppliers = await _context.Suppliers.FindAsync(id);

            var brands = await _context.BrandSuppliers.Where(b => b.SupplierId == id).Select(m => m.BrandId).ToArrayAsync();

            var brandNames = await _context.BrandSuppliers.Where(b => b.SupplierId == id).Select(m => m.Brand.BrandName).ToArrayAsync();

            BrandSuppliersViewModel model = new BrandSuppliersViewModel(suppliers, brands, brandNames);



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

            return(model);
        }