Skip to content

Commit

Permalink
fix controller and add model
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 6dbc7be commit 44ba09e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 32 deletions.
11 changes: 6 additions & 5 deletions src/deskstar-backend/Deskstar/Controllers/CompanyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Deskstar.Core;
using Deskstar.Models;
using Deskstar.Usecases;
using Deskstar.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

Expand Down Expand Up @@ -42,17 +43,17 @@ public CompanyController(ILogger<CompanyController> logger, ICompanyUsecases com
/// <response code="200">Returns a list of companies</response>
/// <response code="500">Internal Server Error</response>
[HttpGet]
[ProducesResponseType(StatusCodes.Status200OK)]
[AllowAnonymous]
[ProducesResponseType(typeof(List<CompanyDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[Produces("application/json")]
public IActionResult GetCompanies()
{
List<CompanyDto> companies;
try
{
var companies = _companyUsecases.GetCompanies();
var mapper = _autoMapperConfiguration.GetConfiguration().CreateMapper();
var companiesDto = mapper.Map<List<Entities.Company>>(companies);
return Ok(companiesDto);
companies = _companyUsecases.GetCompanies();
return Ok(companies);
}
catch (Exception ex)
{
Expand Down
21 changes: 21 additions & 0 deletions src/deskstar-backend/Deskstar/Models/CompanyDto.cs
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!;
}
47 changes: 24 additions & 23 deletions src/deskstar-backend/Deskstar/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -61,19 +61,19 @@
var dbPassword = builder.Configuration.GetValue<string>(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<string>(Constants.CONFIG_EMAIL_PASSWORD) ?? null;
var emailHost = builder.Configuration.GetValue<string>(Constants.CONFIG_EMAIL_HOST) ?? null;
var emailPort = builder.Configuration.GetValue<int>(Constants.CONFIG_EMAIL_PORT);
var emailUsername = builder.Configuration.GetValue<string>(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<DataContext>(options => options.UseNpgsql($"Host={dbHost};Database={dbDatabase};Username={dbUsername};Password={dbPassword}"));
Expand All @@ -84,6 +84,7 @@

builder.Services.AddScoped<IAuthUsecases, AuthUsecases>();
builder.Services.AddScoped<IBookingUsecases, BookingUsecases>();
builder.Services.AddScoped<ICompanyUsecases, CompanyUsecases>();
builder.Services.AddScoped<IUserUsecases, UserUsecases>();
builder.Services.AddScoped<IAutoMapperConfiguration, AutoMapperConfiguration>();
builder.Services.AddScoped<IResourceUsecases, ResourceUsecases>();
Expand All @@ -98,8 +99,8 @@
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();
Expand Down
18 changes: 14 additions & 4 deletions src/deskstar-backend/Deskstar/Usecases/CompanyUsecases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Deskstar.Usecases;

public interface ICompanyUsecases
{
public List<Company> GetCompanies();
public List<CompanyDto> GetCompanies();
}

public class CompanyUsecases : ICompanyUsecases
Expand All @@ -21,8 +21,18 @@ public CompanyUsecases(DataContext context, ILogger<CompanyUsecases> logger)
_logger = logger;
}

public List<Company> GetCompanies()
public List<CompanyDto> GetCompanies()
{
return _context.Companies.ToList();
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;
}
}
}

0 comments on commit 44ba09e

Please sign in to comment.