Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Description of version 0.0.3
auto-generated ASP.NET server code
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.
```
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`
### - 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`
contains 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
### - 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`:
```
docker-compose down
```
## to dump database
execute the following command in docker:
```
mongodump --db **insert database_name** --out /data-dump/`date +"%Y-%m-%d"`
```
## to import database:
execute the following command in docker:
```
mongorestore --db **insert database_name** **insert path_to_bson_file**
```