Пример #1
0
        public async Task<IHttpActionResult> PutProductTab(int id, ProductTabDetailsDto productTabDetails)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var ProductTypeList = productTabDetails.ProductTypes;
            List<ProductType> productTypes = new List<ProductType>();

            foreach (var i in ProductTypeList)
            {
                var pT = from pt in db.ProductTypes where pt.Id == i select pt;
                pT.ForEach(pt => productTypes.Add(pt));
            }

            var productTab = db.ProductTabs.Find(id);

            if (productTab != null)
            {
                productTab.Active = productTabDetails.Active;
                productTab.Color = productTabDetails.Color;
                productTab.Name = productTabDetails.Name;
                productTab.Priority = productTabDetails.Priority;
            }

            productTab.ProductTypes.Clear();

            productTypes?.ForEach(e => productTab.ProductTypes.Add(e));



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

            db.Set<ProductTab>().Attach(productTab);
            db.Entry(productTab).State = EntityState.Modified;

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

            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #2
0
        public async Task<IHttpActionResult> GetProductTab(int id)
        {
            ProductTab productTab = await db.ProductTabs.Include(pt => pt.ProductTypes).SingleOrDefaultAsync(pt => pt.Id == id);
            if (productTab == null)
            {
                return NotFound();
            }

            var productTabDto = new ProductTabDetailsDto() { Active = productTab.Active, Color = productTab.Color, Id = productTab.Id, Name = productTab.Name, Priority = productTab.Priority, ProductTypes = new List<int>() };

            productTab.ProductTypes.ForEach(pt => productTabDto.ProductTypes.Add(pt.Id));

            return Ok(productTabDto);
        }
Пример #3
0
        public async Task<IHttpActionResult> PostProductTab(ProductTabDetailsDto productTabDetails)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var workwrok = productTabDetails.ProductTypes;
            List<ProductType> productTypes = new List<ProductType>();

            foreach (var i in workwrok)
            {
                var pT = from pt in db.ProductTypes where pt.Id == i select pt;
                pT.ForEach(pt => productTypes.Add(pt));
            }

            var productTab = new ProductTab {Active = productTabDetails.Active, Color = productTabDetails.Color, Name = productTabDetails.Name, Priority = productTabDetails.Priority};

            productTypes?.ForEach(e => productTab.ProductTypes.Add(e));

            db.ProductTabs.Add(productTab);
            await db.SaveChangesAsync();

            productTabDetails.Id = productTab.Id;

            return CreatedAtRoute("DefaultApi", new { id = productTabDetails.Id }, productTabDetails);
        }