252 lines
8.8 KiB
C#
252 lines
8.8 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.Exceptions;
|
|
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 EventAlertController : RepoControllerBase
|
|
{
|
|
private readonly IEventAlertService _eventAlertService;
|
|
private readonly IRepoAccountService _repoAccountService;
|
|
|
|
public EventAlertController(
|
|
IEventAlertService eventAlertService,
|
|
IRepoAccountService repoAccountService,
|
|
IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
|
|
{
|
|
_eventAlertService = eventAlertService;
|
|
_repoAccountService = repoAccountService;
|
|
}
|
|
|
|
// GET: api/EventAlert
|
|
[HttpGet]
|
|
[ProducesResponseType(typeof(IEnumerable<EventAlert>), 200)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Get(bool unreadOnly = false, bool actionNeeded = false)
|
|
{
|
|
try
|
|
{
|
|
if (unreadOnly)
|
|
{
|
|
var unread = await _eventAlertService.ListUnread();
|
|
return Ok(unread);
|
|
}
|
|
|
|
if (actionNeeded)
|
|
{
|
|
var needed = await _eventAlertService.ListActionNeeded();
|
|
return Ok(needed);
|
|
}
|
|
|
|
var results = await _eventAlertService.ListAll();
|
|
return Ok(results);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// GET: api/EventAlert/5
|
|
[HttpGet("{id}", Name = "GetEventAlertById")]
|
|
[ProducesResponseType(typeof(EventAlert), 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 _eventAlertService.Get(id);
|
|
return Ok(result);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// GET: api/EventAlert/
|
|
[HttpGet]
|
|
[Route("[action]")]
|
|
[ProducesResponseType(typeof(EventAlert), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> GetWithinDateRange(DateTime start, DateTime end)
|
|
{
|
|
try
|
|
{
|
|
if (start == null || end == null || end < start)
|
|
{
|
|
return BadRequest("Invalid dates");
|
|
}
|
|
|
|
var result = await _eventAlertService.ListWithinDateRange(start, end);
|
|
return Ok(result);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// POST: api/EventAlert
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(EventAlert), 201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Post([FromBody] EventAlert eventAlert)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var result = await _eventAlertService.SaveAsync(eventAlert);
|
|
return Ok(result);
|
|
}
|
|
catch (DuplicateResourceException)
|
|
{
|
|
return StatusCode(StatusCodes.Status409Conflict, "A Duplicate Event Alert already exists.");
|
|
}
|
|
catch (UnwantedStatusException)
|
|
{
|
|
return StatusCode(StatusCodes.Status409Conflict, $"Status of Repo: Reposession ID: {eventAlert.RepoId} is Cancelled or Redeemed. Event Alert not created.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// POST: api/EventAlert/ByAuctionAssignmentId/:AuctionAssignmentId
|
|
[HttpPost("ByAuctionAssignmentId/{auctionAssignmentId}")]
|
|
[ProducesResponseType(typeof(EventAlert), 201)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> CreateByAuctionAssignmentId(int auctionAssignmentId,
|
|
[FromBody] EventAlert eventAlert)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var repo = await _repoAccountService.FindByAuctionAssignmentId(auctionAssignmentId);
|
|
if (repo == null)
|
|
{
|
|
return BadRequest("Repo Account Not Found For Auction Assignment");
|
|
}
|
|
|
|
eventAlert.RepoId = repo.RepoId;
|
|
eventAlert.AccountNo = repo.AccountNo;
|
|
var result = await _eventAlertService.SaveAsync(eventAlert);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// PUT: api/EventAlert/5
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(EventAlert), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public async Task<IActionResult> Put(int id, [FromBody] EventAlert eventAlert)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState);
|
|
}
|
|
|
|
if (id <= 0 || id != eventAlert.Id)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var currentUser = CreateUserFromAuth();
|
|
|
|
var result = await _eventAlertService.UpdateAsync(eventAlert, currentUser);
|
|
return Ok(result);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// DELETE: api/ApiWithActions/5
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(typeof(EventAlert), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> Delete(int id)
|
|
{
|
|
try
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var result = await _eventAlertService.DeleteAsync(id);
|
|
return Ok(result);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {this.GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
} |