Skip to content

Commit

Permalink
Merge pull request #191 from dotnetcore/dev
Browse files Browse the repository at this point in the history
Refactor Node API
  • Loading branch information
kklldog authored Sep 28, 2024
2 parents 1cc5196 + dec81ec commit a7fa882
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 105 deletions.
24 changes: 21 additions & 3 deletions src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiAppVM.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using AgileConfig.Server.Apisite.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Data.Entity;
using NuGet.Common;

namespace AgileConfig.Server.Apisite.Controllers.api.Models
{
Expand Down Expand Up @@ -46,4 +43,25 @@ public class ApiAppVM : IAppModel

public DateTime CreateTime { get; set; }
}

public static class ApiAppVMExtension
{
public static AppVM ToAppVM(this ApiAppVM vm)
{
if (vm == null)
{
return null;
}

return new AppVM
{
Id = vm.Id,
Name = vm.Name,
Secret = vm.Secret,
AppAdmin = vm.AppAdmin,
Inheritanced = vm.Inheritanced,
Group = vm.Group
};
}
}
}
25 changes: 22 additions & 3 deletions src/AgileConfig.Server.Apisite/Controllers/api/Models/ApiNodeVM.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using AgileConfig.Server.Data.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Models;

namespace AgileConfig.Server.Apisite.Controllers.api.Models
{
Expand All @@ -25,4 +23,25 @@ public class ApiNodeVM
/// </summary>
public DateTime? LastEchoTime { get; set; }
}

public static class ApiNodeVMExtension
{
public static ServerNodeVM ToServerNodeVM(this ApiNodeVM node)
{
if (node == null)
{
return null;
}

var vm = new ServerNodeVM
{
Address = node.Address,
Remark = node.Remark,
LastEchoTime = node.LastEchoTime,
Status = node.Status
};

return vm;
}
}
}
29 changes: 9 additions & 20 deletions src/AgileConfig.Server.Apisite/Controllers/api/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Models.Mapping;

namespace AgileConfig.Server.Apisite.Controllers.api
{
Expand Down Expand Up @@ -43,13 +44,7 @@ public async Task<ActionResult<IEnumerable<ApiNodeVM>>> GetAll()
{
var nodes = await _serverNodeService.GetAllNodesAsync();

var vms = nodes.Select(x => new ApiNodeVM
{
Address = x.Id,
Remark = x.Remark,
LastEchoTime = x.LastEchoTime,
Status = x.Status
});
var vms = nodes.Select(x => x.ToApiNodeVM());

return Json(vms);
}
Expand Down Expand Up @@ -77,24 +72,18 @@ public async Task<IActionResult> Add([FromBody] ApiNodeVM model)

var ctrl = new ServerNodeController(_serverNodeService, _sysLogService, _remoteServerNodeProxy, _tinyEventBus);
ctrl.ControllerContext.HttpContext = HttpContext;
var result = (await ctrl.Add(new ServerNodeVM
{
Address = model.Address,
Remark = model.Remark,
LastEchoTime = model.LastEchoTime,
Status = model.Status
})) as JsonResult;
var result = (await ctrl.Add(model.ToServerNodeVM())) as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success == true)
{
return Created("", "");
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand All @@ -112,16 +101,16 @@ public async Task<IActionResult> Delete([FromQuery] string address)
ctrl.ControllerContext.HttpContext = HttpContext;
var result = (await ctrl.Delete(new ServerNodeVM { Address = address })) as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
dynamic obj = result?.Value;
if (obj?.success == true)
{
return NoContent();
}

Response.StatusCode = 400;
return Json(new
{
obj.message
obj?.message
});
}

Expand Down
69 changes: 68 additions & 1 deletion src/AgileConfig.Server.Apisite/Models/AppVM.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using AgileConfig.Server.Data.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
Expand Down Expand Up @@ -46,4 +47,70 @@ public class AppListVM : AppVM

public List<AppListVM> children { get; set; }
}

public static class AppVMExtension
{
public static App ToApp(this AppVM vm)
{
if (vm == null)
{
return null;
}

var app = new App();
app.Id = vm.Id;
app.Name = vm.Name;
app.Secret = vm.Secret;
app.Enabled = vm.Enabled;
app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE;
app.AppAdmin = vm.AppAdmin;
app.Group = vm.Group;
app.CreateTime = vm.CreateTime;

return app;
}

public static App ToApp(this AppVM vm, App app)
{
if (vm == null)
{
return null;
}

app.Id = vm.Id;
app.Name = vm.Name;
app.Secret = vm.Secret;
app.Enabled = vm.Enabled;
app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE;
app.AppAdmin = vm.AppAdmin;
app.Group = vm.Group;
if (vm.CreateTime > DateTime.MinValue)
{
app.CreateTime = vm.CreateTime;
}

return app;
}

public static ApiAppVM ToApiAppVM(this AppVM vm)
{
if (vm == null)
{
return null;
}

return new ApiAppVM
{
Id = vm.Id,
Name = vm.Name,
Secret = vm.Secret,
Inheritanced = vm.Inheritanced,
Enabled = vm.Enabled,
InheritancedApps = vm.inheritancedApps,
AppAdmin = vm.AppAdmin,
Group = vm.Group,
CreateTime = vm.CreateTime
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using AgileConfig.Server.Data.Entity;
using System;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using Google.Protobuf.WellKnownTypes;

namespace AgileConfig.Server.Apisite.Models.Mapping
{
Expand Down Expand Up @@ -76,72 +74,6 @@ public static ApiAppVM ToApiAppVM(this App vm)
}
}

public static class AppVMExtension
{
public static App ToApp(this AppVM vm)
{
if (vm == null)
{
return null;
}

var app = new App();
app.Id = vm.Id;
app.Name = vm.Name;
app.Secret = vm.Secret;
app.Enabled = vm.Enabled;
app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE;
app.AppAdmin = vm.AppAdmin;
app.Group = vm.Group;
app.CreateTime = vm.CreateTime;

return app;
}

public static App ToApp(this AppVM vm, App app)
{
if (vm == null)
{
return null;
}

app.Id = vm.Id;
app.Name = vm.Name;
app.Secret = vm.Secret;
app.Enabled = vm.Enabled;
app.Type = vm.Inheritanced ? AppType.Inheritance : AppType.PRIVATE;
app.AppAdmin = vm.AppAdmin;
app.Group = vm.Group;
if (vm.CreateTime > DateTime.MinValue)
{
app.CreateTime = vm.CreateTime;
}

return app;
}

public static ApiAppVM ToApiAppVM(this AppVM vm)
{
if (vm == null)
{
return null;
}

return new ApiAppVM
{
Id = vm.Id,
Name = vm.Name,
Secret = vm.Secret,
Inheritanced = vm.Inheritanced,
Enabled = vm.Enabled,
InheritancedApps = vm.inheritancedApps,
AppAdmin = vm.AppAdmin,
Group = vm.Group,
CreateTime = vm.CreateTime
};
}
}

public static class PublishTimelineExtension
{
public static ApiPublishTimelineVM ToApiPublishTimelimeVM(this PublishTimeline timeline)
Expand All @@ -164,24 +96,23 @@ public static ApiPublishTimelineVM ToApiPublishTimelimeVM(this PublishTimeline t
}
}

public static class ApiAppVMExtension
public static class ServerNodeExtension
{
public static AppVM ToAppVM(this ApiAppVM vm)
public static ApiNodeVM ToApiNodeVM(this ServerNode node)
{
if (vm == null)
if (node == null)
{
return null;
}

return new AppVM
return new ApiNodeVM
{
Id = vm.Id,
Name = vm.Name,
Secret = vm.Secret,
AppAdmin = vm.AppAdmin,
Inheritanced = vm.Inheritanced,
Group = vm.Group
Address = node.Id,
Remark = node.Remark,
LastEchoTime = node.LastEchoTime,
Status = node.Status
};
}
}

}
1 change: 1 addition & 0 deletions src/AgileConfig.Server.Apisite/Models/ServerNodeVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public class ServerNodeVM
public NodeStatus Status { get; set; }
public DateTime? LastEchoTime { get; set; }
}

}

0 comments on commit a7fa882

Please sign in to comment.