Skip to content

Commit 85a3ecd

Browse files
committed
fix PKColumn.Name not equal to "Id" bug. Ref #28
1 parent 1897c81 commit 85a3ecd

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

build/version.props

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<VersionMajor>2</VersionMajor>
44
<VersionMinor>2</VersionMinor>
5-
<VersionPatch>42</VersionPatch>
5+
<VersionPatch>44</VersionPatch>
66
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
77
</PropertyGroup>
88
</Project>

src/SmartCode.Generator/RazorTemplates/CSharp/API/APIController.cshtml

+5-5
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ namespace @(project.Module).API.Controllers
4444
Body = @(serviceName).Insert(@entityCamelName)
4545
};
4646
}
47-
[HttpPost]
48-
public ResponseMessageWrap@("<int>") DeleteById([FromBody]@pkType id)
47+
[HttpDelete]
48+
public ResponseMessageWrap@("<int>") DeleteById(@pkType id)
4949
{
5050
return new ResponseMessageWrap@("<int>")
5151
{
5252
Body = @(serviceName).DeleteById(id)
5353
};
5454
}
55-
[HttpPost]
55+
[HttpPut]
5656
public ResponseMessageWrap@("<int>") Update([FromBody]@entityName @entityCamelName)
5757
{
5858
return new ResponseMessageWrap@("<int>")
@@ -61,8 +61,8 @@ namespace @(project.Module).API.Controllers
6161
};
6262
}
6363

64-
[HttpPost]
65-
public ResponseMessageWrap@($"<{entityName}>") GetById([FromBody]@pkType id)
64+
[HttpGet]
65+
public ResponseMessageWrap@($"<{entityName}>") GetById(@pkType id)
6666
{
6767
var @entityCamelName = @(repositoryName).GetById(id);
6868
return new ResponseMessageWrap@($"<{entityName}>")

src/SmartCode.Generator/RazorTemplates/CSharp/Proj-API.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@model BuildContext
33
@{
44
var project = Model.Project;
5-
var smartsqlVersion = "4.0.33";
5+
var smartsqlVersion = "4.0.42";
66
if (project.Parameters.Value("SmartSqlVersion", out string version))
77
{
88
smartsqlVersion = version;

src/SmartCode.Generator/RazorTemplates/CSharp/Proj-Repository.cshtml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
var dbProviderAssNames = dbFactory.GetType().Assembly.FullName.Split(',');
99
var dbProviderName = dbProviderAssNames[0];
1010
var dbProviderVersion = dbProviderAssNames[1].Split('=')[1];
11-
var smartSqlVersion = "4.0.33";
11+
var smartSqlVersion = "4.0.42";
1212
if (project.Parameters.Value("SmartSqlVersion", out string version))
1313
{
1414
smartSqlVersion = version;
1515
}
16-
var smartSqlSchemaVersion = "4.0.26";
16+
var smartSqlSchemaVersion = "4.0.42";
1717
if (project.Parameters.Value("SmartSqlSchemaVersion", out string schemaVersion))
1818
{
1919
smartSqlSchemaVersion = schemaVersion;

src/SmartCode.Generator/RazorTemplates/CSharp/Repository.cshtml

+43-2
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,31 @@
66
var project = Model.Project;
77
var buildTask = Model.Build;
88
var table = Model.GetCurrentTable();
9-
var primaryKeyType = table.PKColumn?.LanguageType ?? "int";
9+
var pkCol = table.PKColumn;
10+
if (pkCol == null)
11+
{
12+
throw new SmartCodeException($"Repository Template can not find PKColumn,Table:{table.Name}.");
13+
}
14+
var primaryKeyType = pkCol.LanguageType ?? "int";
1015
var autoIncrement = table.AutoIncrement;
1116
var dbSource = Model.GetDataSource<DbSource>();
17+
18+
var PKNameEQId = pkCol.Name == "Id";
1219
}
1320

1421
using System;
1522
using System.Collections.Generic;
1623
using System.Data;
1724
using System.Text;
25+
using System.Threading.Tasks;
1826
using SmartSql.DyRepository;
1927
using SmartSql.DyRepository.Annotations;
2028
using @(project.Module).Entity;
2129

2230
namespace @(project.Module).@buildTask.Module
2331
{
24-
public interface I@(table.ConvertedName)Repository : IRepository<@(table.ConvertedName), @(primaryKeyType)>
32+
public interface I@(table.ConvertedName)Repository : IRepository<@(table.ConvertedName), @(primaryKeyType)>
33+
,IRepositoryAsync<@(table.ConvertedName), @(primaryKeyType)>
2534
{
2635
@if (dbSource.DbRepository.DbProvider == DbProvider.PostgreSql
2736
&& autoIncrement)
@@ -30,11 +39,43 @@ namespace @(project.Module)[email protected]
3039
@Html.NewLine()
3140
@Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
3241
@Html.NewLine()
42+
43+
<text>[Statement(Execute = ExecuteBehavior.QuerySingle)]</text>
44+
@Html.NewLine()
45+
@Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text>
46+
@Html.NewLine()
3347
}
3448
else if (autoIncrement && primaryKeyType != "int")
3549
{
3650
@Html.PadLeft(8) <text>new @primaryKeyType Insert(@table.ConvertedName entity);</text>
3751
@Html.NewLine()
52+
53+
@Html.PadLeft(8) <text>new Task<@primaryKeyType> InsertAsync(@table.ConvertedName entity);</text>
54+
@Html.NewLine()
55+
}
56+
57+
@if (!PKNameEQId)
58+
{
59+
<text>[Statement(Id = "GetEntity")]</text>
60+
@Html.NewLine()
61+
@Html.PadLeft(8) <text>new @table.ConvertedName GetById([Param("@pkCol.Name")]@primaryKeyType id);</text>
62+
@Html.NewLine()
63+
64+
<text>[Statement(Id = "GetEntity")]</text>
65+
@Html.NewLine()
66+
@Html.PadLeft(8) <text>new Task<@table.ConvertedName> GetByIdAsync([Param("@pkCol.Name")]@primaryKeyType id);</text>
67+
@Html.NewLine()
68+
69+
<text>[Statement(Id = "Delete")]</text>
70+
@Html.NewLine()
71+
@Html.PadLeft(8) <text>new int DeleteById([Param("@pkCol.Name")]@primaryKeyType id);</text>
72+
@Html.NewLine()
73+
74+
<text>[Statement(Id = "Delete")]</text>
75+
@Html.NewLine()
76+
@Html.PadLeft(8) <text>new Task<@("int")> DeleteByIdAsync([Param("@pkCol.Name")]@primaryKeyType id);</text>
77+
@Html.NewLine()
78+
3879
}
3980
}
4081
}

src/SmartCode.Generator/RazorTemplates/CSharp/Service.cshtml

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
Layout = "_CSharpLayout.cshtml";
55
var project = Model.Project;
66
var table = Model.GetCurrentTable();
7-
8-
var pkType = table.PKColumn.LanguageType;
7+
var pkCol = table.PKColumn;
8+
if (pkCol == null)
9+
{
10+
throw new SmartCodeException($"Service Template can not find PKColumn,Table:{table.Name}.");
11+
}
12+
var pkType = pkCol.LanguageType;
913
var insertRetType = table.AutoIncrement ? pkType : "int";
1014
var entityName = table.ConvertedName;
1115
var entityCamelName = NamingUtil.CamelCase(table.ConvertedName);

0 commit comments

Comments
 (0)