includes description and code for a fully functional server with MongoDB integration
# Generate or update the server
In the following, we distinguish between a server code generation from scratch and an update of an existing solution
---
# First generation
We provide a zip-file `additions.zip` with a folder structure and some files to simplify your work.
If you want to use it unpack `additions.zip` in this folder and continue with the instruction for an [update](#update).
Otherwise skip the unzip operation and continue with the next step (if you like to, you can compare your version with the one in the zip-file afterwards)
The zip-file additions.zip contains:
* server/src/Org.OpenAPITools/Dockerfile - a corrected Dockerfile
* server/src/Org.OpenAPITools/docker-compose.yml - for a stack of server and MongoDB
* server/src/Org.OpenAPITools/Startup.cs - the adapted original auto-generated file
* server/src/Org.OpenAPITools/appsettings.json - added database settings to the appsettings.json
* server/src/Org.OpenAPITools/Services/ - contains C# code for accessing MongoDB
* server/src/Org.OpenAPITools/ControllersImpl/ - for c# classes inherting classes from /src/Org.OpenAPITools/Controllers/
* server/src/Org.OpenAPITools/docker/ - contains appsettings.json for the use in docker and folder data-dump for later dumping the database for transfer if desired
* server/programs/ - contains a readme.txt about how to install and run MongoDB for Windows
* server/data/ - empty folder for the databases of MongoDB
* server/.openapi-generator-ignore - to prevent openapi-generator to override these files
## auto-generate server code
open a command shell and execute
```
openapi-generator-cli generate -i OpenAPI.yaml -g aspnetcore -o server
```
## set files to ignore
open the file `.openapi-generator-ignore` in server
add the following file and folder entries:
```
**/Startup.cs
**/appsettings.json
**/Dockerfile
**/docker-compoese.yml
**/.openapi-generator-ignore
**/ControllersImpl
**/Services
**/programs
**/data
**/docker
```
## In Visual Studio:
open `NuGet Package Manager` and add `MongoDB.Driver`
### - folder `Controllers`:
copy all files from `Controllers` to `ControllersImpl`
In all files in the folder `Controllers` change `public class` to `public abstract class`
### - folder `ControllersImpl`:
change classnames by appending `Impl` to the original classnames (and change filenames accordingly) and inherit from original class in `Controllers`
and replace `virtual` by `override` with all methods.
Add
```
using Org.OpenAPITools.Services;
using MongoDB.Driver;
```
Add a private readonly service class variable, e.g.
opts.SerializerSettings.ContractResolver = new DefaultContractResolver();
```
### - file `appsettings.json`
add Database settings in `appsettings.json`, like
```
"TrackableDatabaseSettings": {
"CollectionName": "Trackables",
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "TrackablesDatabase"
},
```
## MongoDB
if you don't have a MongoDB add the folders
* server/programs/MongoDB (for MongoDB)
* server/data/ (to store database of MongoDB)
and put MongoDB in folder `server/programs/MongoDB` (download MongoDB as zip-file from https://www.mongodb.com/try/download/community and unzip the file into this directory, so that the bin-directory is in this folder)
---
# Update
## File adaptations:
change version number in all files if a new version is provided
### - folder `Controllers`:
change `public class` to `public abstract class`
compare files in `ControllersImpl` with the corresponding files in `Controllers`
methods should be the same with `override` instead of `virtual`
### - folder `Models`:
add:
```
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
```
at the value that is to become the MongoDB ID, add:
```
[BsonId]
[BsonRepresentation(BsonType.String)]
```
### Solution
open `NuGet Package Manager` and add `MongoDB.Driver`
---
# Use in Visual Studio
make sure, that in instance of MongoDB is running (have a look in the above mentioned zip-file `additions.zip`: in the sub-folder `programs/MongoDB` you find a description, of how to install and run MongoDB for Windows)
start application with IIS Express
---
# Use in Docker
remove the substring `src/Org.OpenAPITools/` in Dockerfile (if not already done)
open a command shell and generate docker by executing in `server/src/Org.OpenAPITools`:
```
docker build -t org.openapitools .
```
## to start:
open a command shell and use docker-compose (if necessary adapt docker-compose.yml) by executing in `server/src/Org.OpenAPITools`:
```
docker-compose up
```
## to stop:
open a command shell by executing in `server/src/Org.OpenAPITools`: