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.
Merge pull request #174 from amosproj/111-improve-registration2
111 improve registration2
- Loading branch information
Showing
5 changed files
with
195 additions
and
23 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
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,64 @@ | ||
/** | ||
* Program | ||
* | ||
* Version 1.0 | ||
* | ||
* 2023-01-03 | ||
* | ||
* MIT License | ||
*/ | ||
using Deskstar.Core; | ||
using Deskstar.Models; | ||
using Deskstar.Usecases; | ||
using Deskstar.Entities; | ||
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] | ||
[AllowAnonymous] | ||
[ProducesResponseType(typeof(List<CompanyDto>), StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status500InternalServerError)] | ||
[Produces("application/json")] | ||
public IActionResult GetCompanies() | ||
{ | ||
List<CompanyDto> companies; | ||
try | ||
{ | ||
companies = _companyUsecases.GetCompanies(); | ||
return Ok(companies); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "Error while getting companies"); | ||
return Problem(statusCode: 500); | ||
} | ||
} | ||
} |
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,21 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using AutoMapper; | ||
|
||
namespace Deskstar.Models; | ||
|
||
public class CompanyDto | ||
{ | ||
public static void createMappings(IMapperConfigurationExpression cfg) | ||
{ | ||
cfg.CreateMap<Entities.Company, CompanyDto>(); | ||
} | ||
public CompanyDto() | ||
{ | ||
|
||
} | ||
|
||
[Required] | ||
public string CompanyId { get; set; } = null!; | ||
[Required] | ||
public string CompanyName { get; set; } = null!; | ||
} |
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
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,38 @@ | ||
using Deskstar.DataAccess; | ||
using Deskstar.Entities; | ||
using Deskstar.Models; | ||
|
||
namespace Deskstar.Usecases; | ||
|
||
public interface ICompanyUsecases | ||
{ | ||
public List<CompanyDto> GetCompanies(); | ||
} | ||
|
||
public class CompanyUsecases : ICompanyUsecases | ||
{ | ||
private readonly DataContext _context; | ||
|
||
private readonly ILogger<CompanyUsecases> _logger; | ||
|
||
public CompanyUsecases(DataContext context, ILogger<CompanyUsecases> logger) | ||
{ | ||
_context = context; | ||
_logger = logger; | ||
} | ||
|
||
public List<CompanyDto> GetCompanies() | ||
{ | ||
var dbCompanies = _context.Companies.ToList(); | ||
|
||
if (dbCompanies.ToList().Count == 0) return new List<CompanyDto>(); | ||
|
||
var mapCompaniesToCompaniesDto = dbCompanies.Select((c) => new CompanyDto | ||
{ | ||
CompanyId = c.CompanyId.ToString(), | ||
CompanyName = c.CompanyName, | ||
}).ToList(); | ||
|
||
return mapCompaniesToCompaniesDto; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.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,48 @@ | ||
using Deskstar.Core.Exceptions; | ||
using Deskstar.DataAccess; | ||
using Deskstar.Entities; | ||
using Deskstar.Usecases; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.Extensions.Logging; | ||
using Moq; | ||
|
||
namespace Teststar.Tests.Tests; | ||
|
||
public class CompanyUsecasesTests | ||
{ | ||
private void setupMockData(DataContext db, Guid companyID, String companyName) | ||
{ | ||
var company = new Company | ||
{ | ||
CompanyId = companyID, | ||
CompanyName = companyName | ||
}; | ||
db.Companies.Add(company); | ||
db.SaveChanges(); | ||
} | ||
|
||
private void GetCompanies_ShouldReturnAllCompanies() | ||
{ | ||
// setup | ||
using var db = new DataContext(); | ||
var companyID = Guid.NewGuid(); | ||
var companyName = "Test Company"; | ||
setupMockData(db, companyID, companyName); | ||
|
||
// arrange | ||
var logger = new Mock<ILogger<CompanyUsecases>>(); | ||
var companyUsecases = new CompanyUsecases(db, logger.Object); | ||
|
||
|
||
// act | ||
var companies = companyUsecases.GetCompanies(); | ||
|
||
// assert | ||
Assert.That(1 == companies.Count()); | ||
Assert.That(companyID.ToString() == companies.First().CompanyId); | ||
Assert.That(companyName == companies.First().CompanyName); | ||
|
||
// cleanup | ||
db.Database.EnsureDeleted(); | ||
} | ||
} |