Skip to content

Commit 4032abe

Browse files
authored
Merge pull request #192 from dotnetcore/dev
Refactor RegisterCenter API
2 parents a7fa882 + 24dd584 commit 4032abe

File tree

4 files changed

+78
-77
lines changed

4 files changed

+78
-77
lines changed

src/AgileConfig.Server.Apisite/Controllers/api/Models/ServiceInfoVM.cs renamed to src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiServiceInfoVM.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace AgileConfig.Server.Apisite.Controllers.api.Models
55
{
6-
public class ServiceInfoVM
6+
public class ApiServiceInfoVM
77
{
88
public string ServiceId { get; set; } = "";
99

@@ -20,6 +20,6 @@ public class ServiceInfoVM
2020

2121
public class QueryServiceInfoResultVM
2222
{
23-
public List<ServiceInfoVM> Data { get; set; }
23+
public List<ApiServiceInfoVM> Data { get; set; }
2424
}
2525
}

src/AgileConfig.Server.Apisite/Controllers/api/Models/RegisterServiceInfoVM.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using AgileConfig.Server.Data.Entity;
3+
using Newtonsoft.Json;
24

35
namespace AgileConfig.Server.Apisite.Controllers.api.Models
46
{
@@ -20,4 +22,27 @@ public class RegisterServiceInfoVM
2022

2123
public string HeartBeatMode { get; set; }
2224
}
25+
26+
public static class RegisterServiceInfoVMExtension
27+
{
28+
public static ServiceInfo ToServiceInfo(this RegisterServiceInfoVM model)
29+
{
30+
if (model == null)
31+
{
32+
return null;
33+
}
34+
35+
return new ServiceInfo
36+
{
37+
ServiceId = model.ServiceId,
38+
ServiceName = model.ServiceName,
39+
Ip = model.Ip,
40+
Port = model.Port,
41+
MetaData = model.MetaData is null ? "[]" : JsonConvert.SerializeObject(model.MetaData),
42+
CheckUrl = model.CheckUrl,
43+
AlarmUrl = model.AlarmUrl,
44+
HeartBeatMode = model.HeartBeatMode
45+
};
46+
}
47+
}
2348
}

src/AgileConfig.Server.Apisite/Controllers/api/RegisterCenterController.cs

Lines changed: 17 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
using System.Collections.Generic;
77
using System.Threading.Tasks;
88
using Agile.Config.Protocol;
9-
using Microsoft.Extensions.Logging;
10-
using Newtonsoft.Json;
9+
using AgileConfig.Server.Apisite.Models.Mapping;
1110
using AgileConfig.Server.Common.EventBus;
1211
using AgileConfig.Server.Event;
1312

@@ -23,32 +22,23 @@ public class RegisterCenterController : Controller
2322
private readonly IRegisterCenterService _registerCenterService;
2423
private readonly IServiceInfoService _serviceInfoService;
2524
private readonly ITinyEventBus _tinyEventBus;
26-
private readonly ILogger<RegisterCenterController> _logger;
2725

2826
public RegisterCenterController(IRegisterCenterService registerCenterService
2927
, IServiceInfoService serviceInfoService,
30-
ILoggerFactory loggerFactory,
3128
ITinyEventBus tinyEventBus
3229
)
3330
{
3431
_registerCenterService = registerCenterService;
3532
_serviceInfoService = serviceInfoService;
3633
_tinyEventBus = tinyEventBus;
37-
_logger = loggerFactory.CreateLogger<RegisterCenterController>();
3834
}
3935

4036
[HttpPost]
4137
public async Task<RegisterResultVM> Register([FromBody] RegisterServiceInfoVM model)
4238
{
43-
var entity = new ServiceInfo();
44-
entity.ServiceId = model.ServiceId;
45-
entity.ServiceName = model.ServiceName;
46-
entity.Ip = model.Ip;
47-
entity.Port = model.Port;
48-
entity.CheckUrl = model.CheckUrl;
49-
entity.AlarmUrl = model.AlarmUrl;
50-
entity.HeartBeatMode = model.HeartBeatMode;
51-
entity.MetaData = model.MetaData is null ? "[]" : JsonConvert.SerializeObject(model.MetaData);
39+
ArgumentNullException.ThrowIfNull(model);
40+
41+
var entity = model.ToServiceInfo();
5242
entity.RegisterWay = RegisterWay.Auto;
5343

5444
var id = await _registerCenterService.RegisterAsync(entity);
@@ -94,10 +84,7 @@ public async Task<ActionResult<RegisterResultVM>> UnRegister(string id, [FromBod
9484
[HttpPost("heartbeat")]
9585
public async Task<ActionResult<HeartbeatResultVM>> Heartbeat([FromBody] HeartbeatParam param)
9686
{
97-
if (param == null)
98-
{
99-
throw new ArgumentNullException(nameof(param));
100-
}
87+
ArgumentNullException.ThrowIfNull(param);
10188

10289
bool serviceHeartbeatResult = false;
10390
if (!string.IsNullOrEmpty(param.UniqueId))
@@ -120,89 +107,44 @@ public async Task<ActionResult<HeartbeatResultVM>> Heartbeat([FromBody] Heartbea
120107
}
121108

122109
[HttpGet("services")]
123-
public async Task<List<ServiceInfoVM>> AllServices()
110+
public async Task<List<ApiServiceInfoVM>> AllServices()
124111
{
125112
var services = await _serviceInfoService.GetAllServiceInfoAsync();
126-
var vms = new List<ServiceInfoVM>();
113+
var vms = new List<ApiServiceInfoVM>();
127114
foreach (var serviceInfo in services)
128115
{
129-
var vm = new ServiceInfoVM
130-
{
131-
ServiceId = serviceInfo.ServiceId,
132-
ServiceName = serviceInfo.ServiceName,
133-
Ip = serviceInfo.Ip,
134-
Port = serviceInfo.Port,
135-
MetaData = new List<string>(),
136-
Status = serviceInfo.Status
137-
};
138-
try
139-
{
140-
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
141-
}
142-
catch (Exception e)
143-
{
144-
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
145-
}
116+
var vm = serviceInfo.ToApiServiceInfoVM();
117+
146118
vms.Add(vm);
147119
}
148120

149121
return vms;
150122
}
151123

152124
[HttpGet("services/online")]
153-
public async Task<List<ServiceInfoVM>> OnlineServices()
125+
public async Task<List<ApiServiceInfoVM>> OnlineServices()
154126
{
155127
var services = await _serviceInfoService.GetOnlineServiceInfoAsync();
156-
var vms = new List<ServiceInfoVM>();
128+
var vms = new List<ApiServiceInfoVM>();
157129
foreach (var serviceInfo in services)
158130
{
159-
var vm = new ServiceInfoVM
160-
{
161-
ServiceId = serviceInfo.ServiceId,
162-
ServiceName = serviceInfo.ServiceName,
163-
Ip = serviceInfo.Ip,
164-
Port = serviceInfo.Port,
165-
MetaData = new List<string>(),
166-
Status = serviceInfo.Status
167-
};
168-
try
169-
{
170-
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
171-
}
172-
catch (Exception e)
173-
{
174-
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
175-
}
131+
var vm = serviceInfo.ToApiServiceInfoVM();
132+
176133
vms.Add(vm);
177134
}
178135

179136
return vms;
180137
}
181138

182139
[HttpGet("services/offline")]
183-
public async Task<List<ServiceInfoVM>> OfflineServices()
140+
public async Task<List<ApiServiceInfoVM>> OfflineServices()
184141
{
185142
var services = await _serviceInfoService.GetOfflineServiceInfoAsync();
186-
var vms = new List<ServiceInfoVM>();
143+
var vms = new List<ApiServiceInfoVM>();
187144
foreach (var serviceInfo in services)
188145
{
189-
var vm = new ServiceInfoVM
190-
{
191-
ServiceId = serviceInfo.ServiceId,
192-
ServiceName = serviceInfo.ServiceName,
193-
Ip = serviceInfo.Ip,
194-
Port = serviceInfo.Port,
195-
MetaData = new List<string>(),
196-
Status = serviceInfo.Status
197-
};
198-
try
199-
{
200-
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
201-
}
202-
catch (Exception e)
203-
{
204-
_logger.LogError(e, $"deserialize meta data error, serviceId:{serviceInfo.ServiceId}");
205-
}
146+
var vm = serviceInfo.ToApiServiceInfoVM();
147+
206148
vms.Add(vm);
207149
}
208150

src/AgileConfig.Server.Apisite/Models/Mapping/ModelMappingExtension.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using AgileConfig.Server.Data.Entity;
22
using AgileConfig.Server.Apisite.Controllers.api.Models;
3+
using System.Collections.Generic;
4+
using System;
5+
using Newtonsoft.Json;
36

47
namespace AgileConfig.Server.Apisite.Models.Mapping
58
{
@@ -115,4 +118,35 @@ public static ApiNodeVM ToApiNodeVM(this ServerNode node)
115118
}
116119
}
117120

121+
public static class ServiceInfoExtension
122+
{
123+
public static ApiServiceInfoVM ToApiServiceInfoVM(this ServiceInfo serviceInfo)
124+
{
125+
if (serviceInfo == null)
126+
{
127+
return null;
128+
}
129+
130+
var vm = new ApiServiceInfoVM
131+
{
132+
ServiceId = serviceInfo.ServiceId,
133+
ServiceName = serviceInfo.ServiceName,
134+
Ip = serviceInfo.Ip,
135+
Port = serviceInfo.Port,
136+
MetaData = new List<string>(),
137+
Status = serviceInfo.Status
138+
};
139+
140+
try
141+
{
142+
vm.MetaData = JsonConvert.DeserializeObject<List<string>>(serviceInfo.MetaData);
143+
}
144+
catch
145+
{
146+
}
147+
148+
return vm;
149+
}
150+
}
151+
118152
}

0 commit comments

Comments
 (0)