public async Task <ActionResult <SoapDto> > Get(int id) { var soap = await _context.Soaps .Include(t => t.SoapType) .Include(d => d.SoapDetails) .Include(t => t.Images) .FirstOrDefaultAsync(e => e.Id == id); if (soap == null) { return(NotFound()); } SoapDto soapDto = new SoapDto(); _mapper.Map(soap, soapDto); return(soapDto); }
public async Task <ActionResult <SoapDto> > Post([FromBody] SoapDto soapDto) { try { Soap soap = new Soap(); _mapper.Map(soapDto, soap); soap.SoapType = await _context.SoapTypes.FindAsync(int.Parse(soapDto.SoapTypeId)); _context.Soaps.Add(soap); await _context.SaveChangesAsync(); _mapper.Map(soap, soapDto); return(CreatedAtAction("Get", new { id = soapDto.Id }, soapDto)); } catch (ArgumentException) { return(this.ValidationProblem()); } catch { return(this.StatusCode(500, "Internal Server error")); } }
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; } } }