53 lines
2.3 KiB
C#
53 lines
2.3 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using OI.Metrology.Shared.Models.Stateless;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
|
|
namespace OI.Metrology.Server.ApiControllers;
|
|
|
|
[Route("api/[controller]")]
|
|
public class ReactorsController : Controller, IReactorsController<IActionResult>
|
|
{
|
|
|
|
private readonly IReactorsRepository _ReactorsRepository;
|
|
|
|
public ReactorsController(IReactorsRepository reactorsRepository) =>
|
|
_ReactorsRepository = reactorsRepository;
|
|
|
|
[HttpGet("{even}")]
|
|
public IActionResult Get(bool even) =>
|
|
Json(even ? _ReactorsRepository.EvenReactors() : _ReactorsRepository.OddReactors(), new JsonSerializerOptions { PropertyNamingPolicy = null, WriteIndented = true });
|
|
|
|
[HttpPost()]
|
|
public IActionResult Post(Shared.DataModels.WorkMaterialOut workMaterialOut)
|
|
{
|
|
string? result = null;
|
|
if (workMaterialOut is null)
|
|
throw new Exception();
|
|
if (workMaterialOut.Username is null || workMaterialOut.Username.Length < 2)
|
|
throw new ArgumentException(nameof(workMaterialOut.Username));
|
|
if (workMaterialOut.RunDataSheet is null || workMaterialOut.RunDataSheet.Length < 2)
|
|
throw new ArgumentException(nameof(workMaterialOut.RunDataSheet));
|
|
string? fileName = null;
|
|
DateTime dateTime = DateTime.Now;
|
|
Calendar calendar = new CultureInfo("en-US").Calendar;
|
|
string json = JsonSerializer.Serialize(workMaterialOut, new JsonSerializerOptions { WriteIndented = true });
|
|
string weekOfYear = $"{dateTime:yyyy}_Week_{calendar.GetWeekOfYear(dateTime, CalendarWeekRule.FirstDay, DayOfWeek.Sunday):00}";
|
|
string directory = Path.Combine("D:/Tmp/Metrology", weekOfYear, dateTime.ToString("yyyy-MM-dd"));
|
|
if (!Directory.Exists(directory))
|
|
_ = Directory.CreateDirectory(directory);
|
|
for (int i = 1; i < int.MaxValue; i++)
|
|
{
|
|
result = $"{workMaterialOut.Username.ToUpper()[0]}{dateTime:mmss}";
|
|
fileName = Path.Combine(directory, $"WMO-{result}.json");
|
|
if (!System.IO.File.Exists(fileName))
|
|
break;
|
|
dateTime = dateTime.AddSeconds(i);
|
|
}
|
|
if (fileName is null)
|
|
throw new Exception();
|
|
System.IO.File.WriteAllText(fileName, json);
|
|
return Ok(result);
|
|
}
|
|
|
|
} |