ASCU_ALL/ApiServer/Controllers/PasportController.cs
2021-08-01 19:48:09 +05:00

109 lines
3.2 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using Newtonsoft;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using NLog;
using DataClient.Struct;
using System.IO;
using ApiServer.ApiStruct;
namespace ApiServer.Controllers
{
[ApiController, Route("[controller]")]
public class PasportController : ControllerBase
{
private Logger log = LogManager.GetCurrentClassLogger();
[HttpPost, Route("check")]
public PasportCheckClient Check([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<PasportCheckApi>(value.ToString());
if (!getResult.Status || string.IsNullOrEmpty(getResult.Name))
{
log.Warn("Wrong answer.");
return new PasportCheckClient();
}
var paspName =
getResult.DateAndTime.Hour.ToString("D2") +
getResult.DateAndTime.Minute.ToString("D2") +
getResult.DateAndTime.Second.ToString("D2") +
"-" + getResult.Name;
var paspDir = Path.Combine(
Directory.GetCurrentDirectory(),
"data", "pasport",
getResult.DateAndTime.Year.ToString("D4"),
getResult.DateAndTime.Month.ToString("D2"),
getResult.DateAndTime.Day.ToString("D2"),
paspName);
log.Info("Search pasport: " + paspDir);
if (!System.IO.File.Exists(paspDir))
{
log.Info("Psport not exist: " + paspDir);
return new PasportCheckClient { Status = true, Exist = false, PaspSum = 0 };
}
var pasport = System.IO.File.ReadAllBytes(paspDir);
ulong paspResult = 0;
foreach (var b in pasport)
paspResult += b;
log.Info("Send pasport size: " + paspResult + " | " + paspDir);
return new PasportCheckClient { Status = true, Exist = true, PaspSum = paspResult };
}
catch(Exception e)
{
log.Warn(e);
return new PasportCheckClient();
}
}
[HttpPost, Route("create")]
public PasportCreateClient Create([FromBody] object value)
{
try
{
var getResult = JsonConvert.DeserializeObject<PasportCreateApi>(value.ToString());
if (!getResult.Status ||
getResult.Pasp == null ||
!getResult.Pasp.HasData)
{
log.Warn("Wrong answer.");
return new PasportCreateClient();
}
var paspName =
getResult.Pasp.dEnd.Value.Hour.ToString("D2") +
getResult.Pasp.dEnd.Value.Minute.ToString("D2") +
getResult.Pasp.dEnd.Value.Second.ToString("D2") +
"-" + (string.IsNullOrEmpty(getResult.Pasp.nplav) ?
getResult.Pasp.numVDP.Value.ToString("D2") :
getResult.Pasp.nplav);
var dir = Path.Combine(
Directory.GetCurrentDirectory(),
"data", "pasport",
getResult.Pasp.dEnd.Value.Year.ToString("D4"),
getResult.Pasp.dEnd.Value.Month.ToString("D2"),
getResult.Pasp.dEnd.Value.Day.ToString("D2"));
var paspDir = Path.Combine(dir, paspName);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
System.IO.File.WriteAllBytes(paspDir, getResult.Pasp.PaspByte);
log.Info("Save pasport: " + paspDir);
return new PasportCreateClient { Status = true };
}
catch (Exception e)
{
log.Warn(e);
return new PasportCreateClient();
}
}
}
}