Skip to content

Commit

Permalink
Merge pull request #190 from dotnetcore/dev
Browse files Browse the repository at this point in the history
Refactor api.AppController
  • Loading branch information
kklldog authored Sep 24, 2024
2 parents 5792cad + d1c2171 commit 1cc5196
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 72 deletions.
80 changes: 13 additions & 67 deletions src/AgileConfig.Server.Apisite/Controllers/api/AppController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using AgileConfig.Server.Apisite.Controllers.api.Models;
using AgileConfig.Server.Apisite.Filters;
using AgileConfig.Server.Apisite.Models;
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.Data.Entity;
using AgileConfig.Server.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AgileConfig.Server.Apisite.Models.Mapping;

namespace AgileConfig.Server.Apisite.Controllers.api
{
Expand All @@ -20,31 +19,21 @@ namespace AgileConfig.Server.Apisite.Controllers.api
public class AppController : Controller
{
private readonly IConfigService _configService;
private readonly ITinyEventBus _tinyEventBus;
private readonly IAppService _appService;
private readonly IPremissionService _premissionService;
private readonly IUserService _userService;

private readonly Controllers.AppController _appController;
private readonly Controllers.ConfigController _configController;

public AppController(IAppService appService,
IPremissionService premissionService,
IUserService userService,
IConfigService configService,
ITinyEventBus tinyEventBus,

Controllers.AppController _appController,
Controllers.ConfigController _configController)
Controllers.AppController appController,
Controllers.ConfigController configController)
{
_appService = appService;
_premissionService = premissionService;
_userService = userService;
_configService = configService;
_tinyEventBus = tinyEventBus;

this._appController = _appController;
this._configController = _configController;
_appController = appController;
_configController = configController;
}

/// <summary>
Expand All @@ -55,17 +44,7 @@ public AppController(IAppService appService,
public async Task<ActionResult<IEnumerable<ApiAppVM>>> GetAll()
{
var apps = await _appService.GetAllAppsAsync();
var vms = apps.Select(x =>
{
return new ApiAppVM
{
Id = x.Id,
Name = x.Name,
Secret = x.Secret,
Inheritanced = x.Type == AppType.Inheritance,
Enabled = x.Enabled,
};
});
var vms = apps.Select(x => x.ToApiAppVM());

return Json(vms);
}
Expand All @@ -84,16 +63,7 @@ public async Task<ActionResult<ApiAppVM>> GetById(string id)
if (obj.success)
{
AppVM appVM = obj.data;
return Json(new ApiAppVM
{
Id = appVM.Id,
Name = appVM.Name,
Secret = appVM.Secret,
Inheritanced = appVM.Inheritanced,
Enabled = appVM.Enabled,
InheritancedApps = appVM.inheritancedApps,
AppAdmin = appVM.AppAdmin
});
return Json(appVM.ToApiAppVM());
}

Response.StatusCode = 400;
Expand Down Expand Up @@ -126,14 +96,7 @@ public async Task<IActionResult> Add([FromBody] ApiAppVM model)

_appController.ControllerContext.HttpContext = HttpContext;

var result = (await _appController.Add(new AppVM
{
Id = model.Id,
Name = model.Name,
Secret = model.Secret,
AppAdmin = model.AppAdmin,
Inheritanced = model.Inheritanced
})) as JsonResult;
var result = (await _appController.Add(model.ToAppVM())) as JsonResult;

dynamic obj = result.Value;

Expand Down Expand Up @@ -174,14 +137,7 @@ public async Task<IActionResult> Edit(string id, [FromBody] ApiAppVM model)
_appController.ControllerContext.HttpContext = HttpContext;

model.Id = id;
var result = (await _appController.Edit(new AppVM
{
Id = model.Id,
Name = model.Name,
Secret = model.Secret,
AppAdmin = model.AppAdmin,
Inheritanced = model.Inheritanced
})) as JsonResult;
var result = (await _appController.Edit(model.ToAppVM())) as JsonResult;

dynamic obj = result.Value;
if (obj.success == true)
Expand Down Expand Up @@ -262,25 +218,15 @@ public async Task<IActionResult> Publish(string appId, EnvString env)
/// <returns></returns>
[TypeFilter(typeof(AdmBasicAuthenticationAttribute))]
[HttpGet("Publish_History")]
public async Task<ActionResult<IEnumerable<ApiPublishTimelineVM>>> PublishHistory(string appId, string env)
public async Task<ActionResult<IEnumerable<ApiPublishTimelineVM>>> PublishHistory(string appId, EnvString env)
{
ArgumentNullException.ThrowIfNullOrEmpty(appId);

ISettingService.IfEnvEmptySetDefault(ref env);
ArgumentException.ThrowIfNullOrEmpty(appId);

var history = await _configService.GetPublishTimelineHistoryAsync(appId, env);
var history = await _configService.GetPublishTimelineHistoryAsync(appId, env.Value);

history = history.OrderByDescending(x => x.Version).ToList();

var vms = history.Select(x => new ApiPublishTimelineVM
{
Id = x.Id,
Version = x.Version,
Log = x.Log,
PublishTime = x.PublishTime,
PublishUserId = x.PublishUserId,
Env = x.Env
});
var vms = history.Select(x => x.ToApiPublishTimelimeVM());

return Json(vms);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using AgileConfig.Server.Common.EventBus;
using AgileConfig.Server.IService;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Threading.Tasks;
using Agile.Config.Protocol;
using AgileConfig.Server.Common;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using AgileConfig.Server.Common.EventBus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using AgileConfig.Server.Data.Entity;
using System;
using AgileConfig.Server.Apisite.Controllers.api.Models;
using Google.Protobuf.WellKnownTypes;

namespace AgileConfig.Server.Apisite.Models.Mapping
{
/// <summary>
/// Do not ask me why not use AutoMapper, I don't know. Just like mannual mapping, it's simple and clear.
/// Do not ask me why not use AutoMapper, I don't know. Just like manual mapping, it's simple and clear.
/// </summary>
public static class ModelMappingExtension
public static class AppExtension
{
public static AppVM ToAppVM(this App app)
{
Expand Down Expand Up @@ -53,6 +55,29 @@ public static AppListVM ToAppListVM(this App app)
return vm;
}

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

return new ApiAppVM
{
Id = vm.Id,
Name = vm.Name,
Secret = vm.Secret,
Inheritanced = vm.Type == AppType.Inheritance,
Enabled = vm.Enabled,
AppAdmin = vm.AppAdmin,
Group = vm.Group,
CreateTime = vm.CreateTime
};
}
}

public static class AppVMExtension
{
public static App ToApp(this AppVM vm)
{
if (vm == null)
Expand Down Expand Up @@ -94,5 +119,69 @@ public static App ToApp(this AppVM vm, App app)

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)
{
if (timeline == null)
{
return null;
}

return new ApiPublishTimelineVM
{
Id = timeline.Id,
Version = timeline.Version,
AppId = timeline.AppId,
Log = timeline.Log,
PublishTime = timeline.PublishTime,
PublishUserId = timeline.PublishUserId,
Env = timeline.Env
};
}
}

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
};
}
}
}

0 comments on commit 1cc5196

Please sign in to comment.