Ejemplo n.º 1
0
        public async Task <ItemEditViewModel> EditByIdAsync(int id, ItemEditInputModel itemFromView)
        {
            var itemToDelete = this.itemRepository.All().FirstOrDefault(x => x.Id == id);

            if (itemToDelete == null)
            {
                Exception innerException = new Exception(nameof(id));
                throw new NullReferenceException(string.Format(ItemConstants.NullReferenceItemId, id), innerException);
            }

            var checkForName = this.itemRepository.All().FirstOrDefault(x => x.Name == itemFromView.Name);

            if (checkForName != null && checkForName.Id != id)
            {
                throw new ArgumentException(string.Format(ItemConstants.ArgumentExceptionItemName, itemFromView.Name), nameof(itemFromView.Name));
            }

            var newItem = itemFromView.To <Item>();

            this.itemRepository.Delete(itemToDelete);
            await this.itemRepository.AddAsync(newItem);

            await this.itemRepository.SaveChangesAsync();

            return(newItem.To <ItemEditViewModel>());
        }
Ejemplo n.º 2
0
        public async Task <ItemEditViewModel> EditByIdAsync(int id, ItemEditInputModel itemFromView)
        {
            var itemToDelete = this.itemRepository.All().FirstOrDefault(x => x.Id == id);

            if (itemToDelete == null)
            {
                throw new NullReferenceException(string.Format(ItemConstants.NullReferenceItemId, id));
            }

            var newItem = itemFromView.To <Item>();

            this.itemRepository.Delete(itemToDelete);
            await this.itemRepository.AddAsync(newItem);

            await this.itemRepository.SaveChangesAsync();

            return(newItem.To <ItemEditViewModel>());
        }