diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-11 09:35:37 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-11 09:35:37 +0300 |
| commit | 3eb5578cf5479fdf978901ce63bd933f8080523e (patch) | |
| tree | bd573a219bb8af99257b8694ee76b951904e01ad | |
| parent | 414ba6688b1b04664fe54fc6569a6bf3495a4858 (diff) | |
| download | it-kariera-exam-template-3eb5578cf5479fdf978901ce63bd933f8080523e.tar it-kariera-exam-template-3eb5578cf5479fdf978901ce63bd933f8080523e.tar.gz it-kariera-exam-template-3eb5578cf5479fdf978901ce63bd933f8080523e.zip | |
Implemented service layer preset adding to script and fixed some data layer file bugs
| -rwxr-xr-x | tools/add-feature-template.sh | 145 |
1 files changed, 134 insertions, 11 deletions
diff --git a/tools/add-feature-template.sh b/tools/add-feature-template.sh index 9a24bf6..9a2bc10 100755 --- a/tools/add-feature-template.sh +++ b/tools/add-feature-template.sh @@ -17,26 +17,149 @@ fi dir="../$project_name/Data" # Create the Feature.cs DB model class -cat > $dir/$feature_name.cs<< FILE +cat > $dir/Models/$feature_name.cs<< EOF using System; namespace $project_name.Data.Models { public class $feature_name { - $( - for feature in "${features[@]}"; do - echo -e "\r\t\tpublic $feature { get; set; }" - done - ) + public Guid Id { get; set; } +$( +for feature in "${features[@]}"; do +echo -e "\t\tpublic $feature { get; set; }" +done +) } } -FILE +EOF # Add the DbSet of the Feature to DbContext dbset="public DbSet<$feature_name> ${feature_name}s { get; set; }" dbcontext_path="$dir/${project_name}Context.cs" -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 +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; + +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" + +models=("" "Create" "Edit") +for model in "${models[@]}" +do +cat > $subdir/$model${feature_name}ServiceModel.cs<< EOF +using System; + +namespace $project_name.Services.Models { + 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/${feature_name}Mappings.cs<< EOF +using AutoMapper; +using $project_name.Data.Models; +using $project_name.Services.Models; + +namespace $project_name.Services.Configurations +{ + public class ${feature_name}Mappings : Profile + { + public ${feature_name}Mappings() + { +$( +for map in "${mappings[@]}"; do +echo -e "\t\t\tCreateMap<$map>();" +done +) + } + } +} +EOF |
