Files
laatzen/LaaProductionWeb/LaaProduction.Web/Areas/Personalization/Controllers/SoftwareController.cs
T

328 lines
10 KiB
C#

namespace LaaProduction.Web.Areas.Personalization.Controllers
{
using LaaProduction.Personalization;
using LaaProduction.Web.App_Infrastructure;
using LaaProduction.Web.Areas.Personalization.Models.Software;
using LaaProduction.Services.Interfaces;
using System;
using System.Linq;
using System.Web.Mvc;
[RouteArea("Personalization")]
[AllowedRoles(UsersRoles.WEB_Admin)]
public class SoftwareController : Controller
{
private readonly ISoftwareService softwareService;
public SoftwareController()
=> this.softwareService = ServiceProvider.GetService<ISoftwareService>();
[HttpGet]
public ActionResult Index()
{
var model = new SoftwareModel
{
SoftwareInfo = this.softwareService
.GetSoftwares()
.Select(x => new SoftwareOverviewModel
{
AppId = x.AppId,
Name = x.Name
.Split('.')
.LastOrDefault(),
Versions = x.Versions,
Functions = x.Functions
})
};
return this.View(model);
}
[HttpGet]
public ActionResult SoftwareVersions(short? id)
{
var model = new SoftwareVersionsModel();
if (id.HasValue)
{
var appId = id.Value;
model = this.softwareService
.GetSoftwareOverview(appId)
.Map(x => new SoftwareVersionsModel
{
AppId = x.AppId,
Name = x.Name
.Split('.')
.LastOrDefault(),
Versions = x.Versions,
Functions = x.Functions
});
if (model != null)
{
model.SoftwareVersions = this.softwareService
.GetSoftwareVersions(appId)
.Select(x => new SoftwareVersionModel
{
AppId = x.AppId,
BuildV = x.BuildV,
Description = x.Description,
FullName = x.FullName,
Id = x.Id,
MajorV = x.MajorV,
MinorV = x.MinorV,
ValidTo = x.ValidTo
});
}
}
return this.View(model);
}
[HttpGet]
public ActionResult SoftwareFunctions(short? id)
{
var appId = id ?? 0;
var model = new SoftwareFunctionsModel();
if (appId > 0)
{
model = this.softwareService
.GetSoftwareOverview(appId)
.Map(x => new SoftwareFunctionsModel
{
AppId = x.AppId,
Name = x.Name
.Split('.')
.LastOrDefault(),
Versions = x.Versions,
Functions = x.Functions
}) ?? model;
model.SoftwareFunctions = this.softwareService
.GetSoftwareFunctions(appId)
.Select(x => new SoftwareFunctionModel
{
Id = x.Id,
AppId = x.AppId,
Function = x.Function
});
}
else
{
model.SoftwareFunctions = this.softwareService
.GetAllSoftwareFunctions()
.Select(x => new SoftwareFunctionModel
{
Id = x.Id,
AppId = x.AppId,
Function = x.Function
});
}
model.Users = this.softwareService
.GetUsersWithFunctions()
.Select(x => new UserFunctions
{
Id = x.Id,
FirstName = x.FirstName,
LastName = x.LastName,
Username = x.Username,
FunctionsCount = x.FunctionsCount,
HasPassword = x.HasPassword,
Functions = x.Functions
?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
?.Select(f => int.TryParse(f, out int funcId) ? funcId : 0)
?.Where(f => f > 0)
?? Array.Empty<int>()
});
return this.View(model);
}
[HttpPost]
public ActionResult SoftwareFunctions(FunctionUsersInputModel model)
{
if (this.ModelState.IsValid)
{
this.softwareService.UpdateUsersFunctions(x =>
{
x.FunctionId = model.FunctionId.Value;
x.Users = model.Users;
});
}
return this.Redirect(this.Request.UrlReferrer.ToString());
}
[HttpGet]
public ActionResult SoftwareListGroup()
{
var softwareList = this.softwareService
.GetSoftwares()
.Select(x => new SoftwareOverviewModel
{
AppId = x.AppId,
Name = x.Name
.Split('.')
.LastOrDefault(),
Versions = x.Versions,
Functions = x.Functions
});
return this.PartialView(softwareList);
}
[HttpPost]
public string AddFunction(AddFunctionModel model)
{
var appId = model.AppId.Value;
if (this.ModelState.IsValid)
{
this.softwareService.AddFunction(appId, model.Function);
}
return this.ModelState
.SelectMany(x => x.Value.Errors.Select(e => e.ErrorMessage))
.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
}
[HttpPost]
public string EditFunction(EditFunctionModel model)
{
if (this.ModelState.IsValid)
{
if (string.IsNullOrWhiteSpace(model.Function))
{
this.softwareService.DeleteFunction(model.FuncId.Value);
}
else
{
this.softwareService.UpdateFunction(model.FuncId.Value, model.Function);
}
}
return this.ModelState
.SelectMany(x => x.Value.Errors.Select(e => e.ErrorMessage))
.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
}
[HttpGet]
public ActionResult Users()
{
var usersModel = this.softwareService
.GetUsers()
.Select(x => new SoftwareUser
{
Id = x.Id,
FirstName = x.FirstName,
LastName = x.LastName,
Username = x.Username,
FunctionsCount = x.FunctionsCount,
HasPassword = x.HasPassword
});
return this.View(usersModel);
}
[HttpPost]
public ActionResult Users(string username)
{
var usersModel = this.softwareService
.GetUsers(username)
.Select(x => new SoftwareUser
{
Id = x.Id,
FirstName = x.FirstName,
LastName = x.LastName,
Username = x.Username,
FunctionsCount = x.FunctionsCount,
HasPassword = x.HasPassword
});
return this.View(usersModel);
}
[HttpGet]
public ActionResult UpdatePWDS()
{
this.softwareService.UpdatePasswordHashes();
return this.RedirectToAction(nameof(Users));
}
[HttpPost]
public bool ResetPWD(short id)
{
this.softwareService.ResetPassword(id);
return true;
}
[HttpGet]
public ActionResult UsersFunctions(int? id, string username)
{
object model;
if (id > 0)
{
model = this.UsersFunctionsModel(id: id.Value, username: $"{id}");
}
else
{
model = this.UsersFunctionsModel(username: username);
}
return this.View(model);
}
[HttpPost]
public ActionResult UsersFunctions(UsersFunctionsInputModel model)
{
if (this.ModelState.IsValid)
{
this.softwareService.UpdateUsersFunctions(x =>
{
x.UserId = model.UserId.Value;
x.Functions = model.Functions;
});
}
return this.Redirect(this.Request.UrlReferrer.ToString());
}
private UsersFunctionsModel UsersFunctionsModel(int id = -1, string username = null)
=> new UsersFunctionsModel
{
Functions = this.softwareService
.GetAllSoftwareFunctions()
.Select(x => new SoftwareFunctionModel
{
AppId = x.AppId,
Function = x.Function,
Id = x.Id,
}),
UsersFunctions = this.softwareService
.GetUsersWithFunctions(id, username)
.Select(x => new UserFunctions
{
Id = x.Id,
FirstName = x.FirstName,
LastName = x.LastName,
Username = x.Username,
FunctionsCount = x.FunctionsCount,
HasPassword = x.HasPassword,
Functions = x.Functions
?.Split(new[] { '-' }, StringSplitOptions.RemoveEmptyEntries)
?.Select(f => int.TryParse(f, out int funcId) ? funcId : 0)
?.Where(f => f > 0)
?? Array.Empty<int>()
})
};
}
}