134 lines
4.3 KiB
C#
134 lines
4.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.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 CollateralController : RepoControllerBase
|
|
{
|
|
private readonly ICollateralService _collateralService;
|
|
|
|
public CollateralController(
|
|
ICollateralService collateralService,
|
|
IHttpContextAccessor httpContextAccessor) :
|
|
base(httpContextAccessor)
|
|
{
|
|
_collateralService = collateralService;
|
|
}
|
|
|
|
// GET: api/Collateral
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(IEnumerable<Collateral>), 200)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
try
|
|
{
|
|
var results = await _collateralService.ListAll();
|
|
return Ok(results);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// GET: api/Collateral/5
|
|
[HttpGet("{id}", Name = "GetCollateralById")]
|
|
[ProducesResponseType(typeof(Collateral), 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 _collateralService.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);
|
|
}
|
|
}
|
|
|
|
// POST: api/Collateral
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(Collateral), 201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Post([FromBody] Collateral collateral)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var result = await _collateralService.SaveAsync(collateral);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// PUT: api/Collateral/5
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(Collateral), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Put(int id, [FromBody] Collateral collateral)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (id <= 0 || id != collateral.CollateralId)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var result = await _collateralService.UpdateAsync(collateral);
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |