Beispiel #1
0
        public void Add([FromBody] BookViewModel viewModel)
        {
            Book book = new Book()
            {
                Title          = viewModel.Title,
                PagesCount     = viewModel.PagesCount,
                Price          = viewModel.Price,
                PublishingYear = viewModel.PublishingYear,
                EncodedPhoto   = viewModel.Image,
                Count          = viewModel.Count,
                Description    = viewModel.Description,
                Rating         = 0
            };

            bookRepository.Add(book);

            foreach (string name in viewModel.Authors)
            {
                BookToAuthor bookToAuthor = new BookToAuthor {
                    BookId = book.Id, AuthorId = authorRepository.GetByName(name).Id
                };
                bookToAuthorRepository.Add(bookToAuthor);
            }

            foreach (string genre in viewModel.Genres)
            {
                BookToGenre bookToGenre = new BookToGenre {
                    BookId = book.Id, Genre = (Genre)Enum.Parse(typeof(Genre), genre)
                };
                bookToGenreRepository.Add(bookToGenre);
            }
            ;
        }
Beispiel #2
0
        public void Update([FromBody] BookViewModel viewModel)
        {
            Book book = bookRepository.GetById(viewModel.Id);

            if (book != null)
            {
                book.Id             = viewModel.Id;
                book.Title          = viewModel.Title;
                book.PagesCount     = viewModel.PagesCount;
                book.Price          = viewModel.Price;
                book.PublishingYear = viewModel.PublishingYear;
                book.EncodedPhoto   = viewModel.Image;
                book.Count          = viewModel.Count;
                book.Description    = viewModel.Description;

                book.BookToGenres.Clear();
                book.BookToAuthors.Clear();
                foreach (var genre in viewModel.Genres)
                {
                    BookToGenre bookToGenre = new BookToGenre {
                        BookId = book.Id, Genre = (Genre)Enum.Parse(typeof(Genre), genre)
                    };
                    book.BookToGenres.Add(bookToGenre);
                }


                foreach (var author in viewModel.Authors)
                {
                    BookToAuthor bookToAuthor = new BookToAuthor {
                        BookId = book.Id, AuthorId = authorRepository.GetByName(author).Id
                    };
                    book.BookToAuthors.Add(bookToAuthor);
                }

                bookRepository.Update(book);
            }
            ;
        }