#!/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 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 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 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 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"; }
$( for feature in "${features[@]}"; do echo -e "\t
\n\t\t@Model.$(echo $feature | cut -d ' ' -f 2-)\n\t
" done )
EOF cat > $subdir/Create.cshtml<< EOF @model Create${feature_name}WebModel @{ ViewData["Title"] = "Creating a new $feature_name"; }
$( for feature in "${features[@]}"; do feature_name=$(echo $feature | cut -d ' ' -f 2-) echo -e "\t\n\t\n" done )
EOF cat > $subdir/Edit.cshtml<< EOF @model Edit${feature_name}WebModel @{ ViewData["Title"] = "Editing $feature_name"; }
$( for feature in "${features[@]}"; do feature_name=$(echo $feature | cut -d ' ' -f 2-) echo -e "\t\n\t\n\t\n" done )
EOF cat > $subdir/Delete.cshtml<< EOF @{ ViewData["Title"] = "Deleting $feature_name"; }

Are you sure you want to delete this $feature_name?

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 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 Create(Create${feature_name}WebModel webModel) { Create${feature_name}ServiceModel create${feature_name}ServiceModel = this._autoMapper.Map(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 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(serviceModel); return View(edit${feature_name}WebModel); } [HttpPost] public async Task Edit(Edit${feature_name}WebModel webModel) { Edit${feature_name}ServiceModel edit${feature_name}ServiceModel = this._autoMapper.Map(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 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 # }}} ---------------------------< # }}} =============================<