public async Task <IActionResult> UpdateAsync(int?id, [FromBody] ItemUpdateDto updateDto)
        {
            if (id == null || id.Value != updateDto.Id)
            {
                return(BadRequest(new { Error = "Invalid item id." }));
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var item = await _unitOfWork.ItemRepository.GetByIdAsync(id.Value);

            if (item == null)
            {
                return(BadRequest(new { Error = $"Item with id {id.Value} cannot be found." }));
            }

            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            item = _mapper.Map <Item>(updateDto);
            item.UpdateUserId = currentUser?.Id;
            item.UpdateDate   = DateTime.Now;

            await _unitOfWork.CompleteAsync();

            var dto = _mapper.Map <ItemDto>(item);

            return(Ok(dto));
        }
Ejemplo n.º 2
0
        public ItemUpdateDtoValidator(ItemUpdateDto itemUpdateDto, DocumentStructureModel documentStructure)
        {
            this.itemUpdateDto     = itemUpdateDto;
            this.documentStructure = documentStructure;

            newInnerItemValidator = new NewInnerItemValidator(itemUpdateDto.NewInnerItem);
        }
Ejemplo n.º 3
0
        public IActionResult UpdateItem(int playerId, int id,
                                        [FromBody] ItemUpdateDto item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var player = PlayersDataStore.Current.Players.
                         FirstOrDefault(p => p.Id == playerId);

            if (player == null)
            {
                return(NotFound());
            }

            var itemFromStore = player.Item.
                                FirstOrDefault(i => i.Id == id);

            if (itemFromStore == null)
            {
                return(NotFound());
            }

            itemFromStore.Name  = item.Name;
            itemFromStore.Value = item.Value;

            return(NoContent());
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> UpdateItem(Guid id, [FromBody] ItemUpdateDto itemDto)
        {
            var hasGroup = await GroupApplication.ExistGroup(g => g.Id.Equals(itemDto.GroupId));

            if (!hasGroup)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"O grupo, {itemDto.GroupId}, não existe.");
                return(BadRequest(error));
            }

            // Check if the item exists
            ItemResponseDto item = await ItemApplication.GetItem(i => i.Id.Equals(id));

            if (item == null)
            {
                ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado.");
                return(NotFound(error));
            }

            if (item.Name != itemDto.Name)
            {
                // Check if the item already exists
                ItemResponseDto itemByName = await ItemApplication.GetItem((i) => i.Name.ToLower().Equals(itemDto.Name.ToLower()));

                if (itemByName != null)
                {
                    ErrorMessage error = new ErrorMessage((int)HttpStatusCode.BadRequest, $"O item, {itemDto.Name}, já existe.");
                    return(BadRequest(error));
                }
            }

            await ItemApplication.UpdateItem(id, itemDto);

            return(Ok());
        }
Ejemplo n.º 5
0
        public async Task <bool> UpdateItemAsync(string userId, ItemUpdateDto model)
        {
            var item = await _context.Items
                       .SingleOrDefaultAsync(e => e.Id == model.Id && e.UserId == userId);

            _mapper.Map(model, item);

            return(await _context.SaveChangesAsync() == 1);
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> PutItem(int id, ItemUpdateDto itemToUpdate)
        {
            var item = _mapper.Map <Item>(itemToUpdate);

            item.Id = id;
            _repo.UpdateItem(item);
            var saved = await _repo.SaveChangesAsync();

            return(NoContent());
        }
Ejemplo n.º 7
0
        public async Task UpdateItem(Guid id, ItemUpdateDto itemDto)
        {
            var item      = this.Mapper.Map <Item>(itemDto);
            var itemModel = this.Repository.GetAllAsQueryable().Where(e => e.Id.Equals(id)).First();

            item.Id        = itemModel.Id;
            item.ImagePath = itemModel.ImagePath;
            item.ImageUrl  = itemModel.ImageUrl;

            this.Repository.Udate(item);
            await this.Repository.SaveAsync();
        }
Ejemplo n.º 8
0
        public ActionResult UpdateItem(int id, ItemUpdateDto itemUpdateDto)
        {
            var itemModelFromRepo = _item.GetItemById(id);

            if (itemModelFromRepo == null)
            {
                return(NotFound());
            }
            _mapper.Map(itemUpdateDto, itemModelFromRepo);
            _item.UpdateItem(itemModelFromRepo);
            _item.SaveChanges();
            return(NoContent());
        }
        public ActionResult Update(int id, ItemUpdateDto itemUpdateDto)
        {
            var itemFromDb = _itemService.Get(id);

            if (itemFromDb == null)
            {
                return(NotFound());
            }
            _mapper.Map(itemUpdateDto, itemFromDb);
            _itemService.Update(itemFromDb);
            _itemService.SaveChanges();
            return(NoContent());
        }
Ejemplo n.º 10
0
        public async Task UpdateItem(int id)
        {
            // Arrange
            var itemName = "Updated Test Item " + id;
            ItemUpdateDto itemUpdate = new ItemUpdateDto() { ItemName = itemName };
            var json = JsonConvert.SerializeObject(itemUpdate);
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            // Act
            var response = await TestClient.PutAsync(Url + id, content);

            // Assert
            response.StatusCode.Should().Be(HttpStatusCode.NoContent);
        }
        public ActionResult UpdateItem(int id, ItemUpdateDto itemUpdateDto)
        {
            var itemModelFromRepo = _repository.GetItemById(id);

            if (itemModelFromRepo == null)
            {
                return(NotFound());
            }
            _mapper.Map(itemUpdateDto, itemModelFromRepo);
            // don't ned to do that but it;s a good practice
            _repository.UpdateItem(itemModelFromRepo);
            _repository.SaveChanges();
            return(NoContent());
        }
Ejemplo n.º 12
0
        public async Task <ActionResult> Put(int id, [FromBody] ItemUpdateDto itemUpdate)
        {
            Item item = _mapper.Map <Item>(itemUpdate);

            item.Id = id;

            if (!_itemGroupRepository.Update(item))
            {
                return(NotFound());
            }

            await _itemGroupRepository.Save();

            return(NoContent());
        }
Ejemplo n.º 13
0
        public IActionResult UpdateItem(int id, [FromBody] ItemUpdateDto itemDto)
        {
            if (itemDto == null || id != itemDto.Id)
            {
                return(BadRequest(ModelState));
            }

            var itemObj = _mapper.Map <Item>(itemDto);

            if (!_itemRepository.UpdateItem(itemObj))
            {
                ModelState.AddModelError("", $"Something went wrong when updating the record {itemObj.Name}");
                return(StatusCode(500, ModelState));
            }
            return(NoContent());
        }
Ejemplo n.º 14
0
        public async Task <IActionResult> EditItem(ItemUpdateDto itemUpdate)
        {
            _userId = User.FindFirst(ClaimTypes.NameIdentifier).Value;
            if (!ModelState.IsValid)
            {
                return(BadRequest("Please submit a valid item"));
            }
            var result = await _itemService.UpdateItemAsync(_userId, itemUpdate);

            if (result)
            {
                return(NoContent());
            }

            throw new System.Exception("Creating the item failed on save");
        }
Ejemplo n.º 15
0
        public ActionResult UpdateItem(string name, ItemUpdateDto itemUpdateDto)
        {
            var itemModelFromRepo = _repository.GetItemByName(name);

            if (itemModelFromRepo == null)
            {
                return(NotFound());
            }

            _mapper.Map(itemUpdateDto, itemModelFromRepo);

            _repository.UpdateItem(itemModelFromRepo);

            _repository.SaveChanges();

            return(NoContent());
        }
        public async Task <IActionResult> UpdateItem(int id, int custId, ItemUpdateDto update)
        {
            var customer = await _unitOfWork.Customers.Get(custId);

            var oryginal = await _unitOfWork.Items.GetFirstOrDefaul(item => item.id == id && item.customerId == custId);

            if (customer == null || oryginal == null)
            {
                return(NotFound());
            }
            _unitOfWork.Items.Update(oryginal, update);
            if (await _unitOfWork.Save())
            {
                return(NoContent());
            }
            throw new Exception($"Updating user {id} failed");
        }
Ejemplo n.º 17
0
        public ActionResult UpdateItem(int id, ItemUpdateDto itemUpdateDto)
        {
            var modelFromRepo = _repository.GetItemById(id);

            if (modelFromRepo == null)
            {
                return(NotFound());
            }

            modelFromRepo.IdCategory = _repository.GetCategoryById(itemUpdateDto.CategoryId);
            modelFromRepo.ItemName   = itemUpdateDto.ItemName;

            _repository.UpdateItem(modelFromRepo);
            _repository.SaveChanges();
            _logger.LogInformation("Item with ID: {id} UPDATED by {HttpUser} ", id, HttpContext.User.Identity.Name);

            return(NoContent());
        }
Ejemplo n.º 18
0
        public async Task <ActionResult> UpdateItemAsync(Guid id, ItemUpdateDto itemDto)
        {
            var existingItem = await repository.GetItemByIdAsync(id);

            if (existingItem is null)
            {
                return(NotFound());
            }

            Item updateItem = existingItem with { // only record type can take with
                Name  = itemDto.Name,
                Price = itemDto.Price
            };

            await repository.UpdateItemAsync(updateItem);

            return(NoContent());
        }
Ejemplo n.º 19
0
        public ActionResult UpdateItem(Guid id, ItemUpdateDto itemDto)
        {
            var existingItem = _repository.GetItem(id);

            if (existingItem == null)
            {
                return(NotFound());
            }

            Item updatedItem = existingItem with {
                Name  = itemDto.Name,
                Price = itemDto.Price
            };

            _repository.UpdateItem(updatedItem);

            return(NoContent());
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> Update([FromBody] ItemUpdateDto item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            string email  = Helper.GetEmail(HttpContext);
            var    result = await _repo.Update(item, email);

            if (result)
            {
                var updatedItem = await _repo.GetItem(item.Id, email);

                return(Ok(updatedItem));
            }
            return(StatusCode(500));
        }
        public async Task UpdateItem_WithUnexistingItem_ReturnsNotFound()
        {
            // Arrange
            var itemToUpdate = new ItemUpdateDto()
            {
                Name  = Guid.NewGuid().ToString(),
                Price = rand.Next(1000)
            };

            itemRepositoryStub.Setup(repo => repo.GetItemAsync(It.IsAny <Guid>()))
            .ReturnsAsync((Item)null);
            var controller = new ItemsController(itemRepositoryStub.Object);

            // Act
            var result = await controller.UpdateItem(Guid.NewGuid(), itemToUpdate);

            // Assert
            result.Should().BeOfType <NotFoundResult>();
        }
Ejemplo n.º 22
0
        public IActionResult PartiallyUpdateItem(int playerId, int id,
                                                 [FromBody] JsonPatchDocument <ItemUpdateDto> pathDoc)
        {
            var player = PlayersDataStore.Current.Players.
                         FirstOrDefault(p => p.Id == playerId);

            if (player == null)
            {
                return(NotFound());
            }

            var itemFromStore = player.Item.
                                FirstOrDefault(i => i.Id == id);

            if (itemFromStore == null)
            {
                return(NotFound());
            }

            var itemToPatch =
                new ItemUpdateDto()
            {
                Name  = itemFromStore.Name,
                Value = itemFromStore.Value
            };

            pathDoc.ApplyTo(itemToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            if (!TryValidateModel(itemToPatch))
            {
                return(BadRequest(ModelState));
            }

            itemFromStore.Name  = itemToPatch.Name;
            itemFromStore.Value = itemToPatch.Value;

            return(NoContent());
        }
Ejemplo n.º 23
0
        public async Task <bool> Update(ItemUpdateDto item, string email)
        {
            var oldItem = await _context.Items
                          .FirstOrDefaultAsync(i => i.Id == item.Id &&
                                               i.Todo.Author.NormalizedEmail == email);

            if (oldItem == null)
            {
                return(false);
            }

            oldItem.Content = item.Content;
            oldItem.isDone  = item.IsDone;
            oldItem.Order   = item.Order;

            var res = await _context.SaveChangesAsync();

            return(res > 0);
        }
Ejemplo n.º 24
0
        public async Task <IActionResult> UpdateItem([FromRoute] Guid id, [FromBody] ItemUpdateDto itemDto)
        {
            Item item = await _itemRepository.GetItemAsync(id);

            if (item == null)
            {
                return(NotFound());
            }
            if (!CheckItemOwner(item))
            {
                return(BadRequest(new { Errors = "You aren't the owner of this post." }));
            }

            item.Name      = itemDto.Name;
            item.Price     = itemDto.Price ?? 0;
            item.UpdatedAt = DateTime.UtcNow;

            await _itemRepository.UpdateItemAsync(item);

            return(NoContent());
        }
Ejemplo n.º 25
0
 public async Task <ItemDto> Update([FromRoute] int id, [FromBody] ItemUpdateDto dto)
 {
     return(await ItemAppService.UpdateAsync(id, dto));
 }
Ejemplo n.º 26
0
        public async Task UpdateStructureAsync(DocumentStructureModel documentStructure, ItemUpdateDto itemUpdateDto,
                                               ObjectId userID)
        {
            try
            {
                Item itemToUpdate = FindItemInList(documentStructure.Items, itemUpdateDto.ItemID);

                if (itemToUpdate is null)
                {
                    throw new ItemNotFoundException("The item deos not present in the structure");
                }

                if (itemUpdateDto.NewInnerItem is {})
                {
                    InsertNewItemInItem(itemToUpdate, itemUpdateDto.NewInnerItem, userID);
                }

                if (itemUpdateDto.NewName is {} && itemUpdateDto.NewName.Length > 0)
Ejemplo n.º 27
0
 public Task <ItemGetDto> UpdateUserAsync(int id, ItemUpdateDto item)
 {
     throw new NotImplementedException();
 }