public async Task Update(int id, CreatePublisherDTO dto) { var publisher = await _context.Publishers.FirstOrDefaultAsync(p => p.Id == id); if (publisher == null) { throw new EntityNotFoundException("Publisher"); } if (publisher.Name != dto.Name) { publisher.Name = dto.Name; } if (publisher.HQ != dto.Name) { publisher.HQ = dto.HQ; } if (publisher.Founded != dto.Founded) { publisher.Founded = (DateTime)dto.Founded; } if (publisher.Website != dto.Website) { publisher.Website = dto.Website; } _context.Entry(publisher).State = EntityState.Modified; await _context.SaveChangesAsync(); }
public async Task Create(CreatePublisherDTO dto) { await _context.Publishers.AddAsync(new Domain.Publisher { Name = dto.Name, ISIN = dto.ISIN, Founded = dto.Founded, HQ = dto.HQ, Website = dto.Website }); await _context.SaveChangesAsync(); }
public async Task <IActionResult> Post([FromBody] CreatePublisherDTO dto) { var validator = new PublisherFluentValidator(_context); var errors = await validator.ValidateAsync(dto); if (!errors.IsValid) { return(UnprocessableEntity(ValidationFormatter.Format(errors))); } try { await _publisherService.Create(dto); return(StatusCode(201)); } catch (Exception) { return(StatusCode(500, new { ServerErrorResponse.Message })); } }
public async Task <IActionResult> Put(int id, [FromBody] CreatePublisherDTO dto) { var validator = new PublisherUpdateFluentValidator(_context, id); var errors = await validator.ValidateAsync(dto); if (!errors.IsValid) { return(UnprocessableEntity(ValidationFormatter.Format(errors))); } try { await _publisherService.Update(id, dto); return(NoContent()); } catch (EntityNotFoundException e) { return(NotFound(new { e.Message })); } catch (Exception) { return(StatusCode(500, new { ServerErrorResponse.Message })); } }