389 lines
16 KiB
C#
389 lines
16 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.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Headers;
|
|
using System.Security.Claims;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using static CMS_CORE_Library.DataStructures;
|
|
using static Step.Config.ServerConfig;
|
|
using static Step.Model.Constants;
|
|
|
|
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();
|
|
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.GetMaintenances(out List<DTOMaintenanceModel> maintenances, Convert.ToInt32(userId.Value));
|
|
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, Convert.ToInt32(userId.Value));
|
|
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);
|
|
}
|
|
}
|
|
|
|
[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}/attachment"), HttpPost]
|
|
public async Task<IHttpActionResult> AddAttachment(int maintenanceId)
|
|
{
|
|
// Check whether the POST operation is MultiPart?
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
|
|
// Create CustomMultipartFormDataStreamProvider
|
|
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(MAINTENANCE_ATTACHMENT_PATH);
|
|
List<string> files = new List<string>();
|
|
|
|
// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
|
|
await Request.Content.ReadAsMultipartAsync(provider);
|
|
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
foreach (MultipartFileData file in provider.FileData)
|
|
{
|
|
var fileName = Path.GetFileName(file.LocalFileName);
|
|
if (File.Exists(MAINTENANCE_ATTACHMENT_PATH + fileName))
|
|
return BadRequest("file_already_exists");
|
|
|
|
files.Add(fileName);
|
|
maintenancesController.AddAttachment(fileName, maintenanceId);
|
|
}
|
|
}
|
|
|
|
// Send OK Response along with saved file names to the client.
|
|
return Ok();
|
|
}
|
|
|
|
[Route("maintenance/{maintenanceId:int}/attachments"), HttpGet]
|
|
public IHttpActionResult GetAttachments(int maintenanceId)
|
|
{
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
return Ok(maintenancesController.FindAttachmentByMaintenance(maintenanceId));
|
|
}
|
|
}
|
|
|
|
[Route("attachment/{attachmentId:int}"), HttpGet]
|
|
public IHttpActionResult GetAttachment(int attachmentId)
|
|
{
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Get single file
|
|
MaintenanceFileModel attachment = maintenancesController.FindAttachmentById(attachmentId);
|
|
// Check if exist in db or physically
|
|
if (attachment == null)
|
|
return NotFound();
|
|
if(!File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.FileName))
|
|
return NotFound();
|
|
|
|
return new FileResult(MAINTENANCE_ATTACHMENT_PATH + attachment.FileName);
|
|
}
|
|
}
|
|
|
|
[Route("attachment/{attachmentId:int}"), HttpDelete]
|
|
public IHttpActionResult DeleteAttachment(int attachmentId)
|
|
{
|
|
using (MaintenancesController maintenancesController = new MaintenancesController())
|
|
{
|
|
// Get single file
|
|
MaintenanceFileModel attachment = maintenancesController.FindAttachmentById(attachmentId);
|
|
// Check if exist in db or physically
|
|
if (attachment == null)
|
|
return NotFound();
|
|
|
|
maintenancesController.DeleteAttachment(attachment);
|
|
|
|
if (File.Exists(MAINTENANCE_ATTACHMENT_PATH + attachment.FileName))
|
|
File.Delete(MAINTENANCE_ATTACHMENT_PATH + attachment.FileName);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public class FileResult : IHttpActionResult
|
|
{
|
|
private readonly string filePath;
|
|
private readonly string contentType;
|
|
|
|
public FileResult(string filePath, string contentType = null)
|
|
{
|
|
this.filePath = filePath;
|
|
this.contentType = contentType;
|
|
}
|
|
|
|
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
|
|
{
|
|
return Task.Run(() =>
|
|
{
|
|
// Create response
|
|
var response = new HttpResponseMessage(HttpStatusCode.OK)
|
|
{
|
|
Content = new StreamContent(File.OpenRead(filePath)) // Set file
|
|
};
|
|
// Set header content type
|
|
var contentType = this.contentType ?? MimeMapping.GetMimeMapping(Path.GetExtension(filePath));
|
|
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
|
|
|
|
return response;
|
|
}, cancellationToken);
|
|
}
|
|
}
|
|
|
|
// Override MultipartFormDataStreamProvider to override the GetLocalFileName
|
|
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
|
|
{
|
|
public CustomMultipartFormDataStreamProvider(string path) : base(path)
|
|
{
|
|
}
|
|
|
|
public override string GetLocalFileName(HttpContentHeaders headers)
|
|
{
|
|
return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
|
|
}
|
|
}
|
|
} |