Beispiel #1
0
        public IActionResult Edit([Bind("Title, ReleaseDate, Description, SelectedWriterID, SelectedGenreID")] CreateOrEditBookDTO createBookDTO, int id)
        {
            var book = this.BooksRepository.GetById(id);

            if (string.IsNullOrEmpty(createBookDTO.Title))
            {
                createBookDTO.Writers = this.WritersRepository.GetAll().ToList();
                createBookDTO.Genres  = this.GenresRepository.GetAll().ToList();

                var indexOfWriter = createBookDTO.Writers.FindIndex(b => b.Id == book.IdWriter);
                var tempWriter    = createBookDTO.Writers[0];
                createBookDTO.Writers[0]             = createBookDTO.Writers[indexOfWriter];
                createBookDTO.Writers[indexOfWriter] = tempWriter;

                var indexOfGenre = createBookDTO.Genres.FindIndex(g => g.Id == book.IdGenre);
                var tempGenre    = createBookDTO.Genres[0];
                createBookDTO.Genres[0]            = createBookDTO.Genres[indexOfGenre];
                createBookDTO.Genres[indexOfGenre] = tempGenre;

                createBookDTO.Title       = book.Title;
                createBookDTO.ReleaseDate = book.ReleaseDate;
                createBookDTO.Description = book.Description;

                return(View(createBookDTO));
            }
            else
            {
                book.Title       = createBookDTO.Title;
                book.ReleaseDate = createBookDTO.ReleaseDate;
                book.Description = createBookDTO.Description;
                book.IdGenre     = createBookDTO.SelectedGenreID;
                book.IdWriter    = createBookDTO.SelectedWriterID;
                this.BooksRepository.Update(book);
                this.BooksRepository.Save();

                return(RedirectToAction("Index"));
            }
        }
Beispiel #2
0
        public IActionResult Create([Bind("Title, ReleaseDate, Description, SelectedWriterID, SelectedGenreID")] CreateOrEditBookDTO createBookDTO)
        {
            if (string.IsNullOrEmpty(createBookDTO.Title))
            {
                createBookDTO.Writers = this.WritersRepository.GetAll().ToList();
                createBookDTO.Genres  = this.GenresRepository.GetAll().ToList();

                return(View(createBookDTO));
            }
            else
            {
                var res = this.BooksRepository.Insert(new Book()
                {
                    Title       = createBookDTO.Title,
                    ReleaseDate = createBookDTO.ReleaseDate,
                    IdWriter    = createBookDTO.SelectedWriterID,
                    IdGenre     = createBookDTO.SelectedGenreID,
                    Description = createBookDTO.Description
                });
                this.BooksRepository.Save();
                return(RedirectToAction("Index"));
            }
        }