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

87 lines
2.5 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Server.Services;
using SharedModels.DataTransferObjects;
using SharedModels.QueryParameters.Objects;
namespace Server.Controllers;
[Route("api/vehicleEnrollments")]
[ApiController]
public class VehicleEnrollmentController : ControllerBase
{
private readonly IVehicleEnrollmentManagementService _vehicleEnrollmentManagementService;
public VehicleEnrollmentController(IVehicleEnrollmentManagementService vehicleEnrollmentManagementService)
{
_vehicleEnrollmentManagementService = vehicleEnrollmentManagementService;
}
[HttpPost]
public async Task<IActionResult> AddEnrollment(CreateVehicleEnrollmentDto enrollment)
{
var result = await _vehicleEnrollmentManagementService.AddEnrollment(enrollment);
if (!result.isSucceed)
{
return result.actionResult;
}
return CreatedAtAction(nameof(GetEnrollment), new {id = result.enrollment.Id}, result.enrollment);
}
[HttpGet]
public async Task<IActionResult> GetEnrollments([FromQuery] VehicleEnrollmentParameters parameters)
{
var result = await _vehicleEnrollmentManagementService.GetEnrollments(parameters);
if (!result.isSucceed)
{
return result.actionResult;
}
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.pagingMetadata));
return Ok(result.enrollments);
}
[HttpGet("{id}")]
public async Task<IActionResult> GetEnrollment(int id, [FromQuery] string? fields)
{
var result = await _vehicleEnrollmentManagementService.GetEnrollment(id, fields);
if (!result.isSucceed)
{
return result.actionResult;
}
return Ok(result.enrollment);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdateVehicle(int id, UpdateVehicleEnrollmentDto enrollment)
{
var result = await _vehicleEnrollmentManagementService.UpdateEnrollment(enrollment);
if (!result.isSucceed)
{
return result.actionResult;
}
return Ok(result.enrollment);
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteEnrollment(int id)
{
var result = await _vehicleEnrollmentManagementService.DeleteEnrollment(id);
if (!result.isSucceed)
{
return result.actionResult;
}
return NoContent();
}
}