Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DustSwiffer committed Jul 22, 2024
0 parents commit 9f8efe2
Show file tree
Hide file tree
Showing 70 changed files with 3,711 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[*.cs]


# SA1404: Code analysis suppression should have justification
dotnet_diagnostic.SA1404.severity = none

# SA1633: File header
dotnet_diagnostic.SA1633.severity = none

# SA1101: Prefix local calls with this
dotnet_diagnostic.SA1101.severity = none

# SA1611: Element parameters should be documented
dotnet_diagnostic.SA1611.severity = none

# SA1309: starting underscore
dotnet_diagnostic.SA1309.severity = none

dotnet_diagnostic.SA1615.severity = none

dotnet_diagnostic.SA1642.severity = none

dotnet_diagnostic.SA1623.severity = none
86 changes: 86 additions & 0 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# This workflow will build, test, sign and package a WPF or Windows Forms desktop application
# built on .NET Core.
# To learn how to migrate your existing application to .NET Core,
# refer to https://docs.microsoft.com/en-us/dotnet/desktop-wpf/migration/convert-project-from-net-framework
#
# To configure this workflow:
#
# 1. Configure environment variables
# GitHub sets default environment variables for every workflow run.
# Replace the variables relative to your project in the "env" section below.
#
# 2. Signing
# Generate a signing certificate in the Windows Application
# Packaging Project or add an existing signing certificate to the project.
# Next, use PowerShell to encode the .pfx file using Base64 encoding
# by running the following Powershell script to generate the output string:
#
# $pfx_cert = Get-Content '.\SigningCertificate.pfx' -Encoding Byte
# [System.Convert]::ToBase64String($pfx_cert) | Out-File 'SigningCertificate_Encoded.txt'
#
# Open the output file, SigningCertificate_Encoded.txt, and copy the
# string inside. Then, add the string to the repo as a GitHub secret
# and name it "Base64_Encoded_Pfx."
# For more information on how to configure your signing certificate for
# this workflow, refer to https://github.com/microsoft/github-actions-for-desktop-apps#signing
#
# Finally, add the signing certificate password to the repo as a secret and name it "Pfx_Key".
# See "Build the Windows Application Packaging project" below to see how the secret is used.
#
# For more information on GitHub Actions, refer to https://github.com/features/actions
# For a complete CI/CD sample to get started with GitHub Action workflows for Desktop Applications,
# refer to https://github.com/microsoft/github-actions-for-desktop-apps

name: .NET Core Desktop

on:
push:
branches: [ "master", "develop" ]
pull_request:
branches: [ "master", "develop" ]

jobs:

build:

strategy:
matrix:
configuration: [Debug]

runs-on: windows-latest # For a list of available runner types, refer to
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idruns-on

env:
Solution_Name: AdvancedAPI.sln
Test_Project_Path: AdvancedAPI.Tests\AdvancedAPI.Tests.csproj

steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0

# Install the .NET Core workload
- name: Install .NET Core
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x

# Add MSBuild to the PATH: https://github.com/microsoft/setup-msbuild
- name: Setup MSBuild.exe
uses: microsoft/setup-msbuild@v2

# Execute all unit tests in the solution
- name: Execute unit tests
run: dotnet test

# Restore the application to populate the obj folder with RuntimeIdentifiers
- name: Restore the application
run: msbuild $env:Solution_Name /t:Restore /p:Configuration=$env:Configuration
env:
Configuration: ${{ matrix.configuration }}
135 changes: 135 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.svclog
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml
*.pubxml
*.azurePubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/
## TODO: If the tool you use requires repositories.config, also uncomment the next line
!packages/repositories.config

# Windows Azure Build Output
csx/
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
![Ss]tyle[Cc]op.targets
~$*
*~
*.dbmdl
*.[Pp]ublish.xml

*.publishsettings

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf

# =========================
# Windows detritus
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

_NCrunch*
*.idea*
.vs
29 changes: 29 additions & 0 deletions AdvancedAPI.Business/AdvancedAPI.Business.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-Business-63FD5173-E1FF-446F-A3C3-0D5E7DA4C0FF</UserSecretsId>
<RootNamespace>Business</RootNamespace>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\AdvancedAPI.Data\AdvancedAPI.Data.csproj" />
</ItemGroup>

<ItemGroup>
<AdditionalFiles Include="stylecop.json" />
</ItemGroup>
</Project>

19 changes: 19 additions & 0 deletions AdvancedAPI.Business/MappingProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using AdvancedAPI.Data.Models;
using AdvancedAPI.Data.ViewModels.NewsArticle;
using AutoMapper;

namespace Business;

/// <summary>
/// Auto mapper profiles.
/// </summary>
public class MappingProfile : Profile
{
/// <summary>
/// Mapping Models against entities and opposite.
/// </summary>
public MappingProfile()
{
CreateMap<NewsArticleRequestModel, NewsArticle>();
}
}
7 changes: 7 additions & 0 deletions AdvancedAPI.Business/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Business;

IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services => { services.AddHostedService<Worker>(); })
.Build();

await host.RunAsync();
11 changes: 11 additions & 0 deletions AdvancedAPI.Business/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Business": {
"commandName": "Project",
"dotnetRunMessages": true,
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
23 changes: 23 additions & 0 deletions AdvancedAPI.Business/ServiceExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Business.Services;
using Business.Services.Interfaces;

namespace Business;

/// <summary>
/// Service extension used to prepare the business layer for usage.
/// </summary>
public static class ServiceExtension
{
/// <summary>
/// Registers everything business layer related.
/// </summary>
public static IServiceCollection AddBusinessServices(this IServiceCollection services)
{
services.AddAutoMapper(typeof(MappingProfile).Assembly);

services.AddScoped<IAuthenticationService, AuthenticationService>();
services.AddScoped<INewsArticleService, NewsArticleService>();

return services;
}
}
55 changes: 55 additions & 0 deletions AdvancedAPI.Business/Services/AuthenticationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using AdvancedAPI.Data.Repositories.Interfaces;
using AdvancedAPI.Data.ViewModels.Authentication;
using Business.Services.Interfaces;
using Microsoft.AspNetCore.Identity;
using Microsoft.IdentityModel.Tokens;

namespace Business.Services;

/// <inheritdoc />
public class AuthenticationService : IAuthenticationService
{
private readonly IIdentityRepository _identityRepository;
private readonly IConfiguration _configuration;

/// <summary>
/// Constructor.
/// </summary>
public AuthenticationService(IIdentityRepository identityRepository, IConfiguration configuration)
{
_identityRepository = identityRepository;
_configuration = configuration;
}

/// <inheritdoc />
public async Task<JwtSecurityToken?> Login(LoginRequestModel requestModel, CancellationToken ct = default)
{
IdentityUser? user = await _identityRepository.GetUser(requestModel.Username);
if (user != null && await _identityRepository.CheckPassword(user, requestModel.Password))
{
List<Claim> authClaims = new()
{
new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
};

IList<string> userRoles = await _identityRepository.GetRoles(user);
authClaims.AddRange(userRoles.Select(role => new Claim(ClaimTypes.Role, role)));
SymmetricSecurityKey authSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["Jwt:Key"]));

JwtSecurityToken token = new JwtSecurityToken(
issuer: _configuration["Jwt:Issuer"],
audience: _configuration["Jwt:Audience"],
expires: DateTime.Now.AddHours(3),
claims: authClaims,
signingCredentials: new SigningCredentials(authSigningKey, SecurityAlgorithms.HmacSha256));

return token;
}

return null;
}
}
Loading

0 comments on commit 9f8efe2

Please sign in to comment.