add method to write to temp

This commit is contained in:
DESKTOP-T0O5CDB\DESK-555BD 2025-11-04 13:04:27 -07:00
parent 50f4308417
commit a90b8bdf93
4 changed files with 40 additions and 30 deletions

View File

@ -321,13 +321,13 @@ namespace CarCareTracker.Controllers
[HttpPost] [HttpPost]
public IActionResult ExportTranslation(IFormFile file) public IActionResult ExportTranslation(IFormFile file)
{ {
var translationData = new Dictionary<string, string>(); var result = string.Empty;
using (var sReader = new StreamReader(file.OpenReadStream())) using (var sReader = new StreamReader(file.OpenReadStream()))
{ {
var fileName = $"{Guid.NewGuid()}.json";
var sData = sReader.ReadToEnd(); var sData = sReader.ReadToEnd();
translationData = JsonSerializer.Deserialize<Dictionary<string, string>>(sData); result = _fileHelper.WriteFileToTemp(fileName, sData);
} }
var result = _translationHelper.ExportTranslation(translationData);
return Json(result); return Json(result);
} }
[Authorize(Roles = nameof(UserData.IsRootUser))] [Authorize(Roles = nameof(UserData.IsRootUser))]

View File

@ -24,6 +24,7 @@ namespace CarCareTracker.Helper
bool WidgetsExist(); bool WidgetsExist();
bool SaveWidgets(string widgetsData); bool SaveWidgets(string widgetsData);
bool DeleteWidgets(); bool DeleteWidgets();
string WriteFileToTemp(string fileName, string data);
} }
public class FileHelper : IFileHelper public class FileHelper : IFileHelper
{ {
@ -514,5 +515,25 @@ namespace CarCareTracker.Helper
return false; return false;
} }
} }
public string WriteFileToTemp(string fileName, string data)
{
try
{
//check if temp folder exists
var tempFolder = GetFullFilePath("/temp", false);
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
var tempFileName = $"/temp/{fileName}";
var fullTempFileName = GetFullFilePath(tempFileName, false);
File.WriteAllText(fullTempFileName, data);
return tempFileName;
} catch (Exception ex)
{
_logger.LogError(ex.Message);
return string.Empty;
}
}
} }
} }

View File

@ -9,7 +9,6 @@ namespace CarCareTracker.Helper
string Translate(string userLanguage, string text); string Translate(string userLanguage, string text);
Dictionary<string, string> GetTranslations(string userLanguage); Dictionary<string, string> GetTranslations(string userLanguage);
OperationResponse SaveTranslation(string userLanguage, Dictionary<string, string> translations); OperationResponse SaveTranslation(string userLanguage, Dictionary<string, string> translations);
string ExportTranslation(Dictionary<string, string> translations);
} }
public class TranslationHelper : ITranslationHelper public class TranslationHelper : ITranslationHelper
{ {
@ -167,31 +166,5 @@ namespace CarCareTracker.Helper
return OperationResponse.Failed(); return OperationResponse.Failed();
} }
} }
public string ExportTranslation(Dictionary<string, string> translations)
{
try
{
var tempFileName = $"/temp/{Guid.NewGuid()}.json";
string uploadDirectory = _fileHelper.GetFullFilePath("temp/", false);
if (!Directory.Exists(uploadDirectory))
{
Directory.CreateDirectory(uploadDirectory);
}
var saveFilePath = _fileHelper.GetFullFilePath(tempFileName, false);
//standardize translation format for export only.
Dictionary<string, string> sortedTranslations = new Dictionary<string, string>();
foreach (var translation in translations.OrderBy(x => x.Key))
{
sortedTranslations.Add(translation.Key, translation.Value);
};
File.WriteAllText(saveFilePath, JsonSerializer.Serialize(sortedTranslations, new JsonSerializerOptions { WriteIndented = true }));
return tempFileName;
}
catch(Exception ex)
{
_logger.LogError(ex.Message);
return string.Empty;
}
}
} }
} }

View File

@ -184,6 +184,22 @@ namespace CarCareTracker.Models
Action = $"{userName} {GetFriendlyActionType(actionType)} Description: {planRecord.Description}" Action = $"{userName} {GetFriendlyActionType(actionType)} Description: {planRecord.Description}"
}; };
} }
public static WebHookPayload FromInspectionRecord(InspectionRecord inspectionRecord, string actionType, string userName)
{
Dictionary<string, string> payloadDictionary = new Dictionary<string, string>();
payloadDictionary.Add("user", userName);
payloadDictionary.Add("description", inspectionRecord.Description);
payloadDictionary.Add("vehicleId", inspectionRecord.VehicleId.ToString());
payloadDictionary.Add("cost", inspectionRecord.Cost.ToString("F2"));
return new WebHookPayload
{
Type = actionType,
Data = payloadDictionary,
VehicleId = inspectionRecord.VehicleId.ToString(),
Username = userName,
Action = $"{userName} {GetFriendlyActionType(actionType)} Description: {inspectionRecord.Description}"
};
}
public static WebHookPayload FromReminderRecord(ReminderRecord reminderRecord, string actionType, string userName) public static WebHookPayload FromReminderRecord(ReminderRecord reminderRecord, string actionType, string userName)
{ {
Dictionary<string, string> payloadDictionary = new Dictionary<string, string>(); Dictionary<string, string> payloadDictionary = new Dictionary<string, string>();