From 25952cce5063201fd00db22854631d4fe9bb799a Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Tue, 19 Nov 2024 12:26:05 -0700 Subject: [PATCH] Clean up OperationResponse method Part 1 --- Controllers/AdminController.cs | 2 +- Controllers/FilesController.cs | 4 +-- Controllers/HomeController.cs | 8 ++--- Controllers/MigrationController.cs | 10 +++--- Controllers/Vehicle/PlanController.cs | 14 ++++---- Controllers/Vehicle/ReportController.cs | 4 +-- Controllers/VehicleController.cs | 4 +-- Helper/MailHelper.cs | 28 +++++++-------- Helper/StaticHelper.cs | 20 +++++------ Helper/TranslationHelper.cs | 8 ++--- Logic/LoginLogic.cs | 46 ++++++++++++------------- Logic/UserLogic.cs | 8 ++--- Models/OperationResponse.cs | 21 +++++++++-- Views/Vehicle/_Collaborators.cshtml | 2 ++ 14 files changed, 99 insertions(+), 80 deletions(-) diff --git a/Controllers/AdminController.cs b/Controllers/AdminController.cs index 3a8fa5b..6e911b1 100644 --- a/Controllers/AdminController.cs +++ b/Controllers/AdminController.cs @@ -55,7 +55,7 @@ namespace CarCareTracker.Controllers } } } - var successResponse = new OperationResponse { Success = true, Message = "Token Generated!" }; + var successResponse = OperationResponse.Succeed("Token Generated!"); return Json(successResponse); } else { diff --git a/Controllers/FilesController.cs b/Controllers/FilesController.cs index 7e2361a..902c2e6 100644 --- a/Controllers/FilesController.cs +++ b/Controllers/FilesController.cs @@ -32,7 +32,7 @@ namespace CarCareTracker.Controllers var originalFileName = Path.GetFileNameWithoutExtension(file.FileName); if (originalFileName == "en_US") { - return Json(new OperationResponse { Success = false, Message = "The translation file name en_US is reserved." }); + return Json(OperationResponse.Failed("The translation file name en_US is reserved.")); } var fileName = UploadFile(file); //move file from temp to translation folder. @@ -43,7 +43,7 @@ namespace CarCareTracker.Controllers var result = _fileHelper.RenameFile(uploadedFilePath, originalFileName); return Json(new OperationResponse { Success = result, Message = string.Empty }); } - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } [HttpPost] diff --git a/Controllers/HomeController.cs b/Controllers/HomeController.cs index 10da731..1d2d807 100644 --- a/Controllers/HomeController.cs +++ b/Controllers/HomeController.cs @@ -280,12 +280,12 @@ namespace CarCareTracker.Controllers var result = _loginLogic.UpdateUserDetails(userId, userAccount); return Json(result); } - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } catch (Exception ex) { _logger.LogError(ex.Message); - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } } [HttpGet] @@ -496,13 +496,13 @@ namespace CarCareTracker.Controllers return Json(new OperationResponse() { Success = true, Message = $"{translationsDownloaded} Translations Downloaded" }); } else { - return Json(new OperationResponse() { Success = false, Message = "No Translations Downloaded" }); + return Json(OperationResponse.Failed("No Translations Downloaded")); } } catch (Exception ex) { _logger.LogError($"Unable to retrieve translations: {ex.Message}"); - return Json(new OperationResponse() { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } } public ActionResult GetVehicleSelector(int vehicleId) diff --git a/Controllers/MigrationController.cs b/Controllers/MigrationController.cs index edfc2ba..31dda6f 100644 --- a/Controllers/MigrationController.cs +++ b/Controllers/MigrationController.cs @@ -66,7 +66,7 @@ namespace CarCareTracker.Controllers { if (string.IsNullOrWhiteSpace(_configHelper.GetServerPostgresConnection())) { - return Json(new OperationResponse { Success = false, Message = "Postgres connection not set up" }); + return Json(OperationResponse.Failed("Postgres connection not set up")); } var tempFolder = $"temp/{Guid.NewGuid()}"; var tempPath = $"{tempFolder}/cartracker.db"; @@ -424,19 +424,19 @@ namespace CarCareTracker.Controllers catch (Exception ex) { _logger.LogError(ex.Message); - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } } public IActionResult Import(string fileName) { if (string.IsNullOrWhiteSpace(_configHelper.GetServerPostgresConnection())) { - return Json(new OperationResponse { Success = false, Message = "Postgres connection not set up" }); + return Json(OperationResponse.Failed("Postgres connection not set up")); } var fullFileName = _fileHelper.GetFullFilePath(fileName); if (string.IsNullOrWhiteSpace(fullFileName)) { - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } try { @@ -749,7 +749,7 @@ namespace CarCareTracker.Controllers catch (Exception ex) { _logger.LogError(ex.Message); - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } } } diff --git a/Controllers/Vehicle/PlanController.cs b/Controllers/Vehicle/PlanController.cs index 6ee1858..493c080 100644 --- a/Controllers/Vehicle/PlanController.cs +++ b/Controllers/Vehicle/PlanController.cs @@ -47,7 +47,7 @@ namespace CarCareTracker.Controllers var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplatesByVehicleId(planRecord.VehicleId).Where(x => x.Description == planRecord.Description).Any(); if (planRecord.Id == default && existingRecord) { - return Json(new OperationResponse { Success = false, Message = "A template with that description already exists for this vehicle" }); + return Json(OperationResponse.Failed("A template with that description already exists for this vehicle")); } planRecord.Files = planRecord.Files.Select(x => { return new UploadedFiles { Name = x.Name, Location = _fileHelper.MoveFileFromTemp(x.Location, "documents/") }; }).ToList(); var result = _planRecordTemplateDataAccess.SavePlanRecordTemplateToVehicle(planRecord); @@ -72,7 +72,7 @@ namespace CarCareTracker.Controllers var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplateById(planRecordTemplateId); if (existingRecord.Id == default) { - return Json(new OperationResponse { Success = false, Message = "Unable to find template" }); + return Json(OperationResponse.Failed("Unable to find template")); } if (existingRecord.Supplies.Any()) { @@ -81,7 +81,7 @@ namespace CarCareTracker.Controllers } else { - return Json(new OperationResponse { Success = false, Message = "Template has No Supplies" }); + return Json(OperationResponse.Failed("Template has No Supplies")); } } [HttpPost] @@ -90,7 +90,7 @@ namespace CarCareTracker.Controllers var existingRecord = _planRecordTemplateDataAccess.GetPlanRecordTemplateById(planRecordTemplateId); if (existingRecord.Id == default) { - return Json(new OperationResponse { Success = false, Message = "Unable to find template" }); + return Json(OperationResponse.Failed("Unable to find template")); } if (existingRecord.Supplies.Any()) { @@ -98,11 +98,11 @@ namespace CarCareTracker.Controllers var supplyAvailability = CheckSupplyRecordsAvailability(existingRecord.Supplies); if (supplyAvailability.Any(x => x.Missing)) { - return Json(new OperationResponse { Success = false, Message = "Missing Supplies, Please Delete This Template and Recreate It." }); + return Json(OperationResponse.Failed("Missing Supplies, Please Delete This Template and Recreate It.")); } else if (supplyAvailability.Any(x => x.Insufficient)) { - return Json(new OperationResponse { Success = false, Message = "Insufficient Supplies" }); + return Json(OperationResponse.Failed("Insufficient Supplies")); } } if (existingRecord.ReminderRecordId != default) @@ -111,7 +111,7 @@ namespace CarCareTracker.Controllers var existingReminder = _reminderRecordDataAccess.GetReminderRecordById(existingRecord.ReminderRecordId); if (existingReminder is null || existingReminder.Id == default || !existingReminder.IsRecurring) { - return Json(new OperationResponse { Success = false, Message = "Missing or Non-recurring Reminder, Please Delete This Template and Recreate It." }); + return Json(OperationResponse.Failed("Missing or Non-recurring Reminder, Please Delete This Template and Recreate It.")); } } //populate createdDate diff --git a/Controllers/Vehicle/ReportController.cs b/Controllers/Vehicle/ReportController.cs index 7bfe25e..f138c41 100644 --- a/Controllers/Vehicle/ReportController.cs +++ b/Controllers/Vehicle/ReportController.cs @@ -311,13 +311,13 @@ namespace CarCareTracker.Controllers var result = _fileHelper.MakeAttachmentsExport(attachmentData); if (string.IsNullOrWhiteSpace(result)) { - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } return Json(new OperationResponse { Success = true, Message = result }); } else { - return Json(new OperationResponse { Success = false, Message = "No Attachments Found" }); + return Json(OperationResponse.Failed("No Attachments Found")); } } public IActionResult GetReportParameters() diff --git a/Controllers/VehicleController.cs b/Controllers/VehicleController.cs index d11fdc7..5386559 100644 --- a/Controllers/VehicleController.cs +++ b/Controllers/VehicleController.cs @@ -188,7 +188,7 @@ namespace CarCareTracker.Controllers } else { - return Json(new OperationResponse { Success = false, Message = "Both vehicles already have identical collaborators" }); + return Json(OperationResponse.Failed("Both vehicles already have identical collaborators")); } } return Json(new OperationResponse { Success = true, Message = "Collaborators Copied" }); @@ -196,7 +196,7 @@ namespace CarCareTracker.Controllers catch (Exception ex) { _logger.LogError(ex.Message); - return Json(new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }); + return Json(OperationResponse.Failed()); } } diff --git a/Helper/MailHelper.cs b/Helper/MailHelper.cs index 1cc1f4c..0e7b770 100644 --- a/Helper/MailHelper.cs +++ b/Helper/MailHelper.cs @@ -31,10 +31,10 @@ namespace CarCareTracker.Helper { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) { - return new OperationResponse { Success = false, Message = "SMTP Server Not Setup" }; + return OperationResponse.Failed("SMTP Server Not Setup"); } if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) { - return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + return OperationResponse.Failed("Email Address or Token is invalid"); } string emailSubject = "Your Registration Token for LubeLogger"; string emailBody = $"A token has been generated on your behalf, please complete your registration for LubeLogger using the token: {token}"; @@ -44,18 +44,18 @@ namespace CarCareTracker.Helper return new OperationResponse { Success = true, Message = "Email Sent!" }; } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } public OperationResponse NotifyUserForPasswordReset(string emailAddress, string token) { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) { - return new OperationResponse { Success = false, Message = "SMTP Server Not Setup" }; + return OperationResponse.Failed("SMTP Server Not Setup"); } if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) { - return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + return OperationResponse.Failed("Email Address or Token is invalid"); } string emailSubject = "Your Password Reset Token for LubeLogger"; string emailBody = $"A token has been generated on your behalf, please reset your password for LubeLogger using the token: {token}"; @@ -66,18 +66,18 @@ namespace CarCareTracker.Helper } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } public OperationResponse NotifyUserForAccountUpdate(string emailAddress, string token) { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) { - return new OperationResponse { Success = false, Message = "SMTP Server Not Setup" }; + return OperationResponse.Failed("SMTP Server Not Setup"); } if (string.IsNullOrWhiteSpace(emailAddress) || string.IsNullOrWhiteSpace(token)) { - return new OperationResponse { Success = false, Message = "Email Address or Token is invalid" }; + return OperationResponse.Failed("Email Address or Token is invalid"); } string emailSubject = "Your User Account Update Token for LubeLogger"; string emailBody = $"A token has been generated on your behalf, please update your account for LubeLogger using the token: {token}"; @@ -88,22 +88,22 @@ namespace CarCareTracker.Helper } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } public OperationResponse NotifyUserForReminders(Vehicle vehicle, List emailAddresses, List reminders) { if (string.IsNullOrWhiteSpace(mailConfig.EmailServer)) { - return new OperationResponse { Success = false, Message = "SMTP Server Not Setup" }; + return OperationResponse.Failed("SMTP Server Not Setup"); } if (!emailAddresses.Any()) { - return new OperationResponse { Success = false, Message = "No recipients could be found" }; + return OperationResponse.Failed("No recipients could be found"); } if (!reminders.Any()) { - return new OperationResponse { Success = false, Message = "No reminders could be found" }; + return OperationResponse.Failed("No reminders could be found"); } //get email template, this file has to exist since it's a static file. var emailTemplatePath = _fileHelper.GetFullFilePath(StaticHelper.ReminderEmailTemplate); @@ -126,11 +126,11 @@ namespace CarCareTracker.Helper return new OperationResponse { Success = true, Message = "Email Sent!" }; } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } catch (Exception ex) { - return new OperationResponse { Success = false, Message = ex.Message }; + return OperationResponse.Failed(ex.Message); } } private bool SendEmail(List emailTo, string emailSubject, string emailBody) { diff --git a/Helper/StaticHelper.cs b/Helper/StaticHelper.cs index 2039967..0e22490 100644 --- a/Helper/StaticHelper.cs +++ b/Helper/StaticHelper.cs @@ -9,16 +9,16 @@ namespace CarCareTracker.Helper /// public static class StaticHelper { - public static string VersionNumber = "1.4.0"; - public static string DbName = "data/cartracker.db"; - public static string UserConfigPath = "config/userConfig.json"; - public static string AdditionalWidgetsPath = "data/widgets.html"; - public static string GenericErrorMessage = "An error occurred, please try again later"; - public static string ReminderEmailTemplate = "defaults/reminderemailtemplate.txt"; - public static string DefaultAllowedFileExtensions = ".png,.jpg,.jpeg,.pdf,.xls,.xlsx,.docx"; - public static string SponsorsPath = "https://hargata.github.io/hargata/sponsors.json"; - public static string TranslationPath = "https://hargata.github.io/lubelog_translations"; - public static string TranslationDirectoryPath = $"{TranslationPath}/directory.json"; + public const string VersionNumber = "1.4.0"; + public const string DbName = "data/cartracker.db"; + public const string UserConfigPath = "config/userConfig.json"; + public const string AdditionalWidgetsPath = "data/widgets.html"; + public const string GenericErrorMessage = "An error occurred, please try again later"; + public const string ReminderEmailTemplate = "defaults/reminderemailtemplate.txt"; + public const string DefaultAllowedFileExtensions = ".png,.jpg,.jpeg,.pdf,.xls,.xlsx,.docx"; + public const string SponsorsPath = "https://hargata.github.io/hargata/sponsors.json"; + public const string TranslationPath = "https://hargata.github.io/lubelog_translations"; + public const string TranslationDirectoryPath = $"{TranslationPath}/directory.json"; public const string ReportNote = "Report generated by LubeLogger, a Free and Open Source Vehicle Maintenance Tracker - LubeLogger.com"; public static string GetTitleCaseReminderUrgency(ReminderUrgency input) { diff --git a/Helper/TranslationHelper.cs b/Helper/TranslationHelper.cs index b4fab9f..a0767f5 100644 --- a/Helper/TranslationHelper.cs +++ b/Helper/TranslationHelper.cs @@ -130,15 +130,15 @@ namespace CarCareTracker.Helper bool isDefaultLanguage = userLanguage == "en_US"; if (isDefaultLanguage && !create) { - return new OperationResponse { Success = false, Message = "The translation file name en_US is reserved." }; + return OperationResponse.Failed("The translation file name en_US is reserved."); } if (string.IsNullOrWhiteSpace(userLanguage)) { - return new OperationResponse { Success = false, Message = "File name is not provided." }; + return OperationResponse.Failed("File name is not provided."); } if (!translations.Any()) { - return new OperationResponse { Success = false, Message = "Translation has no data." }; + return OperationResponse.Failed("Translation has no data."); } var translationFilePath = isDefaultLanguage ? _fileHelper.GetFullFilePath($"/defaults/en_US.json") : _fileHelper.GetFullFilePath($"/translations/{userLanguage}.json", false); try @@ -164,7 +164,7 @@ namespace CarCareTracker.Helper catch (Exception ex) { _logger.LogError(ex.Message); - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } public string ExportTranslation(Dictionary translations) diff --git a/Logic/LoginLogic.cs b/Logic/LoginLogic.cs index 49798aa..73be68e 100644 --- a/Logic/LoginLogic.cs +++ b/Logic/LoginLogic.cs @@ -71,13 +71,13 @@ namespace CarCareTracker.Logic var existingUser = _userData.GetUserRecordById(userId); if (existingUser.Id == default) { - return new OperationResponse { Success = false, Message = "Invalid user" }; + return OperationResponse.Failed("Invalid user"); } //validate user token var existingToken = _tokenData.GetTokenRecordByBody(credentials.Token); if (existingToken.Id == default || existingToken.EmailAddress != existingUser.EmailAddress) { - return new OperationResponse { Success = false, Message = "Invalid Token" }; + return OperationResponse.Failed("Invalid Token"); } if (!string.IsNullOrWhiteSpace(credentials.UserName) && existingUser.UserName != credentials.UserName) { @@ -85,7 +85,7 @@ namespace CarCareTracker.Logic var existingUserWithUserName = _userData.GetUserRecordByUserName(credentials.UserName); if (existingUserWithUserName.Id != default) { - return new OperationResponse { Success = false, Message = "Username already taken" }; + return OperationResponse.Failed("Username already taken"); } existingUser.UserName = credentials.UserName; } @@ -95,7 +95,7 @@ namespace CarCareTracker.Logic var existingUserWithEmailAddress = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress); if (existingUserWithEmailAddress.Id != default) { - return new OperationResponse { Success = false, Message = "A user with that email already exists" }; + return OperationResponse.Failed("A user with that email already exists"); } existingUser.EmailAddress = credentials.EmailAddress; } @@ -115,21 +115,21 @@ namespace CarCareTracker.Logic var existingToken = _tokenData.GetTokenRecordByBody(credentials.Token); if (existingToken.Id == default || existingToken.EmailAddress != credentials.EmailAddress) { - return new OperationResponse { Success = false, Message = "Invalid Token" }; + return OperationResponse.Failed("Invalid Token"); } if (string.IsNullOrWhiteSpace(credentials.EmailAddress) || string.IsNullOrWhiteSpace(credentials.UserName)) { - return new OperationResponse { Success = false, Message = "Username cannot be blank" }; + return OperationResponse.Failed("Username cannot be blank"); } var existingUser = _userData.GetUserRecordByUserName(credentials.UserName); if (existingUser.Id != default) { - return new OperationResponse { Success = false, Message = "Username already taken" }; + return OperationResponse.Failed("Username already taken"); } var existingUserWithEmail = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress); if (existingUserWithEmail.Id != default) { - return new OperationResponse { Success = false, Message = "A user with that email already exists" }; + return OperationResponse.Failed("A user with that email already exists"); } _tokenData.DeleteToken(existingToken.Id); var newUser = new UserData() @@ -145,7 +145,7 @@ namespace CarCareTracker.Logic } else { - return new OperationResponse { Success = false, Message = "Something went wrong, please try again later." }; + return OperationResponse.Failed("Something went wrong, please try again later."); } } //handles user registration @@ -155,22 +155,22 @@ namespace CarCareTracker.Logic var existingToken = _tokenData.GetTokenRecordByBody(credentials.Token); if (existingToken.Id == default || existingToken.EmailAddress != credentials.EmailAddress) { - return new OperationResponse { Success = false, Message = "Invalid Token" }; + return OperationResponse.Failed("Invalid Token"); } //token is valid, check if username and password is acceptable and that username is unique. if (string.IsNullOrWhiteSpace(credentials.EmailAddress) || string.IsNullOrWhiteSpace(credentials.UserName) || string.IsNullOrWhiteSpace(credentials.Password)) { - return new OperationResponse { Success = false, Message = "Neither username nor password can be blank" }; + return OperationResponse.Failed("Neither username nor password can be blank"); } var existingUser = _userData.GetUserRecordByUserName(credentials.UserName); if (existingUser.Id != default) { - return new OperationResponse { Success = false, Message = "Username already taken" }; + return OperationResponse.Failed("Username already taken"); } var existingUserWithEmail = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress); if (existingUserWithEmail.Id != default) { - return new OperationResponse { Success = false, Message = "A user with that email already exists" }; + return OperationResponse.Failed("A user with that email already exists"); } //username is unique then we delete the token and create the user. _tokenData.DeleteToken(existingToken.Id); @@ -187,7 +187,7 @@ namespace CarCareTracker.Logic } else { - return new OperationResponse { Success = false, Message = "Something went wrong, please try again later." }; + return OperationResponse.Failed(); } } /// @@ -212,17 +212,17 @@ namespace CarCareTracker.Logic var existingToken = _tokenData.GetTokenRecordByBody(credentials.Token); if (existingToken.Id == default || existingToken.EmailAddress != credentials.EmailAddress) { - return new OperationResponse { Success = false, Message = "Invalid Token" }; + return OperationResponse.Failed("Invalid Token"); } if (string.IsNullOrWhiteSpace(credentials.Password)) { - return new OperationResponse { Success = false, Message = "New Password cannot be blank" }; + return OperationResponse.Failed("New Password cannot be blank"); } //if token is valid. var existingUser = _userData.GetUserRecordByEmailAddress(credentials.EmailAddress); if (existingUser.Id == default) { - return new OperationResponse { Success = false, Message = "Unable to locate user" }; + return OperationResponse.Failed("Unable to locate user"); } existingUser.Password = GetHash(credentials.Password); var result = _userData.SaveUserRecord(existingUser); @@ -233,7 +233,7 @@ namespace CarCareTracker.Logic return new OperationResponse { Success = true, Message = "Password resetted, you will be redirected to login page shortly." }; } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } /// @@ -310,7 +310,7 @@ namespace CarCareTracker.Logic var existingToken = _tokenData.GetTokenRecordByEmailAddress(emailAddress); if (existingToken.Id != default) { - return new OperationResponse { Success = false, Message = "There is an existing token tied to this email address" }; + return OperationResponse.Failed("There is an existing token tied to this email address"); } var token = new Token() { @@ -323,7 +323,7 @@ namespace CarCareTracker.Logic result = _mailHelper.NotifyUserForRegistration(emailAddress, token.Body).Success; if (!result) { - return new OperationResponse { Success = false, Message = "Token Generated, but Email failed to send, please check your SMTP settings." }; + return OperationResponse.Failed("Token Generated, but Email failed to send, please check your SMTP settings."); } } if (result) @@ -332,7 +332,7 @@ namespace CarCareTracker.Logic } else { - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } } public bool DeleteUserToken(int tokenId) @@ -351,7 +351,7 @@ namespace CarCareTracker.Logic var existingUser = _userData.GetUserRecordByUserName(credentials.UserName); if (existingUser.Id == default) { - return new OperationResponse { Success = false, Message = "Unable to find user" }; + return OperationResponse.Failed("Unable to find user"); } var newPassword = Guid.NewGuid().ToString().Substring(0, 8); existingUser.Password = GetHash(newPassword); @@ -362,7 +362,7 @@ namespace CarCareTracker.Logic } else { - return new OperationResponse { Success = false, Message = "Something went wrong, please try again later." }; + return OperationResponse.Failed(); } } #endregion diff --git a/Logic/UserLogic.cs b/Logic/UserLogic.cs index 0d908bf..9b6bdbc 100644 --- a/Logic/UserLogic.cs +++ b/Logic/UserLogic.cs @@ -51,16 +51,16 @@ namespace CarCareTracker.Logic var userAccess = _userAccess.GetUserAccessByVehicleAndUserId(existingUser.Id, vehicleId); if (userAccess != null) { - return new OperationResponse { Success = false, Message = "User is already a collaborator" }; + return OperationResponse.Failed("User is already a collaborator"); } var result = AddUserAccessToVehicle(existingUser.Id, vehicleId); if (result) { - return new OperationResponse { Success = true, Message = "Collaborator Added" }; + return OperationResponse.Succeed("Collaborator Added"); } - return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + return OperationResponse.Failed(); } - return new OperationResponse { Success = false, Message = $"Unable to find user {username} in the system" }; + return OperationResponse.Failed($"Unable to find user {username} in the system"); } public bool DeleteCollaboratorFromVehicle(int userId, int vehicleId) { diff --git a/Models/OperationResponse.cs b/Models/OperationResponse.cs index a5be6c7..b74cc75 100644 --- a/Models/OperationResponse.cs +++ b/Models/OperationResponse.cs @@ -1,8 +1,25 @@ -namespace CarCareTracker.Models +using CarCareTracker.Helper; + +namespace CarCareTracker.Models { - public class OperationResponse + public class OperationResponseBase { public bool Success { get; set; } public string Message { get; set; } } + public class OperationResponse: OperationResponseBase + { + public static OperationResponse Succeed(string message) + { + return new OperationResponse { Success = true, Message = message }; + } + public static OperationResponse Failed(string message = "") + { + if (string.IsNullOrWhiteSpace(message)) + { + message = StaticHelper.GenericErrorMessage; + } + return new OperationResponse { Success = false, Message = message}; + } + } } diff --git a/Views/Vehicle/_Collaborators.cshtml b/Views/Vehicle/_Collaborators.cshtml index 27da881..9ce0ad1 100644 --- a/Views/Vehicle/_Collaborators.cshtml +++ b/Views/Vehicle/_Collaborators.cshtml @@ -42,6 +42,7 @@ function deleteCollaborator(userId, vehicleId) { $.post('/Vehicle/DeleteCollaboratorFromVehicle', {userId: userId, vehicleId: vehicleId}, function(data){ if (data) { + successToast('Collaborator Removed'); refreshCollaborators(); } else { errorToast(genericErrorMessage()); @@ -68,6 +69,7 @@ var vehicleId = GetVehicleId().vehicleId; $.post('/Vehicle/AddCollaboratorsToVehicle', { username: result.value.userName, vehicleId: vehicleId }, function (data) { if (data.success) { + successToast(data.message); refreshCollaborators(); } else { errorToast(data.message)