Exemple #1
0
        public static async Task<BookModel> PopulateFullBookInfo(Book book)
        {
            if (book == null) return null;
            using (var authorRepo = new AuthorRepository())
            {
                using (var categoryRepo = new BookCategoryRepository())
                {
                    var model = new BookModel();
                    model.InjectFrom(book);
                    var author = await authorRepo.GetById(book.AuthorId);
                    var coAuthor = book.CoAuthorId != null && book.CoAuthorId > 0
                        ? await authorRepo.GetById(Convert.ToInt32(book.CoAuthorId)) : null;
                    var category = await categoryRepo.GetById(book.BookCategoryId);

                    if (author != null)
                        model.AuthorName = string.Format("{0} {1}", author.FirstName, author.LastName);
                    if (coAuthor != null)
                        model.CoAuthorName = string.Format("{0} {1}", coAuthor.FirstName, coAuthor.LastName);
                    if (category != null)
                        model.CategoryText = category.Name;

                    return model;
                }
            }
        } 
        public async Task<IHttpActionResult> SaveBook(BookModel bookModel)
        {
            if (ModelState.IsValid)
            {
                var entity = new Book();
                entity.InjectFrom(bookModel);

                return Ok(await _repository.Save(entity));
            }
            var errors = ModelState.Values.SelectMany(x => x.Errors);
            return BadRequest(Helper.GenerateModelStateError(errors));
        }