81b4c60abd
GET/POST maintenance note API
200 lines
8.1 KiB
C#
200 lines
8.1 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]
|
|
public IHttpActionResult GetMaintenances()
|
|
{
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
CmsError cmsError = ncHandler.GetMaintenances(out List<DTOMaintenanceModel> maintenances);
|
|
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.GetMaintenances(out List<DTOMaintenanceModel> maintenances);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(maintenances.Where(x => x.Id == dbMaint.MaintenanceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}"), HttpPut]
|
|
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.USER_FUNCTIONS, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult AddMaintenance(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
|
|
maintenancesController.Update(maintenanceId, newMaint);
|
|
using (NcHandler ncHandler = new NcHandler())
|
|
{
|
|
ncHandler.Connect();
|
|
CmsError cmsError = ncHandler.GetMaintenances(out List<DTOMaintenanceModel> maintenances);
|
|
if (cmsError.IsError())
|
|
return BadRequest(cmsError.localizationKey);
|
|
|
|
return Ok(maintenances.Where(x => x.Id == dbMaint.MaintenanceId).FirstOrDefault());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
[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);
|
|
}
|
|
}
|
|
}
|
|
} |