//updating author
        public async Task <int> UpdateAuthor(AuthorUpdatingModel updatedAuthor)
        {
            var author = await _db.Authors.Include(x => x.BooksAuthors)
                         .SingleOrDefaultAsync(x => x.Id == updatedAuthor.Id);

            author.Dob          = updatedAuthor.Dob;
            author.FullName     = updatedAuthor.FullName;
            author.BooksAuthors = updatedAuthor.Books?
                                  .Select(x => new BooksAuthor()
            {
                BookId = x, AuthorId = updatedAuthor.Id
            }).ToList();
            _db.Authors.Update(author);
            return(await _db.SaveChangesAsync());
        }
Example #2
0
        public async Task <IActionResult> Edit(AuthorUpdatingModel editingAuthor)
        {
            //Validation
            if (ModelState.IsValid)
            {
                var result = await _authorService.UpdateAuthor(editingAuthor);

                if (result < 0)
                {
                    Console.WriteLine("Mistake!");
                }
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View(await _authorService.GetAuthor(editingAuthor.Id)));
            }
        }