Skip to content

Commit

Permalink
Merge pull request #120 from DotNetDevs/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
matteobruni authored Mar 26, 2018
2 parents e946f24 + cea7ec5 commit 6b8bb68
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 132 deletions.
175 changes: 89 additions & 86 deletions Synology.TestWebApplication/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,90 +11,93 @@
using Synology.TestWebApplication.Models;

namespace Synology.TestWebApplication.Controllers
{
using IOFile = System.IO.File;

public class HomeController : Controller
{
private readonly IServiceProvider _serviceProvider;

public HomeController(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IActionResult Index()
{
if (!IOFile.Exists("synosettings.json")) return RedirectToAction(nameof(Settings));

var settings = JsonConvert.DeserializeObject<SettingsViewModel>(IOFile.ReadAllText("synosettings.json"));

return View();
}

public IActionResult Settings()
{
SettingsViewModel model = null;

if (IOFile.Exists("synosettings.json"))
model = JsonConvert.DeserializeObject<SettingsViewModel>(IOFile.ReadAllText("synosettings.json"));

return View(model);
}

[HttpPost]
public async Task<IActionResult> Settings(SettingsViewModel model)
{
if (ModelState.IsValid)
{
var settings = _serviceProvider.GetService<ISynologyConnectionSettings>();

settings.BaseHost = model.SynologyHost;
settings.Password = model.SynologyPass;
settings.Port = model.SynologyPort;
settings.Ssl = model.UseSsl;
settings.SslPort = model.SynologyPort;
settings.Username = model.SynologyUser;

using (var syno = _serviceProvider.GetService<ISynologyConnection>())
{
var result = await syno.Api().Auth().LoginAsync();

if (!result.Success && result.Error.Code != 403)
{
ModelState.AddModelError("", "Invalid connection settings.");
}
else
{
var json = JsonConvert.SerializeObject(model, Formatting.Indented);

IOFile.WriteAllText("synosettings.json", json);

return RedirectToAction("Index");
}
}
}

return View(model);
}

public IActionResult About()
{
ViewData["Message"] = "Your application description page.";

return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
{
using IOFile = System.IO.File;

public class HomeController : Controller
{
private readonly IServiceProvider _serviceProvider;

public HomeController(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}

public IActionResult Index()
{
if (!IOFile.Exists("synosettings.json")) return RedirectToAction(nameof(Settings));

var settings = JsonConvert.DeserializeObject<SettingsViewModel>(IOFile.ReadAllText("synosettings.json"));

return View();
}

public IActionResult Settings()
{
SettingsViewModel model = null;

if (IOFile.Exists("synosettings.json"))
model = JsonConvert.DeserializeObject<SettingsViewModel>(IOFile.ReadAllText("synosettings.json"));

return View(model);
}

[HttpPost]
public async Task<IActionResult> Settings(SettingsViewModel model)
{
if (ModelState.IsValid)
{
var settings = _serviceProvider.GetService<ISynologyConnectionSettings>();

settings.BaseHost = model.SynologyHost;
settings.Password = model.SynologyPass;
settings.Port = model.SynologyPort;
settings.Ssl = model.UseSsl;
settings.SslPort = model.SynologyPort;
settings.Username = model.SynologyUser;

using (var syno = _serviceProvider.GetService<ISynologyConnection>())
{
var result = await syno.Api().Auth().LoginAsync();

if (!result.Success && result.Error.Code != 403)
{
ModelState.AddModelError("", "Invalid connection settings.");
}
else
{
var json = JsonConvert.SerializeObject(model, Formatting.Indented);

IOFile.WriteAllText("synosettings.json", json);

return RedirectToAction("Index");
}
}
}

return View(model);
}

public IActionResult About()
{
ViewData["Message"] = "Your application description page.";

return View();
}

public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";

return View();
}

public IActionResult Error()
{
return View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
});
}
}
}
2 changes: 2 additions & 0 deletions Synology.TestWebApplication/Views/Home/Settings.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
@{
ViewData["Title"] = "Settings";
}
<div asp-validation-summary="All" class="text-danger">
</div>
<form asp-controller="Home" asp-action="Settings" method="post">
<div class="form-group">
<label asp-for="SynologyHost">Hostname</label>
Expand Down
61 changes: 33 additions & 28 deletions Synology/Classes/SynologyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,73 +31,78 @@ public SynologyBuilder(IServiceCollection services)
Services = services;
}

public IServiceCollection Services { get; }
private IServiceCollection Services { get; }

public ISynologyBuilder AddApi()
{
Services.AddTransient<IApi, Api.Api>();
Services.AddTransient<Api.Info.IInfoRequest, Api.Info.InfoRequest>();
Services.AddTransient<IAuthRequest, AuthRequest>();
Services.AddApi<IApi, Api.Api>();

Services.AddRequest<Api.Info.IInfoRequest, Api.Info.InfoRequest>();
Services.AddRequest<IAuthRequest, AuthRequest>();

return this;
}

public ISynologyBuilder AddAudioStation()
{
Services.AddTransient<IAudioStationApi, AudioStationApi>();
Services.AddApi<IAudioStationApi, AudioStationApi>();

return this;
}

public ISynologyBuilder AddDownloadStation()
{
Services.AddTransient<IDownloadStationApi, DownloadStationApi>();
Services.AddTransient<DownloadStation.Info.IInfoRequest, DownloadStation.Info.InfoRequest>();
Services.AddTransient<IScheduleRequest, ScheduleRequest>();
Services.AddTransient<DownloadStation.Task.ITaskRequest, DownloadStation.Task.TaskRequest>();
Services.AddApi<IDownloadStationApi, DownloadStationApi>();

Services.AddRequest<DownloadStation.Info.IInfoRequest, DownloadStation.Info.InfoRequest>();
Services.AddRequest<IScheduleRequest, ScheduleRequest>();
Services.AddRequest<DownloadStation.Task.ITaskRequest, DownloadStation.Task.TaskRequest>();

return this;
}

public ISynologyBuilder AddDownloadStation2()
{
Services.AddTransient<IDownloadStation2Api, DownloadStation2Api>();
Services.AddTransient<DownloadStation2.Task.ITaskRequest, DownloadStation2.Task.TaskRequest>();
Services.AddApi<IDownloadStation2Api, DownloadStation2Api>();

Services.AddRequest<DownloadStation2.Task.ITaskRequest, DownloadStation2.Task.TaskRequest>();

return this;
}

public ISynologyBuilder AddFileStation()
{
Services.AddTransient<IFileStationApi, FileStationApi>();
Services.AddTransient<FileStation.Info.IInfoRequest, FileStation.Info.InfoRequest>();
Services.AddTransient<ICheckPermissionRequest, CheckPermissionRequest>();
Services.AddTransient<ICopyMoveRequest, CopyMoveRequest>();
Services.AddTransient<IDirSizeRequest, DirSizeRequest>();
Services.AddTransient<IFavoriteRequest, FavoriteRequest>();
Services.AddTransient<IFileShareRequest, FileShareRequest>();
Services.AddTransient<IListRequest, ListRequest>();
Services.AddTransient<IMD5Request, MD5Request>();
Services.AddTransient<IRenameRequest, RenameRequest>();
Services.AddTransient<ISearchRequest, SearchRequest>();
Services.AddTransient<IThumbRequest, ThumbRequest>();
Services.AddTransient<IUploadRequest, UploadRequest>();
Services.AddTransient<IVirtualFolderRequest, VirtualFolderRequest>();
Services.AddApi<IFileStationApi, FileStationApi>();

Services.AddRequest<FileStation.Info.IInfoRequest, FileStation.Info.InfoRequest>();
Services.AddRequest<ICheckPermissionRequest, CheckPermissionRequest>();
Services.AddRequest<ICopyMoveRequest, CopyMoveRequest>();
Services.AddRequest<IDirSizeRequest, DirSizeRequest>();
Services.AddRequest<IFavoriteRequest, FavoriteRequest>();
Services.AddRequest<IFileShareRequest, FileShareRequest>();
Services.AddRequest<IListRequest, ListRequest>();
Services.AddRequest<IMD5Request, MD5Request>();
Services.AddRequest<IRenameRequest, RenameRequest>();
Services.AddRequest<ISearchRequest, SearchRequest>();
Services.AddRequest<IThumbRequest, ThumbRequest>();
Services.AddRequest<IUploadRequest, UploadRequest>();
Services.AddRequest<IVirtualFolderRequest, VirtualFolderRequest>();

return this;
}

public ISynologyBuilder AddSurveillanceStation()
{
Services.AddTransient<ISurveillanceStationApi, SurveillanceStationApi>();
Services.AddTransient<SurveillanceStation.Info.IInfoRequest, SurveillanceStation.Info.InfoRequest>();
Services.AddApi<ISurveillanceStationApi, SurveillanceStationApi>();

Services.AddRequest<SurveillanceStation.Info.IInfoRequest, SurveillanceStation.Info.InfoRequest>();

return this;
}

public ISynologyBuilder AddVideoStation()
{
Services.AddTransient<IVideoStationApi, VideoStationApi>();
Services.AddApi<IVideoStationApi, VideoStationApi>();

return this;
}
Expand Down
23 changes: 20 additions & 3 deletions Synology/Extensions/ServiceCollectionExtension.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Synology.Classes;
using Synology.Interfaces;
using Synology.Settings;
Expand Down Expand Up @@ -36,13 +38,28 @@ public static IServiceCollection AddSynology(this IServiceCollection services, A
if (configure == null) throw new ArgumentNullException(nameof(configure));

services.AddOptions();
services.AddSingleton<ISynologyConnectionSettings, SynologyConnectionSettings>();
services.AddSingleton<SidContainer, SidContainer>();
services.AddTransient<ISynologyConnection, SynologyConnection>();
services.AddScoped<ISynologyConnectionSettings, SynologyConnectionSettings>();
services.AddScoped<SidContainer>();
services.AddScoped<HttpClient>();
services.AddScoped<ISynologyConnection, SynologyConnection>(t => new SynologyConnection(t.GetService<ISynologyConnectionSettings>(), t.GetService<SidContainer>(), t.GetService<ILoggerFactory>(), t, t.GetService<HttpClient>()));

configure(new SynologyBuilder(services));

return services;
}

internal static IServiceCollection AddRequest<TRequestInterface, TRequest>(this IServiceCollection services)
where TRequestInterface : class, ISynologyRequest
where TRequest : SynologyRequest, TRequestInterface
{
return services.AddTransient<TRequestInterface, TRequest>();
}

internal static IServiceCollection AddApi<TApiInterface, TApi>(this IServiceCollection services)
where TApiInterface : class, ISynologyApi
where TApi : SynologyApi, TApiInterface
{
return services.AddTransient<TApiInterface, TApi>();
}
}
}
5 changes: 3 additions & 2 deletions Synology/Extensions/SynologyConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Synology.Classes;
using Synology.Interfaces;
Expand Down Expand Up @@ -183,14 +184,14 @@ private static async Task<T> GenericPostDataFromApiAsync<T>(this ISynologyConnec

internal static string GetSid(this ISynologyConnection connection)
{
var sidContainer = connection.ServiceProvider.GetService(typeof(SidContainer)) as SidContainer;
var sidContainer = connection.ServiceProvider.GetService<SidContainer>() as SidContainer;

return sidContainer?.Sid;
}

internal static void SetSid(this ISynologyConnection connection, string value)
{
if (connection.ServiceProvider.GetService(typeof(SidContainer)) is SidContainer sidContainer)
if (connection.ServiceProvider.GetService<SidContainer>() is SidContainer sidContainer)
sidContainer.Sid = value;
}
}
Expand Down
Loading

0 comments on commit 6b8bb68

Please sign in to comment.