Example of a CRUD API for managing hospital patients.
Solution consists of 6 projects:
Hospital.Presentation - entry point for api;
Hospital.Persistence - database context;
Hospital.Domain - domain models;
Hospital.Application - commands and infrastructure;
Hospital.Console - helper console application for seeding and clearing database;
Hospital.Tests - unit tests.
There's a postman collection json file Patients.postman_collection.json
available in root solution folder.
As per external limitations this project uses .net 6 so packages downgraded accordingly.
Patient record:
{
"name": {
"id": "d8ff176f-bd0a-4b8e-b329-871952e32e1f",
"use": "official",
"family": "Family Name",
"given": [
"Name",
"Surname"
]
},
"gender": "male",
"birthDate": "2024-01-13T18:25:43",
"active": true
}
You can run it manually in Visual Studio or using docker-compose.
Keep in mind there is a dependency on MsSql server.
There's a file Hospital.Console\patients_date_guid.txt
containing hundred predefined patient IDs and birth dates. This data will be used during the seed process to allow easier api testing.
- Download current repository by running:
git clone https://github.com/denis-marchenkov/hospital-api.git
over an empty folder; - Build
Hospital.sln
solution; - Adjust connection string in
Hospital.Presentation\appsettings.json
, don't mind the DbType option - keep it sql; - Initialize DB by running EF migration in package manager console:
Update-Database -Migration InitialMigration
; - Start application (startup projects by defaut are
Hospital.Presentation
andHospital.Console
); - Seed database as described below.
By default swagger entry points are:
https://localhost:7031
http://localhost:5250
In order to seed database navigate to console window of Hospital.Console
(run this project if it's not running already).
Console application takes in the following options:
-s [amount] - seed N records (if used without [amount] - seeds 100 records)
-u [url] - specify api url for creating patient. If specified, seed will use api endpoint to create patients, otherwise it falls back to entity framework.
-c - clear database
-x - exit
Example of seeding database:
-s 100 -u https://localhost:5250/patients
Or simply:
-s
- Download current repository by running:
git clone https://github.com/denis-marchenkov/hospital-api.git
over an empty folder; - In command line switch to
Hospital
solution root folder; - Run
docker-compose up --build
- Seed database as described below.
Container structure:
In order to seed database attach to console container. You will be greeted with empty prompt, so press enter to trigger console output:
Seed commands are the same as described in manual installation, however keep in mind that the urls have changed since we're in docker environment now.
The api url will be http://webapi/patients
since the default name of the service in docker-compose.yml
is webapi
. So in order to seed database via url use:
-s 100 -u http://webapi/patients
Or simply:
-s
Swagger url also changed and mapped to 5000 port (TLS amended):
http://localhost:5000
- Note that Search api accepting one argument - raw query string. It's a subject for a change but will suffice for now even though it creates inconveniences while using Postman. However it allows passing any number of filters.