aboutsummaryrefslogtreecommitdiff
path: root/tools/add-feature-template.sh
blob: 9a2bc1094d50dad914a17ff22f18eb231e0dabaa (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
#!/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 Feature.cs 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 of the Feature 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;

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