using System; using System.Collections.Generic; using System.Net; 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.Filters; 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.Exceptions; using LCU.Shared.Collections.RepoApiLib.Models; 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 AuctionAssignmentController : RepoControllerBase { private readonly IAuctionAssignmentService _auctionAssignmentService; private readonly IFloorPricingService _floorPricingService; public AuctionAssignmentController( IAuctionAssignmentService auctionAssignmentService, IFloorPricingService floorPricingService, IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor) { _auctionAssignmentService = auctionAssignmentService; _floorPricingService = floorPricingService; } // GET: api/AuctionAssignment [HttpGet(Name = "GetAllAuctionAssignments")] [ProducesResponseType(typeof(PagedResponse), 200)] [ProducesResponseType(500)] public async Task Get([FromQuery] AuctionAssignmentFilter filter) { try { var searchResult = await _auctionAssignmentService.ListByFilterAsync(filter); var result = new PagedResponse(searchResult, filter, this.Url, Request); return Ok(result); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // GET: api/AuctionAssignment/5 [HttpGet("{id}", Name = "GetAuctionAssignmentById")] [ProducesResponseType(typeof(AuctionAssignment), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(500)] public async Task GetById(int id) { try { if (id <= 0) { return BadRequest("Invalid ID"); } var result = await _auctionAssignmentService.GetByIdAsync(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); } } // Get: api/AuctionAssignment/repoId/7 [HttpGet("repoId/{repoId}", Name = "GetAuctionAssignmentsByRepoId")] [ProducesResponseType(typeof(IEnumerable), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(500)] public async Task GetByRepoId(int repoId) { try { if (repoId <= 0) { return BadRequest("Invalid ID"); } var result = await _auctionAssignmentService.GetByRepoIdAsync(repoId); 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); } } // GET: api/AuctionAssignment/5/FloorPrice [HttpGet("{id}/FloorPrice", Name = "GetAuctionAssignmentFloorPriceById")] [ProducesResponseType(typeof(FloorPriceCalculationResultSet), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(500)] public IActionResult GetAuctionAssignmentFloorPriceById(int id) { try { if (id <= 0) { return BadRequest("Invalid ID"); } var result = _floorPricingService.DetermineFloorPrice(id); return Ok(result); } catch (NotFoundException nfe) { Log.Error(nfe, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // POST: api/AuctionAssignment [HttpPost] [ProducesResponseType(typeof(AuctionAssignmentPostRequest), 201)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(500)] public async Task Post([FromBody] AuctionAssignmentPostRequest auctionAssignmentPostRequest) { try { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorMessages()); } var result = await _auctionAssignmentService.SaveAsync(auctionAssignmentPostRequest); return Ok(result); } catch (WebException we) { Log.Error(we, $"Failure in {this.GetType().Name} at [AuctionAssignmentPostRequest]"); return StatusCode(StatusCodes.Status500InternalServerError, we.Message); } catch (DuplicateResourceException) { return StatusCode(StatusCodes.Status409Conflict, "An active auction assignment already exists. You must remove the auction before creating a new one."); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError, "Failed to add auction to AutoIMS."); } } // PUT: api/AuctionAssignment/5 [HttpPut("{id}")] [ProducesResponseType(typeof(AuctionAssignment), 200)] [ProducesResponseType(typeof(List), 400)] [ProducesResponseType(500)] public async Task Put(int id, [FromBody] AuctionAssignment auctionAssignment) { try { if (!ModelState.IsValid) { return BadRequest(ModelState.GetErrorMessages()); } if (id <= 0 || id != auctionAssignment.ID) { return BadRequest("Invalid ID"); } var result = await _auctionAssignmentService.UpdateAsync(auctionAssignment); return Ok(result); } catch (NotFoundException nfe) { Log.Error(nfe, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError); } } // DELETE: api/ApiWithActions/5 [HttpDelete("{id}")] [ProducesResponseType(typeof(AuctionAssignment), 200)] [ProducesResponseType(500)] public async Task Delete(int id) { try { if (id <= 0) { return BadRequest("Invalid ID"); } var deletedRepoCompany = await _auctionAssignmentService.RemoveAuction(id); return Ok(deletedRepoCompany); } catch (WebException we) { return StatusCode(StatusCodes.Status500InternalServerError, we.Message); } catch (NotFoundException nfe) { return NotFound(nfe.Message); } catch (Exception ex) { Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}"); return StatusCode(StatusCodes.Status500InternalServerError, "Failed to remove auction from AutoIMS."); } } } }