Exemplo n.º 1
0
        public ActionResult UpdateBook(int id)
        {
            GetAuthorAndCategories();

            using (var context = new DataContext())
            {
                var book = context.Books.Include("Author").Include("Categories").Include("Author.Country").FirstOrDefault(b => b.Id == id);

                var updateBook = new UpdateBook
                {
                    Id              = book.Id,
                    Name            = book.Name,
                    Year            = book.Year,
                    NumberPages     = book.NumberPages,
                    Description     = book.Description,
                    TodayBestChoice = book.TodayBestChoice,
                    Rating          = book.Rating,
                    ForAuthorize    = book.ForAuthorize,
                    AuthorId        = book.Author.Id,
                    CategoryId      = book.Categories.FirstOrDefault().Id,
                    ImageName       = book.Image
                };

                return(View(updateBook));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> EditBook(int bookId)
        {
            var bookDto = _mapper.Map <BookDto>(await _bookService.GetByIdAsync(bookId));
            var command = new UpdateBook(bookDto);

            return(View(command));
        }
        public async Task <IActionResult> Put(Guid id, [FromBody] UpdateBook command)
        {
            var book = await _context.Books.FindAsync(id);

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

            book.Title           = command.Title;
            book.Author          = command.Author;
            book.Cost            = command.Cost;
            book.InventoryAmount = command.InventoryAmount;

            _context.Books.Update(book);
            await _context.SaveChangesAsync();

            var @event = new BookUpdated
            {
                Id              = book.Id,
                Author          = book.Author,
                Cost            = book.Cost,
                Title           = book.Title,
                InventoryAmount = book.InventoryAmount,
                UserId          = command.UserId,
                Timestamp       = DateTime.UtcNow
            };
            await _messageProducer.PublishAsync(@event);

            return(Ok());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Put([FromBody] UpdateBook command)
        {
            if (command == null)
            {
                return(BadRequest());
            }

            await CommandDispatcher.DispatchAsync(command);

            return(NoContent());
        }
Exemplo n.º 5
0
        public UpdateBookShould()
        {
            _bookServiceMock = new Mock <IBookService>();
            _loggerMock      = new Mock <ILogger <UpdateBook> >();
            _mapperMock      = new Mock <IMapper>();
            _httpRequestMock = new Mock <HttpRequest>();

            _func = new UpdateBook(
                _bookServiceMock.Object,
                _loggerMock.Object,
                _mapperMock.Object);
        }
        public async Task <IActionResult> Patch(Guid id, [FromBody] UpdateBookRequest request)
        {
            if (id == Guid.Empty || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            var command = new UpdateBook(request.ToBook(id), UserId);
            await Mediator.Send(command);

            return(Ok());
        }
        public async Task <Books> Update(UpdateBook input)
        {
            var updateBook = await Get(input.Id);

            updateBook.Name        = input.Name;
            updateBook.Author      = input.Author;
            updateBook.Description = input.Description;
            _context.Books.Update(updateBook);
            await _context.SaveChangesAsync();

            return(updateBook);
        }
Exemplo n.º 8
0
        public async Task <ActionResult> Update(int id)
        {
            var model = await _booksService.Get(id);

            UpdateBook updateModel = new UpdateBook
            {
                Id            = model.Id,
                CreatorUserId = model.CreatorUserId,
                Description   = model.Description,
                Name          = model.Name,
                Author        = model.Author
            };

            return(View(updateModel));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> UpdateBook(UpdateBook command)
        {
            if (!ModelState.IsValid)
            {
                return(View("EditBook", command));
            }

            var success = await _bookService.UpdateAsync(command);

            if (!success)
            {
                return(View("EditBook", command));
            }

            return(RedirectToAction("Books"));
        }
Exemplo n.º 10
0
        public IActionResult Update(UpdateBook model)
        {
            if (ModelState.IsValid)
            {
                var result = new UpdateBookResult();
                result = Helper.ApiHelper <UpdateBookResult> .HttpPostAsync("api/Book/update", "POST", model);

                if (result.BookId > 0)
                {
                    return(RedirectToAction("index"));
                }
                ModelState.AddModelError("", result.Message);
                return(View(model));
            }
            return(View(model));
        }
Exemplo n.º 11
0
        public IActionResult UpdateBookForAuthor(Guid id, Guid authorId,
                                                 [FromBody] UpdateBook book)
        {
            if (book == null)
            {
                return(BadRequest());
            }

            if (book.Description == book.Title)
            {
                ModelState.AddModelError(nameof(UpdateBook), "The description should differ from the title.");
            }

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


            var existingBook = _libraryRepository.GetBookForAuthor(authorId, id);

            if (existingBook == null)
            {
                var bookToAdd = Mapper.Map <Entities.Book>(book);
                bookToAdd.Id = id;
                _libraryRepository.AddBookForAuthor(authorId, bookToAdd);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting couldn't save the book {id}");
                }
                var bookToReturn = Mapper.Map <Book>(bookToAdd);

                return(CreatedAtRoute(nameof(GetBookForAuthor), new { authorId, id }, bookToReturn));
            }


            Mapper.Map(book, existingBook);
            _libraryRepository.UpdateBookForAuthor(existingBook);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Cound't save the updated book {id}");
            }

            return(NoContent());
        }
Exemplo n.º 12
0
        public ActionResult UpdateBook(UpdateBook book)
        {
            if (!ModelState.IsValid)
            {
                GetAuthorAndCategories();
                return(View(book));
            }

            using (var context = new DataContext())
            {
                var updateBook = context.Books.Include("Categories").Include("Author").Include("Author.Country").FirstOrDefault(b => b.Id == book.Id);

                if (book.BookCover != null)
                {
                    string   bookCover = Guid.NewGuid().ToString().Substring(0, 10) + "_" + System.IO.Path.GetFileName(book.BookCover.FileName);
                    WebImage img       = new WebImage(book.BookCover.InputStream);
                    img.Resize(380, 600);
                    img.Save(Server.MapPath("~/Content/images/book/" + bookCover));
                    updateBook.Image = bookCover;
                }

                if (book.BookFile != null)
                {
                    string bookPath = Guid.NewGuid().ToString().Substring(0, 10) + "_" + System.IO.Path.GetFileName(book.BookFile.FileName);
                    book.BookFile.SaveAs(Server.MapPath("~/Content/books/" + bookPath));
                    updateBook.BookPath = bookPath;
                }

                updateBook.Name            = book.Name;
                updateBook.Slug            = book.Name.GenerateSlug();
                updateBook.Year            = book.Year;
                updateBook.NumberPages     = book.NumberPages;
                updateBook.Author          = context.Authors.FirstOrDefault(a => a.Id == book.AuthorId);
                updateBook.Description     = book.Description;
                updateBook.View            = 0;
                updateBook.Rating          = book.Rating;
                updateBook.ForAuthorize    = book.ForAuthorize;
                updateBook.TodayBestChoice = book.TodayBestChoice;
                updateBook.Create          = DateTime.Now;
                updateBook.Categories      = context.Categories.Where(c => c.Id == book.CategoryId).ToList();

                context.SaveChanges();
            }

            return(Redirect("/admin/books"));
        }
Exemplo n.º 13
0
        public async Task <ActionResult> Update(UpdateBook model)
        {
            if (ModelState.IsValid)
            {
                var updatedBook = await _booksService.Update(model);

                UpdateBook updateModel = new UpdateBook
                {
                    Id            = updatedBook.Id,
                    CreatorUserId = updatedBook.CreatorUserId,
                    Description   = updatedBook.Description,
                    Name          = updatedBook.Name,
                    Author        = updatedBook.Author
                };
                return(View(updateModel));
            }
            return(View(model));
        }
        public async Task Handle(UpdateBook message, CancellationToken cancellationToken)
        {
            var oldBook = await BookEntityHandler.Get(message.Book.Id);

            if (oldBook == null)
            {
                throw new EntityNotFoundException <Book>(message.Book);
            }

            await CheckAndAddAuthors(message.Book, message.UserId);

            var newBook = oldBook.Update(message.Book, DateTime.UtcNow);

            BookEntityHandler.Update(newBook);

            await Mediator.Send(new SaveChanges());

            await Mediator.Publish(new BookUpdated(newBook, oldBook, message.UserId));
        }
Exemplo n.º 15
0
 public BookViewModel(MainViewModel mainViewModel)
 {
     Books       = new ObservableCollection <BookEntity>();
     filials     = new List <FilialEntity>();
     BuyBookCMD  = new BuyBook(mainViewModel, this);
     RentCMD     = new Rent(mainViewModel, this);
     addBook     = new AddBook(this);
     DeleteBook  = new DeleteBook(this);
     UpdateBook  = new UpdateBook(this);
     currentbook = new BookEntity();
     selectbook  = new BookEntity();
     //if (File.Exists("Filials.json"))
     //{
     //    string jsonFilial = File.ReadAllText("Filials.json");
     //   this.filials = JsonConvert.DeserializeObject<List<FilialEntity>>(jsonFilial);
     //}
     //if (File.Exists("Books.json"))
     //{
     //    string jsonBook = File.ReadAllText("Books.json");
     //    this.Books = JsonConvert.DeserializeObject<ObservableCollection<BookEntity>>(jsonBook);
     //}
 }
Exemplo n.º 16
0
        public async Task <bool> UpdateAsync(UpdateBook command)
        {
            try
            {
                var isSuccess = false;
                var book      = await GetByIdAsync(command.Id);

                if (book != null)
                {
                    book.Update(command.Title, command.Author, command.Description, command.ReleaseDate);

                    _context.Books.Update(book);
                    await _context.SaveChangesAsync();

                    isSuccess = true;
                }

                return(isSuccess);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Exemplo n.º 17
0
        public IActionResult PartiallyUpdateBookForAuthor(Guid id, Guid authorId, [FromBody] JsonPatchDocument <UpdateBook> patchDocument)
        {
            if (patchDocument == null)
            {
                return(BadRequest());
            }

            var existingBook = _libraryRepository.GetBookForAuthor(authorId, id);

            if (existingBook == null)
            {
                var bookDto = new UpdateBook();
                patchDocument.ApplyTo(bookDto, ModelState);


                TryValidateModel(bookDto);

                if (ModelState.IsValid && bookDto.Description == bookDto.Title)
                {
                    ModelState.AddModelError(nameof(CreateBook), "The description should differ from the title.");
                }

                if (!ModelState.IsValid)
                {
                    return(UnprocessableEntity(ModelState));
                }
                var bookToAdd = Mapper.Map <Entities.Book>(bookDto);
                bookToAdd.Id = id;

                _libraryRepository.AddBookForAuthor(authorId, bookToAdd);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting couldn't save the book {id}");
                }
                var bookToReturn = Mapper.Map <Book>(bookToAdd);

                return(CreatedAtRoute(nameof(GetBookForAuthor), new { authorId, id }, bookToReturn));
            }

            var bookToPatch = Mapper.Map <UpdateBook>(existingBook);

            patchDocument.ApplyTo(bookToPatch, ModelState);

            TryValidateModel(bookToPatch);

            if (ModelState.IsValid && bookToPatch.Description == bookToPatch.Title)
            {
                ModelState.AddModelError(nameof(CreateBook), "The description should differ from the title.");
            }

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

            Mapper.Map(bookToPatch, existingBook);

            _libraryRepository.UpdateBookForAuthor(existingBook);
            if (!_libraryRepository.Save())
            {
                throw new Exception($"Patching book {id} for author {authorId} failed to save");
            }

            return(NoContent());
        }