From b33592d744653a90d0795fc79d7891bd13496d59 Mon Sep 17 00:00:00 2001 From: Jan Tiegges Date: Mon, 23 Jan 2023 10:45:11 +0000 Subject: [PATCH 1/5] add CompanyUsecase Signed-off-by: Jan Tiegges --- .../Deskstar/Usecases/CompanyUsecases.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs diff --git a/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs b/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs new file mode 100644 index 00000000..dfcde539 --- /dev/null +++ b/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs @@ -0,0 +1,28 @@ +using Deskstar.DataAccess; +using Deskstar.Entities; +using Deskstar.Models; + +namespace Deskstar.Usecases; + +public interface ICompanyUsecases +{ + public List GetCompanies(); +} + +public class CompanyUsecases : ICompanyUsecases +{ + private readonly DataContext _context; + + private readonly ILogger _logger; + + public CompanyUsecases(DataContext context, ILogger logger) + { + _context = context; + _logger = logger; + } + + public List GetCompanies() + { + return _context.Companies.ToList(); + } +} \ No newline at end of file From 9fa7179b51cfa7e17b0f60df6e9fcf6f9df5234b Mon Sep 17 00:00:00 2001 From: Jan Tiegges Date: Mon, 23 Jan 2023 11:07:09 +0000 Subject: [PATCH 2/5] add CompanyUsecases Tests Signed-off-by: Jan Tiegges --- .../Tests/CompanyUsecasesTests.cs | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs diff --git a/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs b/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs new file mode 100644 index 00000000..3812d6c8 --- /dev/null +++ b/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs @@ -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>(); + var companyUsecases = new CompanyUsecases(db, logger.Object); + + + // act + var companies = companyUsecases.GetCompanies(); + + // assert + Assert.That(1 == companies.Count()); + Assert.That(companyID == companies.First().CompanyId); + Assert.That(companyName == companies.First().CompanyName); + + // cleanup + db.Database.EnsureDeleted(); + } +} \ No newline at end of file From 6dbc7bea212163db74e44bab34e7d88a2b93da9d Mon Sep 17 00:00:00 2001 From: Jan Tiegges Date: Mon, 23 Jan 2023 11:39:51 +0000 Subject: [PATCH 3/5] add CompanyController Signed-off-by: Jan Tiegges --- .../Deskstar/Controllers/CompanyController.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/deskstar-backend/Deskstar/Controllers/CompanyController.cs diff --git a/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs b/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs new file mode 100644 index 00000000..f25b84b3 --- /dev/null +++ b/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs @@ -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 _logger; + private readonly IAutoMapperConfiguration _autoMapperConfiguration; + + public CompanyController(ILogger logger, ICompanyUsecases companyUsecases, IAutoMapperConfiguration autoMapperConfiguration) + { + _logger = logger; + _companyUsecases = companyUsecases; + _autoMapperConfiguration = autoMapperConfiguration; + } + + /// + /// Get all companies + /// + /// A list of companies + /// + /// Sample request: Get /companies + /// + /// Returns a list of companies + /// Internal Server Error + [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>(companies); + return Ok(companiesDto); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while getting companies"); + return Problem(statusCode: 500); + } + } +} From 44ba09ecf50717e89d03de7ea173b64649d8697c Mon Sep 17 00:00:00 2001 From: Jan Tiegges Date: Mon, 23 Jan 2023 13:02:47 +0000 Subject: [PATCH 4/5] fix controller and add model Signed-off-by: Jan Tiegges --- .../Deskstar/Controllers/CompanyController.cs | 11 +++-- .../Deskstar/Models/CompanyDto.cs | 21 +++++++++ src/deskstar-backend/Deskstar/Program.cs | 47 ++++++++++--------- .../Deskstar/Usecases/CompanyUsecases.cs | 18 +++++-- 4 files changed, 65 insertions(+), 32 deletions(-) create mode 100644 src/deskstar-backend/Deskstar/Models/CompanyDto.cs diff --git a/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs b/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs index f25b84b3..ebe33e06 100644 --- a/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs +++ b/src/deskstar-backend/Deskstar/Controllers/CompanyController.cs @@ -10,6 +10,7 @@ using Deskstar.Core; using Deskstar.Models; using Deskstar.Usecases; +using Deskstar.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -42,17 +43,17 @@ public CompanyController(ILogger logger, ICompanyUsecases com /// Returns a list of companies /// Internal Server Error [HttpGet] - [ProducesResponseType(StatusCodes.Status200OK)] + [AllowAnonymous] + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] [Produces("application/json")] public IActionResult GetCompanies() { + List companies; try { - var companies = _companyUsecases.GetCompanies(); - var mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper(); - var companiesDto = mapper.Map>(companies); - return Ok(companiesDto); + companies = _companyUsecases.GetCompanies(); + return Ok(companies); } catch (Exception ex) { diff --git a/src/deskstar-backend/Deskstar/Models/CompanyDto.cs b/src/deskstar-backend/Deskstar/Models/CompanyDto.cs new file mode 100644 index 00000000..0af4400e --- /dev/null +++ b/src/deskstar-backend/Deskstar/Models/CompanyDto.cs @@ -0,0 +1,21 @@ +using System.ComponentModel.DataAnnotations; +using AutoMapper; + +namespace Deskstar.Models; + +public class CompanyDto +{ + public static void createMappings(IMapperConfigurationExpression cfg) + { + cfg.CreateMap(); + } + public CompanyDto() + { + + } + + [Required] + public string CompanyId { get; set; } = null!; + [Required] + public string CompanyName { get; set; } = null!; +} diff --git a/src/deskstar-backend/Deskstar/Program.cs b/src/deskstar-backend/Deskstar/Program.cs index 4b3b4999..cc9141b2 100644 --- a/src/deskstar-backend/Deskstar/Program.cs +++ b/src/deskstar-backend/Deskstar/Program.cs @@ -26,26 +26,26 @@ // Add services to the container. builder.Services.AddAuthentication(options => { - options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; - options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; + options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(o => { - o.TokenValidationParameters = new TokenValidationParameters - { - ValidIssuer = builder.Configuration["Jwt:Issuer"], - ValidAudience = builder.Configuration["Jwt:Audience"], - IssuerSigningKey = new SymmetricSecurityKey - (Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])), - ValidateIssuer = true, - ValidateAudience = true, - ValidateLifetime = false, - ValidateIssuerSigningKey = true - }; + o.TokenValidationParameters = new TokenValidationParameters + { + ValidIssuer = builder.Configuration["Jwt:Issuer"], + ValidAudience = builder.Configuration["Jwt:Audience"], + IssuerSigningKey = new SymmetricSecurityKey + (Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"])), + ValidateIssuer = true, + ValidateAudience = true, + ValidateLifetime = false, + ValidateIssuerSigningKey = true + }; }); builder.Services.AddAuthorization(options => { - options.AddPolicy("Admin", policy => policy.RequireClaim("IsCompanyAdmin")); + options.AddPolicy("Admin", policy => policy.RequireClaim("IsCompanyAdmin")); }); builder.Services.AddControllers(); @@ -61,9 +61,9 @@ var dbPassword = builder.Configuration.GetValue(Constants.CONFIG_DB_PASSWORD) ?? null; if (dbHost == null || dbDatabase == null || dbUsername == null || dbPassword == null) { - Console.Error.WriteLine($"missing db configuration. database configuration has host({dbHost != null})," + - $"database name({dbDatabase != null}), username({dbUsername != null}), password({dbPassword != null})"); - return; + Console.Error.WriteLine($"missing db configuration. database configuration has host({dbHost != null})," + + $"database name({dbDatabase != null}), username({dbUsername != null}), password({dbPassword != null})"); + return; } var emailPassword = builder.Configuration.GetValue(Constants.CONFIG_EMAIL_PASSWORD) ?? null; var emailHost = builder.Configuration.GetValue(Constants.CONFIG_EMAIL_HOST) ?? null; @@ -71,9 +71,9 @@ var emailUsername = builder.Configuration.GetValue(Constants.CONFIG_EMAIL_USERNAME) ?? null; if (emailPassword == null || emailHost == null || emailPort == 0 || emailUsername == null) { - Console.Error.WriteLine($"missing email configuration. email configuration has password({emailPassword != null})," + - $" host({emailHost != null}), port({emailPort != 0}), username({emailUsername != null})"); - return; + Console.Error.WriteLine($"missing email configuration. email configuration has password({emailPassword != null})," + + $" host({emailHost != null}), port({emailPort != 0}), username({emailUsername != null})"); + return; } builder.Services.AddDbContext(options => options.UseNpgsql($"Host={dbHost};Database={dbDatabase};Username={dbUsername};Password={dbPassword}")); @@ -84,6 +84,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); @@ -98,8 +99,8 @@ // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { - app.UseSwagger(); - app.UseSwaggerUI(); + app.UseSwagger(); + app.UseSwaggerUI(); } app.UseHttpsRedirection(); diff --git a/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs b/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs index dfcde539..8e0b5816 100644 --- a/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs +++ b/src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs @@ -6,7 +6,7 @@ namespace Deskstar.Usecases; public interface ICompanyUsecases { - public List GetCompanies(); + public List GetCompanies(); } public class CompanyUsecases : ICompanyUsecases @@ -21,8 +21,18 @@ public CompanyUsecases(DataContext context, ILogger logger) _logger = logger; } - public List GetCompanies() + public List GetCompanies() { - return _context.Companies.ToList(); + var dbCompanies = _context.Companies.ToList(); + + if (dbCompanies.ToList().Count == 0) return new List(); + + var mapCompaniesToCompaniesDto = dbCompanies.Select((c) => new CompanyDto + { + CompanyId = c.CompanyId.ToString(), + CompanyName = c.CompanyName, + }).ToList(); + + return mapCompaniesToCompaniesDto; } -} \ No newline at end of file +} From 54d3f510897c9064846395f5726944e3cf5eea5e Mon Sep 17 00:00:00 2001 From: Jan Tiegges Date: Mon, 23 Jan 2023 13:04:23 +0000 Subject: [PATCH 5/5] fix test Signed-off-by: Jan Tiegges --- .../Teststar.Tests/Tests/CompanyUsecasesTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs b/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs index 3812d6c8..0ed2d075 100644 --- a/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs +++ b/src/deskstar-backend/Teststar.Tests/Tests/CompanyUsecasesTests.cs @@ -39,10 +39,10 @@ private void GetCompanies_ShouldReturnAllCompanies() // assert Assert.That(1 == companies.Count()); - Assert.That(companyID == companies.First().CompanyId); + Assert.That(companyID.ToString() == companies.First().CompanyId); Assert.That(companyName == companies.First().CompanyName); // cleanup db.Database.EnsureDeleted(); } -} \ No newline at end of file +}