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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]; then
echo "No name supplied!"
exit
fi
project_name="$1"
feature_name="$2"
features=("${@:3}")
if [ -z features ]; then
features=("string Name")
fi
# {{{ =============================>
# Add to Data layer
dir="../$project_name/Data"
# {{{ --------------------------->
# Create the DB model class
cat > $dir/Models/$feature_name.cs<< EOF
using System;
namespace $project_name.Data.Models {
public class $feature_name {
public Guid Id { get; set; }
$(
for feature in "${features[@]}"; do
echo -e "\t\tpublic $feature { get; set; }"
done
)
}
}
EOF
# }}} ---------------------------<
# {{{ --------------------------->
# Add the DbSet to DbContext
dbset="public DbSet<$feature_name> ${feature_name}s { get; set; }"
dbcontext_path="$dir/${project_name}Context.cs"
if ! grep -q "$dbset" "$dbcontext_path"; then
if grep -Fq "DbSet" $dbcontext_path; then
sed -z -i "s/.*DbSet[^\n]*\n/&\t\t$dbset\n/" $dbcontext_path
else
sed -i "11 i \\\t\t$dbset\n" $dbcontext_path
fi
fi
# }}} ---------------------------<
# }}} =============================<
# {{{ =============================>
# Add to Service layer
dir="../$project_name/Services"
# {{{ --------------------------->
# Create Service
cat > $dir/${feature_name}Service.cs<< EOF
using System;
using System.Threading.Tasks;
using AutoMapper;
using $project_name.Data;
using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;
namespace $project_name.Services
{
public class ${feature_name}Service
{
private readonly IMapper _autoMapper;
private readonly ${project_name}Context _context;
public ${feature_name}Service(IMapper autoMapper, ${project_name}Context context)
{
this._autoMapper = autoMapper;
this._context = context;
}
public async Task<bool> CreateAsync(Create${feature_name}ServiceModel create${feature_name}ServiceModel)
{
$feature_name new$feature_name = this._autoMapper.Map<$feature_name>(create${feature_name}ServiceModel);
await this._context.${feature_name}s
.AddAsync(new$feature_name);
return await this.SaveChangesAsync();
}
public async Task<${feature_name}ServiceModel> GetByIdAsync(Guid id)
{
$feature_name curr$feature_name = await this._context.${feature_name}s
.FindAsync(id);
return this._autoMapper.Map<${feature_name}ServiceModel>(curr$feature_name);
}
public async Task<bool> EditAsync(Edit${feature_name}ServiceModel edit${feature_name}ServiceModel)
{
$feature_name curr$feature_name = this._autoMapper.Map<$feature_name>(edit${feature_name}ServiceModel);
this._context.Update(curr$feature_name);
return await this.SaveChangesAsync();
}
public async Task<bool> DeleteAsync(Guid id)
{
$feature_name curr$feature_name = await this._context.${feature_name}s
.FindAsync(id);
this._context.Remove(curr$feature_name);
return await this.SaveChangesAsync();
}
private async Task<bool> SaveChangesAsync()
{
return await this._context.SaveChangesAsync() >= 1;
}
}
}
EOF
# }}} ---------------------------<
# {{{ --------------------------->
# Create Service Models
subdir="$dir/Models/$feature_name"
mkdir -p $subdir
models=("View" "" "Edit")
for model in "${models[@]}"
do
cat > $subdir/$model${feature_name}ServiceModel.cs<< EOF
using System;
namespace $project_name.Services.Models.$feature_name {
public class $model${feature_name}ServiceModel {$(
if [ ! -z $model ]; then
echo -e "\n\t\tpublic Guid Id { get; set; }"
fi
)
$(
for feature in "${features[@]}"; do
echo -e "\t\tpublic $feature { get; set; }"
done
)
}
}
EOF
done
# }}} ---------------------------<
# {{{ --------------------------->
# Create Service Mappings
subdir="${dir}/Configurations"
mappings=("Create${feature_name}ServiceModel, $feature_name" "$feature_name, ${feature_name}ServiceModel" "Edit${feature_name}ServiceModel, $feature_name")
cat > $subdir/Service${feature_name}Mappings.cs<< EOF
using AutoMapper;
using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;
namespace $project_name.Services.Configurations
{
public class Service${feature_name}Mappings : Profile
{
public Service${feature_name}Mappings()
{
$(
for map in "${mappings[@]}"; do
echo -e "\t\t\tCreateMap<$map>();"
done
)
}
}
}
EOF
# }}} ---------------------------<
# }}} =============================<
# {{{ =============================>
# Add to Web layer
dir="../$project_name/Web"
# {{{ --------------------------->
# Add the dependency injection of the Service class
startup_path="$dir/Startup.cs"
service_name="${feature_name}Service"
if ! grep -q "$service_name" "$startup_path"; then
sed -z -i "s/.*AddTransient[^\n]*\n/&\t\t\tservices.AddTransient<$service_name>();\n/" $startup_path
fi
# }}} ---------------------------<
# {{{ --------------------------->
# Create View Models
subdir="$dir/Models/$feature_name"
mkdir -p $subdir
models=("View" "" "Edit")
for model in "${models[@]}"
do
cat > $subdir/$model${feature_name}WebModel.cs<< EOF
using System;
namespace $project_name.Web.Models.$feature_name {
public class $model${feature_name}WebModel {$(
if [ ! -z $model ]; then
echo -e "\n\t\tpublic Guid Id { get; set; }"
fi
)
$(
for feature in "${features[@]}"; do
echo -e "\t\tpublic $feature { get; set; }"
done
)
}
}
EOF
done
# }}} ---------------------------<
# {{{ --------------------------->
# Update View Imports
viewimports_path="$dir/Views/_ViewImports.cshtml"
import_name="$project_name.Web.Models.$feature_name"
if ! grep -q "$import_name" "$viewimports_path"; then
sed -z -i "s/.*@using[^\n]*\n/&@using $import_name\n/" $viewimports_path
fi
# }}} ---------------------------<
# {{{ --------------------------->
# Create Views
subdir="$dir/Views/$feature_name"
mkdir -p $subdir
cat > $subdir/Index.cshtml<< EOF
@model ${feature_name}WebModel
@{
ViewData["Title"] = "$feature_name";
}
<section>
$(
for feature in "${features[@]}"; do
echo -e "\t<div>\n\t\t@Model.$(echo $feature | cut -d ' ' -f 2-)\n\t</div>"
done
)
</section>
EOF
cat > $subdir/Create.cshtml<< EOF
@model Create${feature_name}WebModel
@{
ViewData["Title"] = "Creating a new $feature_name";
}
<form asp-controller="$feature_name" asp-action="Create" method="post">
$(
for feature in "${features[@]}"; do
feature_name=$(echo $feature | cut -d ' ' -f 2-)
echo -e "\t<input type=\"text\" asp-for=\"$feature_name\" placeholder=\"$feature_name\">\n\t<span asp-validation-for=\"$feature_name\" class=\"form-error\"></span>\n"
done
)
<input type="submit" value="Create">
</form>
EOF
cat > $subdir/Edit.cshtml<< EOF
@model Edit${feature_name}WebModel
@{
ViewData["Title"] = "Editing $feature_name";
}
<form asp-controller="$feature_name" asp-action="Edit" method="post">
<!-- Gets the id from the URL and sets it to the model -->
<input type="hidden" asp-for="Id" value="@Context.Request.Query["id"]">
$(
for feature in "${features[@]}"; do
feature_name=$(echo $feature | cut -d ' ' -f 2-)
echo -e "\t<label asp-for=\"$feature_name\">$feature_name</label>\n\t<input type=\"text\" asp-for=\"$feature_name\" placeholder=\"$feature_name\">\n\t<span asp-validation-for=\"$feature_name\" class=\"form-error\"></span>\n"
done
)
<input type="submit" value="Edit">
</form>
EOF
cat > $subdir/Delete.cshtml<< EOF
@{
ViewData["Title"] = "Deleting $feature_name";
}
<p>
Are you sure you want to delete this $feature_name?
</p>
<form asp-controller="$feature_name" asp-action="Delete" method="post">
<!-- Gets the id from the URL and sets it to the model -->
<input type="hidden" name="id" value="@Context.Request.Query["id"]">
<input type="submit" value="Delete">
</form>
EOF
# }}} ---------------------------<
# {{{ --------------------------->
# Create Controller
subdir="$dir/Controllers"
cat > $subdir/${feature_name}Controller.cs<< EOF
using System;
using System.Threading.Tasks;
using AutoMapper;
using $project_name.Services;
using $project_name.Services.Models.$feature_name;
using $project_name.Web.Models.$feature_name;
using Microsoft.AspNetCore.Mvc;
namespace $project_name.Web.Controllers
{
public class ${feature_name}Controller : Controller
{
private readonly IMapper _autoMapper;
private readonly ${feature_name}Service _service;
public ${feature_name}Controller(IMapper autoMapper, ${feature_name}Service service)
{
this._autoMapper = autoMapper;
this._service = service;
}
[HttpGet]
public async Task<IActionResult> Index(string id)
{
${feature_name}ServiceModel serviceModel = await this._service.GetByIdAsync(Guid.Parse(id));
if (serviceModel == null)
return RedirectToAction("Index", "Home");
${feature_name}WebModel webModel = this._autoMapper.Map<${feature_name}WebModel>(serviceModel);
return View(webModel);
}
[HttpGet]
public IActionResult Create()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Create(Create${feature_name}WebModel webModel)
{
Create${feature_name}ServiceModel create${feature_name}ServiceModel = this._autoMapper.Map<Create${feature_name}ServiceModel>(webModel);
bool result = await this._service.CreateAsync(create${feature_name}ServiceModel);
if (result)
return RedirectToAction("Profile", "Account");
else
return RedirectToAction("Index", "Home");
}
[HttpGet]
public async Task<IActionResult> Edit(string id)
{
${feature_name}ServiceModel serviceModel = await this._service.GetByIdAsync(Guid.Parse(id));
if (serviceModel == null)
return RedirectToAction("Index", "Home");
Edit${feature_name}WebModel edit${feature_name}WebModel = this._autoMapper.Map<Edit${feature_name}WebModel>(serviceModel);
return View(edit${feature_name}WebModel);
}
[HttpPost]
public async Task<IActionResult> Edit(Edit${feature_name}WebModel webModel)
{
Edit${feature_name}ServiceModel edit${feature_name}ServiceModel = this._autoMapper.Map<Edit${feature_name}ServiceModel>(webModel);
bool result = await this._service.EditAsync(edit${feature_name}ServiceModel);
if (result)
return RedirectToAction("Index", new { id = webModel.Id.ToString() });
else
return RedirectToAction("Index", "Home");
}
[HttpGet]
public IActionResult Delete()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Delete(string id)
{
bool result = await this._service.DeleteAsync(Guid.Parse(id));
if (result)
return RedirectToAction("Index", "Home");
else
return RedirectToAction("Index", new { id = id });
}
}
}
EOF
# }}} ---------------------------<
# {{{ --------------------------->
# Create Controller Mappings
subdir="${dir}/Configurations"
mappings=("${feature_name}ServiceModel, ${feature_name}WebModel" "Create${feature_name}WebModel, Create${feature_name}ServiceModel" "${feature_name}ServiceModel, Edit${feature_name}WebModel" "Edit${feature_name}WebModel, Edit${feature_name}ServiceModel")
cat > $subdir/Controller${feature_name}Mappings.cs<< EOF
using AutoMapper;
using $project_name.Services.Models.$feature_name;
using $project_name.Web.Models.$feature_name;
namespace $project_name.Web.Configurations
{
public class Controller${feature_name}Mappings : Profile
{
public Controller${feature_name}Mappings()
{
$(
for map in "${mappings[@]}"; do
echo -e "\t\t\tCreateMap<$map>();"
done
)
}
}
}
EOF
# }}} ---------------------------<
# }}} =============================<
|