Skip to content

Commit

Permalink
add CompanyController
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Tiegges <[email protected]>
  • Loading branch information
jantiegges committed Jan 23, 2023
1 parent 9fa7179 commit 6dbc7be
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/deskstar-backend/Deskstar/Controllers/CompanyController.cs
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);
}
}
}

0 comments on commit 6dbc7be

Please sign in to comment.