140 lines
4.8 KiB
C#
140 lines
4.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.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 RepoCompanyController : RepoControllerBase
|
|
{
|
|
private readonly IRepoCompanyService _repoCompanyService;
|
|
|
|
public RepoCompanyController(
|
|
IRepoCompanyService repoCompanyService,
|
|
IHttpContextAccessor httpContextAccessor) :
|
|
base(httpContextAccessor)
|
|
{
|
|
_repoCompanyService = repoCompanyService;
|
|
}
|
|
|
|
// GET: api/RepoCompany
|
|
[ProducesResponseType(typeof(IEnumerable<RepoCompany>), 200)]
|
|
[ProducesResponseType(500)]
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
try
|
|
{
|
|
var repoCompanies = await _repoCompanyService.ListAll();
|
|
return Ok(repoCompanies);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// GET: api/RepoCompany/5
|
|
[HttpGet("{id}", Name = "GetRepoCompanyById")]
|
|
[ProducesResponseType(typeof(RepoCompany), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(500)]
|
|
public async Task<IActionResult> GetById(int id)
|
|
{
|
|
try
|
|
{
|
|
if (id <= 0)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var repoCompany = await _repoCompanyService.Get(id);
|
|
return Ok(repoCompany);
|
|
}
|
|
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/RepoCompany
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(RepoCompany), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public async Task<IActionResult> Post([FromBody] RepoCompany repoCompany)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
var newRepoCompany = await _repoCompanyService.Save(repoCompany);
|
|
return Ok(newRepoCompany);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
|
|
// PUT: api/RepoCompany/5
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(typeof(RepoCompany), 200)]
|
|
[ProducesResponseType(typeof(List<string>), 400)]
|
|
[ProducesResponseType(500)]
|
|
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
|
|
public async Task<IActionResult> Put(int id, [FromBody] RepoCompany repoCompany)
|
|
{
|
|
try
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return BadRequest(ModelState.GetErrorMessages());
|
|
}
|
|
|
|
if (id <= 0 || id != repoCompany.ID)
|
|
{
|
|
return BadRequest("Invalid ID");
|
|
}
|
|
|
|
var currentUser = CreateUserFromAuth();
|
|
|
|
var updatedRepoCompany = await _repoCompanyService.Update(repoCompany, currentUser);
|
|
return Ok(updatedRepoCompany);
|
|
}
|
|
catch (NotFoundException nfe)
|
|
{
|
|
return NotFound(nfe.Message);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, $"Failure in {GetType().Name} at {GetInvokerAsyncMethodName()}");
|
|
return StatusCode(StatusCodes.Status500InternalServerError);
|
|
}
|
|
}
|
|
}
|
|
} |