Exemple #1
0
        public async Task <IActionResult> Create(CreateNewAuthorViewModel newAuthor)
        {
            //Validation
            if (ModelState.IsValid)
            {
                var result = await _authorService.Create(newAuthor);

                if (result > 0)
                {
                    Console.WriteLine("Mistake!");
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View());
            }
        }
 //creating new author
 public async Task <int> Create(CreateNewAuthorViewModel newAuthor)
 {
     if (newAuthor.Books == null)
     {
         _db.Authors.Add(new Author()
         {
             FullName = newAuthor.FullName, Dob = newAuthor.Dob
         });
     }
     else
     {
         var author = new Author()
         {
             FullName     = newAuthor.FullName,
             Dob          = newAuthor.Dob,
             BooksAuthors = newAuthor.Books.Select(x => new BooksAuthor()
             {
                 BookId = x
             }).ToList()
         };
         _db.Authors.Add(author);
     }
     return(await _db.SaveChangesAsync());
 }