public async Task <IActionResult> UpdateItem(int id, int listId, int itemId, ItemForUpdateDto itemForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); if (!userFromRepo.Lists.Any(l => l.Id == listId)) { return(Unauthorized()); } var listFromRepo = await _repo.GetList(listId); if (!listFromRepo.Items.Any(i => i.Id == itemId)) { return(BadRequest()); } var itemFromRepo = await _repo.GetItem(itemId); _mapper.Map(itemForUpdateDto, itemFromRepo); if (await _repo.SaveAll()) { return(Ok()); } throw new Exception("Updating item failed on save"); }
public async Task <IActionResult> PutItem([FromRoute] int id, [FromBody] ItemForUpdateDto itemForUpdateDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } //if (id != item.Id) //{ // return BadRequest(); //} var item = await _repository.GetAsync(id); if (item == null) { return(NotFound()); } _mapper.Map(itemForUpdateDto, item); if (await _unitOfWork.SaveAsync()) { return(NoContent()); } throw new Exception($"Updating item {id} failed on save"); }
public IActionResult PartiallyUpdateItem(int transactionId, int id, [FromBody] JsonPatchDocument <ItemForUpdateDto> patchDoc) { if (patchDoc == null) { return(BadRequest()); } var transaction = TransDataStore.Current.Transactions.FirstOrDefault(t => t.Id == transactionId); if (transaction == null) { return(NotFound()); } var itemFromTheStore = transaction.Items.FirstOrDefault(i => i.Id == id); if (itemFromTheStore == null) { return(NotFound()); } var itemToPatch = new ItemForUpdateDto() { Amount = itemFromTheStore.Amount, Receiver = new ReceiverDto() { FirstName = itemFromTheStore.ReceiverDto.FirstName, LastName = itemFromTheStore.ReceiverDto.LastName, Phone = itemFromTheStore.ReceiverDto.Phone } }; patchDoc.ApplyTo(itemToPatch, ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (itemToPatch.Receiver.LastName == itemToPatch.Receiver.Country) { ModelState.AddModelError("Reciever Name", "Receiver Name cannot be the same as country"); } TryValidateModel(itemToPatch); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } itemFromTheStore.Amount = itemToPatch.Amount; itemFromTheStore.ReceiverDto.FirstName = itemToPatch.Receiver.FirstName; itemFromTheStore.ReceiverDto.LastName = itemToPatch.Receiver.LastName; itemFromTheStore.ReceiverDto.Phone = itemToPatch.Receiver.Phone; return(NoContent()); }
public async Task <IActionResult> PutItem(int id, [FromBody] ItemForUpdateDto item) { var itemEntity = HttpContext.Items["item"] as Item; _mapper.Map(item, itemEntity); await _context.SaveAsync(); return(NoContent()); }
public async Task <ActionResult> PutItem(int itemId, ItemForUpdateDto itemForUpdateDto) { Item item = await _repo.GetItemByItemIdAsync(itemId); if (item == null) { return(NotFound($"Unable to find item with ID '{itemId}'.")); } _mapper.Map(itemForUpdateDto, item); await _repo.UpdateItemAsync(item); ItemDto itemDto = _mapper.Map <ItemDto>(item); _logger.LogInformation($"Updated Item with the ItemId '{itemDto.ItemId}'."); return(CreatedAtRoute(nameof(GetItem), new { itemId = itemDto.ItemId }, itemDto)); }
public async Task <IActionResult> UpdateItem(int id, int itemid, ItemForUpdateDto itemForUpdateDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var itemFromRepo = await _repo.GetItem(itemid); var userFromRepo = await _repo.GetUser(id); itemFromRepo.LastModifier = userFromRepo; _mapper.Map(itemForUpdateDto, itemFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Nem jó az azlábbi {id}"); }
public async Task <IActionResult> UpdateItem([FromBody] ItemForUpdateDto item) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var currentItemId = item.Id; var currentItem = await _repo.GetItem(currentItemId); if (currentItem == null) { NotFound("No item found."); } if (currentUserId != currentItem.UserId) { return(Unauthorized()); } currentItem.Title = item.Title; currentItem.IsService = item.IsService; currentItem.Description = item.Description; currentItem.ShipingAddress = item.ShipingAddress; currentItem.ShipingCountry = item.ShipingCountry; currentItem.CreatedDate = item.CreatedDate; currentItem.Quantity = item.Quantity; currentItem.UnitPrice = item.UnitPrice; currentItem.ImageUrl = item.ImageUrl; currentItem.OtherUrl = item.OtherUrl; //currentItem.Photo = item.Photo; //_mapper.Map(item, currentItem); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Failed to save item."); }