aboutsummaryrefslogtreecommitdiff
path: root/tools/add-feature-template.sh
blob: 9637384120b796e62c31665f2d4280a04171ba0d (plain) (blame)
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
#!/bin/bash

if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-?" ]; then
	cat <<EOF
./add-feature-template.sh [ProjectName] [FeatureName] "[type Name]" "[type Name]" ...

Example: ./add-feature-template.sh CarShop Car "string Brand" "string Model" "int Year"

For more information: https://gitlab.com/Syndamia/it-kariera-exam-template#add-feature-template
EOF
	exit
fi

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/${project_name}.Data.Models/$feature_name.cs<< EOF
using System;

namespace $project_name.Data.Models
{
	public class $feature_name : BaseModel
	{
$(
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}.Data/${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 "14 i \\\t\t$dbset\n" $dbcontext_path
	fi
fi

# }}} ---------------------------<

# }}} =============================<

# {{{ =============================>
# Add to Service layer

dir="../$project_name/Services"

# {{{ --------------------------->
# Create Service and Interface

cat > $dir/${project_name}.Services/Interfaces/I${feature_name}Service.cs<< EOF
using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;

namespace $project_name.Services.Interfaces
{
	public interface I${feature_name}Service : IBaseService<${feature_name}, ${feature_name}ServiceModel>
	{ }
}
EOF

cat > $dir/${project_name}.Services/Services/${feature_name}Service.cs<< EOF
using $project_name.Services.Interfaces;
using System;
using AutoMapper;
using $project_name.Data;
using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;

namespace $project_name.Services.Services
{
	public class ${feature_name}Service : BaseService<${feature_name}, ${feature_name}ServiceModel>, I${feature_name}Service
	{
		public ${feature_name}Service(IMapper autoMapper, ${project_name}Context context)
			:base(autoMapper, context)
		{ }
	}
}
EOF

# }}} ---------------------------<

# {{{ --------------------------->
# Create Service Model

subdir="$dir/${project_name}.Services.Models/$feature_name"
mkdir -p $subdir

cat > $subdir/$model${feature_name}ServiceModel.cs<< EOF
using System;
using $project_name.Services.Models;

namespace $project_name.Services.Models.$feature_name
{
	public class $model${feature_name}ServiceModel : BaseServiceModel
	{
$(
for feature in "${features[@]}"; do
echo -e "\t\tpublic $feature { get; set; }"
done
)
	}
}
EOF

# }}} ---------------------------<

# {{{ --------------------------->
# Create Service Mappings

subdir="$dir/${project_name}.Services/Configurations"

mappings=("${feature_name}ServiceModel, $feature_name" "${feature_name}, ${feature_name}ServiceModel")
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/${project_name}.Web/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<I$service_name, $service_name>();\n/" $startup_path
fi

# }}} ---------------------------<

# {{{ --------------------------->
# Create View Models

subdir="$dir/${project_name}.Web.Models/$feature_name"
mkdir -p $subdir

models=("" "Create" "Edit")
for model in "${models[@]}"
do
cat > $subdir/$model${feature_name}WebModel.cs<< EOF
using System;$(
if [ "$model" != "Create" ]; then
echo -e "\nusing $project_name.Web.Models;"
fi
)

namespace $project_name.Web.Models.$feature_name
{
	public class $model${feature_name}WebModel$(
if [ "$model" != "Create" ]; then
echo -e ' : BaseWebModel'
fi
)
	{
$(
for feature in "${features[@]}"; do
echo -e "\t\tpublic $feature { get; set; }"
done
)
	}
}
EOF
done

# }}} ---------------------------<

# {{{ --------------------------->
# Update View Imports

viewimports_path="$dir/${project_name}.Web/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/${project_name}.Web/Views/$feature_name"
mkdir -p $subdir

cat > $subdir/Index.cshtml<< EOF
@model IEnumerable<${feature_name}WebModel>
@{
	ViewData["Title"] = "${feature_name}s";
}

<div>
	@foreach (var item in Model)
	{
		<section>
$(
for feature in "${features[@]}"; do
echo -e "\t\t\t<div>\n\t\t\t\t@item.$(echo $feature | cut -d ' ' -f 2-)\n\t\t\t</div>"
done
)
		</section>
	}
</div>
EOF

cat > $subdir/Details.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>

@if (Model != null)
{
	<p class="form-error">
		Couldn't create ${feature_name}!
	</p>
}
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>

@if (Model != null)
{
	<p class="form-error">
		Couldn't edit ${feature_name}!
	</p>
}
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/${project_name}.Web/Controllers"

cat > $subdir/${feature_name}Controller.cs<< EOF
using System;
using System.Threading.Tasks;
using AutoMapper;
using $project_name.Services.Interfaces;
using $project_name.Services.Models.$feature_name;
using $project_name.Web.Models.$feature_name;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

namespace $project_name.Web.Controllers
{
	public class ${feature_name}Controller : Controller
	{
		private readonly IMapper _autoMapper;
		private readonly I${feature_name}Service _service;

		public ${feature_name}Controller(IMapper autoMapper, I${feature_name}Service service)
		{
			this._autoMapper = autoMapper;
			this._service = service;
		}

		[HttpGet]
		public async Task<IActionResult> Index()
		{
			List<${feature_name}ServiceModel> serviceModels = await this._service.GetAllAsync();

			List<${feature_name}WebModel> webModel = serviceModels
				.Select(x => this._autoMapper.Map<${feature_name}WebModel>(x))
				.ToList();
			return View(webModel);
		}

		[HttpGet]
		public async Task<IActionResult> Details(string id)
		{
			${feature_name}ServiceModel serviceModel = await this._service.GetByIdAsync(Guid.Parse(id));

			${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)
		{
			if (!ModelState.IsValid)
				return View(webModel);

			${feature_name}ServiceModel serviceModel = this._autoMapper.Map<${feature_name}ServiceModel>(webModel);

			bool result = await this._service.CreateAsync(serviceModel);

			if (result)
				return RedirectToAction("Index");
			else
				return RedirectToAction("Create");
		}

		[HttpGet]
		public async Task<IActionResult> Edit(string id)
		{
			${feature_name}ServiceModel serviceModel = await this._service.GetByIdAsync(Guid.Parse(id));

			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)
		{
			if (!ModelState.IsValid)
				return View(webModel);

			${feature_name}ServiceModel serviceModel = this._autoMapper.Map<${feature_name}ServiceModel>(webModel);

			bool result = await this._service.EditAsync(serviceModel);

			if (result)
				return RedirectToAction("Details", new { id = webModel.Id.ToString() });
			else
				return RedirectToAction("Edit", new { id = webModel.Id.ToString() });
		}

		[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");
			else
				return RedirectToAction("Delete", new { id = id });
		}
	}
}
EOF

# }}} ---------------------------<

# {{{ --------------------------->
# Create Controller Mappings

subdir="${dir}/${project_name}.Web/Configurations"

mappings=("${feature_name}ServiceModel, ${feature_name}WebModel" "Create${feature_name}WebModel, ${feature_name}ServiceModel" "${feature_name}ServiceModel, Edit${feature_name}WebModel" "Edit${feature_name}WebModel, ${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

# }}} ---------------------------<

# }}} =============================<