generated from amosproj/amos202Xss0Y-projname
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jan Tiegges <[email protected]>
- Loading branch information
1 parent
9fa7179
commit 6dbc7be
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
src/deskstar-backend/Deskstar/Controllers/CompanyController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* Program | ||
* | ||
* Version 1.0 | ||
* | ||
* 2023-01-03 | ||
* | ||
* MIT License | ||
*/ | ||
using Deskstar.Core; | ||
using Deskstar.Models; | ||
using Deskstar.Usecases; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Deskstar.Controllers; | ||
|
||
[ApiController] | ||
[Route("/companies")] | ||
[Produces("application/json")] | ||
public class CompanyController : ControllerBase | ||
{ | ||
private readonly ICompanyUsecases _companyUsecases; | ||
|
||
private readonly ILogger<CompanyController> _logger; | ||
private readonly IAutoMapperConfiguration _autoMapperConfiguration; | ||
|
||
public CompanyController(ILogger<CompanyController> logger, ICompanyUsecases companyUsecases, IAutoMapperConfiguration autoMapperConfiguration) | ||
{ | ||
_logger = logger; | ||
_companyUsecases = companyUsecases; | ||
_autoMapperConfiguration = autoMapperConfiguration; | ||
} | ||
|
||
/// <summary> | ||
/// Get all companies | ||
/// </summary> | ||
/// <returns> A list of companies</returns> | ||
/// <remarks> | ||
/// Sample request: Get /companies | ||
/// </remarks> | ||
/// <response code="200">Returns a list of companies</response> | ||
/// <response code="500">Internal Server Error</response> | ||
[HttpGet] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||
[Produces("application/json")] | ||
public IActionResult GetCompanies() | ||
{ | ||
try | ||
{ | ||
var companies = _companyUsecases.GetCompanies(); | ||
var mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper(); | ||
var companiesDto = mapper.Map<List<Entities.Company>>(companies); | ||
return Ok(companiesDto); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error while getting companies"); | ||
return Problem(statusCode: 500); | ||
} | ||
} | ||
} |