Esempio n. 1
0
        public IActionResult Create([FromBody] Author author)
        {
            if (author == null)
            {
                return(BadRequest());
            }

            _context.Authors.Add(author);
            _context.SaveChanges();

            return(CreatedAtRoute("GetAuthors", new { id = author.ID }, author));
        }
Esempio n. 2
0
 public ActionResult Insert(Authors author)
 {
     if (ModelState.IsValid)
     {
         db.Authors.Add(author);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     else
     {
         return(View(author));
     }
 }
        public ActionResult AddArticle(Article art)
        {
            art.Date = DateTime.Now;

            db.Articles.Add(art);
            db.SaveChanges();
            return(View("Index"));
        }
Esempio n. 4
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 }));
        }
Esempio n. 5
0
        public AuthorsController(AuthorContext context)
        {
            this._context = context;

            if (this._context.AuthorsList.Count() == 0)
            {
                _context.AuthorsList.Add(new Author {
                    Name = "First Author"
                });
                _context.SaveChanges();
            }
        }
Esempio n. 6
0
        public AuthorController(AuthorContext context)
        {
            _context = context;

            if (_context.Authors.Count() == 0)
            {
                _context.Authors.Add(new Author {
                    firstName = "FirstName", lastName = "LastName"
                });
                _context.SaveChanges();
            }
        }
Esempio n. 7
0
 public AuthorController(AuthorContext context)
 {
     _context = context;
     if (_context.authors.Count() == 0)
     {
         _context.authors.Add(new Author {
             firstName = "Profesor",
             lastName  = "Uno",
             bornDate  = DateTime.Now.Date
         });
         _context.SaveChanges();
     }
 }
Esempio n. 8
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                Author user = null;
                using (AuthorContext db = new AuthorContext())
                {
                    user = db.Authors.FirstOrDefault(u => u.Email == model.Name);
                }
                if (user == null)
                {
                    // создаем нового пользователя
                    using (AuthorContext db = new AuthorContext())
                    {
                        if (model.Email != null && model.Name != null && model.Password != null)
                        {
                            db.Authors.Add(new Author
                            {
                                Email = model.Email, Name = model.Name, Password = model.Password
                            });
                            db.SaveChanges();
                            user = db.Authors.Where(u => u.Email == model.Email && u.Password == model.Password)
                                   .FirstOrDefault();
                        }
                        else
                        {
                            ModelState.AddModelError("", "Please, enter registration data");
                        }
                    }
                    // если пользователь удачно добавлен в бд
                    if (user != null)
                    {
                        FormsAuthentication.SetAuthCookie(model.Email, true);
                        return(RedirectToAction("AddArticle", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "This login already exists in another user");
                }
            }

            return(View(model));
        }
Esempio n. 9
0
 public void Post([FromBody] Author author)
 {
     _context.AuthorsList.Add(author);
     _context.SaveChanges();
 }
Esempio n. 10
0
 public void Create(BookTitle bookTitle)
 {
     _context.Add(bookTitle);
     _context.SaveChanges();
 }
Esempio n. 11
0
        private static void SetupDB()
        {
            Genre fantasy = new Genre
            {
                Name = "Fantasy"
            };

            Genre urban = new Genre
            {
                Name = "Urban"
            };

            Genre superheroes = new Genre
            {
                Name = "Superheroes"
            };

            Genre scifi = new Genre
            {
                Name = "Scifi"
            };

            Genre magic = new Genre
            {
                Name = "Magic"
            };


            Book b1 = new()
            {
                Title           = "Rythm of War",
                PageCount       = 1232,
                PublicationYear = 2020,
                ISBN            = "0765326388",
                Genres          = new List <Genre> {
                    fantasy, magic
                }
            };

            Book b2 = new()
            {
                Title           = "The Alloy of Law",
                PageCount       = 332,
                PublicationYear = 2011,
                ISBN            = "0765330423",
                Genres          = new List <Genre> {
                    urban, fantasy, magic
                }
            };
            Book b3 = new()
            {
                Title           = "Steelheart",
                PageCount       = 386,
                PublicationYear = 2013,
                ISBN            = "0385743564",
                Genres          = new List <Genre> {
                    superheroes, urban, scifi
                }
            };
            Book b4 = new()
            {
                Title           = "Judgment of Mars",
                PageCount       = 270,
                PublicationYear = 2017,
                ISBN            = "34538192",
                Genres          = new List <Genre> {
                    scifi, fantasy, magic
                }
            };
            Book b5 = new()
            {
                Title           = "Blood of the Innocent",
                PageCount       = 322,
                PublicationYear = 2017,
                ISBN            = "35823660",
                Genres          = new List <Genre> {
                    urban, fantasy
                }
            };

            Author brandon = new Author
            {
                FirstName = "Brandon",
                LastName  = "Sanderson",
                Address   = new()
                {
                    Street      = "Nowhere",
                    HouseNumber = 3,
                    PostCode    = 837
                },
                Books = new List <Book> {
                    b1, b2, b3
                }
            };

            Author glynn = new Author
            {
                FirstName = "Glynn",
                LastName  = "Stewart",
                Address   = new()
                {
                    Street      = "Anywhere",
                    HouseNumber = 7,
                    PostCode    = 824
                },
                Books = new List <Book> {
                    b4, b5
                }
            };

            using AuthorContext ac = new AuthorContext();
            ac.Add(brandon);
            ac.Add(glynn);
            ac.SaveChanges();
        }
    }
}
 public void Create(Author author)
 {
     _context.Add(author);
     _context.SaveChanges();
 }
Esempio n. 13
0
 public void Create(Reviews review)
 {
     _context.Add(review);
     _context.SaveChanges();
 }
Esempio n. 14
0
 public void AddAuthor(Author author)
 {
     AuthorContext.Author.Add(author);
     AuthorContext.SaveChanges();
 }