Commit 9701b5a3 authored by Detlef Runde's avatar Detlef Runde
Browse files

added additional files directly

parent a1783bd5
Loading
Loading
Loading
Loading

additions.zip

deleted100644 → 0
−10.6 KiB

File deleted.

+13 −162
Original line number Original line Diff line number Diff line
@@ -13,28 +13,7 @@ includes description and code for a fully functional server with MongoDB integra


# Generate or update the server
# Generate or update the server


In the following, we distinguish between a server code generation from scratch and an update of an existing solution
we provided the file `.openapi-generator-ignore` in `server`, which prevents openapi-generator to override some adapted files 

---

# 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
## auto-generate server code
open a command shell and execute
open a command shell and execute
@@ -42,143 +21,20 @@ open a command shell and execute
  openapi-generator-cli generate -i OpenAPI.yaml -g aspnetcore -o server
  openapi-generator-cli generate -i OpenAPI.yaml -g aspnetcore -o server
```
```


## set files to ignore
open the solution `Org.OpenAPITools.sln` (folder `server`) in Visual Studio
open the file `.openapi-generator-ignore` in server 

add the following file and folder entries:
```
**/Startup.cs
**/appsettings.json
**/Dockerfile
**/docker-compose.yml
**/.openapi-generator-ignore

**/ControllersImpl
**/Services
**/programs
**/data
**/docker
```


## In Visual Studio:
## In Visual Studio:
open `NuGet Package Manager` and add `MongoDB.Driver`
open `NuGet Package Manager` and add `MongoDB.Driver`


### - folder `ControllersImpl`:
### File adaptations:
create a folder `ControllersImpl` and copy all files from `Controllers` to `ControllersImpl`

change classnames by appending `Impl` to the original classnames (and change filenames accordingly) and inherit from original class in `Controllers` (instead of `ControllerBase`) 

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.
```
  private readonly TrackableService _trackableService;
``` 

Add a constructor with this service class variable, e.g.
```
  public TrackablesApiControllerImpl(TrackableService trackableService)
  {
    _trackableService = trackableService;
  }
```

remove sample code and replace it by using the appropriate methods of the corresponding classes in the folder `Services` 
(have a look into the above mentioned `additions.zip` for an example)

### - folder `Controllers`: 
In all files in the folder `Controllers` change `public class` to `public abstract class`

### - folder `Models`:
in the files add: 
```
  using MongoDB.Bson;
  using MongoDB.Bson.Serialization.Attributes;
```

at the value that is to become the Database ID, add:
```
  [BsonId]
  [BsonRepresentation(BsonType.String)]
```

### - folder `Services`
create the folder `Services` and have a look into the above mentioned `additions.zip` for an example

the folder `Services` should contain one class with the DatabaseSettings and one with the database-access-methods (create, get, update, remove) for each API

the naming in the DatabaseSettings is the same as defined in `appsettings.json `

### - file `startup.cs`
add
```
  using Org.OpenAPITools.Services;
  using MongoDB.Bson.Serialization;
  using MongoDB.Bson;
  using MongoDB.Bson.Serialization.Serializers;
  using Microsoft.Extensions.Options;
```

add the following code in `public void ConfigureServices(IServiceCollection services)`: 
```      
  BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
  BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
  // requires using Microsoft.Extensions.Options
  services.Configure<**insert classname of DatabaseSettings in Services**>(
          Configuration.GetSection(nameof(**insert classname of DatabaseSettings in Services**)));
  services.AddSingleton<**insert interface of DatabaseSettings in Services**>(sp =>
          sp.GetRequiredService<IOptions<**insert classname of DatabaseSettings in Services**>>().Value);
  services.AddSingleton<**insert classname of service class in Services**>();
```

replace 
```
  opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
  opts.SerializerSettings.Converters.Add(new StringEnumConverter
  {
    NamingStrategy = new CamelCaseNamingStrategy()
  });
```
by
```
  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
change version number in all files if a new version is provided


### - folder `Controllers`: 
### - folder `Controllers`: 
change `public class` to `public abstract class`
change "`public class`" to "`public abstract class`"


compare files in `ControllersImpl` with the corresponding files in `Controllers` 
compare files in "`ControllersImpl`" with the corresponding files in "`Controllers`" and adapt if necessary


methods should be the same with `override` instead of `virtual`
methods should be the same with "`override`" instead of "`virtual`"


### - folder `Models`:
### - folder `Models`:
add: 
add: 
@@ -193,18 +49,18 @@ at the value that is to become the MongoDB ID, add:
  [BsonRepresentation(BsonType.String)]
  [BsonRepresentation(BsonType.String)]
```
```


### Solution
open `NuGet Package Manager` and add `MongoDB.Driver`


# MongoDB
if you don't have a MongoDB, follow the instructions in `readme.txt` in `server/programs/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) 


---


# Use in Visual Studio
# 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)
make sure, that an instance of MongoDB is running


start application with IIS Express
start application with IIS Express


---


# Use in Docker
# Use in Docker
remove the substring `src/Org.OpenAPITools/` in Dockerfile (if not already done)
remove the substring `src/Org.OpenAPITools/` in Dockerfile (if not already done)
@@ -217,18 +73,13 @@ open a command shell and generate docker by executing in `server/src/Org.OpenAPI
## to start:
## to start:
the easiest way is to use docker-compose:
the easiest way is to use docker-compose:


if not already done (i.e. by using the above mentioned zip-file `additions.zip`):
* copy file `docker-compose.yml` and folder `docker` from the above mentioned zip-file `additions.zip` or
  * create a folder `docker` 
  * create a sub-folder `data-dump`
  * copy appsettings.json into the folder `docker` and adapt the connectionString for MongoDB for the usage in docker
  * create a file docker-compose.yml for the above created image and MongoDB-image or copy it from the above mentioned zip-file `additions.zip`

open a command shell and use docker-compose (if necessary adapt docker-compose.yml) by executing in `server/src/Org.OpenAPITools`:
open a command shell and use docker-compose (if necessary adapt docker-compose.yml) by executing in `server/src/Org.OpenAPITools`:
```
```
  docker-compose up 
  docker-compose up 
```
```


open http://localhost:8080/openapi/index.html in a web-browser, if you want to check the functionalities using SwaggerUI

## to stop:
## to stop:
open a command shell by executing in `server/src/Org.OpenAPITools`:
open a command shell by executing in `server/src/Org.OpenAPITools`:
```
```
+37 −0
Original line number Original line Diff line number Diff line
# OpenAPI Generator Ignore
# Generated by openapi-generator https://github.com/openapitools/openapi-generator

# Use this file to prevent files from being overwritten by the generator.
# The patterns follow closely to .gitignore or .dockerignore.

# As an example, the C# client generator defines ApiClient.cs.
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
#ApiClient.cs

# You can match any string of characters against a directory, file or extension with a single asterisk (*):
#foo/*/qux
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux

# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
#foo/**/qux
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux

# You can also negate patterns with an exclamation (!).
# For example, you can ignore all files in a docs folder with the file extension .md:
#docs/*.md
# Then explicitly reverse the ignore rule for a single file:
#!docs/README.md


**/Startup.cs
**/appsettings.json
**/Dockerfile
**/docker-compose.yml
**/.openapi-generator-ignore


# ARF
# Implementation of REST request and database functionalities
#
**/ControllersImpl
**/Services
+1 −0
Original line number Original line Diff line number Diff line
taskkill -im mongod.exe
 No newline at end of file
+3 −0
Original line number Original line Diff line number Diff line
1. download MongoDB as zip-file from https://www.mongodb.com/try/download/community
2. unzip the file into this directory, so that the bin-directory is in this folder (where you found this readme.txt)
3. before starting startMongoDB.bat create data-directory and adjust - if necessary - data path in startMongoDB.bat
 No newline at end of file
Loading