151 lines
5.3 KiB
C#
151 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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.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 AuctionHouseController : RepoControllerBase
|
|
{
|
|
private readonly IAuctionHouseService _auctionHouseService;
|
|
|
|
public AuctionHouseController(
|
|
IAuctionHouseService auctionHouseService,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
: base( httpContextAccessor)
|
|
{
|
|
_auctionHouseService = auctionHouseService;
|
|
}
|
|
|
|
[HttpGet(Name = "GetAllAuctionHouses")]
|
|
[ProducesResponseType(typeof(IEnumerable<AuctionHouse>), 200)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
try
|
|
{
|
|
var auctions = await _auctionHouseService.ListAllAsync();
|
|
return Ok(auctions);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id}", Name = "GetAuctionHouseById")]
|
|
[ProducesResponseType(typeof(AuctionHouse), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> GetById(string id)
|
|
{
|
|
try
|
|
{
|
|
var auction = await _auctionHouseService.GetAsync(id);
|
|
return Ok(auction);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(AuctionHouse), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public async Task<IActionResult> Post([FromBody] AuctionHouse auction)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var newAuction = await _auctionHouseService.SaveAsync(auction);
|
|
return Ok(newAuction);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(AuctionHouse), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public async Task<IActionResult> Put(string id, [FromBody] AuctionHouse auction)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
if (id != auction.AuctionID)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var updatedAuction = await _auctionHouseService.UpdateAsync(auction);
|
|
return Ok(updatedAuction);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(typeof(AuctionHouse), 200)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
|
|
public async Task<IActionResult> Delete(string id)
|
|
{
|
|
try
|
|
{
|
|
var deletedAuction = await _auctionHouseService.DeleteAsync(id);
|
|
return Ok(deletedAuction);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
} |