Skip to content

Commit bb1c6e0

Browse files
committed
# 增加网关终结点开发
# 优化代码 # 调整设计,引入sqlsugar仓储注入
1 parent 0df4773 commit bb1c6e0

File tree

37 files changed

+4493
-1071
lines changed

37 files changed

+4493
-1071
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints.Services;
3+
using ZhangPengFei.IoT.Common.Model.GateWay;
4+
5+
namespace ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints;
6+
7+
public static class GateWayEndPoint
8+
{
9+
public static void MapAddGateWayEndPoint(this WebApplication app)
10+
{
11+
var api = app.MapGroup("/api");
12+
var productApi = api.MapGroup("/gateway").WithGroupName("添加网关");
13+
productApi.MapPost("/add", async (GateWayService service, [FromBody] GateWay gateWay) =>
14+
{
15+
if (await service.AddGateWayAsync(gateWay))
16+
{
17+
return Results.Json(new { GateWayId = gateWay.Id });
18+
}
19+
20+
return Results.BadRequest();
21+
});
22+
}
23+
24+
public static void MapDeleteGateWayEndPoint(this WebApplication app)
25+
{
26+
var api = app.MapGroup("/api");
27+
var productApi = api.MapGroup("/gateway").WithGroupName("删除网关");
28+
productApi.MapPost("/delete", async (GateWayService service, [FromBody] GateWay gateWay) =>
29+
{
30+
if (await service.DeleteGateWayAsync(gateWay))
31+
{
32+
return Results.Json(new { GateWayId = gateWay.Id });
33+
}
34+
35+
return Results.BadRequest();
36+
});
37+
}
38+
39+
public static void MapGetGateWaysEndPoint(this WebApplication app)
40+
{
41+
var api = app.MapGroup("/api");
42+
var productApi = api.MapGroup("/gateway").WithGroupName("获取网关列表");
43+
productApi.MapGet("/list",
44+
async (GateWayService service) => Results.Json(new { data = await service.ListGateWayAsync() }));
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using ZhangPengFei.IoT.Common;
2+
using ZhangPengFei.IoT.Common.Model.GateWay;
3+
4+
namespace ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints.Services;
5+
6+
public class GateWayService(Repository<GateWay> gateRepository)
7+
{
8+
Repository<GateWay> _gateRepository = gateRepository;
9+
10+
public async Task<bool> AddGateWayAsync(GateWay gateWay)
11+
{
12+
return await _gateRepository.InsertOrUpdateAsync(gateWay);
13+
}
14+
15+
public async Task<List<GateWay>> ListGateWayAsync()
16+
{
17+
return await _gateRepository.GetListAsync();
18+
}
19+
20+
public async Task<bool> DeleteGateWayAsync(GateWay gateWay)
21+
{
22+
return await _gateRepository.DeleteAsync(gateWay);
23+
}
24+
}

AspireIoT/ZhangPengFei.IoT.ApiService/Program.cs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
using ZhangPengFei.IoT.ApiService.EndPoints.DevicesEndPoints;
22
using ZhangPengFei.IoT.ApiService.EndPoints.DevicesEndPoints.Services;
3+
using ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints;
4+
using ZhangPengFei.IoT.ApiService.EndPoints.GateWayEndPoints.Services;
5+
using ZhangPengFei.IoT.Common;
36

47
var builder = WebApplication.CreateSlimBuilder();
5-
builder.Services.AddCors();
6-
// Add service defaults & Aspire components.
8+
// 注入仓储
9+
builder.Services.AddScoped(typeof(Repository<>));
710
builder.AddServiceDefaults();
811
builder.Services.AddTransient<DeviceService>();
12+
builder.Services.AddTransient<GateWayService>();
13+
14+
15+
// add cor
16+
builder.Services.AddCors();
17+
// Add service defaults & Aspire components.
18+
919
// Add services to the container.
1020
builder.Services.AddProblemDetails();
1121

@@ -14,9 +24,14 @@
1424
builder => builder.WithOrigins("http://localhost:8848").AllowAnyMethod().AllowAnyHeader().AllowCredentials());
1525
// Configure the HTTP request pipeline.
1626
app.UseExceptionHandler();
17-
// 设备注册
18-
27+
// 设备注册 [废弃]
1928
app.MapDeviceRegisterEndPoint();
29+
// 添加网关
30+
app.MapAddGateWayEndPoint();
31+
// 删除网关
32+
app.MapDeleteGateWayEndPoint();
33+
// 获取网关列表
34+
app.MapGetGateWaysEndPoint();
2035

2136
app.MapDefaultEndpoints();
2237

AspireIoT/ZhangPengFei.IoT.ApiService/ZhangPengFei.IoT.ApiService.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<ProjectReference Include="..\ZhangPengFei.IoT.Common\ZhangPengFei.IoT.Common.csproj" />
1011
<ProjectReference Include="..\ZhangPengFei.IoT.ServiceDefaults\ZhangPengFei.IoT.ServiceDefaults.csproj" />
1112
<PackageReference Include="Google.Protobuf" Version="3.26.1" />
1213
<PackageReference Include="Grpc.Net.Client" Version="2.62.0" />

AspireIoT/ZhangPengFei.IoT.ApiService/obj/Debug/net8.0/ZhangPengFei.IoT.ApiService.AssemblyInfo.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
[assembly: System.Reflection.AssemblyCompanyAttribute("ZhangPengFei.IoT.ApiService")]
1414
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
1515
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
16-
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+ef79f5f2699be4b80320f850f3aac9ee3c0f9bac")]
16+
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+0df4773f019ad83aa18ac0102d0e96638ca1398d")]
1717
[assembly: System.Reflection.AssemblyProductAttribute("ZhangPengFei.IoT.ApiService")]
1818
[assembly: System.Reflection.AssemblyTitleAttribute("ZhangPengFei.IoT.ApiService")]
1919
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
305ca8630b8d8598c880c42c2edcdff10e59e3bbf23621ef0d263aab0396de32
1+
b9236d0717ec6096202653e7a3602cf9fecce322b97ce6acbe25fec5b5ba71e1

AspireIoT/ZhangPengFei.IoT.ApiService/obj/ZhangPengFei.IoT.ApiService.csproj.nuget.dgspec.json

+65
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"net8.0": {
3030
"targetAlias": "net8.0",
3131
"projectReferences": {
32+
"C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\ZhangPengFei.IoT.Common.csproj": {
33+
"projectPath": "C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\ZhangPengFei.IoT.Common.csproj"
34+
},
3235
"C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.ServiceDefaults\\ZhangPengFei.IoT.ServiceDefaults.csproj": {
3336
"projectPath": "C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.ServiceDefaults\\ZhangPengFei.IoT.ServiceDefaults.csproj"
3437
}
@@ -81,6 +84,68 @@
8184
}
8285
}
8386
},
87+
"C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\ZhangPengFei.IoT.Common.csproj": {
88+
"version": "1.0.0",
89+
"restore": {
90+
"projectUniqueName": "C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\ZhangPengFei.IoT.Common.csproj",
91+
"projectName": "ZhangPengFei.IoT.Common",
92+
"projectPath": "C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\ZhangPengFei.IoT.Common.csproj",
93+
"packagesPath": "C:\\Users\\doudo\\.nuget\\packages\\",
94+
"outputPath": "C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.Common\\obj\\",
95+
"projectStyle": "PackageReference",
96+
"configFilePaths": [
97+
"C:\\Users\\doudo\\AppData\\Roaming\\NuGet\\NuGet.Config",
98+
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
99+
],
100+
"originalTargetFrameworks": [
101+
"net8.0"
102+
],
103+
"sources": {
104+
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
105+
"C:\\Program Files\\dotnet\\library-packs": {},
106+
"https://api.nuget.org/v3/index.json": {}
107+
},
108+
"frameworks": {
109+
"net8.0": {
110+
"targetAlias": "net8.0",
111+
"projectReferences": {}
112+
}
113+
},
114+
"warningProperties": {
115+
"warnAsError": [
116+
"NU1605"
117+
]
118+
}
119+
},
120+
"frameworks": {
121+
"net8.0": {
122+
"targetAlias": "net8.0",
123+
"dependencies": {
124+
"SqlSugarCore": {
125+
"target": "Package",
126+
"version": "[5.1.4.153-preview23, )"
127+
}
128+
},
129+
"imports": [
130+
"net461",
131+
"net462",
132+
"net47",
133+
"net471",
134+
"net472",
135+
"net48",
136+
"net481"
137+
],
138+
"assetTargetFallback": true,
139+
"warn": true,
140+
"frameworkReferences": {
141+
"Microsoft.NETCore.App": {
142+
"privateAssets": "all"
143+
}
144+
},
145+
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\8.0.300-preview.24203.14/PortableRuntimeIdentifierGraph.json"
146+
}
147+
}
148+
},
84149
"C:\\Users\\doudo\\RiderProjects\\Aspire.IoT\\AspireIoT\\ZhangPengFei.IoT.ServiceDefaults\\ZhangPengFei.IoT.ServiceDefaults.csproj": {
85150
"version": "1.0.0",
86151
"restore": {

AspireIoT/ZhangPengFei.IoT.ApiService/obj/ZhangPengFei.IoT.ApiService.csproj.nuget.g.targets

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<Import Project="$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\8.0.1\buildTransitive\net6.0\Microsoft.Extensions.Logging.Abstractions.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\8.0.1\buildTransitive\net6.0\Microsoft.Extensions.Logging.Abstractions.targets')" />
77
<Import Project="$(NuGetPackageRoot)microsoft.extensions.configuration.binder\8.0.1\buildTransitive\netstandard2.0\Microsoft.Extensions.Configuration.Binder.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.configuration.binder\8.0.1\buildTransitive\netstandard2.0\Microsoft.Extensions.Configuration.Binder.targets')" />
88
<Import Project="$(NuGetPackageRoot)microsoft.extensions.telemetry.abstractions\8.3.0\buildTransitive\net6.0\Microsoft.Extensions.Telemetry.Abstractions.targets" Condition="Exists('$(NuGetPackageRoot)microsoft.extensions.telemetry.abstractions\8.3.0\buildTransitive\net6.0\Microsoft.Extensions.Telemetry.Abstractions.targets')" />
9+
<Import Project="$(NuGetPackageRoot)sqlitepclraw.lib.e_sqlite3\2.1.6\buildTransitive\net8.0\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('$(NuGetPackageRoot)sqlitepclraw.lib.e_sqlite3\2.1.6\buildTransitive\net8.0\SQLitePCLRaw.lib.e_sqlite3.targets')" />
910
<Import Project="$(NuGetPackageRoot)grpc.tools\2.62.0\build\Grpc.Tools.targets" Condition="Exists('$(NuGetPackageRoot)grpc.tools\2.62.0\build\Grpc.Tools.targets')" />
1011
</ImportGroup>
1112
</Project>

0 commit comments

Comments
 (0)