Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Conditional OperationalResponse constructor. #722

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 36 additions & 107 deletions Controllers/APIController.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Controllers/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public IActionResult HandleTranslationFileUpload(IFormFile file)
if (!string.IsNullOrWhiteSpace(uploadedFilePath))
{
var result = _fileHelper.RenameFile(uploadedFilePath, originalFileName);
return Json(new OperationResponse { Success = result, Message = string.Empty });
return Json(OperationResponse.Conditional(result));
}
return Json(OperationResponse.Failed());
}
Expand Down
2 changes: 1 addition & 1 deletion Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public async Task<IActionResult> DownloadAllTranslations()
}
if (translationsDownloaded > 0)
{
return Json(new OperationResponse() { Success = true, Message = $"{translationsDownloaded} Translations Downloaded" });
return Json(OperationResponse.Succeed($"{translationsDownloaded} Translations Downloaded"));
} else
{
return Json(OperationResponse.Failed("No Translations Downloaded"));
Expand Down
4 changes: 2 additions & 2 deletions Controllers/MigrationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public IActionResult Export()
#endregion
var destFilePath = $"{fullFolderPath}.zip";
ZipFile.CreateFromDirectory(fullFolderPath, destFilePath);
return Json(new OperationResponse { Success = true, Message = $"/{tempFolder}.zip" });
return Json(OperationResponse.Succeed($"/{tempFolder}.zip"));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -744,7 +744,7 @@ public IActionResult Import(string fileName)
}
}
#endregion
return Json(new OperationResponse { Success = true, Message = "Data Imported Successfully" });
return Json(OperationResponse.Succeed("Data Imported Successfully"));
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions Controllers/Vehicle/PlanController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public IActionResult SavePlanRecordTemplateToVehicleId(PlanRecordInput planRecor
}
planRecord.Files = planRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList();
var result = _planRecordTemplateDataAccess.SavePlanRecordTemplateToVehicle(planRecord);
return Json(new OperationResponse { Success = result, Message = result ? "Template Added" : StaticHelper.GenericErrorMessage });
return Json(OperationResponse.Conditional(result, "Template Added", string.Empty));
}
[TypeFilter(typeof(CollaboratorFilter))]
[HttpGet]
Expand Down Expand Up @@ -127,7 +127,7 @@ public IActionResult ConvertPlanRecordTemplateToPlanRecord(int planRecordTemplat
}
}
var result = _planRecordDataAccess.SavePlanRecordToVehicle(existingRecord.ToPlanRecord());
return Json(new OperationResponse { Success = result, Message = result ? "Plan Record Added" : StaticHelper.GenericErrorMessage });
return Json(OperationResponse.Conditional(result, "Plan Record Added", string.Empty));
}
[HttpGet]
public IActionResult GetAddPlanRecordPartialView()
Expand Down
2 changes: 1 addition & 1 deletion Controllers/Vehicle/ReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public IActionResult GetVehicleAttachments(int vehicleId, List<ImportMode> expor
{
return Json(OperationResponse.Failed());
}
return Json(new OperationResponse { Success = true, Message = result });
return Json(OperationResponse.Succeed(result));
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion Controllers/VehicleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public IActionResult DuplicateVehicleCollaborators(int sourceVehicleId, int dest
return Json(OperationResponse.Failed("Both vehicles already have identical collaborators"));
}
}
return Json(new OperationResponse { Success = true, Message = "Collaborators Copied" });
return Json(OperationResponse.Succeed("Collaborators Copied"));
}
catch (Exception ex)
{
Expand Down
8 changes: 4 additions & 4 deletions Helper/MailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public OperationResponse NotifyUserForRegistration(string emailAddress, string t
var result = SendEmail(new List<string> { emailAddress }, emailSubject, emailBody);
if (result)
{
return new OperationResponse { Success = true, Message = "Email Sent!" };
return OperationResponse.Succeed("Email Sent!");
} else
{
return OperationResponse.Failed();
Expand All @@ -62,7 +62,7 @@ public OperationResponse NotifyUserForPasswordReset(string emailAddress, string
var result = SendEmail(new List<string> { emailAddress }, emailSubject, emailBody);
if (result)
{
return new OperationResponse { Success = true, Message = "Email Sent!" };
return OperationResponse.Succeed("Email Sent!");
}
else
{
Expand All @@ -84,7 +84,7 @@ public OperationResponse NotifyUserForAccountUpdate(string emailAddress, string
var result = SendEmail(new List<string> { emailAddress}, emailSubject, emailBody);
if (result)
{
return new OperationResponse { Success = true, Message = "Email Sent!" };
return OperationResponse.Succeed("Email Sent!");
}
else
{
Expand Down Expand Up @@ -123,7 +123,7 @@ public OperationResponse NotifyUserForReminders(Vehicle vehicle, List<string> em
var result = SendEmail(emailAddresses, emailSubject, emailBody);
if (result)
{
return new OperationResponse { Success = true, Message = "Email Sent!" };
return OperationResponse.Succeed("Email Sent!");
} else
{
return OperationResponse.Failed();
Expand Down
2 changes: 1 addition & 1 deletion Helper/TranslationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public OperationResponse SaveTranslation(string userLanguage, Dictionary<string,
//write to file
File.WriteAllText(translationFilePath, JsonSerializer.Serialize(translations));
}
return new OperationResponse { Success = true, Message = "Translation Updated" };
return OperationResponse.Succeed("Translation Updated");
}
catch (Exception ex)
{
Expand Down
14 changes: 7 additions & 7 deletions Logic/LoginLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public OperationResponse UpdateUserDetails(int userId, LoginModel credentials)
//delete token
_tokenData.DeleteToken(existingToken.Id);
var result = _userData.SaveUserRecord(existingUser);
return new OperationResponse { Success = result, Message = result ? "User Updated" : StaticHelper.GenericErrorMessage };
return OperationResponse.Conditional(result, "User Updated", string.Empty);
}
public OperationResponse RegisterOpenIdUser(LoginModel credentials)
{
Expand Down Expand Up @@ -141,7 +141,7 @@ public OperationResponse RegisterOpenIdUser(LoginModel credentials)
var result = _userData.SaveUserRecord(newUser);
if (result)
{
return new OperationResponse { Success = true, Message = "You will be logged in briefly." };
return OperationResponse.Succeed("You will be logged in briefly.");
}
else
{
Expand Down Expand Up @@ -183,7 +183,7 @@ public OperationResponse RegisterNewUser(LoginModel credentials)
var result = _userData.SaveUserRecord(newUser);
if (result)
{
return new OperationResponse { Success = true, Message = "You will be redirected to the login page briefly." };
return OperationResponse.Succeed("You will be redirected to the login page briefly.");
}
else
{
Expand All @@ -205,7 +205,7 @@ public OperationResponse RequestResetPassword(LoginModel credentials)
}
//for security purposes we want to always return true for this method.
//otherwise someone can spam the reset password method to sniff out users.
return new OperationResponse { Success = true, Message = "If your user exists in the system you should receive an email shortly with instructions on how to proceed." };
return OperationResponse.Succeed("If your user exists in the system you should receive an email shortly with instructions on how to proceed.");
}
public OperationResponse ResetPasswordByUser(LoginModel credentials)
{
Expand All @@ -230,7 +230,7 @@ public OperationResponse ResetPasswordByUser(LoginModel credentials)
_tokenData.DeleteToken(existingToken.Id);
if (result)
{
return new OperationResponse { Success = true, Message = "Password resetted, you will be redirected to login page shortly." };
return OperationResponse.Succeed("Password resetted, you will be redirected to login page shortly.");
} else
{
return OperationResponse.Failed();
Expand Down Expand Up @@ -328,7 +328,7 @@ public OperationResponse GenerateUserToken(string emailAddress, bool autoNotify)
}
if (result)
{
return new OperationResponse { Success = true, Message = "Token Generated!" };
return OperationResponse.Succeed("Token Generated!");
}
else
{
Expand Down Expand Up @@ -358,7 +358,7 @@ public OperationResponse ResetUserPassword(LoginModel credentials)
var result = _userData.SaveUserRecord(existingUser);
if (result)
{
return new OperationResponse { Success = true, Message = newPassword };
return OperationResponse.Succeed(newPassword);
}
else
{
Expand Down
10 changes: 9 additions & 1 deletion Models/OperationResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class OperationResponseBase
}
public class OperationResponse: OperationResponseBase
{
public static OperationResponse Succeed(string message)
public static OperationResponse Succeed(string message = "")
{
return new OperationResponse { Success = true, Message = message };
}
Expand All @@ -21,5 +21,13 @@ public static OperationResponse Failed(string message = "")
}
return new OperationResponse { Success = false, Message = message};
}
public static OperationResponse Conditional(bool result, string successMessage = "", string errorMessage = "")
{
if (string.IsNullOrWhiteSpace(errorMessage))
{
errorMessage = StaticHelper.GenericErrorMessage;
}
return new OperationResponse { Success = result, Message = result ? successMessage : errorMessage };
}
}
}