public IHttpActionResult Post(AddAuthorBindingModel model)
 {
     Author author = new Author { FirstName = model.FirstName, LastName = model.LastName};
     _context.Authors.Add(author);
     _context.SaveChanges();
     return this.Ok(author);
 }
 public static AuthorViewModel CreateViewModel(Author a)
 {
     return new AuthorViewModel
     {
         Id = a.Id,
         FirstName = a.FirstName,
         LastName = a.LastName
     };
 }
 public AuthorViewModel(Author author)
 {
     Id = author.Id;
     FirstName = author.FirstName;
     LastName = author.LastName;
     Books = new List<BookViewModel>();
     foreach (var book in author.Books)
     {
         Books.Add(new BookViewModel(book));
     }
 }
        public AuthorsBooksViewModel(Author author)
        {
            AuthorId = author.Id;

            AuthorBooks = new List<BookViewModel>();

            foreach (var book in author.Books)
            {
                AuthorBooks.Add(new BookViewModel(book));
            }
        }
        public IHttpActionResult PostAuthor(AddAuthorBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            Author newAuthor = new Author { FirstName = model.FirstName, LastName = model.LastName };
            this.context.Authors.Add(newAuthor);
            this.context.SaveChanges();
            return this.Ok(newAuthor);
        }
        public AuthorViewModel(Author author)
        {
            this.Id = author.Id;
            this.FirstName = author.FirstName;
            this.LastName = author.LastName;
            this.BookTitles = new List<AuthorBooksTitlesViewModel>();

            foreach (var book in author.Books)
            {
                this.BookTitles.Add(new AuthorBooksTitlesViewModel(book));
            }
        }
        public static AuthorViewModelMinified ConvertToAuthorViewModel(Author author)
        {
            string authorName = string.Format("{0} {1}", author.FirstName, author.LastName);

            AuthorViewModelMinified authorViewModel = new AuthorViewModelMinified
            {
                Id = author.Id,
                AuthorName = authorName
            };

            return authorViewModel;
        } 
        public static AuthorViewModel ConvertToAuthorViewModel(Author author)
        {
            AuthorViewModel authorViewModel = new AuthorViewModel
                {
                    Id = author.Id,
                    FirstName = author.FirstName,
                    LastName = author.LastName,
                    BookTitles = author.Books.Select(b => b.Title)
                };

            return authorViewModel;
        }
Exemple #9
0
        public static AuthorViewModel Create(Author author)
        {
            var authorView = new AuthorViewModel
            {
                Id = author.Id,
                FirstName = author.FirstName,
                LastName = author.LastName,
                BookTitles = new List<string>(),
                
            };

            AddBookTitles(authorView, author.Books);

            return authorView;
        }
        public IHttpActionResult PostAuthor(AddAuthorBindingModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var author = new Author()
            {
                FirstName = authorModel.FirstName,
                LastName = authorModel.LastName
            };

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

            return this.Ok("Author created");
        }
        public IHttpActionResult Create(AuthorBindindModel authorBindingModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var author = new Author()
            {
                FirstName = authorBindingModel.FirstName,
                LastName = authorBindingModel.LastName
            };

            this.data.Authors.Add(author);
            this.data.Save();

            AuthorViewModel authorViewModel = AuthorViewModel.ConvertToAuthorViewModel(author);
            return this.Ok(authorViewModel);
        }
        public IHttpActionResult PostAuthor([FromBody]AddAuthorBindingModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var authorToAdd = new Author()
            {
                FirstName = authorModel.FirstName,
                LastName = authorModel.LastName
            };

            context.Authors.AddOrUpdate(authorToAdd);

            context.SaveChanges();

            return this.Ok(authorToAdd);
        }
        public IHttpActionResult CreateAuthor([FromBody]AddAuthorBindingModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return this.BadRequest();
            }

            var author = new Author()
            {
                FirstName = authorModel.FirstName,
                LastName = authorModel.LastName
            };

            unitOfWork.AuthorRepository.Insert(author);

            unitOfWork.Save();

            return this.Ok(author);
        }
        public IHttpActionResult CreateAuthor(AuthorBindingModel authorModel)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newAuthorModel = new Author
            {
                FirstName = authorModel.FirstName,
                LastName = authorModel.LastName
            };

            this.BookShopData.Authors.Add(newAuthorModel);
            this.BookShopData.SaveChanges();

            var authorViewModel = AuthorViewModel.CreateViewModel(newAuthorModel);

            return this.Ok(authorViewModel);
        }
        public IHttpActionResult Post([FromBody]AuthorBindingModel author)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newAuthor = new Author()
            {
                FirstName = author.FirstName,
                LastName = author.LastName
            };

            db.Authors.Add(newAuthor);


            db.SaveChanges();

            return this.Ok(newAuthor);
        }
Exemple #16
0
        public IHttpActionResult AddNewAuthor([FromBody] AddAuthorBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (this.BookShopData.Authors.Any(a => a.FirstName == model.FirstName && a.LastName == model.LastName))
            {
                return this.BadRequest("Duplicate author.");
            }

            var author = new Author
            {
                FirstName = model.FirstName,
                LastName = model.LastName
            };

            this.BookShopData.Authors.Add(author);
            this.BookShopData.SaveChanges();

            return this.Ok(author);
        }
        public IHttpActionResult AddAuthor(AuthorBindingModel model)
        {

            if (model == null)
            {
                this.ModelState.AddModelError("model", "The model cannot be null - (request is empty).");
            }

            if (!ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var author = new Author()
            {
                FirstName = model.FirstName ?? null,
                LastName = model.LastName
            };

            this.Data.Authors.Add(author);
            this.Data.SaveChanges();

            return this.Ok();
        }
 //constructor include navigation properties only
 public Book()
 {
     this.categories = new HashSet<Category>();
     this.author = new Author();
     this.relatedBooks = new HashSet<Book>();
 }