auto.bus_api/Server/Controllers/ReviewController.cs
cuqmbr 1ba0867390 refactor: optimize crud services
fix: data shaping removes fields from objects
2022-11-30 16:38:30 +02:00

86 lines
2.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Server.Services;
using SharedModels.DataTransferObjects;
using SharedModels.QueryParameters.Objects;
namespace Server.Controllers;
[Route("api/reviews")]
[ApiController]
public class ReviewController : ControllerBase
{
private readonly IReviewManagementService _reviewManagementService;
public ReviewController(IReviewManagementService reviewManagementService)
{
_reviewManagementService = reviewManagementService;
}
[HttpPost]
public async Task<IActionResult> AddReview(CreateReviewDto review)
{
var result = await _reviewManagementService.AddReview(review);
if (!result.isSucceed)
{
return result.actionResult;
}
return CreatedAtAction(nameof(GetReview), new {id = result.review.Id}, result.review);
}
[HttpGet]
public async Task<IActionResult> GetReviews([FromQuery] ReviewParameters parameters)
{
var result = await _reviewManagementService.GetReviews(parameters);
if (!result.isSucceed)
{
return result.actionResult;
}
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.pagingMetadata));
return Ok(result.reviews);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetReview(int id, [FromQuery] string? fields)
{
var result = await _reviewManagementService.GetReview(id, fields);
if (!result.isSucceed)
{
return result.actionResult;
}
return Ok(result.review);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateReview(int id, UpdateReviewDto review)
{
var result = await _reviewManagementService.UpdateReview(review);
if (!result.isSucceed)
{
return result.actionResult;
}
return Ok(result.review);
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteReview(int id)
{
var result = await _reviewManagementService.DeleteReview(id);
if (!result.isSucceed)
{
return result.actionResult;
}
return NoContent();
}
}