Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pipeline/lib/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
const options = require("@bcgov/pipeline-cli").Util.parseArguments();
const changeId = options.pr; // aka pull-request
const version = "2.3.12";
const version = "2.3.14";
const name = "transaction";

Object.assign(options.git, { owner: "ychung-mot", repository: "TransAction" });
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ TransAction is a voluntary initiative sponsored by the Ministry of Transportatio
## Requirements

- .NET 7
- Node.js 8+
- NodeJS 12+
- Microsoft SQL Server 2012+
- Pre-configured Keycloak Server

Expand Down
48 changes: 10 additions & 38 deletions api/TransAction.API/Controllers/ImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,60 +30,32 @@ public ImageController(IHttpContextAccessor httpContextAccessor, ILogger<Activit
[HttpGet("{guid}", Name = "GetImageByGuid")]
public IActionResult GetImageByGuid(string guid)
{
TraImage image = null;

try
{
image = _unitOfWork.Image.GetProfileImage(guid);

if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
}
catch (Exception e)
{
throw e;
}
TraImage image = _unitOfWork.Image.GetProfileImage(guid);

if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));

return File(image.Data, image.ContentType, image.Filename);
}

[HttpGet("user/{id}", Name = "GetImageByUserId")]
public IActionResult GetImageByUserId(int id)
{
TraImage image = null;

try
{
image = _unitOfWork.Image.GetUserProfileImage(id);
TraImage image = _unitOfWork.Image.GetUserProfileImage(id);

if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
}
catch (Exception e)
{
throw e;
}
if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));

return File(image.Data, image.ContentType, image.Filename);
}

[HttpGet("user/{id}", Name = "GetImageByTeamId")]
public IActionResult GetImageByTeamId(int id)
{
TraImage image = null;

try
{
image = _unitOfWork.Image.GetTeamProfileImage(id);
TraImage image = _unitOfWork.Image.GetTeamProfileImage(id);

if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));
}
catch (Exception e)
{
throw e;
}
if (image == null)
return StatusCode(404, new TransActionResponse("Image Not Found"));

return File(image.Data, image.ContentType, image.Filename);
}
Expand Down Expand Up @@ -143,7 +115,7 @@ public IActionResult UploadProfileImage([FromForm]ImagePostDto model)
model.Data.CopyTo(memoryStream);
bytes = memoryStream.ToArray();

using (Image<Rgba32> image = Image.Load(bytes))
using (Image<Rgba32> image = Image.Load<Rgba32>(bytes))
{
int maxWidthOrLength = Math.Max(image.Width, image.Height);

Expand Down
194 changes: 159 additions & 35 deletions api/TransAction.API/Program.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,171 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.IdentityModel.Logging;
using Serilog;
using System;
using System.IO;
using TransAction.API.Authentication;
using TransAction.Data.Models;
using TransAction.Data.Repositories.Interfaces;
using TransAction.Data.Repositories;
using TransAction.Data.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Authorization;
using TransAction.API.Helpers;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using TransAction.API.Extensions;
using System.Linq;
using Microsoft.AspNetCore.Http;
using System.Net.Mime;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace TransAction.API
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();

Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();

// Only used for app.ConfigureExceptionHandler()
var loggerFactory = new LoggerFactory().AddSerilog(Log.Logger);
var logger = loggerFactory.CreateLogger("Logger");

var builder = WebApplication.CreateBuilder(args);

// Register services here
builder.Host.UseSerilog(Log.Logger);
builder.Services.AddHttpContextAccessor();
builder.Services.AddAutoMapper(typeof(Program).Assembly);
builder.Services.AddScoped<ITransActionRepo, TransActionRepo>();
builder.Services.AddScoped<IAuthorizationRepo, AuthorizationRepo>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();

var ConnectionString = configuration["CONNECTION_STRING"];
builder.Services.AddDbContext<TransActionContext>(opt =>
opt.UseSqlServer(ConnectionString));
IdentityModelEventSource.ShowPII = true;

builder.Services.AddAuthentication(options =>
{
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();

public static int Main(string[] args)
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddKeycloakAuth(new KeycloakAuthenticationOptions()
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();
Authority = configuration["JWT_AUTHORITY"],
Audience = configuration["JWT_AUDIENCE"]
}
);

try
{
Log.Information("Starting web host");
CreateWebHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
builder.Services.AddControllers(options =>
{
options.Filters.Add(
new AuthorizeFilter(
new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build()
)
);
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.Converters.Add(new TrimmingConverter());
});

builder.Services.AddHealthChecks().AddSqlServer(
ConnectionString,
name: "DB-Check",
failureStatus: HealthStatus.Degraded,
tags: new string[] { "sql", "db" }
);

var app = builder.Build();

// Use services here
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseSerilogRequestLogging();
app.ConfigureExceptionHandler(logger);

var healthCheckOptions = new HealthCheckOptions
{
ResponseWriter = async (c, r) =>
{
c.Response.ContentType = MediaTypeNames.Application.Json;
var result = System.Text.Json.JsonSerializer.Serialize(
new {
checks = r.Entries.Select(e =>
new {
description = e.Key,
status = e.Value.Status.ToString(),
tags = e.Value.Tags,
responseTime = e.Value.Duration.TotalMilliseconds
}
),
totalResponseTime = r.TotalDuration.TotalMilliseconds
}
}
);
await c.Response.WriteAsync(result);
}
};

app.UseHealthChecks("/healthz", healthCheckOptions);
app.UseRouting();
app.UseAuthentication();
app.MapControllers();

app.Run();

/*
public class Program
{
public static IConfiguration Configuration { get; } = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddEnvironmentVariables()
.Build();

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseSerilog();
public static int Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(Configuration)
.CreateLogger();

try
{
Log.Information("Starting web host");
CreateHostBuilder(args).Build().Run();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
.UseSerilog();
}
*/
Loading