Пример #1
0
        public async Task <IActionResult> PutAuthor(long id, Author author)
        {
            if (id != author.Id)
            {
                return(BadRequest());
            }

            _context.Entry(author).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #2
0
        public ActionResult SaveAuthor(Author author, Book[] Books)
        {
            if (author == null)
            {
                return(HttpNotFound());
            }

            Author dbAuthor = db.Authors.Find(author.Id);

            if (dbAuthor == null)
            {
                return(HttpNotFound());
            }
            dbAuthor.Books.Clear();
            dbAuthor.FirstName  = author.FirstName;
            dbAuthor.LastName   = author.LastName;
            dbAuthor.Patronymic = author.Patronymic;

            foreach (Book book in author.Books)
            {
                Genre genreExists = db.Genres.Where(g => g.Name == book.Genre.Name).FirstOrDefault();
                if (genreExists == null)
                {
                    db.Genres.Add(book.Genre);
                }
                else
                {
                    book.Genre = genreExists;
                }

                if (book.Id == 0)
                {
                    db.Books.Add(book);
                }
                dbAuthor.Books.Add(book);
                db.SaveChanges();
            }

            db.Entry(dbAuthor).State = EntityState.Modified;
            db.SaveChanges();
            return(RedirectToAction("GetAuthor", new { id = author.Id }));
        }