laatzen/LaaProductionWeb/LaaProduction.Web/API/Personalization/EmployeesController.cs
2023-06-22 16:36:35 +02:00

123 lines
3.2 KiB
C#

namespace LaaProduction.Web.API.Personalization
{
using LaaProduction.Personalization.Interfaces;
using LaaProduction.Web.App_Infrastructure;
using LaaProductionDI;
using System;
using System.ComponentModel.DataAnnotations;
using System.Web.Http;
[AuthorizeBearer]
[RoutePrefix("api/Personalization/Employees")]
public class EmployeesController : ApiController
{
private readonly IAccountManager accountManager;
private readonly IEmployeesManager employeesManager;
public EmployeesController()
{
this.accountManager = LaaServiceProvider.GetService<IAccountManager>();
this.employeesManager = LaaServiceProvider.GetService<IEmployeesManager>();
}
[HttpGet]
public IHttpActionResult Get()
{
var employees = this.employeesManager.GetEmployees();
return this.Json(employees);
}
[HttpGet]
public IHttpActionResult Find(short id)
{
var employee = this.employeesManager.GetEmployee(id);
return this.Json(employee);
}
[HttpPost]
[Route(nameof(EditRoles))]
public IHttpActionResult EditRoles([FromBody] EditRolesModel model)
{
if (this.ModelState.IsValid)
{
this.employeesManager.UpdateEmployeeRoles(model.EmployeeId.Value, model.Roles);
}
return this.Json(true);
}
[HttpPost]
[Route(nameof(EditFunctions))]
public IHttpActionResult EditFunctions([FromBody] EditFunctionsModel model)
{
if (this.ModelState.IsValid)
{
this.employeesManager.UpdateEmployeeFunctions(model.EmployeeId.Value, model.Functions);
}
return this.Json(true);
}
[HttpGet]
[Route("ResetPassword/{id}")]
public IHttpActionResult ResetPassword(short id)
{
if (this.ModelState.IsValid)
{
this.accountManager.ResetPassword(id);
}
return this.Json(true);
}
[HttpPost]
[Route(nameof(SetPassword))]
public IHttpActionResult SetPassword(PasswordModel model)
{
if (this.ModelState.IsValid)
{
this.accountManager.SetPassword(model.EmployeeId.Value, model.Password);
}
return this.Json(true);
}
[HttpGet]
[Route(nameof(List))]
public IHttpActionResult List()
{
var employees = this.employeesManager.ListEmployees();
return this.Json(employees);
}
}
public class EditRolesModel
{
[Required]
public short? EmployeeId { get; set; }
public string[] Roles { get; set; } = Array.Empty<string>();
}
public class EditFunctionsModel
{
[Required]
public short? EmployeeId { get; set; }
public string[] Functions { get; set; } = Array.Empty<string>();
}
public class PasswordModel
{
[Required]
public short? EmployeeId { get; set; }
[Required]
public string Password { get; set; }
}
}