public async Task <IActionResult> AddItem(int id, int listId, ItemForCreationDto itemForCreationDto) { if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); if (!userFromRepo.Lists.Any(p => p.Id == listId)) { return(Unauthorized()); } var listFromRepo = await _repo.GetList(listId); itemForCreationDto.Created = DateTime.Now; var item = _mapper.Map <Item>(itemForCreationDto); listFromRepo.Items.Add(item); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Could not add list")); }
public async Task <IActionResult> NewItemAsync([FromBody] ItemForCreationDto item) { var finalItem = new Item { Owner = await userManager.FindByEmailAsync(item.OwnerEmail), Description = item.Description, CurrentHolderEmail = item.OwnerEmail, PhotoUrl = item.PhotoUrl, Title = item.Title, OwnerEmail = item.OwnerEmail }; var user = await userManager.FindByEmailAsync(finalItem.Owner.Email); user.Credit += 1; await itemRepository.AddItemAsync(finalItem); if (!await itemRepository.SaveAsync()) { return(BadRequest("Try agian another time.")); } return(Created($"api/items/{finalItem.Id}", finalItem)); }
private async void AddButton_Click(object sender, RoutedEventArgs e) { string Title = string.Empty; string Description = string.Empty; string ImageUrl = string.Empty; if (TitleTextBox.Text.Equals("")) { NeedTitle.Visibility = Visibility.Visible; TitleTextBox.PlaceholderForeground = new SolidColorBrush(Colors.Red); } else if (DescriptionTextBox.Text.Equals("")) { NeedTitle.Visibility = Visibility.Collapsed; NeedDescription.Visibility = Visibility.Visible; DescriptionTextBox.PlaceholderForeground = new SolidColorBrush(Colors.Red); } else if (UrlTextBox.Text.Equals("")) { NeedDescription.Visibility = Visibility.Collapsed; NeedURL.Visibility = Visibility.Visible; UrlTextBox.PlaceholderForeground = new SolidColorBrush(Colors.Red); } else { NeedURL.Visibility = Visibility.Collapsed; Title = TitleTextBox.Text; Description = DescriptionTextBox.Text; ImageUrl = UrlTextBox.Text; } try { Authentication.Instance.CurrentUser.Credit += 1; ItemForCreationDto dto = new ItemForCreationDto { Title = TitleTextBox.Text, Description = DescriptionTextBox.Text, OwnerEmail = Authentication.Instance.CurrentUser.Email, PhotoUrl = UrlTextBox.Text }; var response = await ItemData.PostItem(dto); await successDialog.ShowAsync(); Debug.WriteLine($"Response on creation of book: {response.IsSuccessStatusCode}"); } catch (Exception ex) { Debug.WriteLine($"An exception occurred creating the data transfer object: {ex}"); } }
public async Task <IActionResult> UpdateItem(int id, ItemForCreationDto itemForCreationDto) { var item = await itemRepository.GetItem(id); mapper.Map(itemForCreationDto, item); await itemRepository.SaveAll(); return(Ok()); }
public async Task <ActionResult <Item> > PostItem([FromBody] ItemForCreationDto item) { var itemEntity = _mapper.Map <Item>(item); _context.Items.CreateItem(itemEntity); await _context.SaveAsync(); var itemToReturn = _mapper.Map <ItemDto>(itemEntity); return(CreatedAtAction("GetItem", new { id = itemToReturn.Id }, item)); }
public async Task <IActionResult> CreateItem(ItemForCreationDto itemForCreationDto) { var itemToCreated = mapper.Map <Item>(itemForCreationDto); itemRepository.Add(itemToCreated); if (await itemRepository.SaveAll()) { return(Ok()); } return(BadRequest("Failed to add the item.")); }
public async Task <IActionResult> CreateItem([FromBody] ItemForCreationDto itemForCreationDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var item = _mapper.Map <ItemForCreationDto, Item>(itemForCreationDto); _context.Add(item); await _context.SaveChangesAsync(); var itemToReturn = _mapper.Map <Item, ItemDto>(item); return(Ok(itemToReturn)); }
public async Task <ActionResult> PostItem(ItemForCreationDto itemForCreationDto) { int budgetGroupId = itemForCreationDto.BudgetGroupId; if (!_repo.BudgetGroupByBudgetGroupIdExists(budgetGroupId)) { _logger.LogError($"Unable to find BudgetGroup with the BudgetGroupId '{budgetGroupId}'."); return(NotFound()); } Item item = _mapper.Map <Item>(itemForCreationDto); await _repo.AddItemAsync(item); ItemDto itemDto = _mapper.Map <ItemDto>(item); _logger.LogInformation($"Created Item with the ItemId '{itemDto.ItemId}'."); return(CreatedAtRoute(nameof(GetItem), new { itemId = itemDto.ItemId }, itemDto)); }
public void ItemControllerPostItemMethodTest() { //Arrange var item = new Item(); var itemDto = new ItemForCreationDto(); var itemRepository = new Mock <IItemRepository>(); itemRepository.Setup(repo => repo.CreateItem(item)); var mockManager = new Mock <IRepositoryManager>(); mockManager.Setup(manager => manager.Items).Returns(itemRepository.Object); ItemController controller = new ItemController(mockManager.Object, null, null); //Act var res = controller.PostItem(itemDto).Result; //Assert Assert.AreEqual(res, item); }
public IActionResult CreateItems(int transactionId, [FromBody] ItemForCreationDto item) { if (item == null) { return(BadRequest()); } var transaction = TransDataStore.Current.Transactions.FirstOrDefault(t => t.Id == transactionId); if (transaction == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } int maxItemsId = TransDataStore.Current.Transactions.SelectMany(t => t.Items).Max(i => i.Id); var newItem = new ItemForCreationDto { Id = ++maxItemsId, Amount = item.Amount, Receiver = new ReceiverDto() { Address1 = item.Receiver.Address1, Address2 = item.Receiver.Address2, City = item.Receiver.City, Country = item.Receiver.Country, FirstName = item.Receiver.FirstName, LastName = item.Receiver.LastName, Phone = item.Receiver.Phone } }; // transaction.Items.Add(newItem); return(CreatedAtRoute("GetTransactionItem", new { transactionId, id = newItem.Id }, newItem)); }
public static async Task <HttpResponseMessage> PostItem(ItemForCreationDto book) { return(await Post("api/books", book)); }