Example #1
0
        public async Task <IActionResult> PutSoap(int id, SoapType soapType)
        {
            if (id != soapType.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <ActionResult <IEnumerable <SoapDto> > > Put(int id, SoapDto soapDto)
        {
            if (id != soapDto.Id)
            {
                return(BadRequest());
            }

            Soap soap = await _context.Soaps
                        .Include(t => t.SoapType)
                        .Include(d => d.SoapDetails)
                        .Include(t => t.Images)
                        .FirstOrDefaultAsync(e => e.Id == id);

            soap.SoapDetails.Clear();
            soap.Name        = soapDto.Name;
            soap.Description = soapDto.Description;
            soap.Available   = soapDto.Available;
            soap.Price       = soapDto.Price;
            soap.SoapType    = await _context.SoapTypes.FindAsync(int.Parse(soapDto.SoapTypeId));

            foreach (var soapDetail in soapDto.SoapDetails)
            {
                soap.SoapDetails.Add(new SoapDetail
                {
                    Id   = soapDetail.Id,
                    Name = soapDetail.Name
                });
            }

            if (soapDto.Images.Any())
            {
                soap.Images.Clear();
                foreach (var image in soapDto.Images)
                {
                    soap.Images.Add(new Image
                    {
                        Id   = image.Id,
                        Name = image.Name
                    });
                }
            }

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

            try
            {
                await _context.SaveChangesAsync();

                List <SoapDto> soapDtos = new List <SoapDto>();
                List <Soap>    soaps    = await _context.Soaps
                                          .Include(t => t.SoapType)
                                          .Include(d => d.SoapDetails)
                                          .Include(t => t.Images)
                                          .ToListAsync();

                _mapper.Map(soaps, soapDtos);

                return(soapDtos);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!SoapExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
        }