Skip to content

Commit 4a771a0

Browse files
committed
Ensure robust handling of missing Capacity.json file
Added logic across multiple files to dynamically create a default empty `Capacity.json` file when it is missing, ensuring the application remains functional. Updated `Capacity.razor` to log errors comprehensively and provide a fallback in-memory capacity object. Refactored `Calculations.cs` and `CalculateProposal.cs` to consistently handle missing files and log warnings. Improved the `Response.razor` UI by clarifying the purpose of the "Edit Capacity" button. Enhanced error handling and logging to track file creation and ensure fallback mechanisms are in place for seamless operation.
1 parent 0b88d42 commit 4a771a0

4 files changed

Lines changed: 113 additions & 9 deletions

File tree

RFPResponsePOC/RFPResponsePOC.Client/Pages/Capacity.razor

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,35 @@
602602
{
603603
return await File.ReadAllTextAsync($"{BasePath}//Capacity.json");
604604
}
605+
else
606+
{
607+
// Create a default empty capacity file if it doesn't exist
608+
var defaultCapacity = new CapacityRoot { Rooms = new List<Room>() };
609+
var defaultJson = JsonConvert.SerializeObject(defaultCapacity, Formatting.Indented);
610+
611+
// Ensure the directory exists
612+
var directory = Path.GetDirectoryName($"{BasePath}//Capacity.json");
613+
if (!Directory.Exists(directory))
614+
{
615+
Directory.CreateDirectory(directory);
616+
}
617+
618+
await File.WriteAllTextAsync($"{BasePath}//Capacity.json", defaultJson);
619+
await LogService.WriteToLogAsync($"[{DateTime.Now}] Created default empty Capacity.json file");
620+
621+
return defaultJson;
622+
}
605623
}
606624
catch (Exception ex)
607625
{
608-
// Log the exception if necessary
609-
Console.WriteLine($"Error reading Capacity.json: {ex.Message}");
626+
// Log the exception and create a fallback in-memory capacity data
627+
await LogService.WriteToLogAsync($"[{DateTime.Now}] ERROR: Failed to read/create Capacity.json - {ex.Message}");
628+
Console.WriteLine($"Error reading/creating Capacity.json: {ex.Message}");
629+
630+
// Return empty capacity as fallback
631+
var fallbackCapacity = new CapacityRoot { Rooms = new List<Room>() };
632+
return JsonConvert.SerializeObject(fallbackCapacity, Formatting.Indented);
610633
}
611-
return null;
612634
}
613635

614636
public void Dispose()

RFPResponsePOC/RFPResponsePOC.Client/Pages/Response.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
Placeholder="Select Template" Style="width: 200px;" Change="OnTemplateSelectionChanged" />
6363
<RadzenButton Text="Edit Template" Icon="edit" Style="background-color: gray;"
6464
ButtonStyle="ButtonStyle.Primary" Click="EditTemplate" />
65-
<RadzenButton Text="Edit Capacity" Icon="edit" ButtonStyle="ButtonStyle.Success" Click="EditCapacity" />
65+
<RadzenButton Text="Edit Capacity Chart (required for calculations)" Icon="edit" ButtonStyle="ButtonStyle.Success" Click="EditCapacity" />
6666
</div>
6767
}
6868
else

RFPResponsePOC/RFPResponsePOC.Client/Services/CalculateProposal.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,21 @@ public async Task<List<RoomAssignment>> CalculateAsync(string rfpText, List<Room
100100
var capacityFilePath = $"{_basePath}//Capacity.json";
101101
if (!File.Exists(capacityFilePath))
102102
{
103-
await _logService.WriteToLogAsync($"[{DateTime.Now}] ERROR: Capacity.json file not found at path: {capacityFilePath}");
104-
return assignments;
103+
await _logService.WriteToLogAsync($"[{DateTime.Now}] WARNING: Capacity.json file not found at path: {capacityFilePath}. Creating default empty file.");
104+
105+
// Create a default empty capacity file
106+
var defaultCapacity = new CapacityRoot { Rooms = new List<Room>() };
107+
var defaultJson = JsonConvert.SerializeObject(defaultCapacity, Formatting.Indented);
108+
109+
// Ensure the directory exists
110+
var directory = Path.GetDirectoryName(capacityFilePath);
111+
if (!Directory.Exists(directory))
112+
{
113+
Directory.CreateDirectory(directory);
114+
}
115+
116+
await File.WriteAllTextAsync(capacityFilePath, defaultJson);
117+
await _logService.WriteToLogAsync($"[{DateTime.Now}] Created default empty Capacity.json file at {capacityFilePath}");
105118
}
106119

107120
// Declare variables for deserialized data

RFPResponsePOC/RFPResponsePOC.Client/Services/Calculations.cs

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,30 @@ public static async Task<List<string>> ReCalculateProposal(List<ProposalRow> pro
173173
List<string> roomOptions;
174174
try
175175
{
176-
var capacityJson = await File.ReadAllTextAsync("/RFPResponseAPP/Capacity.json");
176+
var capacityFilePath = "/RFPResponseAPP/Capacity.json";
177+
string capacityJson;
178+
179+
if (File.Exists(capacityFilePath))
180+
{
181+
capacityJson = await File.ReadAllTextAsync(capacityFilePath);
182+
}
183+
else
184+
{
185+
// Create a default empty capacity file
186+
await logService.WriteToLogAsync($"[{DateTime.Now}] WARNING: Capacity.json not found. Creating default empty file.");
187+
var defaultCapacity = new CapacityRoot { Rooms = new List<Room>() };
188+
capacityJson = JsonConvert.SerializeObject(defaultCapacity, Formatting.Indented);
189+
190+
// Ensure the directory exists
191+
var directory = Path.GetDirectoryName(capacityFilePath);
192+
if (!Directory.Exists(directory))
193+
{
194+
Directory.CreateDirectory(directory);
195+
}
196+
197+
await File.WriteAllTextAsync(capacityFilePath, capacityJson);
198+
}
199+
177200
var capacity = JsonConvert.DeserializeObject<CapacityRoot>(capacityJson);
178201
roomOptions = capacity?.Rooms?.Select(r => r.Name).OrderBy(n => n).ToList() ?? new List<string>();
179202
}
@@ -294,7 +317,30 @@ public static async Task<List<string>> CalculateRoomsForExistingRows(List<Propos
294317
// Still return room options even if no calculations are needed
295318
try
296319
{
297-
var capacityJson = await File.ReadAllTextAsync("/RFPResponseAPP/Capacity.json");
320+
var capacityFilePath = "/RFPResponseAPP/Capacity.json";
321+
string capacityJson;
322+
323+
if (File.Exists(capacityFilePath))
324+
{
325+
capacityJson = await File.ReadAllTextAsync(capacityFilePath);
326+
}
327+
else
328+
{
329+
// Create a default empty capacity file
330+
await logService.WriteToLogAsync($"[{DateTime.Now}] WARNING: Capacity.json not found. Creating default empty file.");
331+
var defaultCapacity = new CapacityRoot { Rooms = new List<Room>() };
332+
capacityJson = JsonConvert.SerializeObject(defaultCapacity, Formatting.Indented);
333+
334+
// Ensure the directory exists
335+
var directory = Path.GetDirectoryName(capacityFilePath);
336+
if (!Directory.Exists(directory))
337+
{
338+
Directory.CreateDirectory(directory);
339+
}
340+
341+
await File.WriteAllTextAsync(capacityFilePath, capacityJson);
342+
}
343+
298344
var capacity = JsonConvert.DeserializeObject<CapacityRoot>(capacityJson);
299345
return capacity?.Rooms?.Select(r => r.Name).OrderBy(n => n).ToList() ?? new List<string>();
300346
}
@@ -363,7 +409,30 @@ public static async Task<List<string>> CalculateRoomsForExistingRows(List<Propos
363409
List<string> roomOptions;
364410
try
365411
{
366-
var capacityJson = await File.ReadAllTextAsync("/RFPResponseAPP/Capacity.json");
412+
var capacityFilePath = "/RFPResponseAPP/Capacity.json";
413+
string capacityJson;
414+
415+
if (File.Exists(capacityFilePath))
416+
{
417+
capacityJson = await File.ReadAllTextAsync(capacityFilePath);
418+
}
419+
else
420+
{
421+
// Create a default empty capacity file
422+
await logService.WriteToLogAsync($"[{DateTime.Now}] WARNING: Capacity.json not found. Creating default empty file.");
423+
var defaultCapacity = new CapacityRoot { Rooms = new List<Room>() };
424+
capacityJson = JsonConvert.SerializeObject(defaultCapacity, Formatting.Indented);
425+
426+
// Ensure the directory exists
427+
var directory = Path.GetDirectoryName(capacityFilePath);
428+
if (!Directory.Exists(directory))
429+
{
430+
Directory.CreateDirectory(directory);
431+
}
432+
433+
await File.WriteAllTextAsync(capacityFilePath, capacityJson);
434+
}
435+
367436
var capacity = JsonConvert.DeserializeObject<CapacityRoot>(capacityJson);
368437
roomOptions = capacity?.Rooms?.Select(r => r.Name).OrderBy(n => n).ToList() ?? new List<string>();
369438
}

0 commit comments

Comments
 (0)