Upload project.
This commit is contained in:
59
syski_api/uk.co.syski.api/Controllers/BIOSsController.cs
Normal file
59
syski_api/uk.co.syski.api/Controllers/BIOSsController.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class BIOSsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public BIOSsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/bios")]
|
||||
public IActionResult GetBIOS(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var bios = context.SystemBIOSs.FirstOrDefault(sc => sc.SystemId.Equals(systemId));
|
||||
var biosDTO = CreateBIOSDTO(bios);
|
||||
|
||||
return Ok(biosDTO);
|
||||
}
|
||||
|
||||
private BIOSDTO CreateBIOSDTO(SystemBIOS systemBIOS)
|
||||
{
|
||||
var biosModel = context.BIOSModels.Find(systemBIOS.BIOSModelId);
|
||||
|
||||
var biosDTO = new BIOSDTO()
|
||||
{
|
||||
Id = systemBIOS.BIOSModelId,
|
||||
Caption = systemBIOS.Caption,
|
||||
Date = systemBIOS.Date,
|
||||
Version = systemBIOS.Version
|
||||
};
|
||||
|
||||
if (biosModel.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(biosModel.ManufacturerId);
|
||||
biosDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
|
||||
return biosDTO;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
113
syski_api/uk.co.syski.api/Controllers/CPUsController.cs
Normal file
113
syski_api/uk.co.syski.api/Controllers/CPUsController.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class CPUsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public CPUsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/cpu")]
|
||||
public IActionResult GetCPUs(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var lastUpdatedCPU = context.SystemCPUs.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.LastUpdated).FirstOrDefault();
|
||||
DateTime? lastUpdated = (lastUpdatedCPU != null ? lastUpdatedCPU.LastUpdated : (DateTime?)null);
|
||||
var CPUs = context.SystemCPUs.Where(sc => sc.SystemId.Equals(systemId) && sc.LastUpdated.Equals(lastUpdated)).ToList();
|
||||
|
||||
var CPUDTOs = new List<CPUDTO>();
|
||||
|
||||
foreach (var item in CPUs)
|
||||
{
|
||||
CPUDTOs.Add(CreateCPUDTO(item));
|
||||
}
|
||||
|
||||
return Ok(CPUDTOs);
|
||||
}
|
||||
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/cpu/data")]
|
||||
public IActionResult GetCPUData(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var CPUsData = context.SystemCPUsData.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.CollectionDateTime).FirstOrDefault();
|
||||
|
||||
if (CPUsData == null || CPUsData.CollectionDateTime.AddSeconds(9) < DateTime.Now)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
var cpuDTOs = new List<CPUDataDTO>();
|
||||
cpuDTOs.Add(CreateCPUDataDTO(CPUsData));
|
||||
return Ok(cpuDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
private CPUDTO CreateCPUDTO(SystemCPU systemCPU)
|
||||
{
|
||||
var cpuModel = context.CPUModels.Find(systemCPU.CPUModelID);
|
||||
|
||||
var cpuDTO = new CPUDTO()
|
||||
{
|
||||
Id = systemCPU.CPUModelID,
|
||||
ClockSpeed = systemCPU.ClockSpeed,
|
||||
CoreCount = systemCPU.CoreCount,
|
||||
ThreadCount = systemCPU.ThreadCount
|
||||
};
|
||||
|
||||
if (cpuModel.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(cpuModel.ModelId);
|
||||
cpuDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
cpuDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
if (cpuModel.ArchitectureId != null)
|
||||
{
|
||||
var architecture = context.Architectures.Find(cpuModel.ArchitectureId);
|
||||
cpuDTO.ArchitectureName = architecture.Name;
|
||||
}
|
||||
|
||||
return cpuDTO;
|
||||
}
|
||||
|
||||
private CPUDataDTO CreateCPUDataDTO(SystemCPUData systemCPUData)
|
||||
{
|
||||
return new CPUDataDTO
|
||||
{
|
||||
Processes = systemCPUData.Processes,
|
||||
Load = systemCPUData.Load,
|
||||
CollectionDateTime = systemCPUData.CollectionDateTime
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
70
syski_api/uk.co.syski.api/Controllers/GPUsController.cs
Normal file
70
syski_api/uk.co.syski.api/Controllers/GPUsController.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class GPUsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public GPUsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/gpu")]
|
||||
public IActionResult GetGPUs(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var lastUpdatedGPU = context.SystemGPUs.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.LastUpdated).FirstOrDefault();
|
||||
DateTime? lastUpdated = (lastUpdatedGPU != null ? lastUpdatedGPU.LastUpdated : (DateTime?)null);
|
||||
var GPUs = context.SystemGPUs.Where(sc => sc.SystemId.Equals(systemId) && sc.LastUpdated.Equals(lastUpdated)).ToList();
|
||||
|
||||
var GPUDTOs = new List<GPUDTO>();
|
||||
|
||||
foreach (var item in GPUs)
|
||||
{
|
||||
GPUDTOs.Add(CreateGPUDTO(item));
|
||||
}
|
||||
|
||||
return Ok(GPUDTOs);
|
||||
}
|
||||
|
||||
private GPUDTO CreateGPUDTO(SystemGPU systemGPU)
|
||||
{
|
||||
var gpuModel = context.GPUModels.Find(systemGPU.GPUModelId);
|
||||
|
||||
var gpuDTO = new GPUDTO()
|
||||
{
|
||||
Id = systemGPU.GPUModelId
|
||||
};
|
||||
|
||||
if (gpuModel.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(gpuModel.ModelId);
|
||||
gpuDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
gpuDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
return gpuDTO;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
16
syski_api/uk.co.syski.api/Controllers/HomeController.cs
Normal file
16
syski_api/uk.co.syski.api/Controllers/HomeController.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class HomeController : ControllerBase
|
||||
{
|
||||
|
||||
[HttpGet("/")]
|
||||
public ActionResult Get()
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class MotherboardsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public MotherboardsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/motherboard")]
|
||||
public IActionResult GetMotherboard(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var motherboard = context.SystemMotherboards.FirstOrDefault(sc => sc.SystemId.Equals(systemId));
|
||||
var MotherboardDTO = CreateMotherboardDTO(motherboard);
|
||||
|
||||
return Ok(MotherboardDTO);
|
||||
}
|
||||
|
||||
private MotherboardDTO CreateMotherboardDTO(SystemMotherboard systemMotherboard)
|
||||
{
|
||||
if (systemMotherboard != null)
|
||||
{
|
||||
var motherboardModel = context.MotherboardModels.Find(systemMotherboard.MotherboardModelId);
|
||||
|
||||
var motherboardDTO = new MotherboardDTO()
|
||||
{
|
||||
Id = systemMotherboard.MotherboardModelId,
|
||||
Version = motherboardModel.Version
|
||||
};
|
||||
|
||||
if (motherboardModel.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(motherboardModel.ModelId);
|
||||
motherboardDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
motherboardDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
return motherboardDTO;
|
||||
}
|
||||
else
|
||||
{
|
||||
return new MotherboardDTO();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
64
syski_api/uk.co.syski.api/Controllers/OSsController.cs
Normal file
64
syski_api/uk.co.syski.api/Controllers/OSsController.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class OSsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public OSsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/operatingsystem")]
|
||||
public IActionResult GetOSs(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var SystemOSs = context.SystemOSs.Where(so => so.SystemId == systemId).ToList();
|
||||
|
||||
var OSDTOs = new List<OSDTO>();
|
||||
foreach (var item in SystemOSs)
|
||||
{
|
||||
OSDTOs.Add(CreateOSDTO(item));
|
||||
}
|
||||
|
||||
return Ok(OSDTOs);
|
||||
}
|
||||
|
||||
private OSDTO CreateOSDTO(SystemOS systemOS)
|
||||
{
|
||||
var OperatingSystem = context.OperatingSystemModels.Find(systemOS.OperatingSystemId);
|
||||
|
||||
var OSDTO = new OSDTO()
|
||||
{
|
||||
Id = systemOS.OperatingSystemId,
|
||||
Name = OperatingSystem.Name,
|
||||
Version = systemOS.Version
|
||||
};
|
||||
|
||||
if (systemOS.ArchitectureId != null)
|
||||
{
|
||||
var architecture = context.Architectures.Find(systemOS.ArchitectureId);
|
||||
OSDTO.ArchitectureName = architecture.Name;
|
||||
}
|
||||
|
||||
return OSDTO;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
104
syski_api/uk.co.syski.api/Controllers/RAMsController.cs
Normal file
104
syski_api/uk.co.syski.api/Controllers/RAMsController.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class RAMsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public RAMsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/ram")]
|
||||
public IActionResult GetRAMs(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var lastUpdatedRAM = context.SystemRAMs.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.LastUpdated).FirstOrDefault();
|
||||
DateTime? lastUpdated = (lastUpdatedRAM != null ? lastUpdatedRAM.LastUpdated : (DateTime?) null);
|
||||
var RAMs = context.SystemRAMs.Where(sc => sc.SystemId.Equals(systemId) && sc.LastUpdated.Equals(lastUpdated)).ToList();
|
||||
|
||||
var RAMDTOs = new List<RAMDTO>();
|
||||
|
||||
foreach (var item in RAMs)
|
||||
{
|
||||
RAMDTOs.Add(CreateRAMDTO(item));
|
||||
}
|
||||
|
||||
return Ok(RAMDTOs);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/ram/data")]
|
||||
public IActionResult GetRAMData(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems
|
||||
.Where(u => u.User.Email == ((ClaimsIdentity)User.Identity).FindFirst("email").Value && u.SystemId == systemId).FirstOrDefault();
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var ramData = context.SystemRAMData.Where(sc => sc.SystemId == systemId).OrderByDescending(i => i.CollectionDateTime).FirstOrDefault();
|
||||
|
||||
if (ramData == null || ramData.CollectionDateTime.AddSeconds(9) < DateTime.Now)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
var ramDTOs = new List<RAMDataDTO>();
|
||||
ramDTOs.Add(CreateRAMDataDTO(ramData));
|
||||
return Ok(ramDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
private RAMDTO CreateRAMDTO(SystemRAM systemRAM)
|
||||
{
|
||||
RAMModel ramModel = context.RAMModels.Find(systemRAM.RAMModelId);
|
||||
|
||||
RAMDTO ramDTO = new RAMDTO()
|
||||
{
|
||||
Id = ramModel.Id,
|
||||
MemoryBytes = ramModel.Size
|
||||
};
|
||||
|
||||
if (ramModel.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(ramModel.ModelId);
|
||||
ramDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
ramDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
return ramDTO;
|
||||
}
|
||||
|
||||
private RAMDataDTO CreateRAMDataDTO(SystemRAMData systemRAMData)
|
||||
{
|
||||
return new RAMDataDTO
|
||||
{
|
||||
Free = systemRAMData.Free,
|
||||
CollectionDateTime = systemRAMData.CollectionDateTime
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
114
syski_api/uk.co.syski.api/Controllers/StoragesController.cs
Normal file
114
syski_api/uk.co.syski.api/Controllers/StoragesController.cs
Normal file
@@ -0,0 +1,114 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class StoragesController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public StoragesController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/storage")]
|
||||
public IActionResult GetStorages(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var lastUpdatedStorage = context.SystemStorages.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.LastUpdated).FirstOrDefault();
|
||||
DateTime? lastUpdated = (lastUpdatedStorage != null ? lastUpdatedStorage.LastUpdated : (DateTime?) null);
|
||||
var Storages = context.SystemStorages.Where(sc => sc.SystemId.Equals(systemId) && sc.LastUpdated.Equals(lastUpdated)).ToList();
|
||||
|
||||
var StorageDTOs = new List<StorageDTO>();
|
||||
|
||||
foreach (var item in Storages)
|
||||
{
|
||||
StorageDTOs.Add(CreateStorageDTO(item));
|
||||
}
|
||||
|
||||
return Ok(StorageDTOs);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/storage/data")]
|
||||
public IActionResult GetStorageData(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var storageData = context.SystemStorageData.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.CollectionDateTime).FirstOrDefault();
|
||||
|
||||
if (storageData == null || storageData.CollectionDateTime.AddSeconds(9) < DateTime.Now)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
var storageDTOs = new List<StorageDataDTO>();
|
||||
storageDTOs.Add(CreateStorageDataDTO(storageData));
|
||||
return Ok(storageDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
private StorageDTO CreateStorageDTO(SystemStorage systemStorage)
|
||||
{
|
||||
StorageModel storageModel = context.StorageModels.Find(systemStorage.StorageModelId);
|
||||
|
||||
StorageDTO storageDTO = new StorageDTO()
|
||||
{
|
||||
Id = storageModel.Id,
|
||||
MemoryBytes = storageModel.Size
|
||||
};
|
||||
|
||||
if (storageModel.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(storageModel.ModelId);
|
||||
storageDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
storageDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
if (systemStorage.StorageInterfaceId != null)
|
||||
{
|
||||
StorageInterfaceType StorageType = context.StorageInterfaceTypes.Find(systemStorage.StorageInterfaceId);
|
||||
storageDTO.MemoryTypeName = (StorageType != null ? StorageType.Name : null);
|
||||
}
|
||||
|
||||
return storageDTO;
|
||||
}
|
||||
|
||||
private StorageDataDTO CreateStorageDataDTO(SystemStorageData systemStorageData)
|
||||
{
|
||||
return new StorageDataDTO
|
||||
{
|
||||
ByteReads = systemStorageData.ByteReads,
|
||||
ByteWrites = systemStorageData.ByteWrites,
|
||||
Reads = systemStorageData.Reads,
|
||||
Writes = systemStorageData.Writes,
|
||||
Time = systemStorageData.Time,
|
||||
Transfers = systemStorageData.Transfers,
|
||||
CollectionDateTime = systemStorageData.CollectionDateTime
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
214
syski_api/uk.co.syski.api/Controllers/SystemsController.cs
Normal file
214
syski_api/uk.co.syski.api/Controllers/SystemsController.cs
Normal file
@@ -0,0 +1,214 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Syski.API.Models;
|
||||
using Syski.Data;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
public class SystemsController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly SyskiDBContext context;
|
||||
|
||||
public SystemsController(SyskiDBContext context)
|
||||
{
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/all")]
|
||||
public IActionResult GetSystemsIndex()
|
||||
{
|
||||
var applicationUserSystems = context.ApplicationUserSystems.Where(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value)).ToList();
|
||||
|
||||
List<SystemDTO> systemDTOs = new List<SystemDTO>();
|
||||
foreach (var item in applicationUserSystems)
|
||||
{
|
||||
systemDTOs.Add(CreateSystemDTO(context.Systems.First(s => s.Id == item.SystemId)));
|
||||
}
|
||||
return Ok(systemDTOs);
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/ping")]
|
||||
public IActionResult GetPing(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var pingData = context.SystemPingData.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.CollectionDateTime).FirstOrDefault();
|
||||
|
||||
if (pingData == null || pingData.CollectionDateTime.AddSeconds(30) < DateTime.Now)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Ok(new SystemPingDTO
|
||||
{
|
||||
ping = pingData.CollectionDateTime.Subtract(pingData.SendPingTime).TotalMilliseconds
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("/system/{systemId}/restart")]
|
||||
public IActionResult RestartSystem(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
context.Add(new SystemCommand
|
||||
{
|
||||
SystemId = systemId,
|
||||
Action = "restart",
|
||||
QueuedTime = DateTime.Now
|
||||
});
|
||||
context.SaveChanges();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("/system/{systemId}/shutdown")]
|
||||
public IActionResult ShutdownSystem(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
context.Add(new SystemCommand
|
||||
{
|
||||
SystemId = systemId,
|
||||
Action = "shutdown",
|
||||
QueuedTime = DateTime.Now
|
||||
});
|
||||
context.SaveChanges();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("/system/{systemId}/remove")]
|
||||
public IActionResult RemoveSystem(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
context.Remove(applicationUserSystem);
|
||||
context.SaveChanges();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpPost("/system/{systemId}/processes/kill")]
|
||||
public IActionResult KillProcess(Guid systemId, [FromBody] KillProcessDTO killProcessDTO)
|
||||
{
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity)User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
JObject properties = new JObject { { "id", killProcessDTO.Id } };
|
||||
context.Add(new SystemCommand
|
||||
{
|
||||
SystemId = systemId,
|
||||
Action = "killprocess",
|
||||
Properties = properties.ToString(),
|
||||
QueuedTime = DateTime.Now
|
||||
});
|
||||
context.SaveChanges();
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet("/system/{systemId}/processes")]
|
||||
public IActionResult GetProcesses(Guid systemId)
|
||||
{
|
||||
var applicationUserSystem = context.ApplicationUserSystems.FirstOrDefault(u => u.User.Email.Equals(((ClaimsIdentity) User.Identity).FindFirst("email").Value) && u.SystemId.Equals(systemId));
|
||||
|
||||
if (applicationUserSystem == null)
|
||||
return NotFound();
|
||||
|
||||
var processesData = context.SystemRunningProcesses.Where(sc => sc.SystemId.Equals(systemId)).OrderByDescending(i => i.CollectionDateTime).FirstOrDefault();
|
||||
if (processesData == null || processesData.CollectionDateTime.AddSeconds(30) <= DateTime.Now)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
else
|
||||
{
|
||||
var runningProcessesDTOs = new List<RunningProcessesDTO>();
|
||||
var processesList = context.SystemRunningProcesses.Where(sc => sc.SystemId.Equals(systemId) && sc.CollectionDateTime.Equals(processesData.CollectionDateTime)).ToList();
|
||||
foreach (var process in processesList)
|
||||
{
|
||||
runningProcessesDTOs.Add(new RunningProcessesDTO
|
||||
{
|
||||
Id = process.Id,
|
||||
CollectionDateTime = process.CollectionDateTime,
|
||||
KernelTime = process.KernelTime,
|
||||
MemSize = process.MemSize,
|
||||
Name = process.Name,
|
||||
ParentId = process.ParentId,
|
||||
Path = process.Path,
|
||||
Threads = process.Threads,
|
||||
UpTime = process.UpTime
|
||||
});
|
||||
}
|
||||
return Ok(runningProcessesDTOs);
|
||||
}
|
||||
}
|
||||
|
||||
private SystemDTO CreateSystemDTO(Data.System item)
|
||||
{
|
||||
var systemDTO = new SystemDTO()
|
||||
{
|
||||
Id = item.Id,
|
||||
HostName = item.HostName,
|
||||
LastUpdated = item.LastUpdated
|
||||
};
|
||||
|
||||
if (item.ModelId != null)
|
||||
{
|
||||
var model = context.Models.Find(item.ModelId);
|
||||
systemDTO.ModelName = model.Name;
|
||||
if (model.ManufacturerId != null)
|
||||
{
|
||||
var manufacturer = context.Manufacturers.Find(model.ManufacturerId);
|
||||
systemDTO.ManufacturerName = manufacturer.Name;
|
||||
}
|
||||
}
|
||||
|
||||
var systemTypes = context.SystemTypes.Where(smt => smt.SystemId == item.Id).ToList();
|
||||
var types = new List<string>();
|
||||
foreach (var systemType in systemTypes)
|
||||
{
|
||||
var systemTypeName = context.SystemTypeNames.Find(systemType.TypeId);
|
||||
types.Add(systemTypeName.Name);
|
||||
}
|
||||
systemDTO.SystemTypes = types;
|
||||
|
||||
return systemDTO;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Syski.API.Models;
|
||||
using Syski.API.Services;
|
||||
using Syski.Data;
|
||||
using System.Collections.Generic;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Syski.API.Controllers
|
||||
{
|
||||
public class UserAuthenticationController : ControllerBase
|
||||
{
|
||||
|
||||
private readonly IConfiguration configuration;
|
||||
private readonly UserManager<ApplicationUser> userManager;
|
||||
private readonly UserTokenManager tokenManager;
|
||||
|
||||
public UserAuthenticationController(IConfiguration configuration, UserManager<ApplicationUser> userManager, UserTokenManager tokenManager)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
this.userManager = userManager;
|
||||
this.tokenManager = tokenManager;
|
||||
}
|
||||
|
||||
[HttpPost("/auth/user/login")]
|
||||
public async Task<IActionResult> UserLogin([FromBody] UserLoginDTO userLoginDTO)
|
||||
{
|
||||
IActionResult result = null;
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
result = BadRequest(ModelState);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApplicationUser user = userManager.Users.SingleOrDefault(r => r.Email == userLoginDTO.Email);
|
||||
if (user != null)
|
||||
{
|
||||
if (await userManager.CheckPasswordAsync(user, userLoginDTO.Password))
|
||||
{
|
||||
string refreshToken = null;
|
||||
AuthenticationToken token = tokenManager.CreateToken(user, ref refreshToken);
|
||||
result = Ok(new UserTokenDTO()
|
||||
{
|
||||
Id = token.User.Id,
|
||||
Email = token.User.Email,
|
||||
AccessToken = GenerateJwtToken(token),
|
||||
RefreshToken = refreshToken,
|
||||
Expiry = token.Expires
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
result = BadRequest();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
result = BadRequest();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[HttpPost("/auth/user/register")]
|
||||
public async Task<IActionResult> UserRegister([FromBody] UserRegisterDTO registerDTO)
|
||||
{
|
||||
IActionResult result = null;
|
||||
if (!ModelState.IsValid)
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
else
|
||||
{
|
||||
ApplicationUser newUser = new ApplicationUser
|
||||
{
|
||||
UserName = registerDTO.Email,
|
||||
Email = registerDTO.Email
|
||||
};
|
||||
if (((IdentityResult) await userManager.CreateAsync(newUser, registerDTO.Password)).Succeeded)
|
||||
{
|
||||
var user = userManager.Users.SingleOrDefault(r => r.Email == registerDTO.Email);
|
||||
string refreshToken = null;
|
||||
AuthenticationToken token = tokenManager.CreateToken(user, ref refreshToken);
|
||||
result = Ok(new UserTokenDTO()
|
||||
{
|
||||
Id = token.User.Id,
|
||||
Email = token.User.Email,
|
||||
AccessToken = GenerateJwtToken(token),
|
||||
RefreshToken = refreshToken,
|
||||
Expiry = token.Expires
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return BadRequest(ModelState);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
[HttpPost("/auth/user/token/refresh")]
|
||||
[Authorize("refreshtoken")]
|
||||
public IActionResult UserRefreshToken([FromBody] UserRefreshTokenDTO refreshTokenDTO)
|
||||
{
|
||||
var claimsIdentity = User.Identity as ClaimsIdentity;
|
||||
if (tokenManager.CheckValidRefreshToken(refreshTokenDTO.RefreshToken, claimsIdentity.FindFirst("jti").Value))
|
||||
{
|
||||
var user = userManager.Users.SingleOrDefault(r => r.Email == claimsIdentity.FindFirst("email").Value);
|
||||
string refreshToken = null;
|
||||
AuthenticationToken token = tokenManager.CreateToken(user, ref refreshToken, claimsIdentity.FindFirst("jti").Value);
|
||||
return Ok(new UserTokenDTO()
|
||||
{
|
||||
Id = token.User.Id,
|
||||
Email = token.User.Email,
|
||||
AccessToken = GenerateJwtToken(token),
|
||||
RefreshToken = refreshToken,
|
||||
Expiry = token.Expires
|
||||
});
|
||||
}
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
private string GenerateJwtToken(AuthenticationToken Token)
|
||||
{
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Jti, Token.Id.ToString()),
|
||||
new Claim(JwtRegisteredClaimNames.Email, Token.User.Email)
|
||||
};
|
||||
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"]));
|
||||
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
|
||||
|
||||
var token = new JwtSecurityToken(
|
||||
configuration["Jwt:Issuer"],
|
||||
configuration["Jwt:Audience"],
|
||||
claims,
|
||||
//expires: DateTime.Now.AddDays(Convert.ToDouble(_configuration["Jwt:ExpireDays"])),
|
||||
expires: Token.Expires,
|
||||
notBefore: Token.NotBefore,
|
||||
signingCredentials: creds
|
||||
);
|
||||
|
||||
return new JwtSecurityTokenHandler().WriteToken(token);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user