Пример #1
0
        public async Task <ActionResult <BookResource> > GetBooks(int id)
        {
            var bookFromRepo = await _bookRepository.Get(id);

            if (bookFromRepo == null)
            {
                return(NotFound());
            }
            var listOfAuthorResource = new List <AuthorCreateResource>();

            foreach (var author in bookFromRepo.Authors)
            {
                var authorResource = new AuthorCreateResource
                {
                    Id       = author.Id,
                    FullName = author.FullName
                };
                listOfAuthorResource.Add(authorResource);
            }


            var bookResource = new BookResource
            {
                Id            = bookFromRepo.Id,
                Title         = bookFromRepo.Title,
                Description   = bookFromRepo.Description,
                IsAvailable   = bookFromRepo.IsAvailable,
                Publisher     = bookFromRepo.Publisher.Name,
                PublishedDate = bookFromRepo.PublishedDate,
                AuthorNames   = listOfAuthorResource
                                //AuthorNames = bookFromRepo.Authors.Select(e => e.FullName).ToList()
            };

            return(Ok(bookResource));
        }
Пример #2
0
        public async Task <ActionResult <AuthorCreateResource> > PostAuthor([FromBody] AuthorModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Here map (model) -> entity

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

            if (authorModel.Email == null && authorModel.Age == null)
            {
                return(BadRequest("At least some name must be added"));
            }
            authorEntity.Email = authorModel.Email;
            authorEntity.Age   = authorModel.Age;
            // Entity from Book
            var newAuthor = await _authorRepository.Create(authorEntity);

            // Here map (newBook which is Entity) -> Resource
            var authorResource = new AuthorCreateResource
            {
                Id       = newAuthor.Id,
                FullName = newAuthor.FullName,
                //Age = newAuthor.Age,
                //Email = newAuthor.Email
            };


            return(CreatedAtAction(nameof(GetAuthors), new { id = newAuthor.Id }, authorResource));
        }
Пример #3
0
        public async Task <ActionResult <AuthorCreateResource> > PutAuthor(int id, [FromBody] AuthorModel authorModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // You can make it like Yazan said from his document.
            var authorToUpdate = await _authorRepository.Get(id);

            if (authorToUpdate == null)
            {
                return(NotFound());
            }

            authorToUpdate.FirstName = authorModel.FirstName;
            authorToUpdate.LastName  = authorModel.LastName;
            authorToUpdate.Email     = authorModel.Email;
            authorToUpdate.Age       = authorModel.Age;


            await _authorRepository.Update(authorToUpdate);

            var authorResource = new AuthorCreateResource
            {
                Id       = authorToUpdate.Id,
                FullName = authorToUpdate.FullName,
                //Age = authorToUpdate.Age,
                //Email = authorToUpdate.Email
            };

            //JObject obj = (JObject)JToken.FromObject(bookResource);
            //Console.WriteLine(bookResource);
            return(Ok(authorResource));
        }
Пример #4
0
        public async Task <ActionResult <BookResource> > PostBook([FromBody] BookModel bookModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // Here map (model) -> entity
            var authors = _authorRepository.Get().Result.ToList().Where(e => bookModel.AuthorIds.Contains(e.Id)).ToList();

            if (authors.Count < bookModel.AuthorIds.Count)
            {
                throw new Exception("Id is not correct");
            }

            var bookEntity = new Book
            {
                Title         = bookModel.Title,
                Description   = bookModel.Description,
                IsAvailable   = bookModel.IsAvailable,
                PublisherId   = bookModel.PublisherId,
                PublishedDate = bookModel.PublishedDate,
                Authors       = authors
            };

            // insert this record to database by repo
            var newBook = await _bookRepository.Create(bookEntity);

            // map author list into book resource
            var listOfAuthorResource = new List <AuthorCreateResource>();

            foreach (var author in authors)
            {
                var authorResource = new AuthorCreateResource
                {
                    Id       = author.Id,
                    FullName = author.FullName
                };
                listOfAuthorResource.Add(authorResource);
            }

            // Here map (newBook which is Entity) -> Resource
            var bookResource = new BookResource
            {
                Id            = newBook.Id,
                Title         = newBook.Title,
                Description   = newBook.Description,
                IsAvailable   = newBook.IsAvailable,
                Publisher     = newBook.Publisher.Name,
                PublishedDate = newBook.PublishedDate,
                AuthorNames   = listOfAuthorResource
            };


            return(CreatedAtAction(nameof(GetBooks), new { id = newBook.Id }, bookResource));
        }
Пример #5
0
        public async Task <ActionResult <BookResource> > PutBook(int id, [FromBody] BookModel bookModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            // You can make it like Yazan said from his document.
            var bookToUpdate = await _bookRepository.Get(id);

            if (bookToUpdate == null)
            {
                return(NotFound());
            }

            var authors = _authorRepository.Get().Result.ToList().Where(e => bookModel.AuthorIds.Contains(e.Id)).ToList();

            if (authors.Count < bookModel.AuthorIds.Count)
            {
                throw new Exception("Id is not correct");
            }

            bookToUpdate.Title         = bookModel.Title;
            bookToUpdate.Description   = bookModel.Description;
            bookToUpdate.IsAvailable   = bookModel.IsAvailable;
            bookToUpdate.PublisherId   = bookModel.PublisherId;
            bookToUpdate.PublishedDate = bookModel.PublishedDate;
            bookToUpdate.Authors       = authors;

            if (bookToUpdate == null)
            {
                return(NotFound());
            }
            await _bookRepository.Update(bookToUpdate);

            // map authors list into book resource
            var listOfAuthorResource = new List <AuthorCreateResource>();

            foreach (var author in authors)
            {
                var authorResource = new AuthorCreateResource
                {
                    Id       = author.Id,
                    FullName = author.FullName
                };
                listOfAuthorResource.Add(authorResource);
            }

            var bookResource = new BookResource
            {
                Id            = bookToUpdate.Id,
                Title         = bookToUpdate.Title,
                Description   = bookToUpdate.Description,
                IsAvailable   = bookToUpdate.IsAvailable,
                Publisher     = bookToUpdate.Publisher.Name,
                PublishedDate = bookToUpdate.PublishedDate,
                AuthorNames   = listOfAuthorResource
            };

            //JObject obj = (JObject)JToken.FromObject(bookResource);
            //Console.WriteLine(bookResource);
            return(Ok(bookResource));
        }