using Microsoft.AspNetCore.Mvc;
using TestEnvironment.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Text.RegularExpressions;
using System.Linq;

namespace TestEnvironment.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    public class ReviewController : ControllerBase
    {

        private readonly BookDbContext _bookDbContext;
        public ReviewController(BookDbContext bookDbContext)
        {
            _bookDbContext = bookDbContext;
        }


        [HttpGet]
        public ActionResult<IEnumerable<Review>> GetReviews()
        {
            return _bookDbContext.Review;
        }


        [HttpGet("{id:int}")]

        public async Task<ActionResult<Review>> GetReviewById(int id)
        {
            var review = await _bookDbContext.Review.FindAsync(id);
            return Ok(review);
        }



        [HttpPost]

        public async Task<ActionResult> CreateReview(Review review)
        {
            await _bookDbContext.Review.AddAsync(review);
            await _bookDbContext.SaveChangesAsync();
            return Ok();
        }


        [HttpPut]

        public async Task<ActionResult> UpdateReview(Review review)
        {
            _bookDbContext.Review.Update(review);
            await _bookDbContext.SaveChangesAsync();
            return Ok();
        }


        [HttpDelete("{id:int}")]

        public async Task<ActionResult> DeleteReview(int id)
        {
            var review = await _bookDbContext.Review.FindAsync(id);
            _bookDbContext.Review.Remove(review);
            return Ok();
        }
    }
}