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()); }
public async Task <ActionResult <ItemResponseDto> > GetItemById(Guid id) { ItemResponseDto itemDto = await ItemApplication.GetItem(i => i.Id.Equals(id)); if (itemDto == null) { ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado."); return(NotFound(error)); } return(Ok(itemDto)); }
public async Task <ActionResult> DeleteItem(Guid id) { // Check if the item exists ItemResponseDto itemDto = await ItemApplication.GetItem(i => i.Id.Equals(id)); if (itemDto == null) { ErrorMessage error = new ErrorMessage((int)HttpStatusCode.NotFound, $"O item, {id}, não foi encontrado."); return(NotFound(error)); } await ItemApplication.DeleteItem(itemDto); return(NoContent()); }
public async Task <ActionResult> UpdateItemImage(Guid id, [FromForm] IFormFile photo) { // 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)); } await ItemApplication.UpdateItemImage(id, photo, Request); return(Ok()); }
public void GetItem_MockedExternalServiceWith1ItemIDxxx_ReturnsItemIDxxx() { #region Arrange string itemId = "xxx"; const decimal price = 55; const string title = "Article Title"; const string largeDescription = "This is my large description"; const string url = "www.google.com"; const string attributeName = "CaracteristicaA"; const string attributeValue = "ValorDeCaracteristicaA"; const int soldQuantity = 5; const int availableQuantity = 10; var mockedItem = new ItemEntity { id = itemId, price = price, title = title, sold_quantity = soldQuantity, available_quantity = availableQuantity, pictures = new List <PictureEntity> { new PictureEntity { url = url } }, attributes = new List <ItemAttributeEntity> { new ItemAttributeEntity { name = attributeName, value_name = attributeValue } } }; var mockedLargeDescription = new ItemLargeDescriptionEntity { plain_text = largeDescription, date_created = DateTime.Now, last_updated = DateTime.Now, snapshot = new SnapshotEntity { url = "", height = 0, status = "", width = 0 }, text = "" }; var externalService = new Mock <IExternalService>(); string externalServiceResponseMessage = string.Empty; externalService .Setup(m => m.GetItemLargeDescription(itemId, ref externalServiceResponseMessage)) .Returns(mockedLargeDescription); externalService .Setup(m => m.GetItem(itemId, ref externalServiceResponseMessage)) .Returns(mockedItem); var loggerService = new Mock <ILoggerService>(); ItemApplication.MapperReset(); var applicationItem = new ItemApplication(externalService.Object, loggerService.Object); #endregion #region Act string responseMessage = string.Empty; var item = applicationItem.GetItem(itemId, ref responseMessage); #endregion #region Assert Assert.IsNotNull(item); Assert.AreEqual(price, item.Price); Assert.AreEqual(title, item.Title); Assert.AreEqual(url, item.PicturesUrl.First()); Assert.AreEqual(attributeName, item.Attributes.First().Item1); Assert.AreEqual(attributeValue, item.Attributes.First().Item2); Assert.AreEqual(availableQuantity, item.AvailableQuantity); Assert.AreEqual(soldQuantity, item.SoldQuantity); Assert.AreEqual(largeDescription, item.ItemLargeDescription); #endregion }