aboutsummaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorSyndamia <kamen.d.mladenov@protonmail.com>2021-05-13 15:05:53 +0300
committerSyndamia <kamen.d.mladenov@protonmail.com>2021-05-13 15:05:53 +0300
commitef163726a5444ba0cbc631f74730184172c1c486 (patch)
treed7d60837bb093c4256ad6506bc10ab981dafd491 /tools
parent19bb4631a7c0b57c8da19ca7aa89864ac1555836 (diff)
downloadit-kariera-exam-template-ef163726a5444ba0cbc631f74730184172c1c486.tar
it-kariera-exam-template-ef163726a5444ba0cbc631f74730184172c1c486.tar.gz
it-kariera-exam-template-ef163726a5444ba0cbc631f74730184172c1c486.zip
Implemented BaseService that uses generics to implement very basic CRUD; Moved away from 3 service model to 1 service model in add feature script
Diffstat (limited to 'tools')
-rwxr-xr-xtools/add-feature-template.sh112
1 files changed, 16 insertions, 96 deletions
diff --git a/tools/add-feature-template.sh b/tools/add-feature-template.sh
index c5ebe45..e5433f3 100755
--- a/tools/add-feature-template.sh
+++ b/tools/add-feature-template.sh
@@ -67,100 +67,31 @@ dir="../$project_name/Services"
# Create Service and Interface
cat > $dir/Interfaces/I${feature_name}Service.cs<< EOF
-using System;
-using System.Threading.Tasks;
+using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;
-using System.Collections.Generic;
namespace $project_name.Services.Interfaces
{
- public interface I${feature_name}Service
- {
- Task<bool> CreateAsync(Create${feature_name}ServiceModel create${feature_name}ServiceModel);
-
- Task<${feature_name}ServiceModel> GetByIdAsync(Guid id);
-
- List<${feature_name}ServiceModel> GetAll();
-
- Task<bool> EditAsync(Edit${feature_name}ServiceModel edit${feature_name}ServiceModel);
-
- Task<bool> DeleteAsync(Guid id);
- }
+ public interface I${feature_name}Service : IBaseService<${feature_name}, ${feature_name}ServiceModel>
+ { }
}
EOF
cat > $dir/Services/${feature_name}Service.cs<< EOF
using $project_name.Services.Interfaces;
using System;
-using System.Threading.Tasks;
using AutoMapper;
using $project_name.Data;
using $project_name.Data.Models;
using $project_name.Services.Models.$feature_name;
-using System.Collections.Generic;
-using System.Linq;
namespace $project_name.Services.Services
{
- public class ${feature_name}Service : I${feature_name}Service
+ public class ${feature_name}Service : BaseService<${feature_name}, ${feature_name}ServiceModel>, I${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<List<${feature_name}ServiceModel>> GetAll()
- {
- return await this._context.${feature_name}s
- .Select(x => this._autoMapper.Map<${feature_name}ServiceModel>(x))
- .ToListAsync();
- }
-
- 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;
- }
+ :base(autoMapper, context)
+ { }
}
}
EOF
@@ -168,28 +99,18 @@ EOF
# }}} ---------------------------<
# {{{ --------------------------->
-# Create Service Models
+# Create Service Model
subdir="$dir/Models/$feature_name"
mkdir -p $subdir
-models=("" "Create" "Edit")
-for model in "${models[@]}"
-do
cat > $subdir/$model${feature_name}ServiceModel.cs<< EOF
-using System;$(
-if [ "$model" != "Create" ]; then
-echo -e "\nusing $project_name.Services.Models;"
-fi
-)
+using System;
+using $project_name.Services.Models;
namespace $project_name.Services.Models.$feature_name
{
- public class $model${feature_name}ServiceModel$(
-if [ "$model" != "Create" ]; then
-echo -e ' : BaseServiceModel'
-fi
-)
+ public class $model${feature_name}ServiceModel : BaseServiceModel
{
$(
for feature in "${features[@]}"; do
@@ -199,7 +120,6 @@ done
}
}
EOF
-done
# }}} ---------------------------<
@@ -208,7 +128,7 @@ done
subdir="${dir}/Configurations"
-mappings=("Create${feature_name}ServiceModel, $feature_name" "$feature_name, ${feature_name}ServiceModel" "Edit${feature_name}ServiceModel, $feature_name")
+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;
@@ -422,9 +342,9 @@ namespace $project_name.Web.Controllers
[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);
+ ${feature_name}ServiceModel serviceModel = this._autoMapper.Map<${feature_name}ServiceModel>(webModel);
- bool result = await this._service.CreateAsync(create${feature_name}ServiceModel);
+ bool result = await this._service.CreateAsync(serviceModel);
if (result)
return RedirectToAction("Profile", "Account");
@@ -447,9 +367,9 @@ namespace $project_name.Web.Controllers
[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);
+ ${feature_name}ServiceModel serviceModel = this._autoMapper.Map<${feature_name}ServiceModel>(webModel);
- bool result = await this._service.EditAsync(edit${feature_name}ServiceModel);
+ bool result = await this._service.EditAsync(serviceModel);
if (result)
return RedirectToAction("Index", new { id = webModel.Id.ToString() });
@@ -484,7 +404,7 @@ EOF
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")
+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;