// private helper private async Task AddProjectNamesToViewModel(TimesheetEntryViewModel viewModel) { var currentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier); MyUser currentUser = await _userManager.FindByIdAsync(currentUserId); var roles = await _userManager.GetRolesAsync(currentUser); List <Project> projects = new List <Project>(); if (roles.Contains("Admin")) { projects = _context.DepartmentProjects .Include(dp => dp.Project) .Select(dp => dp.Project) .ToList(); } else if (roles.Contains("Manager") || roles.Contains("Employee")) { projects = _context.DepartmentProjects .Include(dp => dp.Project) .Where(dp => dp.DepartmentId == currentUser.DepartmentId) .Select(dp => dp.Project) .ToList(); } foreach (Project project in projects) { viewModel.ProjectNames.Add(project.Name); } }
// GET: TimesheetEntries/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var timesheetEntry = _context.TimesheetEntries.Include(t => t.RelatedUser).Include(t => t.RelatedProject).First(x => x.Id == id); if (timesheetEntry == null) { return(NotFound()); } TimesheetEntryViewModel viewModel = new TimesheetEntryViewModel { Id = timesheetEntry.Id, RelatedUserName = timesheetEntry.RelatedUser.UserName, ProjectName = timesheetEntry.RelatedProject.Name, DateCreated = timesheetEntry.DateCreated, HoursWorked = timesheetEntry.HoursWorked }; await this.AddUsernamesToViewModel(viewModel); await this.AddProjectNamesToViewModel(viewModel); return(View(viewModel)); }
// private helper private async Task AddUsernamesToViewModel(TimesheetEntryViewModel viewModel) { var currentUserId = User.FindFirstValue(ClaimTypes.NameIdentifier); MyUser currentUser = await _userManager.FindByIdAsync(currentUserId); var roles = await _userManager.GetRolesAsync(currentUser); List <MyUser> users = new List <MyUser>(); if (roles.Contains("Admin")) { users = _context.Users.ToList(); } else if (roles.Contains("Manager")) { users = _context.Users.Where(u => u.ManagerId.Equals(currentUser.Id)).ToList(); } else if (roles.Contains("Employee")) { users = _context.Users.Where(u => u.Id == currentUser.Id).ToList(); } foreach (MyUser user in users) { viewModel.UserNames.Add(user.UserName); } }
public IActionResult CreateTimeSheet(TimesheetEntryViewModel model) { var projects = _applicationDbContext.Projects.ToList(); var users = _applicationDbContext.Users.ToList(); Project project = null; MyUser user = null; foreach (var p in projects) { if (p.Name.Equals(model.RelatedProject)) { project = p; break; } } foreach (var u in users) { if (u.UserName.Equals(model.RelatedUser)) { user = u; break; } } TimesheetEntry t = new TimesheetEntry(user, project, model.DateCreated, model.HoursWorked); _applicationDbContext.Add(t); _applicationDbContext.SaveChanges(); ViewBag.Message = "TimeSheet saved successfully"; return(View("~/Views/Home/Index.cshtml")); }
public IActionResult Edit(int Id) { var model = new TimesheetEntryViewModel(); var users = _applicationDbContext.Users.ToList(); var projects = _applicationDbContext.Projects.ToList(); model.Id = Id; foreach (var p in projects) { if (p != null) { model.projects.Add(p.Name); } } foreach (var u in users) { if (u != null) { model.users.Add(u.UserName); } } ViewBag.Message = ""; return(View(model)); }
public IActionResult EditTimesheet(TimesheetEntryViewModel model) { var timesheet = _applicationDbContext.TimesheetEntries.First(t => t.Id == model.Id); timesheet.DateCreated = model.DateCreated; timesheet.HoursWorked = model.HoursWorked; var projects = _applicationDbContext.Projects.ToList(); var users = _applicationDbContext.Users.ToList(); Project project = null; MyUser user = null; foreach (var p in projects) { if (p.Name.Equals(model.RelatedProject)) { project = p; break; } } foreach (var u in users) { if (u.UserName.Equals(model.RelatedUser)) { user = u; break; } } timesheet.RelatedProject = project; timesheet.RelatedUser = user; _applicationDbContext.SaveChanges(); ViewBag.Message = "Edit successful"; return(View("~/Views/Home/Index.cshtml")); }
public TimesheetEntry MapViewModelToTimesheetEntry(TimesheetEntryViewModel viewModel) { MyUser relatedUser = _context.Users.First(user => user.UserName.Equals(viewModel.RelatedUserName)); Project relatedProject = _context.Projects.First(project => project.Name.Equals(viewModel.ProjectName)); return(new TimesheetEntry(viewModel.Id, relatedUser, relatedProject, viewModel.DateCreated ?? DateTime.Now, viewModel.HoursWorked)); }
public async Task <IActionResult> Delete(TimesheetEntryViewModel viewModel) { Project project = await _projectRepository.GetById(viewModel.ProjectId); TimesheetEntry entry = _mapper.ConvertFromViewModel(viewModel, project); await _timesheetEntryRepository.Delete(entry.Id); return(RedirectToAction(nameof(Index))); }
// GET: TimesheetEntries/Create public async Task <IActionResult> Create() { TimesheetEntryViewModel viewModel = new TimesheetEntryViewModel(); await this.AddUsernamesToViewModel(viewModel); await this.AddProjectNamesToViewModel(viewModel); return(View(viewModel)); }
private Button CreateSubmitButton(TimesheetEntryViewModel timesheet, Picker hours, Entry comment, Switch sickLeave) { var submitButton = new Button { Text = "Submit", Font = Font.BoldSystemFontOfSize(NamedSize.Medium), HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, }; submitButton.SetBinding <TimesheetEntryViewModel>(Button.CommandProperty, m => m.SaveTimesheetCommand); return(submitButton); }
public async Task <IActionResult> IndexAsync() { TimesheetEntryViewModel timesheetEntryViewModel = new TimesheetEntryViewModel(); //Get email of logged in user var result = (await _userManager.GetUserAsync(HttpContext.User))?.Email; if (result != null) { //find user using email var user = await _userManager.FindByEmailAsync(result); //find user role var role = await _userManager.GetRolesAsync(user); //if role admin return all timesheets if (role[0].Equals("Admin")) { timesheetEntryViewModel.timesheets = _applicationDbContext.TimesheetEntries.ToList(); } //if role manager return his timesheets + his employees' timesheets else if (role[0].Equals("Manager")) { var timesheets = _applicationDbContext.TimesheetEntries.ToList(); foreach (var t in timesheets) { if (t.RelatedUser == user) { timesheetEntryViewModel.timesheets.Add(t); } if (t.RelatedUser.Manager == user) { timesheetEntryViewModel.timesheets.Add(t); } } } else if (role[0].Equals("Employee")) { var timesheets = _applicationDbContext.TimesheetEntries.ToList(); foreach (var t in timesheets) { if (t.RelatedUser == user) { timesheetEntryViewModel.timesheets.Add(t); } } } } //timesheetEntryViewModel.timesheets = _applicationDbContext.TimesheetEntries.ToList(); return(View(timesheetEntryViewModel)); }
public TimesheetEntryViewModel ConvertToViewModel(TimesheetEntry entry) { TimesheetEntryViewModel viewModel = new TimesheetEntryViewModel(); if (entry != null) { viewModel.Id = entry.Id; viewModel.EntryDate = entry.EntryDate; viewModel.HoursWorked = entry.HoursWorked; viewModel.ProjectId = entry.ProjectId; viewModel.UserId = entry.UserId; } return(viewModel); }
public TimesheetEntry ConvertFromViewModel(TimesheetEntryViewModel viewModel, Project project) { TimesheetEntry entry = new TimesheetEntry { Id = viewModel.Id, //User = await _dbContext.FindAsync<TimesheetEntry>(viewModel.UserId), //Project = await _projectRepository.GetById(viewModel.ProjectId), EntryDate = viewModel.EntryDate, HoursWorked = viewModel.HoursWorked }; if (project != null) { entry.Project = project; } return(entry); }
public async Task <IActionResult> Edit(TimesheetEntryViewModel viewModel) { TimesheetEntry entry = await _timesheetEntryRepository.GetById(viewModel.Id); Project project = await _projectRepository.GetById(entry.ProjectId); User user = await _userRepository.GetByGuid(entry.UserId); entry.User = user; entry.UserId = user.Id; entry.Project = project; entry.ProjectId = project.Id; entry.HoursWorked = viewModel.HoursWorked; entry.EntryDate = viewModel.EntryDate; await _timesheetEntryRepository.Update(entry); return(RedirectToAction(nameof(Details), "TimesheetEntry", new { id = entry.Id })); }
public async Task <IActionResult> Edit(int id, TimesheetEntryViewModel viewModel) { if (id != viewModel.Id) { return(NotFound()); } if (ModelState.IsValid) { TimesheetEntry timesheetEntry = _mapper.MapViewModelToTimesheetEntry(viewModel); if (NoEntryExistsForSameDateAndProjectExcludingSelf(timesheetEntry)) { try { _context.Update(timesheetEntry); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TimesheetEntryExists(timesheetEntry.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } else { ViewBag.ErrorTitle = "Error"; ViewBag.ErrorMessage = "User already has a timesheet entry for the same date and project"; return(View("CustomError")); } } return(View(viewModel)); }
public async Task <IActionResult> Create(TimesheetEntryViewModel viewModel) { User user = await _userRepository.GetByGuid(User.FindFirstValue(ClaimTypes.NameIdentifier)); viewModel.UserId = user.Id; viewModel.UserFullName = string.Format("{0} {1}", user.FirstName, user.LastName); viewModel.ProjectName = (await _projectRepository.GetById(viewModel.ProjectId)).Name; Project project = await _projectRepository.GetById(viewModel.ProjectId); Department department = await _departmentRepository.GetById(project.DepartmentOwnerId); TimesheetEntry entry = _mapper.ConvertFromViewModel(viewModel, project); entry.User = user; // Add TimesheetEntry to database await _timesheetEntryRepository.Create(entry); // Return to Index return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create(TimesheetEntryViewModel viewModel) { if (ModelState.IsValid) { TimesheetEntry timesheetEntry = _mapper.MapViewModelToTimesheetEntry(viewModel); if (NoEntryExistsForSameDateAndProject(timesheetEntry)) { _context.Add(timesheetEntry); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { ViewBag.ErrorTitle = "Error"; ViewBag.ErrorMessage = "User already has a timesheet entry for the same date and project"; return(View("CustomError")); } } return(View(viewModel)); }
private Button CreateSubmitButton(TimesheetEntryViewModel timesheet, Picker hours, Entry comment, Switch sickLeave) { var submitButton = new Button { Text = "Submit", Font = Font.BoldSystemFontOfSize(NamedSize.Medium), HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, }; submitButton.SetBinding<TimesheetEntryViewModel>(Button.CommandProperty, m => m.SaveTimesheetCommand); return submitButton; }
public TimesheetEntryPage(TimesheetEntryViewModel timesheet) { BindingContext = timesheet; var dateLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateDateLabel(), CreateDate() } }; dateLayout.Padding = new Thickness(0, 0, 0, 5); var customerLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateCustomerLabel(), CreateCustomer() } }; customerLayout.Padding = new Thickness(0, 0, 0, 5); var projectLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateProjectLabel(), CreateProject() } }; projectLayout.Padding = new Thickness(0, 0, 0, 5); var hoursInput = CreateHoursInput(); var hoursLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateHoursLabel(), hoursInput } }; var commentInput = CreateCommentInput(); var commentLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateCommentLabel(), commentInput } }; var sickLeaveInput = CreateSickLeaveInput(); var sickLeaveLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { CreateSickLeaveLabel(), sickLeaveInput } }; var submitButton = CreateSubmitButton(timesheet, hoursInput, commentInput, sickLeaveInput); var content = new StackLayout { Children = { dateLayout, customerLayout, projectLayout, hoursLayout, commentLayout, sickLeaveLayout, submitButton } }; //var layout = CreateLoadingIndicatorRelativeLayout(content); var layout = CreateLoadingIndicatorAbsoluteLayout(content); Padding = new Thickness(15, 10); Title = "Submit"; Content = layout; }