Exemplo n.º 1
0
        public ActionResult Add([Bind("BookId", "LibraryId")] BookLibraryDto BookLibrary)
        {
            if (BookLibrary.BookId == 0)
            {
                return(HttpBadRequest("Could not add book to library. Book id:" + BookLibrary.BookId + " is not a valid book id."));
            }

            if (BookLibrary.LibraryId == 0)
            {
                return(HttpBadRequest("Could not add book to library. Library id:" + BookLibrary.LibraryId + " is not a valid library id."));
            }

            var existingBook = _bookService.Get(BookLibrary.BookId);

            if (existingBook == null)
            {
                return(HttpBadRequest("Could not add book to library. Book id:" + BookLibrary.BookId + " does not exist."));
            }

            var existingLibrary = _libraryService.Get(BookLibrary.LibraryId);

            if (existingLibrary == null)
            {
                return(HttpBadRequest("Could not add book to library. Library id:" + BookLibrary.LibraryId + " does not exist."));
            }

            BookLibrary addedBookLibrary = null;

            if (ModelState.IsValid)
            {
                var newBookLibrary = new BookLibrary
                {
                    Book    = existingBook,
                    Library = existingLibrary
                };

                addedBookLibrary = _bookService.AddToLibrary(newBookLibrary);
            }

            if (addedBookLibrary == null)
            {
                return(HttpBadRequest("Could not add book to library."));
            }

            return(new JsonResult(addedBookLibrary));
        }
        public async Task AddBookLibrary(BookLibraryDto input)
        {
            List <string> errorList = new List <string>();

            var value = new BookLibrary
            {
                BookId    = input.BookId,
                LibraryId = input.LibraryId,
                Stock     = input.Stock
            };

            BookLibraryValidator validator        = new BookLibraryValidator();
            ValidationResult     validationResult = validator.Validate(value);

            var datas = _bookLibraryRepository.GetAll();

            if (!validationResult.IsValid)
            {
                foreach (var failure in validationResult.Errors)
                {
                    errorList.Add(string.Format("{0}", failure.ErrorMessage));
                }
                string errorString = string.Join(" ", errorList.ToArray());
                throw new UserFriendlyException(errorString);
            }

            var data = datas.Where(d => d.BookId == input.BookId && d.LibraryId == input.LibraryId).ToList();

            if (data.Count != 0)
            {
                foreach (var ed in data)
                {
                    ed.Stock = ed.Stock + input.Stock;

                    await _bookLibraryRepository.UpdateAsync(ed);
                }
            }
            else
            {
                await _bookLibraryRepository.InsertAsync(value);
            }
        }
        public async Task UpdateBookLibrary(BookLibraryDto input)
        {
            List <string> errorList = new List <string>();

            var data = await GetBookLibraryById(input.Id);

            data.Stock = input.Stock;

            BookLibraryValidator validator        = new BookLibraryValidator();
            ValidationResult     validationResult = validator.Validate(data);

            if (!validationResult.IsValid)
            {
                foreach (var failure in validationResult.Errors)
                {
                    errorList.Add(string.Format("{0}", failure.ErrorMessage));
                }
                string errorString = string.Join(" ", errorList.ToArray());
                throw new UserFriendlyException(errorString);
            }
            await _bookLibraryRepository.UpdateAsync(data);
        }