Compare commits
63 Commits
ddd
...
16bc250789
| Author | SHA1 | Date | |
|---|---|---|---|
| 16bc250789 | |||
| 9847b6e6f7 | |||
| ada0e2f665 | |||
| b28bd2826b | |||
| a1999bfe41 | |||
| 9d71c86474 | |||
| d91b837e44 | |||
| b3ca1ba703 | |||
| 108960d074 | |||
| 05686c4cdd | |||
| cb440e7c6d | |||
| 4ea0978cf6 | |||
| ff2707a0e8 | |||
| 6d23494fd3 | |||
| bbac953660 | |||
| 854be19fd5 | |||
| cf1a086e31 | |||
| 918477fb3a | |||
| 857863a4d8 | |||
| 7a2c50cb9a | |||
| 5d0a49632a | |||
| 0e065b58b7 | |||
| 22f47f4461 | |||
| d6c75654b0 | |||
| 136dd2311d | |||
| 351a1a4635 | |||
| 4db35dbdb5 | |||
| d0704aea12 | |||
| 92e91de9c2 | |||
| de7e9a7131 | |||
| 6b422545d9 | |||
| 4a1f1a5a67 | |||
| d3d3675e3d | |||
| 88090878ee | |||
| f410f69e9d | |||
| 036f4d1dfc | |||
| ea689bb7a1 | |||
| 4855336c33 | |||
| ad9391093d | |||
| 89afc435fc | |||
| 2d79b5a0bf | |||
| dcb82414b9 | |||
| d19d68f5a2 | |||
| 1c88d2b2c6 | |||
| 155ed22fb0 | |||
| 4a46c46222 | |||
| f4846bc66a | |||
| 70f47b0dd1 | |||
| e20f713fdb | |||
| 2463c11be3 | |||
| d47e4c1971 | |||
| 4bfc57ef9f | |||
| 5c532a6bb5 | |||
| 4f287d85dd | |||
| 7f734aa2a2 | |||
| 81b5c89a25 | |||
| 1d6ecfee6e | |||
| 4be9fd2043 | |||
| 19b105b0e8 | |||
| ff2da66a22 | |||
| 877e7989cd | |||
| a708ed25e7 | |||
| e579d76560 |
30
.dockerignore
Normal file
30
.dockerignore
Normal file
@@ -0,0 +1,30 @@
|
||||
**/.classpath
|
||||
**/.dockerignore
|
||||
**/.env
|
||||
#**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
||||
!**/.gitignore
|
||||
!.git/HEAD
|
||||
!.git/config
|
||||
!.git/packed-refs
|
||||
!.git/refs/heads/**
|
||||
87
.drone.yml
Normal file
87
.drone.yml
Normal file
@@ -0,0 +1,87 @@
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: Build and test
|
||||
|
||||
trigger:
|
||||
event:
|
||||
include:
|
||||
- push
|
||||
- pull_request
|
||||
- custom
|
||||
|
||||
steps:
|
||||
- name: compile
|
||||
image: mcr.microsoft.com/dotnet/sdk:9.0-alpine
|
||||
environment:
|
||||
CI_WORKSPACE: "/drone/src"
|
||||
commands:
|
||||
- dotnet build
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run
|
||||
|
||||
- name: test
|
||||
image: quay.io/testcontainers/dind-drone-plugin
|
||||
environment:
|
||||
CI_WORKSPACE: "/drone/src"
|
||||
settings:
|
||||
build_image: mcr.microsoft.com/dotnet/sdk:9.0-alpine
|
||||
cmd:
|
||||
- dotnet test --no-build
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run
|
||||
depends_on:
|
||||
- compile
|
||||
|
||||
- name: docker build and push
|
||||
image: docker:24.0.7
|
||||
commands:
|
||||
- docker build . -t $docker_registry$docker_repo:$DRONE_BRANCH
|
||||
- echo $docker_password | docker login --username $docker_username --password-stdin $docker_registry
|
||||
- docker push $docker_registry$docker_repo:$DRONE_BRANCH
|
||||
environment:
|
||||
docker_username:
|
||||
from_secret: docker_username
|
||||
docker_password:
|
||||
from_secret: docker_password
|
||||
docker_repo:
|
||||
from_secret: docker_repo
|
||||
docker_registry:
|
||||
from_secret: docker_registry
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run
|
||||
when:
|
||||
branch:
|
||||
- main
|
||||
depends_on:
|
||||
- compile
|
||||
- test
|
||||
|
||||
- name: Telegram notification
|
||||
image: appleboy/drone-telegram
|
||||
settings:
|
||||
token:
|
||||
from_secret: telegram_token
|
||||
to:
|
||||
from_secret: telegram_user_id
|
||||
when:
|
||||
status:
|
||||
- failure
|
||||
depends_on:
|
||||
- compile
|
||||
- test
|
||||
- docker build and push
|
||||
|
||||
services:
|
||||
- name: docker
|
||||
image: docker:dind
|
||||
privileged: true
|
||||
volumes:
|
||||
- name: dockersock
|
||||
path: /var/run
|
||||
|
||||
volumes:
|
||||
- name: dockersock
|
||||
temp: { }
|
||||
484
.gitignore
vendored
Normal file
484
.gitignore
vendored
Normal file
@@ -0,0 +1,484 @@
|
||||
## Ignore Visual Studio temporary files, build results, and
|
||||
## files generated by popular Visual Studio add-ons.
|
||||
##
|
||||
## Get latest from `dotnet new gitignore`
|
||||
|
||||
# dotenv files
|
||||
.env
|
||||
|
||||
# User-specific files
|
||||
*.rsuser
|
||||
*.suo
|
||||
*.user
|
||||
*.userosscache
|
||||
*.sln.docstates
|
||||
|
||||
# User-specific files (MonoDevelop/Xamarin Studio)
|
||||
*.userprefs
|
||||
|
||||
# Mono auto generated files
|
||||
mono_crash.*
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
[Dd]ebugPublic/
|
||||
[Rr]elease/
|
||||
[Rr]eleases/
|
||||
x64/
|
||||
x86/
|
||||
[Ww][Ii][Nn]32/
|
||||
[Aa][Rr][Mm]/
|
||||
[Aa][Rr][Mm]64/
|
||||
bld/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
||||
[Ll]og/
|
||||
[Ll]ogs/
|
||||
|
||||
# Visual Studio 2015/2017 cache/options directory
|
||||
.vs/
|
||||
# Uncomment if you have tasks that create the project's static files in wwwroot
|
||||
#wwwroot/
|
||||
|
||||
# Visual Studio 2017 auto generated files
|
||||
Generated\ Files/
|
||||
|
||||
# MSTest test Results
|
||||
[Tt]est[Rr]esult*/
|
||||
[Bb]uild[Ll]og.*
|
||||
|
||||
# NUnit
|
||||
*.VisualState.xml
|
||||
TestResult.xml
|
||||
nunit-*.xml
|
||||
|
||||
# Build Results of an ATL Project
|
||||
[Dd]ebugPS/
|
||||
[Rr]eleasePS/
|
||||
dlldata.c
|
||||
|
||||
# Benchmark Results
|
||||
BenchmarkDotNet.Artifacts/
|
||||
|
||||
# .NET
|
||||
project.lock.json
|
||||
project.fragment.lock.json
|
||||
artifacts/
|
||||
|
||||
# Tye
|
||||
.tye/
|
||||
|
||||
# ASP.NET Scaffolding
|
||||
ScaffoldingReadMe.txt
|
||||
|
||||
# StyleCop
|
||||
StyleCopReport.xml
|
||||
|
||||
# Files built by Visual Studio
|
||||
*_i.c
|
||||
*_p.c
|
||||
*_h.h
|
||||
*.ilk
|
||||
*.meta
|
||||
*.obj
|
||||
*.iobj
|
||||
*.pch
|
||||
*.pdb
|
||||
*.ipdb
|
||||
*.pgc
|
||||
*.pgd
|
||||
*.rsp
|
||||
*.sbr
|
||||
*.tlb
|
||||
*.tli
|
||||
*.tlh
|
||||
*.tmp
|
||||
*.tmp_proj
|
||||
*_wpftmp.csproj
|
||||
*.log
|
||||
*.tlog
|
||||
*.vspscc
|
||||
*.vssscc
|
||||
.builds
|
||||
*.pidb
|
||||
*.svclog
|
||||
*.scc
|
||||
|
||||
# Chutzpah Test files
|
||||
_Chutzpah*
|
||||
|
||||
# Visual C++ cache files
|
||||
ipch/
|
||||
*.aps
|
||||
*.ncb
|
||||
*.opendb
|
||||
*.opensdf
|
||||
*.sdf
|
||||
*.cachefile
|
||||
*.VC.db
|
||||
*.VC.VC.opendb
|
||||
|
||||
# Visual Studio profiler
|
||||
*.psess
|
||||
*.vsp
|
||||
*.vspx
|
||||
*.sap
|
||||
|
||||
# Visual Studio Trace Files
|
||||
*.e2e
|
||||
|
||||
# TFS 2012 Local Workspace
|
||||
$tf/
|
||||
|
||||
# Guidance Automation Toolkit
|
||||
*.gpState
|
||||
|
||||
# ReSharper is a .NET coding add-in
|
||||
_ReSharper*/
|
||||
*.[Rr]e[Ss]harper
|
||||
*.DotSettings.user
|
||||
|
||||
# TeamCity is a build add-in
|
||||
_TeamCity*
|
||||
|
||||
# DotCover is a Code Coverage Tool
|
||||
*.dotCover
|
||||
|
||||
# AxoCover is a Code Coverage Tool
|
||||
.axoCover/*
|
||||
!.axoCover/settings.json
|
||||
|
||||
# Coverlet is a free, cross platform Code Coverage Tool
|
||||
coverage*.json
|
||||
coverage*.xml
|
||||
coverage*.info
|
||||
|
||||
# Visual Studio code coverage results
|
||||
*.coverage
|
||||
*.coveragexml
|
||||
|
||||
# NCrunch
|
||||
_NCrunch_*
|
||||
.*crunch*.local.xml
|
||||
nCrunchTemp_*
|
||||
|
||||
# MightyMoose
|
||||
*.mm.*
|
||||
AutoTest.Net/
|
||||
|
||||
# Web workbench (sass)
|
||||
.sass-cache/
|
||||
|
||||
# Installshield output folder
|
||||
[Ee]xpress/
|
||||
|
||||
# DocProject is a documentation generator add-in
|
||||
DocProject/buildhelp/
|
||||
DocProject/Help/*.HxT
|
||||
DocProject/Help/*.HxC
|
||||
DocProject/Help/*.hhc
|
||||
DocProject/Help/*.hhk
|
||||
DocProject/Help/*.hhp
|
||||
DocProject/Help/Html2
|
||||
DocProject/Help/html
|
||||
|
||||
# Click-Once directory
|
||||
publish/
|
||||
|
||||
# Publish Web Output
|
||||
*.[Pp]ublish.xml
|
||||
*.azurePubxml
|
||||
# Note: Comment the next line if you want to checkin your web deploy settings,
|
||||
# but database connection strings (with potential passwords) will be unencrypted
|
||||
*.pubxml
|
||||
*.publishproj
|
||||
|
||||
# Microsoft Azure Web App publish settings. Comment the next line if you want to
|
||||
# checkin your Azure Web App publish settings, but sensitive information contained
|
||||
# in these scripts will be unencrypted
|
||||
PublishScripts/
|
||||
|
||||
# NuGet Packages
|
||||
*.nupkg
|
||||
# NuGet Symbol Packages
|
||||
*.snupkg
|
||||
# The packages folder can be ignored because of Package Restore
|
||||
**/[Pp]ackages/*
|
||||
# except build/, which is used as an MSBuild target.
|
||||
!**/[Pp]ackages/build/
|
||||
# Uncomment if necessary however generally it will be regenerated when needed
|
||||
#!**/[Pp]ackages/repositories.config
|
||||
# NuGet v3's project.json files produces more ignorable files
|
||||
*.nuget.props
|
||||
*.nuget.targets
|
||||
|
||||
# Microsoft Azure Build Output
|
||||
csx/
|
||||
*.build.csdef
|
||||
|
||||
# Microsoft Azure Emulator
|
||||
ecf/
|
||||
rcf/
|
||||
|
||||
# Windows Store app package directories and files
|
||||
AppPackages/
|
||||
BundleArtifacts/
|
||||
Package.StoreAssociation.xml
|
||||
_pkginfo.txt
|
||||
*.appx
|
||||
*.appxbundle
|
||||
*.appxupload
|
||||
|
||||
# Visual Studio cache files
|
||||
# files ending in .cache can be ignored
|
||||
*.[Cc]ache
|
||||
# but keep track of directories ending in .cache
|
||||
!?*.[Cc]ache/
|
||||
|
||||
# Others
|
||||
ClientBin/
|
||||
~$*
|
||||
*~
|
||||
*.dbmdl
|
||||
*.dbproj.schemaview
|
||||
*.jfm
|
||||
*.pfx
|
||||
*.publishsettings
|
||||
orleans.codegen.cs
|
||||
|
||||
# Including strong name files can present a security risk
|
||||
# (https://github.com/github/gitignore/pull/2483#issue-259490424)
|
||||
#*.snk
|
||||
|
||||
# Since there are multiple workflows, uncomment next line to ignore bower_components
|
||||
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
|
||||
#bower_components/
|
||||
|
||||
# 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
|
||||
ServiceFabricBackup/
|
||||
*.rptproj.bak
|
||||
|
||||
# SQL Server files
|
||||
*.mdf
|
||||
*.ldf
|
||||
*.ndf
|
||||
|
||||
# Business Intelligence projects
|
||||
*.rdl.data
|
||||
*.bim.layout
|
||||
*.bim_*.settings
|
||||
*.rptproj.rsuser
|
||||
*- [Bb]ackup.rdl
|
||||
*- [Bb]ackup ([0-9]).rdl
|
||||
*- [Bb]ackup ([0-9][0-9]).rdl
|
||||
|
||||
# Microsoft Fakes
|
||||
FakesAssemblies/
|
||||
|
||||
# GhostDoc plugin setting file
|
||||
*.GhostDoc.xml
|
||||
|
||||
# Node.js Tools for Visual Studio
|
||||
.ntvs_analysis.dat
|
||||
node_modules/
|
||||
|
||||
# Visual Studio 6 build log
|
||||
*.plg
|
||||
|
||||
# Visual Studio 6 workspace options file
|
||||
*.opt
|
||||
|
||||
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
|
||||
*.vbw
|
||||
|
||||
# Visual Studio 6 auto-generated project file (contains which files were open etc.)
|
||||
*.vbp
|
||||
|
||||
# Visual Studio 6 workspace and project file (working project files containing files to include in project)
|
||||
*.dsw
|
||||
*.dsp
|
||||
|
||||
# Visual Studio 6 technical files
|
||||
*.ncb
|
||||
*.aps
|
||||
|
||||
# Visual Studio LightSwitch build output
|
||||
**/*.HTMLClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/GeneratedArtifacts
|
||||
**/*.DesktopClient/ModelManifest.xml
|
||||
**/*.Server/GeneratedArtifacts
|
||||
**/*.Server/ModelManifest.xml
|
||||
_Pvt_Extensions
|
||||
|
||||
# Paket dependency manager
|
||||
.paket/paket.exe
|
||||
paket-files/
|
||||
|
||||
# FAKE - F# Make
|
||||
.fake/
|
||||
|
||||
# CodeRush personal settings
|
||||
.cr/personal
|
||||
|
||||
# Python Tools for Visual Studio (PTVS)
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
# Cake - Uncomment if you are using it
|
||||
# tools/**
|
||||
# !tools/packages.config
|
||||
|
||||
# Tabs Studio
|
||||
*.tss
|
||||
|
||||
# Telerik's JustMock configuration file
|
||||
*.jmconfig
|
||||
|
||||
# BizTalk build output
|
||||
*.btp.cs
|
||||
*.btm.cs
|
||||
*.odx.cs
|
||||
*.xsd.cs
|
||||
|
||||
# OpenCover UI analysis results
|
||||
OpenCover/
|
||||
|
||||
# Azure Stream Analytics local run output
|
||||
ASALocalRun/
|
||||
|
||||
# MSBuild Binary and Structured Log
|
||||
*.binlog
|
||||
|
||||
# NVidia Nsight GPU debugger configuration file
|
||||
*.nvuser
|
||||
|
||||
# MFractors (Xamarin productivity tool) working folder
|
||||
.mfractor/
|
||||
|
||||
# Local History for Visual Studio
|
||||
.localhistory/
|
||||
|
||||
# Visual Studio History (VSHistory) files
|
||||
.vshistory/
|
||||
|
||||
# BeatPulse healthcheck temp database
|
||||
healthchecksdb
|
||||
|
||||
# Backup folder for Package Reference Convert tool in Visual Studio 2017
|
||||
MigrationBackup/
|
||||
|
||||
# Ionide (cross platform F# VS Code tools) working folder
|
||||
.ionide/
|
||||
|
||||
# Fody - auto-generated XML schema
|
||||
FodyWeavers.xsd
|
||||
|
||||
# VS Code files for those working on multiple tools
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
*.code-workspace
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Windows Installer files from build outputs
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# JetBrains Rider
|
||||
*.sln.iml
|
||||
.idea
|
||||
|
||||
##
|
||||
## Visual studio for Mac
|
||||
##
|
||||
|
||||
|
||||
# globs
|
||||
Makefile.in
|
||||
*.userprefs
|
||||
*.usertasks
|
||||
config.make
|
||||
config.status
|
||||
aclocal.m4
|
||||
install-sh
|
||||
autom4te.cache/
|
||||
*.tar.gz
|
||||
tarballs/
|
||||
test-results/
|
||||
|
||||
# Mac bundle stuff
|
||||
*.dmg
|
||||
*.app
|
||||
|
||||
# content below from: https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
|
||||
# General
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Icon must end with two \r
|
||||
Icon
|
||||
|
||||
|
||||
# Thumbnails
|
||||
._*
|
||||
|
||||
# Files that might appear in the root of a volume
|
||||
.DocumentRevisions-V100
|
||||
.fseventsd
|
||||
.Spotlight-V100
|
||||
.TemporaryItems
|
||||
.Trashes
|
||||
.VolumeIcon.icns
|
||||
.com.apple.timemachine.donotpresent
|
||||
|
||||
# Directories potentially created on remote AFP share
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
|
||||
# content below from: https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
|
||||
# Windows thumbnail cache files
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
ehthumbs_vista.db
|
||||
|
||||
# Dump file
|
||||
*.stackdump
|
||||
|
||||
# Folder config file
|
||||
[Dd]esktop.ini
|
||||
|
||||
# Recycle Bin used on file shares
|
||||
$RECYCLE.BIN/
|
||||
|
||||
# Windows Installer files
|
||||
*.cab
|
||||
*.msi
|
||||
*.msix
|
||||
*.msm
|
||||
*.msp
|
||||
|
||||
# Windows shortcuts
|
||||
*.lnk
|
||||
|
||||
# Vim temporary swap files
|
||||
*.swp
|
||||
2
Create-Migration.ps1
Normal file
2
Create-Migration.ps1
Normal file
@@ -0,0 +1,2 @@
|
||||
dotnet ef migrations add $args[0] --project .\src\WebApi\WebApi.csproj --output-dir Persistence/Migrations
|
||||
dotnet ef migrations script --idempotent --project .\src\WebApi\WebApi.csproj --output migrations/migration.sql
|
||||
9
Directory.Build.props
Normal file
9
Directory.Build.props
Normal file
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Nerdbank.GitVersioning" Condition="!Exists('packages.config')">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<Version>3.6.141</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
27
Dockerfile
Normal file
27
Dockerfile
Normal file
@@ -0,0 +1,27 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
RUN apt-get update && apt-get install -y curl
|
||||
USER app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["src/Vegasco.Server.Api/Vegasco.Server.Api.csproj", "src/Vegasco.Server.Api/"]
|
||||
RUN dotnet restore "./src/Vegasco.Server.Api/Vegasco.Server.Api.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/src/Vegasco.Server.Api"
|
||||
RUN dotnet build "./Vegasco.Server.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./Vegasco.Server.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
HEALTHCHECK --interval=20s --timeout=1s --start-period=10s --retries=3 CMD curl --fail http://localhost:8080/health || exit 1
|
||||
ENTRYPOINT ["dotnet", "Vegasco.Server.Api.dll"]
|
||||
68
README.md
68
README.md
@@ -1 +1,67 @@
|
||||
# vegasco-server
|
||||
# Vegasco Server
|
||||
|
||||
Backend for the vegasco (**VE**hicle **GAS** **CO**nsumption) application.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Configuration
|
||||
|
||||
| Configuration | Description | Default | Required |
|
||||
|--------------------------|---------------------------------------------------------------------------------------------------------------|------------------------------------------------------------|----------|
|
||||
| JWT:MetadataUrl | The oidc meta data url | - | true |
|
||||
| JWT:ValidAudience | The valid audience of the JWT token. | - | true |
|
||||
| JWT:NameClaimType | The claim type of the user's name claim. For keycloak, using `preferred_username` is often the better choice. | http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name | false |
|
||||
| JWT:AllowHttpMetadataUrl | Whether to allow the meta data url to have http as protocol. Always true when `ASPNETCORE_ENVIRONMENT=true` | false | false |
|
||||
|
||||
The application uses the prefix `Vegasco_` for environment variable names. The prefix is removed when the application reads the environment variables and duplicate entries are overwritten by the environment variables.
|
||||
|
||||
Example:
|
||||
|
||||
- `foo=bar1`
|
||||
- `Vegasco_foo=bar2`
|
||||
|
||||
Results in:
|
||||
|
||||
- `foo=bar2`
|
||||
- `Vegasco_foo=bar2`
|
||||
|
||||
Configuration hierarchy in environment variables is usually denoted using a colon (`:`). But because on some systems the colon character is a reserved character, you can use a double underscore (`__`) as an alternative. The application will replace the double underscore with a colon when reading the environment variables.
|
||||
|
||||
Example:
|
||||
|
||||
The environment variable `foo__bar=value` (as well as `Vegasco_foo__bar=value`) will be converted to `foo:bar=value` in the application.
|
||||
|
||||
### Configuration examples
|
||||
|
||||
As environment variables:
|
||||
|
||||
```env
|
||||
Vegasco_JWT__Authority=https://example.authority.com
|
||||
Vegasco_JWT__Audience=example-audience
|
||||
Vegasco_JWT__Issuer=https://example.authority.com/realms/example-realm/
|
||||
Vegasco_JWT__NameClaimType=preferred_username
|
||||
```
|
||||
|
||||
As appsettings.json (or a environment specific appsettings.*.json):
|
||||
|
||||
**Note: the `Vegasco_` prefix is only for environment variables**
|
||||
|
||||
```json
|
||||
{
|
||||
"JWT": {
|
||||
"Authority": "https://example.authority.com/realms/example-realm",
|
||||
"Audience": "example-audience",
|
||||
"Issuer": "https://example.authority.com/realms/example-realm/",
|
||||
"NameClaimType": "preferred_username"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Running the application
|
||||
|
||||
The solution uses Aspire to orchestrate the application. Specifically, it introduces sensible service defaults, including but not limited to OpenTelemetry,
|
||||
creates a Postgres database as a docker container, and starts the Api with the correct configuration to communicate with the database.
|
||||
|
||||
Ensure you have an identity provider set up, for example Keycloak, and configured the relevant options described above.
|
||||
|
||||
Then, to run the application, ensure you have Docker running, then run the `Vegasco.Server.AppHost` launch profile.
|
||||
|
||||
1
Run-PostgresDb.ps1
Normal file
1
Run-PostgresDb.ps1
Normal file
@@ -0,0 +1 @@
|
||||
docker run -d -p 5432:5432 --restart always --name vegasco-test-db -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres postgres:16.3-alpine
|
||||
3
src/Vegasco.Server.Api/Assembly.cs
Normal file
3
src/Vegasco.Server.Api/Assembly.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
using StronglyTypedIds;
|
||||
|
||||
[assembly: StronglyTypedIdDefaults(Template.Guid, "guid-efcore")]
|
||||
28
src/Vegasco.Server.Api/Authentication/JwtOptions.cs
Normal file
28
src/Vegasco.Server.Api/Authentication/JwtOptions.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using FluentValidation;
|
||||
|
||||
namespace Vegasco.Server.Api.Authentication;
|
||||
|
||||
public class JwtOptions
|
||||
{
|
||||
public const string SectionName = "JWT";
|
||||
|
||||
public string ValidAudience { get; set; } = "";
|
||||
|
||||
public string MetadataUrl { get; set; } = "";
|
||||
|
||||
public string? NameClaimType { get; set; }
|
||||
|
||||
public bool AllowHttpMetadataUrl { get; set; }
|
||||
}
|
||||
|
||||
public class JwtOptionsValidator : AbstractValidator<JwtOptions>
|
||||
{
|
||||
public JwtOptionsValidator()
|
||||
{
|
||||
RuleFor(x => x.ValidAudience)
|
||||
.NotEmpty();
|
||||
|
||||
RuleFor(x => x.MetadataUrl)
|
||||
.NotEmpty();
|
||||
}
|
||||
}
|
||||
78
src/Vegasco.Server.Api/Authentication/UserAccessor.cs
Normal file
78
src/Vegasco.Server.Api/Authentication/UserAccessor.cs
Normal file
@@ -0,0 +1,78 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Vegasco.Server.Api.Authentication;
|
||||
|
||||
public sealed class UserAccessor
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
private readonly IOptions<JwtOptions> _jwtOptions;
|
||||
|
||||
/// <summary>
|
||||
/// Stores the username upon first retrieval
|
||||
/// </summary>
|
||||
private string? _cachedUsername;
|
||||
|
||||
/// <summary>
|
||||
/// Stores the id upon first retrieval
|
||||
/// </summary>
|
||||
private string? _cachedId;
|
||||
|
||||
public UserAccessor(IHttpContextAccessor httpContextAccessor, IOptions<JwtOptions> jwtOptions)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
_jwtOptions = jwtOptions;
|
||||
}
|
||||
|
||||
public string GetUsername()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_cachedUsername))
|
||||
{
|
||||
_cachedUsername = GetClaimValue(_jwtOptions.Value.NameClaimType ?? ClaimTypes.Name);
|
||||
}
|
||||
|
||||
return _cachedUsername;
|
||||
}
|
||||
|
||||
public string GetUserId()
|
||||
{
|
||||
if (string.IsNullOrEmpty(_cachedId))
|
||||
{
|
||||
_cachedId = GetClaimValue(ClaimTypes.NameIdentifier);
|
||||
}
|
||||
|
||||
return _cachedId;
|
||||
}
|
||||
|
||||
private string GetClaimValue(string claimType)
|
||||
{
|
||||
HttpContext? httpContext = _httpContextAccessor.HttpContext;
|
||||
|
||||
if (httpContext is null)
|
||||
{
|
||||
ThrowForMissingHttpContext();
|
||||
}
|
||||
|
||||
string? claimValue = httpContext.User.FindFirstValue(claimType);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(claimValue))
|
||||
{
|
||||
ThrowForMissingClaim(claimType);
|
||||
}
|
||||
|
||||
return claimValue;
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
private static void ThrowForMissingHttpContext()
|
||||
{
|
||||
throw new InvalidOperationException("No HttpContext available.");
|
||||
}
|
||||
|
||||
[DoesNotReturn]
|
||||
private static void ThrowForMissingClaim(string claimType)
|
||||
{
|
||||
throw new InvalidOperationException($"No claim of type '{claimType}' found on the current user.");
|
||||
}
|
||||
}
|
||||
42
src/Vegasco.Server.Api/Cars/Car.cs
Normal file
42
src/Vegasco.Server.Api/Cars/Car.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Users;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public class Car
|
||||
{
|
||||
public CarId Id { get; set; } = CarId.New();
|
||||
|
||||
public string Name { get; set; } = "";
|
||||
|
||||
public string UserId { get; set; } = "";
|
||||
|
||||
public virtual User User { get; set; } = null!;
|
||||
|
||||
public virtual ICollection<Consumption> Consumptions { get; set; } = [];
|
||||
}
|
||||
|
||||
public class CarTableConfiguration : IEntityTypeConfiguration<Car>
|
||||
{
|
||||
public const int NameMaxLength = 50;
|
||||
|
||||
public void Configure(EntityTypeBuilder<Car> builder)
|
||||
{
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion<CarId.EfCoreValueConverter>();
|
||||
|
||||
builder.Property(x => x.Name)
|
||||
.IsRequired()
|
||||
.HasMaxLength(NameMaxLength);
|
||||
|
||||
builder.Property(x => x.UserId)
|
||||
.IsRequired();
|
||||
|
||||
builder.HasOne(x => x.User)
|
||||
.WithMany(x => x.Cars);
|
||||
}
|
||||
}
|
||||
6
src/Vegasco.Server.Api/Cars/CarId.cs
Normal file
6
src/Vegasco.Server.Api/Cars/CarId.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using StronglyTypedIds;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
[StronglyTypedId]
|
||||
public partial struct CarId;
|
||||
69
src/Vegasco.Server.Api/Cars/CreateCar.cs
Normal file
69
src/Vegasco.Server.Api/Cars/CreateCar.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
using Vegasco.Server.Api.Users;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class CreateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPost("cars", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
ApplicationDbContext dbContext,
|
||||
UserAccessor userAccessor,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken: cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
string userId = userAccessor.GetUserId();
|
||||
|
||||
User? user = await dbContext.Users.FindAsync([userId], cancellationToken: cancellationToken);
|
||||
if (user is null)
|
||||
{
|
||||
user = new User
|
||||
{
|
||||
Id = userId
|
||||
};
|
||||
await dbContext.Users.AddAsync(user, cancellationToken);
|
||||
}
|
||||
|
||||
Car car = new()
|
||||
{
|
||||
Name = request.Name,
|
||||
UserId = userId
|
||||
};
|
||||
|
||||
await dbContext.Cars.AddAsync(car, cancellationToken);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id.Value, car.Name);
|
||||
return TypedResults.Created($"/v1/cars/{car.Id}", response);
|
||||
}
|
||||
}
|
||||
31
src/Vegasco.Server.Api/Cars/DeleteCar.cs
Normal file
31
src/Vegasco.Server.Api/Cars/DeleteCar.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class DeleteCar
|
||||
{
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapDelete("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
dbContext.Cars.Remove(car);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.NoContent();
|
||||
}
|
||||
}
|
||||
31
src/Vegasco.Server.Api/Cars/GetCar.cs
Normal file
31
src/Vegasco.Server.Api/Cars/GetCar.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class GetCar
|
||||
{
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
var response = new Response(car.Id.Value, car.Name);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
46
src/Vegasco.Server.Api/Cars/GetCars.cs
Normal file
46
src/Vegasco.Server.Api/Cars/GetCars.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class GetCars
|
||||
{
|
||||
public class ApiResponse
|
||||
{
|
||||
public IEnumerable<ResponseDto> Cars { get; set; } = [];
|
||||
}
|
||||
|
||||
public record ResponseDto(Guid Id, string Name);
|
||||
|
||||
public class Request
|
||||
{
|
||||
[FromQuery(Name = "page")] public int? Page { get; set; }
|
||||
[FromQuery(Name = "pageSize")] public int? PageSize { get; set; }
|
||||
}
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("cars", Endpoint)
|
||||
.WithDescription("Returns all cars")
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
private static async Task<Ok<ApiResponse>> Endpoint(
|
||||
[AsParameters] Request request,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ResponseDto> cars = await dbContext.Cars
|
||||
.Select(x => new ResponseDto(x.Id.Value, x.Name))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var response = new ApiResponse
|
||||
{
|
||||
Cars = cars
|
||||
};
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
58
src/Vegasco.Server.Api/Cars/UpdateCar.cs
Normal file
58
src/Vegasco.Server.Api/Cars/UpdateCar.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Cars;
|
||||
|
||||
public static class UpdateCar
|
||||
{
|
||||
public record Request(string Name);
|
||||
public record Response(Guid Id, string Name);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPut("cars/{id:guid}", Endpoint)
|
||||
.WithTags("Cars");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator()
|
||||
{
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty()
|
||||
.MaximumLength(CarTableConfiguration.NameMaxLength);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<IResult> Endpoint(
|
||||
Guid id,
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
ApplicationDbContext dbContext,
|
||||
UserAccessor userAccessor,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(id)], cancellationToken: cancellationToken);
|
||||
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
car.Name = request.Name;
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
Response response = new(car.Id.Value, car.Name);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
9
src/Vegasco.Server.Api/Common/Constants.cs
Normal file
9
src/Vegasco.Server.Api/Common/Constants.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
public static class Authorization
|
||||
{
|
||||
public const string RequireAuthenticatedUserPolicy = "RequireAuthenticatedUser";
|
||||
}
|
||||
}
|
||||
142
src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs
Normal file
142
src/Vegasco.Server.Api/Common/DependencyInjectionExtensions.cs
Normal file
@@ -0,0 +1,142 @@
|
||||
using Asp.Versioning;
|
||||
using FluentValidation;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
public static class DependencyInjectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds all the Api related services to the Dependency Injection container.
|
||||
/// </summary>
|
||||
/// <param name="builder"></param>
|
||||
public static void AddApiServices(this IHostApplicationBuilder builder)
|
||||
{
|
||||
builder.Services
|
||||
.AddMiscellaneousServices()
|
||||
.AddCustomOpenApi()
|
||||
.AddApiVersioning()
|
||||
.AddAuthenticationAndAuthorization(builder.Environment);
|
||||
|
||||
builder.AddDbContext();
|
||||
}
|
||||
|
||||
private static IServiceCollection AddMiscellaneousServices(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton(sp =>
|
||||
{
|
||||
var assemblyName = Assembly.GetExecutingAssembly()
|
||||
.GetName()
|
||||
.Name ?? "Vegasco.Server.Api";
|
||||
return new ActivitySource(assemblyName);
|
||||
});
|
||||
|
||||
services.AddResponseCompression();
|
||||
|
||||
services.AddValidatorsFromAssemblies(
|
||||
[
|
||||
typeof(IApiMarker).Assembly
|
||||
], ServiceLifetime.Singleton);
|
||||
|
||||
services.AddHealthChecks();
|
||||
|
||||
services.AddHttpContextAccessor();
|
||||
|
||||
services.AddHostedService<ApplyMigrationsService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddCustomOpenApi(this IServiceCollection services)
|
||||
{
|
||||
services.AddEndpointsApiExplorer();
|
||||
services.AddOpenApi(o =>
|
||||
{
|
||||
o.CreateSchemaReferenceId = jsonTypeInfo =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(jsonTypeInfo.Type.FullName))
|
||||
{
|
||||
return jsonTypeInfo.Type.Name;
|
||||
}
|
||||
|
||||
string? fullClassName = jsonTypeInfo.Type.FullName;
|
||||
|
||||
if (!string.IsNullOrEmpty(jsonTypeInfo.Type.Namespace))
|
||||
{
|
||||
fullClassName = fullClassName
|
||||
.Replace(jsonTypeInfo.Type.Namespace, "")
|
||||
.TrimStart('.');
|
||||
}
|
||||
|
||||
fullClassName = fullClassName.Replace('+', '_');
|
||||
return fullClassName;
|
||||
};
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddApiVersioning(this IServiceCollection services)
|
||||
{
|
||||
services.AddApiVersioning(o =>
|
||||
{
|
||||
o.DefaultApiVersion = new ApiVersion(1);
|
||||
o.ApiVersionReader = new UrlSegmentApiVersionReader();
|
||||
o.ReportApiVersions = true;
|
||||
})
|
||||
.AddApiExplorer(o =>
|
||||
{
|
||||
o.GroupNameFormat = "'v'V";
|
||||
o.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IServiceCollection AddAuthenticationAndAuthorization(this IServiceCollection services, IHostEnvironment environment)
|
||||
{
|
||||
services.AddOptions<JwtOptions>()
|
||||
.BindConfiguration(JwtOptions.SectionName)
|
||||
.ValidateFluently()
|
||||
.ValidateOnStart();
|
||||
|
||||
var jwtOptions = services.BuildServiceProvider().GetRequiredService<IOptions<JwtOptions>>();
|
||||
|
||||
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
|
||||
{
|
||||
o.MetadataAddress = jwtOptions.Value.MetadataUrl;
|
||||
|
||||
o.TokenValidationParameters.ValidAudience = jwtOptions.Value.ValidAudience;
|
||||
o.TokenValidationParameters.ValidateAudience = true;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(jwtOptions.Value.NameClaimType))
|
||||
{
|
||||
o.TokenValidationParameters.NameClaimType = jwtOptions.Value.NameClaimType;
|
||||
}
|
||||
|
||||
o.RequireHttpsMetadata = !jwtOptions.Value.AllowHttpMetadataUrl && !environment.IsDevelopment();
|
||||
});
|
||||
|
||||
services.AddAuthorizationBuilder()
|
||||
.AddPolicy(Constants.Authorization.RequireAuthenticatedUserPolicy, p => p
|
||||
.RequireAuthenticatedUser()
|
||||
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme));
|
||||
|
||||
services.AddScoped<UserAccessor>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static IHostApplicationBuilder AddDbContext(this IHostApplicationBuilder builder)
|
||||
{
|
||||
builder.AddNpgsqlDbContext<ApplicationDbContext>(AppHost.Shared.Constants.Database.Name);
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
37
src/Vegasco.Server.Api/Common/FluentValidationOptions.cs
Normal file
37
src/Vegasco.Server.Api/Common/FluentValidationOptions.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
public class FluentValidationOptions<TOptions> : IValidateOptions<TOptions>
|
||||
where TOptions : class
|
||||
{
|
||||
private readonly IEnumerable<IValidator<TOptions>> _validators;
|
||||
|
||||
public string? Name { get; set; }
|
||||
|
||||
public FluentValidationOptions(string? name, IEnumerable<IValidator<TOptions>> validators)
|
||||
{
|
||||
Name = name;
|
||||
_validators = validators;
|
||||
}
|
||||
|
||||
public ValidateOptionsResult Validate(string? name, TOptions options)
|
||||
{
|
||||
if (name is not null && name != Name)
|
||||
{
|
||||
return ValidateOptionsResult.Skip;
|
||||
}
|
||||
|
||||
ArgumentNullException.ThrowIfNull(options);
|
||||
|
||||
List<ValidationResult> failedValidations = _validators.ValidateAllAsync(options).Result;
|
||||
if (failedValidations.Count == 0)
|
||||
{
|
||||
return ValidateOptionsResult.Success;
|
||||
}
|
||||
|
||||
return ValidateOptionsResult.Fail(failedValidations.SelectMany(x => x.Errors.Select(x => x.ErrorMessage)));
|
||||
}
|
||||
}
|
||||
3
src/Vegasco.Server.Api/Common/IApiMarker.cs
Normal file
3
src/Vegasco.Server.Api/Common/IApiMarker.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
public interface IApiMarker;
|
||||
53
src/Vegasco.Server.Api/Common/StartupExtensions.cs
Normal file
53
src/Vegasco.Server.Api/Common/StartupExtensions.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using System.Globalization;
|
||||
using Vegasco.Server.Api.Endpoints;
|
||||
using Vegasco.Server.ServiceDefaults;
|
||||
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
internal static class StartupExtensions
|
||||
{
|
||||
internal static WebApplication ConfigureServices(this WebApplicationBuilder builder)
|
||||
{
|
||||
builder.AddServiceDefaults();
|
||||
|
||||
builder.Configuration.AddEnvironmentVariables("Vegasco_");
|
||||
|
||||
builder.AddApiServices();
|
||||
|
||||
WebApplication app = builder.Build();
|
||||
return app;
|
||||
}
|
||||
|
||||
internal static WebApplication ConfigureRequestPipeline(this WebApplication app)
|
||||
{
|
||||
app.UseRequestLocalization(o =>
|
||||
{
|
||||
o.SupportedCultures =
|
||||
[
|
||||
new CultureInfo("en")
|
||||
];
|
||||
|
||||
o.SupportedUICultures = o.SupportedCultures;
|
||||
|
||||
CultureInfo defaultCulture = o.SupportedCultures[0];
|
||||
o.DefaultRequestCulture = new RequestCulture(defaultCulture);
|
||||
});
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapHealthChecks("/health");
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapEndpoints();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi("/swagger/{documentName}/swagger.json");
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
62
src/Vegasco.Server.Api/Common/ValidatorExtensions.cs
Normal file
62
src/Vegasco.Server.Api/Common/ValidatorExtensions.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Vegasco.Server.Api.Common;
|
||||
|
||||
public static class ValidatorExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Asynchronously validates an instance of <typeparamref name="T"/> against all <see cref="IValidator{T}"/> instances in <paramref name="validators"/>.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="validators"></param>
|
||||
/// <param name="instance"></param>
|
||||
/// <returns>The failed validation results.</returns>
|
||||
public static async Task<List<ValidationResult>> ValidateAllAsync<T>(this IEnumerable<IValidator<T>> validators, T instance, CancellationToken cancellationToken = default)
|
||||
{
|
||||
List<Task<ValidationResult>> validationTasks = validators
|
||||
.Select(validator => validator.ValidateAsync(instance, cancellationToken))
|
||||
.ToList();
|
||||
|
||||
await Task.WhenAll(validationTasks);
|
||||
|
||||
List<ValidationResult> failedValidations = validationTasks
|
||||
.Select(x => x.Result)
|
||||
.Where(x => !x.IsValid)
|
||||
.ToList();
|
||||
|
||||
return failedValidations;
|
||||
}
|
||||
|
||||
public static Dictionary<string, string[]> ToCombinedDictionary(this IEnumerable<ValidationResult> validationResults)
|
||||
{
|
||||
// Use a hash set to avoid duplicate error messages.
|
||||
Dictionary<string, HashSet<string>> combinedErrors = [];
|
||||
|
||||
foreach (ValidationFailure? error in validationResults.SelectMany(x => x.Errors))
|
||||
{
|
||||
if (!combinedErrors.TryGetValue(error.PropertyName, out HashSet<string>? value))
|
||||
{
|
||||
value = [error.ErrorMessage];
|
||||
combinedErrors[error.PropertyName] = value;
|
||||
continue;
|
||||
}
|
||||
|
||||
value.Add(error.ErrorMessage);
|
||||
}
|
||||
|
||||
return combinedErrors.ToDictionary(x => x.Key, x => x.Value.ToArray());
|
||||
}
|
||||
|
||||
public static OptionsBuilder<T> ValidateFluently<T>(this OptionsBuilder<T> builder)
|
||||
where T : class
|
||||
{
|
||||
builder.Services.AddTransient<IValidateOptions<T>>(serviceProvider =>
|
||||
{
|
||||
IEnumerable<IValidator<T>> validators = serviceProvider.GetServices<IValidator<T>>() ?? [];
|
||||
return new FluentValidationOptions<T>(builder.Name, validators);
|
||||
});
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
52
src/Vegasco.Server.Api/Consumptions/Consumption.cs
Normal file
52
src/Vegasco.Server.Api/Consumptions/Consumption.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public class Consumption
|
||||
{
|
||||
public ConsumptionId Id { get; set; } = ConsumptionId.New();
|
||||
|
||||
public DateTimeOffset DateTime { get; set; }
|
||||
|
||||
public double Distance { get; set; }
|
||||
|
||||
public double Amount { get; set; }
|
||||
|
||||
public bool IgnoreInCalculation { get; set; }
|
||||
|
||||
public CarId CarId { get; set; }
|
||||
|
||||
public virtual Car Car { get; set; } = null!;
|
||||
}
|
||||
|
||||
public class ConsumptionTableConfiguration : IEntityTypeConfiguration<Consumption>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Consumption> builder)
|
||||
{
|
||||
builder.HasKey(x => x.Id);
|
||||
|
||||
builder.Property(x => x.Id)
|
||||
.HasConversion<ConsumptionId.EfCoreValueConverter>();
|
||||
|
||||
builder.Property(x => x.DateTime)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Distance)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.Amount)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.IgnoreInCalculation)
|
||||
.IsRequired();
|
||||
|
||||
builder.Property(x => x.CarId)
|
||||
.IsRequired()
|
||||
.HasConversion<CarId.EfCoreValueConverter>();
|
||||
|
||||
builder.HasOne(x => x.Car)
|
||||
.WithMany(x => x.Consumptions);
|
||||
}
|
||||
}
|
||||
7
src/Vegasco.Server.Api/Consumptions/ConsumptionId.cs
Normal file
7
src/Vegasco.Server.Api/Consumptions/ConsumptionId.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
using StronglyTypedIds;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
|
||||
[StronglyTypedId]
|
||||
public partial struct ConsumptionId;
|
||||
74
src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs
Normal file
74
src/Vegasco.Server.Api/Consumptions/CreateConsumption.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public static class CreateConsumption
|
||||
{
|
||||
public record Request(DateTimeOffset DateTime, double Distance, double Amount, bool IgnoreInCalculation, Guid CarId);
|
||||
|
||||
public record Response(Guid Id, DateTimeOffset DateTime, double Distance, double Amount, bool IgnoreInCalculation, Guid CarId);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPost("consumptions", Endpoint)
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator(TimeProvider timeProvider)
|
||||
{
|
||||
RuleFor(x => x.DateTime.ToUniversalTime())
|
||||
.LessThanOrEqualTo(timeProvider.GetUtcNow())
|
||||
.WithName(nameof(Request.DateTime));
|
||||
|
||||
RuleFor(x => x.Distance)
|
||||
.GreaterThan(0);
|
||||
|
||||
RuleFor(x => x.Amount)
|
||||
.GreaterThan(0);
|
||||
|
||||
RuleFor(x => x.CarId)
|
||||
.NotEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
Car? car = await dbContext.Cars.FindAsync([new CarId(request.CarId)], cancellationToken);
|
||||
if (car is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
var consumption = new Consumption
|
||||
{
|
||||
DateTime = request.DateTime.ToUniversalTime(),
|
||||
Distance = request.Distance,
|
||||
Amount = request.Amount,
|
||||
IgnoreInCalculation = request.IgnoreInCalculation,
|
||||
CarId = new CarId(request.CarId)
|
||||
};
|
||||
|
||||
dbContext.Consumptions.Add(consumption);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.Created($"consumptions/{consumption.Id.Value}",
|
||||
new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance, consumption.Amount, consumption.IgnoreInCalculation, consumption.CarId.Value));
|
||||
}
|
||||
}
|
||||
30
src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs
Normal file
30
src/Vegasco.Server.Api/Consumptions/DeleteConsumptions.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public static class DeleteConsumption
|
||||
{
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapDelete("consumptions/{id:guid}", Endpoint)
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
Guid id,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Consumption? consumption = await dbContext.Consumptions.FindAsync([new ConsumptionId(id)], cancellationToken);
|
||||
if (consumption is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
dbContext.Consumptions.Remove(consumption);
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.NoContent();
|
||||
}
|
||||
}
|
||||
32
src/Vegasco.Server.Api/Consumptions/GetConsumption.cs
Normal file
32
src/Vegasco.Server.Api/Consumptions/GetConsumption.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public static class GetConsumption
|
||||
{
|
||||
public record Response(Guid Id, DateTimeOffset DateTime, double Distance, double Amount, bool IgnoreInCalculation, Guid CarId);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("consumptions/{id:guid}", Endpoint)
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
Guid id,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
Consumption? consumption = await dbContext.Consumptions.FindAsync([new ConsumptionId(id)], cancellationToken);
|
||||
|
||||
if (consumption is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
var response = new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance,
|
||||
consumption.Amount, consumption.IgnoreInCalculation, consumption.CarId.Value);
|
||||
return TypedResults.Ok(response);
|
||||
}
|
||||
}
|
||||
53
src/Vegasco.Server.Api/Consumptions/GetConsumptions.cs
Normal file
53
src/Vegasco.Server.Api/Consumptions/GetConsumptions.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public static class GetConsumptions
|
||||
{
|
||||
public class ApiResponse
|
||||
{
|
||||
public IEnumerable<ResponseDto> Consumptions { get; set; } = [];
|
||||
}
|
||||
|
||||
public record ResponseDto(
|
||||
Guid Id,
|
||||
DateTimeOffset DateTime,
|
||||
double Distance,
|
||||
double Amount,
|
||||
bool IgnoreInCalculation,
|
||||
Guid CarId);
|
||||
|
||||
public class Request
|
||||
{
|
||||
[FromQuery(Name = "page")] public int? Page { get; set; }
|
||||
[FromQuery(Name = "pageSize")] public int? PageSize { get; set; }
|
||||
}
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("consumptions", Endpoint)
|
||||
.WithDescription("Returns all consumption entries")
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
private static async Task<Ok<ApiResponse>> Endpoint(
|
||||
[AsParameters] Request request,
|
||||
ApplicationDbContext dbContext,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ResponseDto> consumptions = await dbContext.Consumptions
|
||||
.Select(x =>
|
||||
new ResponseDto(x.Id.Value, x.DateTime, x.Distance, x.Amount, x.IgnoreInCalculation, x.CarId.Value))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var apiResponse = new ApiResponse
|
||||
{
|
||||
Consumptions = consumptions
|
||||
};
|
||||
return TypedResults.Ok(apiResponse);
|
||||
}
|
||||
}
|
||||
65
src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs
Normal file
65
src/Vegasco.Server.Api/Consumptions/UpdateConsumption.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using FluentValidation;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Consumptions;
|
||||
|
||||
public static class UpdateConsumption
|
||||
{
|
||||
public record Request(DateTimeOffset DateTime, double Distance, double Amount, bool IgnoreInCalculation);
|
||||
|
||||
public record Response(Guid Id, DateTimeOffset DateTime, double Distance, double Amount, bool IgnoreInCalculation, Guid CarId);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapPut("consumptions/{id:guid}", Endpoint)
|
||||
.WithTags("Consumptions");
|
||||
}
|
||||
|
||||
public class Validator : AbstractValidator<Request>
|
||||
{
|
||||
public Validator(TimeProvider timeProvider)
|
||||
{
|
||||
RuleFor(x => x.DateTime.ToUniversalTime())
|
||||
.LessThanOrEqualTo(timeProvider.GetUtcNow())
|
||||
.WithName(nameof(Request.DateTime));
|
||||
|
||||
RuleFor(x => x.Distance)
|
||||
.GreaterThan(0);
|
||||
|
||||
RuleFor(x => x.Amount)
|
||||
.GreaterThan(0);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<IResult> Endpoint(
|
||||
ApplicationDbContext dbContext,
|
||||
Guid id,
|
||||
Request request,
|
||||
IEnumerable<IValidator<Request>> validators,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
List<ValidationResult> failedValidations = await validators.ValidateAllAsync(request, cancellationToken);
|
||||
if (failedValidations.Count > 0)
|
||||
{
|
||||
return TypedResults.BadRequest(new HttpValidationProblemDetails(failedValidations.ToCombinedDictionary()));
|
||||
}
|
||||
|
||||
Consumption? consumption = await dbContext.Consumptions.FindAsync([new ConsumptionId(id)], cancellationToken);
|
||||
if (consumption is null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
consumption.DateTime = request.DateTime.ToUniversalTime();
|
||||
consumption.Distance = request.Distance;
|
||||
consumption.Amount = request.Amount;
|
||||
consumption.IgnoreInCalculation = request.IgnoreInCalculation;
|
||||
|
||||
await dbContext.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return TypedResults.Ok(new Response(consumption.Id.Value, consumption.DateTime, consumption.Distance, consumption.Amount, consumption.IgnoreInCalculation, consumption.CarId.Value));
|
||||
}
|
||||
}
|
||||
36
src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs
Normal file
36
src/Vegasco.Server.Api/Endpoints/EndpointExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Asp.Versioning.Builder;
|
||||
using Asp.Versioning.Conventions;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Info;
|
||||
|
||||
namespace Vegasco.Server.Api.Endpoints;
|
||||
|
||||
public static class EndpointExtensions
|
||||
{
|
||||
public static void MapEndpoints(this IEndpointRouteBuilder builder)
|
||||
{
|
||||
ApiVersionSet apiVersionSet = builder.NewApiVersionSet()
|
||||
.HasApiVersion(1.0)
|
||||
.Build();
|
||||
|
||||
RouteGroupBuilder versionedApis = builder.MapGroup("/v{apiVersion:apiVersion}")
|
||||
.WithApiVersionSet(apiVersionSet)
|
||||
.RequireAuthorization(Constants.Authorization.RequireAuthenticatedUserPolicy);
|
||||
|
||||
GetCar.MapEndpoint(versionedApis);
|
||||
GetCars.MapEndpoint(versionedApis);
|
||||
CreateCar.MapEndpoint(versionedApis);
|
||||
UpdateCar.MapEndpoint(versionedApis);
|
||||
DeleteCar.MapEndpoint(versionedApis);
|
||||
|
||||
GetConsumptions.MapEndpoint(versionedApis);
|
||||
GetConsumption.MapEndpoint(versionedApis);
|
||||
CreateConsumption.MapEndpoint(versionedApis);
|
||||
UpdateConsumption.MapEndpoint(versionedApis);
|
||||
DeleteConsumption.MapEndpoint(versionedApis);
|
||||
|
||||
GetServerInfo.MapEndpoint(versionedApis);
|
||||
}
|
||||
}
|
||||
29
src/Vegasco.Server.Api/Info/GetServerInfo.cs
Normal file
29
src/Vegasco.Server.Api/Info/GetServerInfo.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
|
||||
namespace Vegasco.Server.Api.Info;
|
||||
|
||||
public class GetServerInfo
|
||||
{
|
||||
public record Response(
|
||||
string FullVersion,
|
||||
string CommitId,
|
||||
DateTime CommitDate,
|
||||
string Environment);
|
||||
|
||||
public static RouteHandlerBuilder MapEndpoint(IEndpointRouteBuilder builder)
|
||||
{
|
||||
return builder
|
||||
.MapGet("info/server", Endpoint)
|
||||
.WithTags("Info");
|
||||
}
|
||||
|
||||
private static Ok<Response> Endpoint(
|
||||
IHostEnvironment environment)
|
||||
{
|
||||
return TypedResults.Ok(new Response(
|
||||
ThisAssembly.AssemblyInformationalVersion,
|
||||
ThisAssembly.GitCommitId,
|
||||
ThisAssembly.GitCommitDate,
|
||||
environment.EnvironmentName));
|
||||
}
|
||||
}
|
||||
22
src/Vegasco.Server.Api/Persistence/ApplicationDbContext.cs
Normal file
22
src/Vegasco.Server.Api/Persistence/ApplicationDbContext.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Common;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Users;
|
||||
|
||||
namespace Vegasco.Server.Api.Persistence;
|
||||
|
||||
public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<Car> Cars { get; set; }
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
public DbSet<Consumption> Consumptions { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(IApiMarker).Assembly);
|
||||
}
|
||||
}
|
||||
24
src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs
Normal file
24
src/Vegasco.Server.Api/Persistence/ApplyMigrationsService.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Vegasco.Server.Api.Persistence;
|
||||
|
||||
public class ApplyMigrationsService(
|
||||
ILogger<ApplyMigrationsService> logger,
|
||||
IServiceScopeFactory scopeFactory,
|
||||
ActivitySource activitySource)
|
||||
: IHostedService
|
||||
{
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
using var activity = activitySource.StartActivity("ApplyMigrations");
|
||||
|
||||
logger.LogInformation("Starting migrations");
|
||||
|
||||
using IServiceScope scope = scopeFactory.CreateScope();
|
||||
await using var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
await dbContext.Database.MigrateAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||
}
|
||||
121
src/Vegasco.Server.Api/Persistence/Migrations/20240818105918_Initial.Designer.cs
generated
Normal file
121
src/Vegasco.Server.Api/Persistence/Migrations/20240818105918_Initial.Designer.cs
generated
Normal file
@@ -0,0 +1,121 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.Server.Api.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
[Migration("20240818105918_Initial")]
|
||||
partial class Initial
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("Cars");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Consumptions.Consumption", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<double>("Amount")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<Guid>("CarId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTimeOffset>("DateTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<double>("Distance")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<bool>("IgnoreInCalculation")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CarId");
|
||||
|
||||
b.ToTable("Consumptions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.Server.Api.Users.User", "User")
|
||||
.WithMany("Cars")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Consumptions.Consumption", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.Server.Api.Cars.Car", "Car")
|
||||
.WithMany("Consumptions")
|
||||
.HasForeignKey("CarId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Car");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.Navigation("Consumptions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Users.User", b =>
|
||||
{
|
||||
b.Navigation("Cars");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.Server.Api.Persistence.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Initial : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Users",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Users", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Cars",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
Name = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
|
||||
UserId = table.Column<string>(type: "text", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Cars", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Cars_Users_UserId",
|
||||
column: x => x.UserId,
|
||||
principalTable: "Users",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Consumptions",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<Guid>(type: "uuid", nullable: false),
|
||||
DateTime = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
|
||||
Distance = table.Column<double>(type: "double precision", nullable: false),
|
||||
Amount = table.Column<double>(type: "double precision", nullable: false),
|
||||
IgnoreInCalculation = table.Column<bool>(type: "boolean", nullable: false),
|
||||
CarId = table.Column<Guid>(type: "uuid", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Consumptions", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_Consumptions_Cars_CarId",
|
||||
column: x => x.CarId,
|
||||
principalTable: "Cars",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Cars_UserId",
|
||||
table: "Cars",
|
||||
column: "UserId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_Consumptions_CarId",
|
||||
table: "Consumptions",
|
||||
column: "CarId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "Consumptions");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Cars");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Users");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace Vegasco.Server.Api.Persistence.Migrations
|
||||
{
|
||||
[DbContext(typeof(ApplicationDbContext))]
|
||||
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
|
||||
{
|
||||
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "8.0.8")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||
|
||||
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("character varying(50)");
|
||||
|
||||
b.Property<string>("UserId")
|
||||
.IsRequired()
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("UserId");
|
||||
|
||||
b.ToTable("Cars");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Consumptions.Consumption", b =>
|
||||
{
|
||||
b.Property<Guid>("Id")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<double>("Amount")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<Guid>("CarId")
|
||||
.HasColumnType("uuid");
|
||||
|
||||
b.Property<DateTimeOffset>("DateTime")
|
||||
.HasColumnType("timestamp with time zone");
|
||||
|
||||
b.Property<double>("Distance")
|
||||
.HasColumnType("double precision");
|
||||
|
||||
b.Property<bool>("IgnoreInCalculation")
|
||||
.HasColumnType("boolean");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("CarId");
|
||||
|
||||
b.ToTable("Consumptions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Users.User", b =>
|
||||
{
|
||||
b.Property<string>("Id")
|
||||
.HasColumnType("text");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Users");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.Server.Api.Users.User", "User")
|
||||
.WithMany("Cars")
|
||||
.HasForeignKey("UserId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("User");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Consumptions.Consumption", b =>
|
||||
{
|
||||
b.HasOne("Vegasco.Server.Api.Cars.Car", "Car")
|
||||
.WithMany("Consumptions")
|
||||
.HasForeignKey("CarId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Car");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Cars.Car", b =>
|
||||
{
|
||||
b.Navigation("Consumptions");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("Vegasco.Server.Api.Users.User", b =>
|
||||
{
|
||||
b.Navigation("Cars");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
||||
6
src/Vegasco.Server.Api/Program.cs
Normal file
6
src/Vegasco.Server.Api/Program.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
using Vegasco.Server.Api.Common;
|
||||
|
||||
WebApplication.CreateBuilder(args)
|
||||
.ConfigureServices()
|
||||
.ConfigureRequestPipeline()
|
||||
.Run();
|
||||
15
src/Vegasco.Server.Api/Properties/launchSettings.json
Normal file
15
src/Vegasco.Server.Api/Properties/launchSettings.json
Normal file
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"profiles": {
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7098;http://localhost:5076"
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json"
|
||||
}
|
||||
10
src/Vegasco.Server.Api/Users/User.cs
Normal file
10
src/Vegasco.Server.Api/Users/User.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Users;
|
||||
|
||||
public class User
|
||||
{
|
||||
public string Id { get; set; } = "";
|
||||
|
||||
public virtual IList<Car> Cars { get; set; } = [];
|
||||
}
|
||||
12
src/Vegasco.Server.Api/Users/UserTableConfiguration.cs
Normal file
12
src/Vegasco.Server.Api/Users/UserTableConfiguration.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Vegasco.Server.Api.Users;
|
||||
|
||||
public class UserTableConfiguration : IEntityTypeConfiguration<User>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<User> builder)
|
||||
{
|
||||
builder.HasKey(user => user.Id);
|
||||
}
|
||||
}
|
||||
45
src/Vegasco.Server.Api/Vegasco.Server.Api.csproj
Normal file
45
src/Vegasco.Server.Api/Vegasco.Server.Api.csproj
Normal file
@@ -0,0 +1,45 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>4bf893d3-0c16-41ec-8b46-2768d841215d</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>..\..</DockerfileContext>
|
||||
<RootNamespace>Vegasco.Server.Api</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.0" />
|
||||
<PackageReference Include="Aspire.Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.3.0" />
|
||||
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="12.0.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.5">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
|
||||
<PackageReference Include="StronglyTypedId" Version="1.0.0-beta08" PrivateAssets="all" ExcludeAssets="runtime" />
|
||||
<PackageReference Include="StronglyTypedId.Templates" Version="1.0.0-beta08" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Vegasco.Server.AppHost.Shared\Vegasco.Server.AppHost.Shared.csproj" />
|
||||
<ProjectReference Include="..\Vegasco.Server.ServiceDefaults\Vegasco.Server.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.115" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
src/Vegasco.Server.Api/appsettings.Development.json
Normal file
8
src/Vegasco.Server.Api/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
10
src/Vegasco.Server.Api/appsettings.json
Normal file
10
src/Vegasco.Server.Api/appsettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Warning",
|
||||
"Vegasco": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
16
src/Vegasco.Server.AppHost.Shared/Constants.cs
Normal file
16
src/Vegasco.Server.AppHost.Shared/Constants.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace Vegasco.Server.AppHost.Shared;
|
||||
|
||||
public static class Constants
|
||||
{
|
||||
public static class Projects
|
||||
{
|
||||
public const string Api = "Vegasco-Server-Api";
|
||||
}
|
||||
|
||||
public static class Database
|
||||
{
|
||||
public const string ServiceName = "postgres";
|
||||
|
||||
public const string Name = "vegasco-database";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
15
src/Vegasco.Server.AppHost/Program.cs
Normal file
15
src/Vegasco.Server.AppHost/Program.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Vegasco.Server.AppHost.Shared;
|
||||
|
||||
IDistributedApplicationBuilder builder = DistributedApplication.CreateBuilder(args);
|
||||
|
||||
IResourceBuilder<PostgresDatabaseResource> postgres = builder.AddPostgres(Constants.Database.ServiceName)
|
||||
.WithLifetime(ContainerLifetime.Persistent)
|
||||
.WithDataVolume()
|
||||
.AddDatabase(Constants.Database.Name);
|
||||
|
||||
builder
|
||||
.AddProject<Projects.Vegasco_Server_Api>(Constants.Projects.Api)
|
||||
.WithReference(postgres)
|
||||
.WaitFor(postgres);
|
||||
|
||||
builder.Build().Run();
|
||||
29
src/Vegasco.Server.AppHost/Properties/launchSettings.json
Normal file
29
src/Vegasco.Server.AppHost/Properties/launchSettings.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"applicationUrl": "https://localhost:17055;http://localhost:15102",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"DOTNET_ENVIRONMENT": "Development",
|
||||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21122",
|
||||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22235"
|
||||
}
|
||||
},
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"applicationUrl": "http://localhost:15102",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development",
|
||||
"DOTNET_ENVIRONMENT": "Development",
|
||||
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19222",
|
||||
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20257"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj
Normal file
27
src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj
Normal file
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" />
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsAspireHost>true</IsAspireHost>
|
||||
<UserSecretsId>bb714834-9872-4af6-b154-0b98b14fcca2</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.3.0" />
|
||||
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="9.3.0" />
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Vegasco.Server.AppHost.Shared\Vegasco.Server.AppHost.Shared.csproj" IsAspireProjectResource="false" />
|
||||
<ProjectReference Include="..\Vegasco.Server.Api\Vegasco.Server.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
8
src/Vegasco.Server.AppHost/appsettings.Development.json
Normal file
8
src/Vegasco.Server.AppHost/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
src/Vegasco.Server.AppHost/appsettings.json
Normal file
9
src/Vegasco.Server.AppHost/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Aspire.Hosting.Dcp": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
119
src/Vegasco.Server.ServiceDefaults/Extensions.cs
Normal file
119
src/Vegasco.Server.ServiceDefaults/Extensions.cs
Normal file
@@ -0,0 +1,119 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenTelemetry;
|
||||
using OpenTelemetry.Metrics;
|
||||
using OpenTelemetry.Trace;
|
||||
|
||||
namespace Vegasco.Server.ServiceDefaults;
|
||||
|
||||
// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry.
|
||||
// This project should be referenced by each service project in your solution.
|
||||
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
|
||||
public static class Extensions
|
||||
{
|
||||
public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
||||
{
|
||||
builder.ConfigureOpenTelemetry();
|
||||
|
||||
builder.AddDefaultHealthChecks();
|
||||
|
||||
builder.Services.AddServiceDiscovery();
|
||||
|
||||
builder.Services.ConfigureHttpClientDefaults(http =>
|
||||
{
|
||||
// Turn on resilience by default
|
||||
http.AddStandardResilienceHandler();
|
||||
|
||||
// Turn on service discovery by default
|
||||
http.AddServiceDiscovery();
|
||||
});
|
||||
|
||||
// Uncomment the following to restrict the allowed schemes for service discovery.
|
||||
// builder.Services.Configure<ServiceDiscoveryOptions>(options =>
|
||||
// {
|
||||
// options.AllowedSchemes = ["https"];
|
||||
// });
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
||||
{
|
||||
builder.Logging.AddOpenTelemetry(logging =>
|
||||
{
|
||||
logging.IncludeFormattedMessage = true;
|
||||
logging.IncludeScopes = true;
|
||||
});
|
||||
|
||||
builder.Services.AddOpenTelemetry()
|
||||
.WithMetrics(metrics =>
|
||||
{
|
||||
metrics.AddAspNetCoreInstrumentation()
|
||||
.AddHttpClientInstrumentation()
|
||||
.AddRuntimeInstrumentation();
|
||||
})
|
||||
.WithTracing(tracing =>
|
||||
{
|
||||
tracing.AddSource(builder.Environment.ApplicationName)
|
||||
.AddAspNetCoreInstrumentation()
|
||||
// Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
|
||||
//.AddGrpcClientInstrumentation()
|
||||
.AddHttpClientInstrumentation();
|
||||
});
|
||||
|
||||
builder.AddOpenTelemetryExporters();
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
||||
{
|
||||
bool useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
|
||||
|
||||
if (useOtlpExporter)
|
||||
{
|
||||
builder.Services.AddOpenTelemetry().UseOtlpExporter();
|
||||
}
|
||||
|
||||
// Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package)
|
||||
//if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
|
||||
//{
|
||||
// builder.Services.AddOpenTelemetry()
|
||||
// .UseAzureMonitor();
|
||||
//}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static TBuilder AddDefaultHealthChecks<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
|
||||
{
|
||||
builder.Services.AddHealthChecks()
|
||||
// Add a default liveness check to ensure app is responsive
|
||||
.AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
public static WebApplication MapDefaultEndpoints(this WebApplication app)
|
||||
{
|
||||
// Adding health checks endpoints to applications in non-development environments has security implications.
|
||||
// See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
// All health checks must pass for app to be considered ready to accept traffic after starting
|
||||
app.MapHealthChecks("/health");
|
||||
|
||||
// Only health checks tagged with the "live" tag must pass for app to be considered alive
|
||||
app.MapHealthChecks("/alive", new HealthCheckOptions
|
||||
{
|
||||
Predicate = r => r.Tags.Contains("live")
|
||||
});
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsAspireSharedProject>true</IsAspireSharedProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<FrameworkReference Include="Microsoft.AspNetCore.App" />
|
||||
|
||||
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.5.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="9.3.0" />
|
||||
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
|
||||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
|
||||
<PackageReference Update="Nerdbank.GitVersioning">
|
||||
<Version>3.7.115</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
19
tests/Vegasco.Server.Api.Tests.Integration/CarFaker.cs
Normal file
19
tests/Vegasco.Server.Api.Tests.Integration/CarFaker.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Bogus;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
internal class CarFaker
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
internal CreateCar.Request CreateCarRequest()
|
||||
{
|
||||
return new CreateCar.Request(_faker.Vehicle.Model());
|
||||
}
|
||||
|
||||
internal UpdateCar.Request UpdateCarRequest()
|
||||
{
|
||||
return new UpdateCar.Request(_faker.Vehicle.Model());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class CreateCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public CreateCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateCar_ShouldCreateCar_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
var createdCar = await response.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
createdCar.Should().BeEquivalentTo(createCarRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Cars.Should().ContainEquivalentOf(createdCar, o => o.Excluding(x => x!.Id))
|
||||
.Which.Id.Value.Should().Be(createdCar!.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateCar_ShouldReturnValidationProblems_WhenRequestIsNotValid()
|
||||
{
|
||||
// Arrange
|
||||
var createCarRequest = new CreateCar.Request("");
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(CreateCar.Request.Name), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Cars.Should().NotContainEquivalentOf(createCarRequest, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class DeleteCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public DeleteCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteCar_ShouldReturnNotFound_WhenCarDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/cars/{randomCarId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteCar_ShouldDeleteCar_WhenCarExists()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/cars/{createdCar!.Id}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
_dbContext.Cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
using FluentAssertions;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public GetCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCar_ShouldReturnNotFound_WhenCarDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/cars/{randomCarId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCar_ShouldReturnCar_WhenCarExists()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/cars/{createdCar!.Id}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var car = await response.Content.ReadFromJsonAsync<GetCar.Response>();
|
||||
car.Should().BeEquivalentTo(createdCar);
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using FluentAssertions;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetCarsTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public GetCarsTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCars_ShouldReturnEmptyList_WhenNoEntriesExist()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
|
||||
apiResponse!.Cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetCars_ShouldReturnEntries_WhenEntriesExist()
|
||||
{
|
||||
// Arrange
|
||||
List<CreateCar.Response> createdCars = [];
|
||||
|
||||
const int numberOfCars = 5;
|
||||
for (var i = 0; i < numberOfCars; i++)
|
||||
{
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
createdCars.Add(createdCar!);
|
||||
}
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/cars");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<GetCars.ApiResponse>();
|
||||
apiResponse!.Cars.Should().BeEquivalentTo(createdCars);
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Cars;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class UpdateCarTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
|
||||
public UpdateCarTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldUpdateCar_WhenCarExistsAndRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
UpdateCar.Request updateCarRequest = _carFaker.UpdateCarRequest();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var updatedCar = await response.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
updatedCar!.Id.Should().Be(createdCar.Id);
|
||||
updatedCar.Should().BeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Cars.Should().ContainEquivalentOf(updatedCar, o =>
|
||||
o.ExcludingMissingMembers()
|
||||
.Excluding(x => x.Id))
|
||||
.Which.Id.Value.Should().Be(updatedCar.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldReturnValidationProblems_WhenRequestIsNotValid()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request createCarRequest = _carFaker.CreateCarRequest();
|
||||
HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCar = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
|
||||
var updateCarRequest = new UpdateCar.Request("");
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{createdCar!.Id}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(CreateCar.Request.Name), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Cars.Should().ContainSingle(x => x.Id.Value == createdCar.Id)
|
||||
.Which
|
||||
.Should().NotBeEquivalentTo(updateCarRequest, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateCar_ShouldReturnNotFound_WhenNoCarWithIdExists()
|
||||
{
|
||||
// Arrange
|
||||
UpdateCar.Request updateCarRequest = _carFaker.UpdateCarRequest();
|
||||
var randomCarId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/cars/{randomCarId}", updateCarRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
|
||||
_dbContext.Cars.Should().BeEmpty();
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using Bogus;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
internal class ConsumptionFaker
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
internal CreateConsumption.Request CreateConsumptionRequest(Guid carId)
|
||||
{
|
||||
return new CreateConsumption.Request(
|
||||
_faker.Date.RecentOffset(),
|
||||
_faker.Random.Int(1, 1_000),
|
||||
_faker.Random.Int(20, 70),
|
||||
_faker.Random.Bool(),
|
||||
carId);
|
||||
}
|
||||
|
||||
internal UpdateConsumption.Request UpdateConsumptionRequest()
|
||||
{
|
||||
CreateConsumption.Request createRequest = CreateConsumptionRequest(default);
|
||||
return new UpdateConsumption.Request(
|
||||
createRequest.DateTime,
|
||||
createRequest.Distance,
|
||||
createRequest.Amount,
|
||||
createRequest.IgnoreInCalculation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class CreateConsumptionTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public CreateConsumptionTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateConsumption_ShouldCreateConsumption_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
||||
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.Created);
|
||||
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
||||
createdConsumption.Should().BeEquivalentTo(createConsumptionRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Consumptions.Should().HaveCount(1)
|
||||
.And.ContainEquivalentOf(createdConsumption, o =>
|
||||
o.ExcludingMissingMembers()
|
||||
.Excluding(x => x!.Id)
|
||||
.Excluding(x => x!.CarId));
|
||||
|
||||
Consumption singleConsumption = _dbContext.Consumptions.Single();
|
||||
singleConsumption.Id.Value.Should().Be(createdConsumption!.Id);
|
||||
singleConsumption.CarId.Value.Should().Be(createdConsumption.CarId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateConsumption_ShouldReturnValidationProblems_WhenRequestIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(Guid.Empty);
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(createConsumptionRequest.CarId), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Consumptions.Should().NotContainEquivalentOf(createConsumptionRequest, o => o.ExcludingMissingMembers());
|
||||
}
|
||||
|
||||
private async Task<CreateCar.Response> CreateCarAsync()
|
||||
{
|
||||
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
||||
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
return createdCarResponse!;
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
FluentAssertionConfiguration.SetupGlobalConfig();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class DeleteConsumptionTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public DeleteConsumptionTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteConsumption_ShouldDeleteConsumption_WhenConsumptionExists()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/consumptions/{createdConsumption.Id}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NoContent);
|
||||
_dbContext.Consumptions.Should().NotContain(x => x.Id.Value == createdConsumption.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteConsumption_ShouldReturnNotFound_WhenConsumptionDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var consumptionId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.DeleteAsync($"v1/consumptions/{consumptionId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
||||
{
|
||||
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
||||
return createdConsumption!;
|
||||
}
|
||||
|
||||
private async Task<CreateCar.Response> CreateCarAsync()
|
||||
{
|
||||
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
||||
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
return createdCarResponse!;
|
||||
}
|
||||
|
||||
public Task InitializeAsync() => Task.CompletedTask;
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetConsumptionTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public GetConsumptionTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumption_ShouldReturnConsumption_WhenConsumptionExist()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/consumptions/{createdConsumption.Id}");
|
||||
|
||||
// Assert
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var consumption = await response.Content.ReadFromJsonAsync<GetConsumption.Response>();
|
||||
consumption.Should().BeEquivalentTo(createdConsumption);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptions_ShouldReturnNotFound_WhenConsumptionDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
var consumptionId = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync($"v1/consumptions{consumptionId}");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
}
|
||||
|
||||
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
||||
{
|
||||
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
||||
return createdConsumption!;
|
||||
}
|
||||
|
||||
private async Task<CreateCar.Response> CreateCarAsync()
|
||||
{
|
||||
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
||||
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
return createdCarResponse!;
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
FluentAssertionConfiguration.SetupGlobalConfig();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetConsumptionsTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public GetConsumptionsTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptions_ShouldReturnConsumptions_WhenConsumptionsExist()
|
||||
{
|
||||
// Arrange
|
||||
List<CreateConsumption.Response> createdConsumptions = [];
|
||||
const int numberOfConsumptions = 3;
|
||||
for (var i = 0; i < numberOfConsumptions; i++)
|
||||
{
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
createdConsumptions.Add(createdConsumption);
|
||||
}
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/consumptions");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<GetConsumptions.ApiResponse>();
|
||||
apiResponse!.Consumptions.Should().BeEquivalentTo(createdConsumptions);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetConsumptions_ShouldReturnEmptyList_WhenNoConsumptionsExist()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("v1/consumptions");
|
||||
|
||||
// Assert
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var apiResponse = await response.Content.ReadFromJsonAsync<GetConsumptions.ApiResponse>();
|
||||
apiResponse!.Consumptions.Should().BeEmpty();
|
||||
}
|
||||
|
||||
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
||||
{
|
||||
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
||||
return createdConsumption!;
|
||||
}
|
||||
|
||||
private async Task<CreateCar.Response> CreateCarAsync()
|
||||
{
|
||||
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
||||
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
return createdCarResponse!;
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
FluentAssertionConfiguration.SetupGlobalConfig();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
using Vegasco.Server.Api.Persistence;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Consumptions;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class UpdateConsumptionTests : IAsyncLifetime
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
private readonly IServiceScope _scope;
|
||||
private readonly ApplicationDbContext _dbContext;
|
||||
|
||||
private readonly CarFaker _carFaker = new();
|
||||
private readonly ConsumptionFaker _consumptionFaker = new();
|
||||
|
||||
public UpdateConsumptionTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
_scope = _factory.Services.CreateScope();
|
||||
_dbContext = _scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateConsumption_ShouldCreateConsumption_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{createdConsumption.Id}", updateConsumptionRequest);
|
||||
|
||||
// Assert
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
response.StatusCode.Should().Be(HttpStatusCode.OK);
|
||||
var updatedConsumption = await response.Content.ReadFromJsonAsync<UpdateConsumption.Response>();
|
||||
updatedConsumption.Should().BeEquivalentTo(updateConsumptionRequest, o => o.ExcludingMissingMembers());
|
||||
|
||||
_dbContext.Consumptions.Should().HaveCount(1)
|
||||
.And.ContainEquivalentOf(updatedConsumption, o =>
|
||||
o.ExcludingMissingMembers()
|
||||
.Excluding(x => x!.Id)
|
||||
.Excluding(x => x!.CarId));
|
||||
|
||||
Consumption singleConsumption = _dbContext.Consumptions.Single();
|
||||
singleConsumption.Id.Value.Should().Be(updatedConsumption!.Id);
|
||||
singleConsumption.CarId.Value.Should().Be(updatedConsumption.CarId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateConsumption_ShouldReturnValidationProblems_WhenRequestIsInvalid()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest() with { Distance = -42 };
|
||||
var randomGuid = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{randomGuid}", updateConsumptionRequest);
|
||||
|
||||
// Assert
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
|
||||
var validationProblemDetails = await response.Content.ReadFromJsonAsync<ValidationProblemDetails>();
|
||||
validationProblemDetails!.Errors.Keys.Should().Contain(x =>
|
||||
x.Equals(nameof(updateConsumptionRequest.Distance), StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
_dbContext.Consumptions.Should().NotContainEquivalentOf(updateConsumptionRequest);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateConsumption_ShouldReturnNotFound_WhenConsumptionDoesNotExist()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Response createdConsumption = await CreateConsumptionAsync();
|
||||
UpdateConsumption.Request updateConsumptionRequest = _consumptionFaker.UpdateConsumptionRequest();
|
||||
var randomGuid = Guid.NewGuid();
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PutAsJsonAsync($"v1/consumptions/{randomGuid}", updateConsumptionRequest);
|
||||
|
||||
// Assert
|
||||
string content = await response.Content.ReadAsStringAsync();
|
||||
response.StatusCode.Should().Be(HttpStatusCode.NotFound);
|
||||
|
||||
_dbContext.Consumptions.Should().NotContainEquivalentOf(updateConsumptionRequest);
|
||||
}
|
||||
|
||||
private async Task<CreateConsumption.Response> CreateConsumptionAsync()
|
||||
{
|
||||
CreateCar.Response createdCarResponse = await CreateCarAsync();
|
||||
CreateConsumption.Request createConsumptionRequest = _consumptionFaker.CreateConsumptionRequest(createdCarResponse.Id);
|
||||
using HttpResponseMessage response = await _factory.HttpClient.PostAsJsonAsync("v1/consumptions", createConsumptionRequest);
|
||||
response.EnsureSuccessStatusCode();
|
||||
var createdConsumption = await response.Content.ReadFromJsonAsync<CreateConsumption.Response>();
|
||||
return createdConsumption!;
|
||||
}
|
||||
|
||||
private async Task<CreateCar.Response> CreateCarAsync()
|
||||
{
|
||||
CreateCar.Request createCarRequest = new CarFaker().CreateCarRequest();
|
||||
using HttpResponseMessage createCarResponse = await _factory.HttpClient.PostAsJsonAsync("v1/cars", createCarRequest);
|
||||
createCarResponse.EnsureSuccessStatusCode();
|
||||
var createdCarResponse = await createCarResponse.Content.ReadFromJsonAsync<CreateCar.Response>();
|
||||
return createdCarResponse!;
|
||||
}
|
||||
|
||||
public Task InitializeAsync()
|
||||
{
|
||||
FluentAssertionConfiguration.SetupGlobalConfig();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
_scope.Dispose();
|
||||
await _dbContext.DisposeAsync();
|
||||
await _factory.ResetDatabaseAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using FluentAssertions;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
internal static class FluentAssertionConfiguration
|
||||
{
|
||||
private const int DateTimeComparisonPrecision = 100;
|
||||
|
||||
internal static void SetupGlobalConfig()
|
||||
{
|
||||
AssertionOptions.AssertEquivalencyUsing(options => options
|
||||
.Using<DateTime>(ctx => ctx.Subject.ToUniversalTime().Should().BeCloseTo(ctx.Expectation.ToUniversalTime(), TimeSpan.FromMilliseconds(DateTimeComparisonPrecision)))
|
||||
.WhenTypeIs<DateTime>());
|
||||
|
||||
AssertionOptions.AssertEquivalencyUsing(options => options
|
||||
.Using<DateTimeOffset>(ctx => ctx.Subject.ToUniversalTime().Should().BeCloseTo(ctx.Expectation.ToUniversalTime(), TimeSpan.FromMilliseconds(DateTimeComparisonPrecision)))
|
||||
.WhenTypeIs<DateTimeOffset>());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System.Net.Http.Json;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions.Extensions;
|
||||
using Vegasco.Server.Api.Info;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration.Info;
|
||||
|
||||
[Collection(SharedTestCollection.Name)]
|
||||
public class GetServerInfoTests
|
||||
{
|
||||
private readonly WebAppFactory _factory;
|
||||
|
||||
public GetServerInfoTests(WebAppFactory factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetServerInfo_ShouldReturnServerInfo_WhenCalled()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
using HttpResponseMessage response = await _factory.HttpClient.GetAsync("/v1/info/server");
|
||||
|
||||
// Assert
|
||||
response.IsSuccessStatusCode.Should().BeTrue();
|
||||
var serverInfo = await response.Content.ReadFromJsonAsync<GetServerInfo.Response>();
|
||||
serverInfo!.Environment.Should().NotBeEmpty();
|
||||
serverInfo.CommitDate.Should().BeAfter(23.August(2024))
|
||||
.And.NotBeAfter(DateTime.Now);
|
||||
serverInfo.CommitId.Should().MatchRegex(@"[0-9a-f]{40}");
|
||||
serverInfo.FullVersion.Should().MatchRegex(@"\d\.\d\.\d(-[0-9a-zA-Z]+)?(\+g?[0-9a-f]{10})?");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Npgsql;
|
||||
using Respawn;
|
||||
using System.Data.Common;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
internal sealed class PostgresRespawner : IDisposable
|
||||
{
|
||||
private readonly DbConnection _connection;
|
||||
private readonly Respawner _respawner;
|
||||
|
||||
private PostgresRespawner(Respawner respawner, DbConnection connection)
|
||||
{
|
||||
_respawner = respawner;
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
public static async Task<PostgresRespawner> CreateAsync(string connectionString)
|
||||
{
|
||||
DbConnection connection = new NpgsqlConnection(connectionString);
|
||||
await connection.OpenAsync();
|
||||
|
||||
var respawner = await Respawner.CreateAsync(connection,
|
||||
new RespawnerOptions
|
||||
{
|
||||
SchemasToInclude = ["public"],
|
||||
DbAdapter = DbAdapter.Postgres
|
||||
});
|
||||
return new PostgresRespawner(respawner, connection);
|
||||
}
|
||||
|
||||
public async Task ResetDatabaseAsync()
|
||||
{
|
||||
await _respawner.ResetAsync(_connection);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
[CollectionDefinition(Name)]
|
||||
public class SharedTestCollection : ICollectionFixture<WebAppFactory>
|
||||
{
|
||||
public const string Name = nameof(SharedTestCollection);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Authorization.Policy;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
public sealed class TestUserAlwaysAuthorizedPolicyEvaluator : IPolicyEvaluator
|
||||
{
|
||||
public const string Username = "Test user";
|
||||
public static readonly string UserId = Guid.NewGuid().ToString();
|
||||
|
||||
public Task<AuthenticateResult> AuthenticateAsync(AuthorizationPolicy policy, HttpContext context)
|
||||
{
|
||||
Claim[] claims =
|
||||
[
|
||||
|
||||
new Claim(ClaimTypes.Name, Username),
|
||||
new Claim("name", Username),
|
||||
new Claim(ClaimTypes.NameIdentifier, UserId),
|
||||
new Claim("aud", "https://localhost")
|
||||
];
|
||||
|
||||
ClaimsIdentity identity = new(claims, JwtBearerDefaults.AuthenticationScheme);
|
||||
ClaimsPrincipal principal = new(identity);
|
||||
AuthenticationTicket ticket = new(principal, JwtBearerDefaults.AuthenticationScheme);
|
||||
AuthenticateResult result = AuthenticateResult.Success(ticket);
|
||||
return Task.FromResult(result); ;
|
||||
}
|
||||
|
||||
public Task<PolicyAuthorizationResult> AuthorizeAsync(AuthorizationPolicy policy, AuthenticateResult authenticationResult, HttpContext context,
|
||||
object? resource)
|
||||
{
|
||||
return Task.FromResult(PolicyAuthorizationResult.Success());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Azure.Identity" Version="1.14.0" />
|
||||
<PackageReference Include="Bogus" Version="35.6.3" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="[7.2.0,8.0.0)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="Respawn" Version="6.2.1" />
|
||||
<PackageReference Include="System.Formats.Asn1" Version="9.0.5" />
|
||||
<PackageReference Include="Testcontainers.PostgreSql" Version="4.5.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Vegasco.Server.AppHost.Shared\Vegasco.Server.AppHost.Shared.csproj" />
|
||||
<ProjectReference Include="..\..\src\Vegasco.Server.Api\Vegasco.Server.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.115" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
72
tests/Vegasco.Server.Api.Tests.Integration/WebAppFactory.cs
Normal file
72
tests/Vegasco.Server.Api.Tests.Integration/WebAppFactory.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using DotNet.Testcontainers.Images;
|
||||
using Microsoft.AspNetCore.Authorization.Policy;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Testcontainers.PostgreSql;
|
||||
using Vegasco.Server.Api.Common;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Integration;
|
||||
|
||||
public sealed class WebAppFactory : WebApplicationFactory<IApiMarker>, IAsyncLifetime
|
||||
{
|
||||
private readonly PostgreSqlContainer _database = new PostgreSqlBuilder()
|
||||
.WithImage(DockerImage)
|
||||
.WithImagePullPolicy(PullPolicy.Always)
|
||||
.Build();
|
||||
|
||||
private const string DockerImage = "postgres:alpine";
|
||||
|
||||
public HttpClient HttpClient => CreateClient();
|
||||
|
||||
private PostgresRespawner? _postgresRespawner;
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _database.StartAsync();
|
||||
|
||||
// Force application startup (i.e. initialization and validation)
|
||||
_ = CreateClient();
|
||||
|
||||
_postgresRespawner = await PostgresRespawner.CreateAsync(_database.GetConnectionString());
|
||||
}
|
||||
|
||||
protected override void ConfigureWebHost(IWebHostBuilder builder)
|
||||
{
|
||||
IEnumerable<KeyValuePair<string, string?>> customConfig =
|
||||
[
|
||||
new KeyValuePair<string, string?>($"ConnectionStrings:{AppHost.Shared.Constants.Database.Name}", _database.GetConnectionString()),
|
||||
new KeyValuePair<string, string?>("JWT:ValidAudience", "https://localhost"),
|
||||
new KeyValuePair<string, string?>("JWT:MetadataUrl", "https://localhost"),
|
||||
new KeyValuePair<string, string?>("JWT:NameClaimType", null),
|
||||
];
|
||||
|
||||
builder.UseConfiguration(new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(customConfig)
|
||||
.Build());
|
||||
|
||||
builder.ConfigureServices(services =>
|
||||
{
|
||||
});
|
||||
|
||||
builder.ConfigureTestServices(services =>
|
||||
{
|
||||
services.RemoveAll<IPolicyEvaluator>();
|
||||
services.AddSingleton<IPolicyEvaluator, TestUserAlwaysAuthorizedPolicyEvaluator>();
|
||||
});
|
||||
}
|
||||
|
||||
public async Task ResetDatabaseAsync()
|
||||
{
|
||||
await _postgresRespawner!.ResetDatabaseAsync();
|
||||
}
|
||||
|
||||
async Task IAsyncLifetime.DisposeAsync()
|
||||
{
|
||||
_postgresRespawner!.Dispose();
|
||||
await _database.DisposeAsync();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
using FluentAssertions;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using System.Security.Claims;
|
||||
using Vegasco.Server.Api.Authentication;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Unit.Authentication;
|
||||
public sealed class UserAccessorTests
|
||||
{
|
||||
private readonly UserAccessor _sut;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
private static readonly string _nameClaimType = "name";
|
||||
private readonly JwtOptions _jwtOptions = new()
|
||||
{
|
||||
NameClaimType = _nameClaimType
|
||||
};
|
||||
|
||||
private readonly IOptions<JwtOptions> _options = Substitute.For<IOptions<JwtOptions>>();
|
||||
|
||||
private static readonly string _defaultUsername = "username";
|
||||
private static readonly string _defaultId = "id";
|
||||
private readonly ClaimsPrincipal _defaultUser = new(new ClaimsIdentity(
|
||||
[
|
||||
new Claim(_nameClaimType, _defaultUsername),
|
||||
new Claim(ClaimTypes.NameIdentifier, _defaultId)
|
||||
]));
|
||||
|
||||
public UserAccessorTests()
|
||||
{
|
||||
_httpContextAccessor = new HttpContextAccessor
|
||||
{
|
||||
HttpContext = new DefaultHttpContext()
|
||||
{
|
||||
User = _defaultUser
|
||||
}
|
||||
};
|
||||
|
||||
_options.Value.Returns(_jwtOptions);
|
||||
|
||||
_sut = new UserAccessor(_httpContextAccessor, _options);
|
||||
}
|
||||
|
||||
#region GetUsername
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldReturnUsername_WhenOptionsNameClaimTypeMatches()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
string result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldReturnUsername_WhenNameClaimTypeIsNotSetAndUsernameIsInUriNameClaimType()
|
||||
{
|
||||
// Arrange
|
||||
_jwtOptions.NameClaimType = null;
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal(new ClaimsIdentity(
|
||||
[
|
||||
new Claim(ClaimTypes.Name, _defaultUsername)
|
||||
]));
|
||||
|
||||
// Act
|
||||
string result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldCacheUsername_WhenFirstCalled()
|
||||
{
|
||||
// Arrange
|
||||
_ = _sut.GetUsername();
|
||||
_options.ClearReceivedCalls();
|
||||
|
||||
// Act
|
||||
string result = _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultUsername);
|
||||
_ = _options.Received(0).Value;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldThrowInvalidOperationException_WhenHttpContextIsNull()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
|
||||
// Act
|
||||
Func<string> action = () => _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be("No HttpContext available.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUsername_ShouldThrowInvalidOperationException_WhenNameClaimIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal();
|
||||
|
||||
// Act
|
||||
Func<string> action = () => _sut.GetUsername();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be($"No claim of type '{_nameClaimType}' found on the current user.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region GetUserId
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldReturnUserId_WhenUserIdClaimExists()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
string result = _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultId);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldCacheUserId_WhenFirstCalled()
|
||||
{
|
||||
// Arrange
|
||||
_ = _sut.GetUserId();
|
||||
_options.ClearReceivedCalls();
|
||||
|
||||
// Act
|
||||
string result = _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
result.Should().Be(_defaultId);
|
||||
_ = _options.Received(0).Value;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldThrowInvalidOperationException_WhenHttpContextIsNull()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext = null;
|
||||
|
||||
// Act
|
||||
Func<string> action = () => _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be("No HttpContext available.");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetUserId_ShouldThrowInvalidOperationException_WhenIdClaimIsNotFound()
|
||||
{
|
||||
// Arrange
|
||||
_httpContextAccessor.HttpContext!.User = new ClaimsPrincipal();
|
||||
|
||||
// Act
|
||||
Func<string> action = () => _sut.GetUserId();
|
||||
|
||||
// Assert
|
||||
action.Should().ThrowExactly<InvalidOperationException>()
|
||||
.Which.Message.Should().Be($"No claim of type '{ClaimTypes.NameIdentifier}' found on the current user.");
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using FluentAssertions;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Unit.Cars;
|
||||
|
||||
public sealed class CreateCarRequestValidatorTests
|
||||
{
|
||||
private readonly CreateCar.Validator _sut = new();
|
||||
|
||||
private readonly CreateCar.Request _validRequest = new("Ford Focus");
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(50)]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenNameIsJustWithinTheLimits(int nameLength)
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request request = _validRequest with { Name = new string('s', nameLength) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
CreateCar.Request request = _validRequest with { Name = "" };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsTooLong()
|
||||
{
|
||||
// Arrange
|
||||
const int nameMaxLength = 50;
|
||||
CreateCar.Request request = _validRequest with { Name = new string('s', nameMaxLength + 1) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(CreateCar.Request.Name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using FluentAssertions;
|
||||
using FluentValidation.Results;
|
||||
using Vegasco.Server.Api.Cars;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Unit.Cars;
|
||||
|
||||
public sealed class UpdateCarRequestValidatorTests
|
||||
{
|
||||
private readonly UpdateCar.Validator _sut = new();
|
||||
|
||||
private readonly UpdateCar.Request _validRequest = new("Ford Focus");
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(50)]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenNameIsJustWithinTheLimits(int nameLength)
|
||||
{
|
||||
// Arrange
|
||||
UpdateCar.Request request = _validRequest with { Name = new string('s', nameLength) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
UpdateCar.Request request = _validRequest with { Name = "" };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(UpdateCar.Request.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldNotBeValid_WhenNameIsTooLong()
|
||||
{
|
||||
// Arrange
|
||||
const int nameMaxLength = 50;
|
||||
UpdateCar.Request request = _validRequest with { Name = new string('s', nameMaxLength + 1) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle()
|
||||
.Which
|
||||
.PropertyName.Should().Be(nameof(UpdateCar.Request.Name));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
using FluentAssertions;
|
||||
using FluentValidation.Results;
|
||||
using NSubstitute;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Unit.Consumptions;
|
||||
public class CreateConsumptionRequestValidatorTests
|
||||
{
|
||||
private readonly CreateConsumption.Validator _sut;
|
||||
private readonly TimeProvider _timeProvider = Substitute.For<TimeProvider>();
|
||||
|
||||
private readonly DateTimeOffset _utcNow = new DateTimeOffset(2024, 8, 18, 13, 2, 53, TimeSpan.Zero);
|
||||
|
||||
private readonly CreateConsumption.Request _validRequest;
|
||||
|
||||
public CreateConsumptionRequestValidatorTests()
|
||||
{
|
||||
_timeProvider.GetUtcNow().Returns(_utcNow);
|
||||
_sut = new CreateConsumption.Validator(_timeProvider);
|
||||
|
||||
_validRequest = new CreateConsumption.Request(
|
||||
_utcNow.AddDays(-1),
|
||||
1,
|
||||
1,
|
||||
false,
|
||||
Guid.NewGuid());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenDateTimeIsGreaterThanUtcNow()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Request request = _validRequest with { DateTime = _utcNow.AddDays(1) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(CreateConsumption.Request.DateTime));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenDistanceIsLessThanOrEqualToZero(double distance)
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Request request = _validRequest with { Distance = distance };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(CreateConsumption.Request.Distance));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenAmountIsLessThanOrEqualToZero(double amount)
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Request request = _validRequest with { Amount = amount };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(CreateConsumption.Request.Amount));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenCarIdIsEmpty()
|
||||
{
|
||||
// Arrange
|
||||
CreateConsumption.Request request = _validRequest with { CarId = Guid.Empty };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(CreateConsumption.Request.CarId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
using FluentAssertions;
|
||||
using FluentValidation.Results;
|
||||
using NSubstitute;
|
||||
using Vegasco.Server.Api.Consumptions;
|
||||
|
||||
namespace Vegasco.Server.Api.Tests.Unit.Consumptions;
|
||||
|
||||
public class UpdateConsumptionRequestValidatorTests
|
||||
{
|
||||
private readonly UpdateConsumption.Validator _sut;
|
||||
private readonly TimeProvider _timeProvider = Substitute.For<TimeProvider>();
|
||||
|
||||
private readonly DateTimeOffset _utcNow = new DateTimeOffset(2024, 8, 18, 13, 2, 53, TimeSpan.Zero);
|
||||
|
||||
private readonly UpdateConsumption.Request _validRequest;
|
||||
|
||||
public UpdateConsumptionRequestValidatorTests()
|
||||
{
|
||||
_timeProvider.GetUtcNow().Returns(_utcNow);
|
||||
_sut = new UpdateConsumption.Validator(_timeProvider);
|
||||
|
||||
_validRequest = new UpdateConsumption.Request(
|
||||
_utcNow.AddDays(-1),
|
||||
1,
|
||||
1,
|
||||
false);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeValid_WhenRequestIsValid()
|
||||
{
|
||||
// Arrange
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(_validRequest);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenDateTimeIsGreaterThanUtcNow()
|
||||
{
|
||||
// Arrange
|
||||
UpdateConsumption.Request request = _validRequest with { DateTime = _utcNow.AddDays(1) };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(UpdateConsumption.Request.DateTime));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenDistanceIsLessThanOrEqualToZero(double distance)
|
||||
{
|
||||
// Arrange
|
||||
UpdateConsumption.Request request = _validRequest with { Distance = distance };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(UpdateConsumption.Request.Distance));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(0)]
|
||||
[InlineData(-1)]
|
||||
public async Task ValidateAsync_ShouldBeInvalid_WhenAmountIsLessThanOrEqualToZero(double amount)
|
||||
{
|
||||
// Arrange
|
||||
UpdateConsumption.Request request = _validRequest with { Amount = amount };
|
||||
|
||||
// Act
|
||||
ValidationResult? result = await _sut.ValidateAsync(request);
|
||||
|
||||
// Assert
|
||||
result.IsValid.Should().BeFalse();
|
||||
result.Errors.Should().ContainSingle(x => x.PropertyName == nameof(UpdateConsumption.Request.Amount));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="8.3.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="9.0.5" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.3" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Vegasco.Server.Api\Vegasco.Server.Api.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Update="Nerdbank.GitVersioning" Version="3.7.115" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
3
vegasco-server.sln.DotSettings
Normal file
3
vegasco-server.sln.DotSettings
Normal file
@@ -0,0 +1,3 @@
|
||||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=respawner/@EntryIndexedValue">True</s:Boolean>
|
||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=Vegasco/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||
18
vegasco-server.slnx
Normal file
18
vegasco-server.slnx
Normal file
@@ -0,0 +1,18 @@
|
||||
<Solution>
|
||||
<Folder Name="/Solution Items/">
|
||||
<File Path=".drone.yml" />
|
||||
<File Path="Dockerfile" />
|
||||
<File Path="README.md" />
|
||||
<File Path="version.json" />
|
||||
</Folder>
|
||||
<Folder Name="/src/">
|
||||
<Project Path="src/Vegasco.Server.AppHost.Shared/Vegasco.Server.AppHost.Shared.csproj" />
|
||||
<Project Path="src/Vegasco.Server.AppHost/Vegasco.Server.AppHost.csproj" />
|
||||
<Project Path="src/Vegasco.Server.ServiceDefaults/Vegasco.Server.ServiceDefaults.csproj" />
|
||||
<Project Path="src/Vegasco.Server.Api/Vegasco.Server.Api.csproj" />
|
||||
</Folder>
|
||||
<Folder Name="/tests/">
|
||||
<Project Path="tests/Vegasco.Server.Api.Tests.Integration/Vegasco.Server.Api.Tests.Integration.csproj" />
|
||||
<Project Path="tests/Vegasco.Server.Api.Tests.Unit/Vegasco.Server.Api.Tests.Unit.csproj" />
|
||||
</Folder>
|
||||
</Solution>
|
||||
10
version.json
Normal file
10
version.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/main/src/NerdBank.GitVersioning/version.schema.json",
|
||||
"version": "2.0-beta",
|
||||
"cloudBuild": {
|
||||
"setAllVariables": true
|
||||
},
|
||||
"publicReleaseRefSpec": [
|
||||
"prod"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user