34c50889ef
- CanPerform field - Perform maintenance API
302 lines
13 KiB
C#
302 lines
13 KiB
C#
using Step.Database.Controllers;
|
|
using Step.Model.DatabaseModels;
|
|
using Step.Model.DTOModels.MaintenanceModels;
|
|
using Step.NC;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Web.Http;
|
|
using static CMS_CORE_Library.DataStructures;
|
|
using static Step.Model.Constants;
|
|
using static Step.Config.ServerConfig;
|
|
|
|
namespace Step.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/maintenance_manager")]
|
|
public class MaintenanceController : ApiController
|
|
{
|
|
[Route("maintenances"), HttpGet]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult GetMaintenances()
|
|
{
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
// Get list of maintenances
|
|
CmsError cmsError = ncHandler.GetMaintenances(out List<DTOMaintenanceModel> maintenances, Convert.ToInt32(userId.Value));
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(maintenances);
|
|
}
|
|
}
|
|
|
|
[Route("maintenance"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult AddMaintenance([Required]DTONewMaintenanceModel newMaint)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
MaintenanceModel dbMaint = maintenancesController.Create(newMaint, Convert.ToInt32(userId.Value));
|
|
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
CmsError cmsError = ncHandler.GetMaintenanceData(out DTOMaintenanceModel maintenance, dbMaint.MaintenanceId, Convert.ToInt32(userId.Value));
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(maintenance);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult EditMaintenance(int maintenanceId, [Required]DTOUpdateMaintenanceModel newMaint)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Find maintenance
|
|
MaintenanceModel dbMaint = maintenancesController.FindById(maintenanceId);
|
|
if (dbMaint == null)
|
|
return NotFound();
|
|
|
|
// Check if is created by CMS
|
|
if (dbMaint.UserId == null)
|
|
return Unauthorized();
|
|
|
|
using (MachinesUsersController machineUsersController = new MachinesUsersController())
|
|
{
|
|
// Check if user can edit the maintenance -> caller id - maintenance user id
|
|
int comparision = machineUsersController.CompareUsersRole(Convert.ToInt32(userId.Value), dbMaint.UserId.Value, MachineConfig.MachineId);
|
|
if (comparision < 0)
|
|
return Unauthorized();
|
|
}
|
|
|
|
// Update data
|
|
dbMaint = maintenancesController.Update(maintenanceId, newMaint);
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
CmsError cmsError = ncHandler.GetMaintenanceData(out DTOMaintenanceModel maintenance, dbMaint.MaintenanceId, Convert.ToInt32(userId.Value));
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(maintenance);
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}"), HttpDelete]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult DeleteMaintenance(int maintenanceId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
MaintenanceModel dbMaint = maintenancesController.FindById(maintenanceId);
|
|
if (dbMaint == null)
|
|
return NotFound();
|
|
if (dbMaint.UserId == null)
|
|
return Unauthorized();
|
|
|
|
using (MachinesUsersController machineUsersController = new MachinesUsersController())
|
|
{
|
|
// Check if user can delete the maintenance -> caller id - maintenance user id
|
|
int comparision = machineUsersController.CompareUsersRole(Convert.ToInt32(userId.Value), dbMaint.UserId.Value, MachineConfig.MachineId);
|
|
if (comparision < 0)
|
|
return Unauthorized();
|
|
}
|
|
|
|
// Update data
|
|
maintenancesController.Delete(dbMaint);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/note"), HttpGet]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult GetMaintenanceNotes(int maintenanceId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
MaintenanceModel dbMaint = maintenancesController.FindById(maintenanceId);
|
|
if (dbMaint == null)
|
|
return NotFound();
|
|
|
|
// Update data
|
|
List<DTOMaintenanceNoteModel> notes = maintenancesController.GetNotesByMaintId(maintenanceId);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/note"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult AddMaintenanceNote(int maintenanceId, DTONewMaintenanceNoteModel note)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
MaintenanceModel dbMaint = maintenancesController.FindById(maintenanceId);
|
|
if (dbMaint == null)
|
|
return NotFound();
|
|
|
|
// Update data
|
|
DTOMaintenanceNoteModel notes = maintenancesController.CreateNote(Convert.ToInt32(userId.Value), maintenanceId, note);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult EditMaintenanceNote(int noteId, DTONewMaintenanceNoteModel newNote)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
MaintenanceNoteModel dbNote = maintenancesController.FindNoteById(noteId);
|
|
if (dbNote == null)
|
|
return NotFound();
|
|
|
|
// Check if user is different
|
|
if (dbNote.UserId != Convert.ToInt32(userId.Value))
|
|
return Unauthorized();
|
|
|
|
// Update data
|
|
DTOMaintenanceNoteModel notes = maintenancesController.UpdateNote(dbNote, newNote);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/note/{noteId:int}"), HttpDelete]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult DeleteMaintenanceNote(int noteId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
MaintenanceNoteModel dbNote = maintenancesController.FindNoteById(noteId);
|
|
if (dbNote == null)
|
|
return NotFound();
|
|
|
|
// Check if user is different
|
|
if (dbNote.UserId != Convert.ToInt32(userId.Value))
|
|
return Unauthorized();
|
|
|
|
// Update data
|
|
maintenancesController.DeleteMessage(dbNote.Id);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/performe"), HttpPost]
|
|
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult PerformeMaintenance(int maintenanceId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var identity = User.Identity as ClaimsIdentity;
|
|
// Find user id from the bearer token
|
|
var userId = identity.Claims.Where(c => c.Type == USER_ID_KEY).FirstOrDefault();
|
|
if (userId == null)
|
|
return Unauthorized();
|
|
|
|
// Update data
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
CmsError cmsError = ncHandler.GetMaintenanceData(out DTOMaintenanceModel maintData, maintenanceId, Convert.ToInt32(userId.Value));
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
if (!maintData.CanPerform)
|
|
return Unauthorized();
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Check if maintenance id
|
|
PerformedMaintenanceModel performed = maintenancesController.PerformeMaintenance(maintData.PlcCounter, maintenanceId);
|
|
if (performed == null)
|
|
return NotFound();
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |