예제 #1
0
        public IActionResult UpdateBookForAuthor(Guid authorId, Guid id, [FromBody] UpdateBookDTO updateBookDTO)
        {
            if (updateBookDTO == null)
            {
                return(BadRequest());
            }

            if (updateBookDTO.Title == updateBookDTO.Description)
            {
                ModelState.AddModelError(nameof(UpdateBookDTO),
                                         "The provided description should be different from the title.");
            }

            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_libraryRepository.AuthorExists(authorId))
            {
                return(NotFound());
            }

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

            if (book == null)
            {
                var newBook = Mapper.Map <Book>(updateBookDTO);
                newBook.Id = id;

                _libraryRepository.AddBookForAuthor(authorId, newBook);

                if (!_libraryRepository.Save())
                {
                    throw new Exception($"Upserting book {id} for author {authorId} failed on save.");
                }

                var bookDTO = Mapper.Map <BookDTO>(newBook);

                return(CreatedAtRoute("GetBookForAuthor", new { authorId, id = bookDTO.Id }, bookDTO));
            }

            Mapper.Map(updateBookDTO, book);

            if (!_libraryRepository.Save())
            {
                throw new Exception($"Updating book {id} for author {authorId} failed on save.");
            }

            return(NoContent());
        }
예제 #2
0
        public async Task <ApiResponse <bool> > UpdateBook(UpdateBookDTO updateBookDTO)
        {
            var book = await FindBook(updateBookDTO.Id);

            book = _mapper.Map(updateBookDTO, book);

            var result = await _bookRepository.Update(book);

            if (!result)
            {
                throw new AppException(StatusCodes.Status500InternalServerError, "Cannot update book.");
            }

            return(ApiResponse <bool> .Ok(result));
        }
예제 #3
0
        public async Task <IActionResult> PutBook([FromForm] UpdateBookDTO book)
        {
            var publisher = _publisherService.Post(book.PublisherName);

            if (publisher != null)
            {
                _context.Publisher.Add(publisher);
            }
            await _context.SaveChangesAsync();

            Book newBook = _bookService.Put(book);

            var entry = _context.Entry(newBook);

            entry.State = EntityState.Modified;
            entry.Property(b => b.BookImgUrl).IsModified = book.Image != null;
            entry.Reference(b => b.Publisher).IsModified = publisher != null;

            string[] writers = book.WriterName.Split(',');

            for (int i = 0; i < writers.Length; i++)
            {
                if (i >= writers.Length)
                {
                    var writer = new Writer {
                        WriterName = writers[i].Trim()
                    };
                    _context.Writer.Add(writer);
                    var newAuthor = new Author {
                        Book = newBook, Writer = writer
                    };
                    _context.Author.Add(newAuthor);
                }
                else if (_context.Writer.Any(e => e.WriterName != writers[i].Trim() && e.WriterID == book.WriterID.ElementAt(i)))
                {
                    Writer writer = new Writer {
                        WriterID = book.WriterID.ElementAt(i), WriterName = writers[i]
                    };
                    _context.Entry(writer).State = EntityState.Modified;
                }
            }

            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #4
0
 public ActionResult Update(int id)
 {
     Book book = _dbContext.Books.FirstOrDefault(x => x.Id == id);
     if (book != null)
     {
         UpdateBookDTO data = new UpdateBookDTO();
         data.Id = book.Id;
         data.Title = book.Title;
         data.Content = book.Content;
         data.ImageUrl = book.ImageUrl;
         data.Genre = book.Genre;
         return View(data);
     }
     else
     {
         return RedirectToAction("List");
     }
 }
예제 #5
0
        public async Task <IActionResult> Update(int id, [FromBody] UpdateBookDTO updateBookDTO)
        {
            try
            {
                if (id < 1 || updateBookDTO == null || id != updateBookDTO.Id)
                {
                    _logger.LogWarn("Bed request from Update0");
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn("Bed request from Create1");
                    return(BadRequest(ModelState));
                }
                var oldImage = await _bookRepository.GetImageFileName(id);

                var record    = _mapper.Map <Book>(updateBookDTO);
                var isSuccess = await _bookRepository.Update(record);

                if (!isSuccess)
                {
                    _logger.LogWarn("Create in db goes wrong");
                    return(StatusCode(500, "Internal Error"));
                }
                if (!updateBookDTO.Image.Equals(oldImage))
                {
                    if (System.IO.File.Exists(GetImgPath(oldImage)))
                    {
                        System.IO.File.Delete(GetImgPath(oldImage));
                    }
                }
                if (!string.IsNullOrEmpty(updateBookDTO.Image))
                {
                    byte[] imageBytes = Convert.FromBase64String(updateBookDTO.File);
                    System.IO.File.WriteAllBytes(GetImgPath(updateBookDTO.Image), imageBytes);
                }
                return(NoContent());
            }
            catch (Exception e)
            {
                _logger.LogInfo($"{e.Message} - {e.InnerException}");
                return(StatusCode(500, "Internal Error"));
            }
        }
        public async Task <ActionResult> UpdateUserAsync(Guid id, UpdateBookDTO userDto)
        {
            Book existingUser = await repo.Get(id);

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


            Book updatedUser = existingUser with
            {
                Id       = existingUser.Id,
                BookName = existingUser.BookName,
                Password = ph.hashPass(userDto.Password),
                Author   = "newAuthor"
            };

            await repo.Update(updatedUser);

            return(NoContent());
        }
예제 #7
0
        public ActionResult Update(UpdateBookDTO data)
        {
            if (ModelState.IsValid)
            {
                Book book = _dbContext.Books.FirstOrDefault(x => x.Id == data.Id);

                book.Title = data.Title;
                book.Content = data.Content;
                book.ImageUrl = data.ImageUrl;
                book.Genre = data.Genre;
                book.UpdateDate = DateTime.Now;
                book.Status = Status.Modified;
                _dbContext.SaveChanges();
                return RedirectToAction("List");

            }
            else
            {
                return View(data);
            }

        }
예제 #8
0
        public Book Put([FromForm] UpdateBookDTO book)
        {
            var publisher = new Publisher {
                PublisherName = book.PublisherName
            };

            _context.Attach(publisher);
            var category = new Category {
                CategoryID = book.Category
            };

            _context.Attach(category);
            string Img;

            if (book.Image != null)
            {
                Img = _imageService.SaveImg(book.Image);
            }
            else
            {
                Img = " ";
            }
            var newBook = new Book
            {
                BookImgUrl      = Img,
                ISBN            = book.ISBN,
                Title           = book.Title,
                Category        = category,
                Publisher       = publisher,
                PublicationDate = book.PublicationDate,
                Edition         = book.Edition,
                Pagination      = book.Pagination,
                Price           = book.Price,
                Plot            = book.Plot,
            };

            return(newBook);
        }
예제 #9
0
 public IActionResult UpdateBook(Guid id, [FromBody] UpdateBookDTO updateBookDTO)
 {
     if (ModelState.IsValid)
     {
         UpdateBookCommand updateBookCommand = new UpdateBookCommand(updateBookDTO.Title, updateBookDTO.Genre, updateBookDTO.AuthorId, id, updateBookDTO.ReleaseYear);
         try
         {
             var result = _messages.Dispatch(updateBookCommand);
             return(Ok(result));
         }
         catch (DomainException ex)
         {
             _logger.LogError(ex.Message);
             return(Error(ex.Message));
         }
         catch (Exception ex)
         {
             _logger.LogCritical(ex.Message);
             return(StatusCode(500));
         }
     }
     return(BadRequest());
 }
예제 #10
0
 public async Task <IActionResult> UpdateBook(UpdateBookDTO model)
 {
     return(Ok(await _bookService.UpdateBook(model)));
 }