public async Task <IActionResult> Put(int id, [FromBody] OneTimeServiceItemDto updatedOneTimeServiceItemDto) { if (!ModelState.IsValid || updatedOneTimeServiceItemDto.Id != id) { return(BadRequest()); } try { var updatedOneTimeServiceItem = await _context.ServiceItems.OfType <OneTimeServiceItem>().SingleAsync(g => g.Id == id); Mapper.Map(updatedOneTimeServiceItemDto, updatedOneTimeServiceItem); _context.Entry(updatedOneTimeServiceItem).OriginalValues["RowVersion"] = updatedOneTimeServiceItemDto.RowVersion; _context.ServiceItems.Update(updatedOneTimeServiceItem); await _context.SaveChangesAsync(); _cache.Remove(IMemoryCacheKeys.customersCacheKey); } catch (DbUpdateConcurrencyException exception) { return(Conflict(exception)); } catch (Exception exception) { return(BadRequest(exception)); } return(new NoContentResult()); }
public async Task <IActionResult> Post([FromBody] OneTimeServiceItemDto newOneTimeServiceItemDto) { if (!ModelState.IsValid) { return(BadRequest()); } var newOneTimeServiceItem = Mapper.Map <OneTimeServiceItem>(newOneTimeServiceItemDto); newOneTimeServiceItem.IsArchived = false; try { _context.ServiceItems.Add(newOneTimeServiceItem); await _context.SaveChangesAsync(); _cache.Remove(IMemoryCacheKeys.customersCacheKey); } catch (Exception exception) { return(BadRequest(exception)); } return(CreatedAtRoute("", new { id = newOneTimeServiceItem.Id }, Mapper.Map <OneTimeServiceItemDto>(newOneTimeServiceItem))); }