275 lines
11 KiB
C#
275 lines
11 KiB
C#
using Step.Database.Controllers;
|
|
using Step.Model.DatabaseModels;
|
|
using Step.Model.DTOModels.AlarmModels;
|
|
using Step.NC;
|
|
using Step.Provider;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Security.Claims;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using static Step.Model.Constants;
|
|
|
|
namespace Step.Controllers.WebApi
|
|
{
|
|
[RoutePrefix("api/alarm")]
|
|
public class ApiAlarmController : ApiController
|
|
{
|
|
[Route("paginated"), HttpPost]
|
|
public IHttpActionResult GetDataPaginated([FromBody]DTOAlarmsFilterModel filter)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
Dictionary<int, string> plcMessages = LanguageController.GetPlcAlarmsTranslations(filter.Language)
|
|
.ToDictionary(
|
|
x => Convert.ToInt32(x.Key.Split('_').Last()), // This function return "alarm_id" as id, i need only the id number
|
|
x => x.Value
|
|
);
|
|
|
|
using (AlarmsController alarm = new AlarmsController())
|
|
{
|
|
List<DTOAlarmHistoricModel> alarms = alarm.GetPaginatedWithFilter(filter.Title, filter.Sources, filter.Page, filter.PageSize, filter.StartDate.Value, filter.EndDate, filter.UserIds, plcMessages, out int pages);
|
|
|
|
return Ok(new DTOPaginatedAlarmsModel()
|
|
{
|
|
Alarms = alarms,
|
|
Pages = pages
|
|
});
|
|
}
|
|
}
|
|
|
|
[Route("data"), HttpPost]
|
|
public IHttpActionResult GetAlarmsData(int pageSize)
|
|
{
|
|
using (AlarmsController alarmController = new AlarmsController())
|
|
{
|
|
var alarms = alarmController.GetAlarmsData(pageSize);
|
|
|
|
return Ok(alarms);
|
|
}
|
|
}
|
|
|
|
public class DTOPaginatedAlarmsModel
|
|
{
|
|
public List<DTOAlarmHistoricModel> Alarms;
|
|
public int Pages;
|
|
}
|
|
|
|
#region Note
|
|
|
|
[Route("{alarmDescId:int}/{source:int}/note"), HttpGet]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.READ)]
|
|
public IHttpActionResult GetAlarmNotes(int alarmDescId, ALARM_SOURCE source)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmOccurrencesModel dbAlarm = alarmsController.FindById(alarmDescId, source);
|
|
if (dbAlarm == null)
|
|
return NotFound();
|
|
|
|
// Update data
|
|
List<DTOAlarmNoteModel> notes = alarmsController.GetNotesByAlarmDescId(alarmDescId, source);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("{alarmDescId:int}/{source:int}/note"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult AddAlarmNote(int alarmDescId, ALARM_SOURCE source, DTONewAlarmNoteModel 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();
|
|
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmOccurrencesModel dbAlarm = alarmsController.FindById(alarmDescId, source);
|
|
if (dbAlarm == null)
|
|
return NotFound();
|
|
|
|
// Update data
|
|
DTOAlarmNoteModel notes = alarmsController.CreateNote(Convert.ToInt32(userId.Value), alarmDescId, source, note);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("{alarmDescId:int}/note/{noteId:int}"), HttpPut]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult EditAlarmNote(int noteId, DTONewAlarmNoteModel 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();
|
|
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmNoteModel dbNote = alarmsController.FindNoteById(noteId);
|
|
if (dbNote == null)
|
|
return NotFound();
|
|
|
|
// Check if user is different
|
|
if (dbNote.UserId != Convert.ToInt32(userId.Value))
|
|
return Unauthorized();
|
|
|
|
// Update data
|
|
DTOAlarmNoteModel notes = alarmsController.UpdateNote(dbNote, newNote);
|
|
|
|
return Ok(notes);
|
|
}
|
|
}
|
|
|
|
[Route("{alarmDescId:int}/note/{noteId:int}"), HttpDelete]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult DeleteAlarmNote(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();
|
|
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmNoteModel dbNote = alarmsController.FindNoteById(noteId);
|
|
if (dbNote == null)
|
|
return NotFound();
|
|
|
|
// Check if user is different
|
|
if (dbNote.UserId != Convert.ToInt32(userId.Value))
|
|
return Unauthorized();
|
|
|
|
// Update data
|
|
alarmsController.DeleteNote(dbNote.NoteId);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
#endregion Note
|
|
|
|
#region Attachment
|
|
|
|
[Route("{alarmDescId:int}/{source:int}/attachments"), HttpGet]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.READ)]
|
|
public IHttpActionResult GetAttachmentsByAlarmId(int alarmDescId, ALARM_SOURCE source)
|
|
{
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmOccurrencesModel dbAlarm = alarmsController.FindById(alarmDescId, source);
|
|
if (dbAlarm == null)
|
|
return NotFound();
|
|
|
|
List<AlarmFileModel> attachments = alarmsController.FindAttachmentByAlarmDescId(alarmDescId, source);
|
|
|
|
return Ok(attachments);
|
|
}
|
|
}
|
|
|
|
[Route("attachment/{attachmentId:int}"), HttpGet]
|
|
public IHttpActionResult GetAttachment(int attachmentId)
|
|
{
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if attachment exist in db or physically
|
|
AlarmFileModel attachment = alarmsController.FindAttachmentById(attachmentId);
|
|
if (attachment == null)
|
|
return NotFound();
|
|
if (!File.Exists(ALARM_ATTACHMENT_PATH + attachment.LocalFileName))
|
|
return NotFound();
|
|
|
|
return new FileResult(ALARM_ATTACHMENT_PATH + attachment.LocalFileName);
|
|
}
|
|
}
|
|
|
|
[Route("{alarmDescId:int}/{source:int}/attachment"), HttpPost]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.WRITE)]
|
|
public async Task<IHttpActionResult> AddAttachment(int alarmDescId, ALARM_SOURCE source)
|
|
{
|
|
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();
|
|
|
|
// Check whether the POST operation is MultiPart?
|
|
if (!Request.Content.IsMimeMultipartContent())
|
|
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
|
|
|
|
// Create CustomMultipartFormDataStreamProvider
|
|
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(ALARM_ATTACHMENT_PATH);
|
|
List<string> files = new List<string>();
|
|
|
|
// Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
|
|
var result = await Request.Content.ReadAsMultipartAsync(provider);
|
|
|
|
AlarmFileModel attachment = null;
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Check if alarm desc exists
|
|
AlarmOccurrencesModel dbAlarm = alarmsController.FindById(alarmDescId, source);
|
|
if (dbAlarm == null)
|
|
return NotFound();
|
|
|
|
// TODO: Remove foreach
|
|
foreach (MultipartFileData file in provider.FileData)
|
|
{
|
|
var fileName = Path.GetFileName(file.LocalFileName);
|
|
|
|
files.Add(fileName);
|
|
attachment = alarmsController
|
|
.AddAttachment(file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty), fileName, alarmDescId, Convert.ToInt32(userId.Value), source);
|
|
}
|
|
}
|
|
|
|
// Send OK Response along with saved file names to the client.
|
|
return Ok(attachment);
|
|
}
|
|
|
|
[Route("attachment/{attachmentId:int}"), HttpDelete]
|
|
[WebApiAuthorize(FunctionAccess = FUNCTIONALITY_NAMES.ALARM_CMD, Action = ACTIONS.WRITE)]
|
|
public IHttpActionResult DeleteAttachment(int attachmentId)
|
|
{
|
|
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();
|
|
|
|
using (AlarmsController alarmsController = new AlarmsController())
|
|
{
|
|
// Get single file
|
|
AlarmFileModel attachment = alarmsController.FindAttachmentById(attachmentId);
|
|
// Check if exist in db or physically
|
|
if (attachment == null)
|
|
return NotFound();
|
|
// Check user
|
|
if (attachment.UserId != Convert.ToInt32(userId.Value))
|
|
return Unauthorized();
|
|
|
|
alarmsController.DeleteAttachment(attachment);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
|
|
#endregion Attachment
|
|
}
|
|
} |