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), 200)] [ProducesResponseType(500)] public async Task 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 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), 400)] [ProducesResponseType(500)] [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")] public async Task 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 UpdateVehiceFloorPricingParameters(VehicleFloorPricingParameters[] vehicleFloorPricingParameters) { var result = new List(); foreach (var param in vehicleFloorPricingParameters) { result.Add(await _parametersService.UpdateAsync(param)); } return result.ToArray(); } } }