示例#1
0
        public async Task <IHttpActionResult> Get()
        {
            using (var context = new LetsJWTContext())
            {
                var books = await context.Books.ToListAsync();

                return(Ok(books));
            }
        }
示例#2
0
        public async Task <IHttpActionResult> Delete(int id)
        {
            using (var context = new LetsJWTContext())
            {
                var review = await context.Reviews.FirstOrDefaultAsync(r => r.Id == id);

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

                context.Reviews.Remove(review);
                await context.SaveChangesAsync();
            }
            return(Ok());
        }
示例#3
0
        public async Task <IHttpActionResult> Post([FromBody] ReviewViewModel review)
        {
            using (var context = new LetsJWTContext())
            {
                var book = await context.Books.FirstOrDefaultAsync(b => b.Id == review.BookId);

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

                var newReview = context.Reviews.Add(new Review
                {
                    BookId      = book.Id,
                    Description = review.Description,
                    Rating      = review.Rating
                });

                await context.SaveChangesAsync();

                return(Ok(new ReviewViewModel(newReview)));
            }
        }