Files
2026-04-20 14:47:24 -05:00

119 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using LCU.API.Collections.RepoApi.Configuration;
using LCU.API.Collections.RepoApi.Exceptions;
using LCU.API.Collections.RepoApi.Extensions;
using LCU.API.Collections.RepoApi.Models.Requests;
using LCU.API.Collections.RepoApi.Models.Responses;
using LCU.API.Collections.RepoApi.Services;
using LCU.Shared.Collections.RepoApiLib.Models;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement.Mvc;
using Serilog;
namespace LCU.API.Collections.RepoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
[FeatureGate(FeatureFlags.EnableControllers)]
public class VehicleFloorPricingParametersController : RepoControllerBase
{
private readonly IVehicleFloorPricingParametersService _parametersService;
public VehicleFloorPricingParametersController(
IVehicleFloorPricingParametersService parametersService,
IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
{
_parametersService = parametersService;
}
// GET: api/VehicleFloorPricingParameters
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<VehicleFloorPricingParameters>), 200)]
[ProducesResponseType(500)]
public async Task<IActionResult> Get()
{
try
{
var results = await _parametersService.ListAll();
return Ok(results);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
// GET: api/VehicleFloorPricingParameters/5
[HttpGet("{id}", Name = "GetVehicleFloorPricingParametersById")]
[ProducesResponseType(typeof(VehicleFloorPricingParameters), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(500)]
public async Task<IActionResult> GetById(int id)
{
try
{
if (id <= 0)
{
return BadRequest("Invalid ID");
}
var result = await _parametersService.Get(id);
return Ok(result);
}
catch (NotFoundException nfe)
{
return NotFound(nfe.Message);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
[HttpPut()]
[ProducesResponseType(typeof(AuctionHouse), 200)]
[ProducesResponseType(typeof(List<string>), 400)]
[ProducesResponseType(500)]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
public async Task<IActionResult> Put([FromBody] VehicleFloorPricingParametersPutRequest request)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState.GetErrorMessages());
}
var updatedParams = await UpdateVehiceFloorPricingParameters(request.vehicleFloorPricingParameters);
return Ok(new VehicleFloorPricingParametersPutResponse { vehicleFloorPricingParameters = updatedParams });
}
catch (NotFoundException nfe)
{
return NotFound(nfe.Message);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
private async Task<VehicleFloorPricingParameters[]> UpdateVehiceFloorPricingParameters(VehicleFloorPricingParameters[] vehicleFloorPricingParameters)
{
var result = new List<VehicleFloorPricingParameters>();
foreach (var param in vehicleFloorPricingParameters)
{
result.Add(await _parametersService.UpdateAsync(param));
}
return result.ToArray();
}
}
}