/// <summary> /// This method will return the View where there project manager /// can save a project assignment /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <IActionResult> SaveProjectAssignment(WorkHourTracker.Web.Models.AssignProjectInput input) { if (TempData.Peek("userName") == null) { return(UserNotAllowedAccess(isUserLoggedIn: false)); } if (TempData.Peek("userRole").ToString() == "Employee") { return(UserNotAllowedAccess(isUserLoggedIn: true)); } //if the ModelState is invalid return the user to the AssignProject page and show them the validation errors if (!ModelState.IsValid) { List <string> errors = ModelState.Values.SelectMany(p => p.Errors.Select(x => x.ErrorMessage)).ToList(); TempData.Add("AssignProjectInvalid", errors); return(RedirectTo("ProjectManager", "AssignProject")); } // Create a WorkHourTrackerListResult object and initializes its properties var resultList = new WorkHourTrackerListResult() { Errors = new List <string>(), WorkHourTrackList = new List <dynamic>() }; // Since ViewModel input is valid, transform it into the DTO to transfer to other layers var databaseInput = new AssignProjectToEmployeeDatabaseInput(input.AssignedProjectName, input.AssignedUserName, input.Capacity, TempData.Peek("userName").ToString()); try { await _IProjectManagerDomain.AssignProject(databaseInput); resultList.Errors.Add("The project has been successfully assigned!"); TempData.Add("AssignedProjectSuccess", resultList.Errors); } catch (AssignAProjectException ex) { resultList.Errors.Add(ex.Message); TempData.Add("AssignedProjectError", resultList.Errors); } catch (Exception) { throw; } return(RedirectTo("ProjectManager", "AssignProject")); }
[ValidateAntiForgeryToken] //this is here to prevent XSS (cross site scripting attacks) public async Task <IActionResult> SaveNewProject(CreateProjectInput newProjectInput) { if (TempData.Peek("userName") == null) { return(UserNotAllowedAccess(isUserLoggedIn: false)); } if (TempData.Peek("userRole").ToString() == "Employee") { return(UserNotAllowedAccess(isUserLoggedIn: true)); } //if the ModelState is invalid return the user to the CreateProject page and show them the validation errors if (!ModelState.IsValid) { List <string> errors = ModelState.Values.SelectMany(p => p.Errors.Select(x => x.ErrorMessage)).ToList(); TempData.Add("CreateProjectInvalid", errors); return(RedirectTo("ProjectManager", "CreateProject")); } var resultList = new WorkHourTrackerListResult() { Errors = new List <string>(), WorkHourTrackList = new List <dynamic>() }; //the request is valid. Now, transform it into CreateProjectDatabaseInput var databaseInput = new CreateProjectDatabaseInput(newProjectInput.ProjectName, newProjectInput.ProjectCodeName, TempData.Peek("userName").ToString()); try { //send the request to the Domain layer await _IProjectManagerDomain.CreateNewProject(databaseInput); resultList.Errors.Add($"Project: {databaseInput.ProjectName} Project CodeName: {databaseInput.ProjectCodeName} has been created. Please assign the project to indivduals to being working on it."); TempData.Add("CreateProjectSuccess", resultList.Errors); } catch (Exception) { resultList.Errors.Add("The ProjectName or ProjectCodeName is already in use in the system. Please use another one."); TempData.Add("CreateProjectError", resultList.Errors); return(RedirectTo("ProjectManager", "CreateProject")); } return(RedirectTo("ProjectManager", "CreateProject")); }
public async Task <IActionResult> CreateUserAccount(CreateUserAccountInput input) { var resultList = new WorkHourTrackerListResult() { Errors = new List <string>(), WorkHourTrackList = new List <dynamic>() }; //If the model state is invalid return set the Errors property of WorkHourTrackListResult object if (!ModelState.IsValid) { resultList.Errors = ModelState.Values.SelectMany(p => p.Errors.Select(x => x.ErrorMessage)).ToList(); TempData.Add("ModelErrors", resultList.Errors); return(RedirectTo("Home", "DisplayCreateUserAccountPage")); } try { //the request is valid so transform to CreateUserAccountDatabaseInput var databaseInput = new CreateUserAccountDatabaseInput(input.UserName, input.UserPassword, input.UserRole, input.FirstName, input.LastName); //send the new account request to Domain layer await _IUserAccount.CreateUserAccount(databaseInput); } catch (CreateUserNameException) { resultList.Errors.Add($"The user name: {input.UserName} is already in use. Please, use a different one."); TempData.Add("ModelErrors", resultList.Errors); return(RedirectTo("Home", "DisplayCreateUserAccountPage")); } catch (Exception ex) { resultList.Errors.Add($"An unexpected error has occured."); TempData.Add("ModelErrors", resultList.Errors); return(RedirectTo("Home", "DisplayCreateUserAccountPage")); } TempData.Add("CreateUserAccountSuccess", $"User account for {input.UserName} was successfully created. You may log into the system now."); return(RedirectTo("Home", "UserLogin")); }
/// <summary> /// Save the user's track time data /// </summary> /// <param name="input"></param> /// <returns></returns> public async Task <IActionResult> SaveTrackTime(TrackTimeList input) { if (TempData.Peek("userName") == null) { return(UserNotAllowedAccess()); } var workHourTrackerList = new WorkHourTrackerListResult() { Errors = new List <string>(), WorkHourTrackList = new List <dynamic>() }; //get the start and end dates so we can display the track time details page when this operation is done string startOfWeek = input.UserTrackTimeList[0].StartDate; string lastOfWeek = input.UserTrackTimeList[0].EndDate; //if the ModelState is invalid return the user to the CreateProject page and show them the validation errors if (!ModelState.IsValid) { List <string> errors = ModelState.Values.SelectMany(p => p.Errors.Select(x => x.ErrorMessage)).ToList(); TempData.Add("SaveTrackTimeError", errors); return(RedirectToAction("DisplayTrackTimeDetails", "TrackTime", new { startDate = startOfWeek, endDate = lastOfWeek })); } try { //foreach record in the input list, save the new track time data foreach (var project in input.UserTrackTimeList) { var saveTrackTimeInput = new TrackTimeDatabaseInput(TempData.Peek("userGuid").ToString(), project.ProjectName, project.HourSun, project.HourMon, project.HourTues, project.HourWed, project.HourThurs, project.HourFri, project.HourSat, project.Comments, project.StartDate, project.EndDate); await _ITrackTimeDomain.InsertTrackTime(saveTrackTimeInput); } } catch (Exception) { throw; } workHourTrackerList.Errors.Add("TrackTime data has been saved!"); TempData.Add("SaveTrackTime", workHourTrackerList.Errors); return(RedirectToAction("DisplayTrackTimeDetails", "TrackTime", new { startDate = startOfWeek, endDate = lastOfWeek })); }
public async Task <IActionResult> ProcessLogin(UserLoginInput input) { //if the ModelState is invalid return the user to the CreateProject page and show them the validation errors if (!ModelState.IsValid) { List <string> errors = ModelState.Values.SelectMany(p => p.Errors.Select(x => x.ErrorMessage)).ToList(); TempData.Add("ProcessLoginError", errors); return(RedirectTo("Home", "UserLogin")); } var resultList = new WorkHourTrackerListResult() { Errors = new List <string>(), WorkHourTrackList = new List <dynamic>() }; try { //Transform the object into it's Model.Entities counter part for the other layers var userLoginDatabaseInput = new UserLoginDatabaseInput() { UserName = input.UserName, Password = input.Password }; var result = await _IUserAccount.UserLogin(userLoginDatabaseInput); //set up a dictionary containing the user's information var userDictionary = new Dictionary <string, object>() { { "userName", result.UserName }, { "userPassword", result.UserPassword }, { "userGuid", result.UserGuid.ToString() }, { "userRole", result.UserRole }, { "employeeGuid", result.EmployeeGuid }, { "firstName", result.FirstName }, { "lastName", result.LastName } }; //Clear out the TempData before adding to avoid Key collisions TempData.Clear(); //foreach through the userDictionary and add the key/value to the TempData foreach (var keyValuePair in userDictionary) { TempData.Add(keyValuePair.Key, keyValuePair.Value); } //Mark all of the data inside TempData for rentention TempData.Keep(); } catch (InvalidLoginException) { resultList.Errors.Add("The user name or password you entered is incorrect, please try again."); TempData.Add("LoginErrors", resultList.Errors); return(RedirectTo("Home", "UserLogin")); } catch (Exception ex) { resultList.Errors.Add("An unexpected error occured."); resultList.Errors.Add($"Exception Message: {ex.Message}"); resultList.Errors.Add($"Base Exception: {ex.GetBaseException()}"); TempData.Add("LoginErrors", resultList.Errors); return(RedirectTo("Home", "UserLogin")); } //Login successful redirect to the index return(RedirectTo("Home", "Index")); }