aboutsummaryrefslogtreecommitdiff
path: root/tools/add-feature-template.sh
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-05-11 11:47:34 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-05-11 11:47:34 +0300
commit3fca091e1643e7a23a40d961fe5bdc72dd711271 (patch)
tree8ad63e36d6ead2225eb0297fe61a2fee5de882ec /tools/add-feature-template.sh
parent3eb5578cf5479fdf978901ce63bd933f8080523e (diff)
downloadit-kariera-exam-template-3fca091e1643e7a23a40d961fe5bdc72dd711271.tar
it-kariera-exam-template-3fca091e1643e7a23a40d961fe5bdc72dd711271.tar.gz
it-kariera-exam-template-3fca091e1643e7a23a40d961fe5bdc72dd711271.zip
Improved formatting and implemented web layer template creation
Diffstat (limited to 'tools/add-feature-template.sh')
-rwxr-xr-xtools/add-feature-template.sh310
1 files changed, 305 insertions, 5 deletions
diff --git a/tools/add-feature-template.sh b/tools/add-feature-template.sh
index 9a2bc10..c1f59d9 100755
--- a/tools/add-feature-template.sh
+++ b/tools/add-feature-template.sh
@@ -13,10 +13,14 @@ if [ -z features ]; then
features=("string Name")
fi
-#=== Add to Data layer
+# {{{ =============================>
+# Add to Data layer
+
dir="../$project_name/Data"
-# Create the Feature.cs DB model class
+# {{{ --------------------------->
+# Create the DB model class
+
cat > $dir/Models/$feature_name.cs<< EOF
using System;
@@ -32,7 +36,12 @@ done
}
EOF
-# Add the DbSet of the Feature to DbContext
+# }}} ---------------------------<
+
+# {{{ --------------------------->
+# Add the DbSet to DbContext
+
+
dbset="public DbSet<$feature_name> ${feature_name}s { get; set; }"
dbcontext_path="$dir/${project_name}Context.cs"
@@ -44,10 +53,18 @@ if ! grep -q "$dbset" "$dbcontext_path"; then
fi
fi
-#=== Add to Service layer
+# }}} ---------------------------<
+
+# }}} =============================<
+
+# {{{ =============================>
+# Add to Service layer
+
dir="../$project_name/Services"
+# {{{ --------------------------->
# Create Service
+
cat > $dir/${feature_name}Service.cs<< EOF
using System;
using System.Threading.Tasks;
@@ -114,8 +131,12 @@ namespace $project_name.Services
}
EOF
+# }}} ---------------------------<
+
+# {{{ --------------------------->
# Create Service Models
-subdir="${dir}/Models"
+
+subdir="$dir/Models"
models=("" "Create" "Edit")
for model in "${models[@]}"
@@ -139,7 +160,11 @@ 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")
@@ -163,3 +188,278 @@ 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=("" "Create" "Edit")
+for model in "${models[@]}"
+do
+cat > $subdir/$model${feature_name}ViewModel.cs<< EOF
+using System;
+
+namespace $project_name.Web.Models.$feature_name {
+ public class $model${feature_name}ViewModel {$(
+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}ViewModel
+@{
+ 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}ViewModel
+@{
+ 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}ViewModel
+@{
+ 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;
+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}ViewModel viewModel = this._autoMapper.Map<${feature_name}ViewModel>(serviceModel);
+ return View(viewModel);
+ }
+
+ [HttpGet]
+ public IActionResult Create()
+ {
+ return View();
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> Create(Create${feature_name}ViewModel viewModel)
+ {
+ Create${feature_name}ServiceModel create${feature_name}ServiceModel = this._autoMapper.Map<Create${feature_name}ServiceModel>(viewModel);
+
+ 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}ViewModel edit${feature_name}ViewModel = this._autoMapper.Map<Edit${feature_name}ViewModel>(serviceModel);
+ return View(edit${feature_name}ViewModel);
+ }
+
+ [HttpPost]
+ public async Task<IActionResult> Edit(Edit${feature_name}ViewModel viewModel)
+ {
+ Edit${feature_name}ServiceModel edit${feature_name}ServiceModel = this._autoMapper.Map<Edit${feature_name}ServiceModel>(viewModel);
+
+ bool result = await this._service.EditAsync(edit${feature_name}ServiceModel);
+
+ if (result)
+ return RedirectToAction("Index", new { id = viewModel.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}ViewModel" "Create${feature_name}ViewModel, Create${feature_name}ServiceModel" "${feature_name}ServiceModel, Edit${feature_name}ViewModel" "Edit${feature_name}ViewModel, Edit${feature_name}ServiceModel")
+cat > $subdir/${feature_name}Mappings.cs<< EOF
+using AutoMapper;
+using $project_name.Services.Models;
+using $project_name.Web.Models.$feature_name;
+
+namespace $project_name.Web.Configurations
+{
+ public class ${feature_name}Mappings : Profile
+ {
+ public ${feature_name}Mappings()
+ {
+$(
+for map in "${mappings[@]}"; do
+echo -e "\t\t\tCreateMap<$map>();"
+done
+)
+ }
+ }
+}
+EOF
+
+# }}} ---------------------------<
+
+# }}} =============================<