public async Task <IActionResult> Edit(int id, [Bind("ShiftParticipationTypeId,Name,IsAbsence,IsBold,ColorString")] ShiftParticipationType shiftParticipationType) { if (id != shiftParticipationType.ShiftParticipationTypeId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(shiftParticipationType); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ShiftParticipationTypeExists(shiftParticipationType.ShiftParticipationTypeId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(shiftParticipationType)); }
public async Task<List<ShiftParticipation>> Handle(CreateShiftParticipationsFromGroupCommand request, CancellationToken cancellationToken) { // Get the shift participation type named normal ShiftParticipationType shiftParticipationType = await _context.ShiftParticipationTypes.Where(spt => spt.Name.ToLower() == "normal").FirstOrDefaultAsync(); // get the employees in the shiftGroup ShiftGroup shiftGroup = await _context.ShiftGroups.Include(sg => sg.Employees).FirstOrDefaultAsync(sg => sg.Id == request.ShiftGroupId); List<string> empIds = shiftGroup.Employees.Where(e => e.UserName != _identityInit.AdminUserName).Select(empl => empl.Id).ToList(); foreach (string empId in empIds) { try { // check if participation exists if ((await _context.ShiftParticipations.Where(sp => (sp.ShiftId == request.ShiftId) && (sp.EmployeeId == empId)).ToListAsync()).Count > 0) { continue; } _context.ShiftParticipations.Add(new ShiftParticipation { EmployeeId = empId, ShiftId = request.ShiftId, ShiftParticipationTypeId = shiftParticipationType.Id }); await _context.SaveChangesAsync(); } catch (Exception ex) { Console.WriteLine($"error occured while adding participation of employee id {empId} to shift id {request.ShiftId} -- {ex.Message}"); continue; } } return await _context.ShiftParticipations.Where(sp => sp.ShiftId == request.ShiftId).ToListAsync(); }
public async Task <IActionResult> Create([Bind("ShiftParticipationTypeId,Name,IsAbsence,IsBold,ColorString")] ShiftParticipationType shiftParticipationType) { if (ModelState.IsValid) { _context.Add(shiftParticipationType); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(shiftParticipationType)); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ShiftParticipationType = await _context.ShiftParticipationTypes.FirstOrDefaultAsync(m => m.Id == id); if (ShiftParticipationType == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } ShiftParticipationType = await _context.ShiftParticipationTypes.FindAsync(id); if (ShiftParticipationType != null) { _context.ShiftParticipationTypes.Remove(ShiftParticipationType); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <LeaveRequest> Handle(CreateLeaveRequestCommand request, CancellationToken cancellationToken) { LeaveRequest lr = request.LeaveRequest; if (!request.IsUserAdmin && request.UserId != request.LeaveRequest.EmployeeId) { throw new Exception("A non-admin user is trying to create a request on others behalf..."); } ShiftParticipationType leaveType = await _context.ShiftParticipationTypes.Where(sp => sp.Id == request.LeaveRequest.LeaveTypeId).FirstOrDefaultAsync(); if (!leaveType.IsAbsence) { throw new Exception("Leave request of non absence type is not valid..."); } _context.LeaveRequests.Add(lr); await _context.SaveChangesAsync(); return(lr); }
public async Task <IActionResult> Calendar(CalendarPrintViewModel vm) { if (ModelState.IsValid) { // check if start date > end date if (vm.StartDate > vm.EndDate) { return(View(vm)); } // Get Employee Id int employeeId = vm.EmployeeId; //fetch the shift types List <ShiftType> shiftTypes = await _context.ShiftTypes.OrderBy(st => st.RoasterSequence).ToListAsync(); List <int> shiftTypeIds = shiftTypes.Select(st => st.ShiftTypeId).ToList(); //get the shiftParticipationTypes from db List <ShiftParticipationType> shiftParticipationTypes = await _context.ShiftParticipationTypes.ToListAsync(); List <int> shiftPartTypeIds = shiftParticipationTypes.Select(spt => spt.ShiftParticipationTypeId).ToList(); // get the shift participations from db List <ShiftParticipation> empShiftParticipations = await _context.ShiftParticipations.Include(sp => sp.Shift).Where(sp => sp.EmployeeId == employeeId && sp.Shift.ShiftDate >= vm.StartDate && sp.Shift.ShiftDate <= vm.EndDate).ToListAsync(); // populate the EmployeeShiftParticipations in view model vm.EmployeeShiftParticipations = empShiftParticipations; // populate the Shift participation types in view model vm.ShiftParticipationTypes = shiftParticipationTypes; // populate the Shift types in view model vm.ShiftTypes = shiftTypes; // create calendar events from the fetched server information vm.CalendarEvents = new List <CalendarEventViewModel>(); foreach (ShiftParticipation empShiftParticipation in empShiftParticipations) { CalendarEventViewModel cevm = new CalendarEventViewModel(); ShiftType eventShiftType = shiftTypes[shiftTypeIds.IndexOf(empShiftParticipation.Shift.ShiftTypeId)]; ShiftParticipationType eventShiftParticipationType = null; // derive the shift participation type if (empShiftParticipation.ShiftParticipationTypeId.HasValue) { eventShiftParticipationType = shiftParticipationTypes[shiftPartTypeIds.IndexOf(empShiftParticipation.ShiftParticipationTypeId.Value)]; } // derive the event title based on shift type cevm.EventTitle = eventShiftType.Name; // derive the event date cevm.ShiftDate = empShiftParticipation.Shift.ShiftDate; // derive the event title css style cevm.TitleBgColor = eventShiftType.ColorString; cevm.EventTextClasses = new List <string> { "default_evnt_title" }; cevm.TooltipText = "participation = Normal"; if (eventShiftParticipationType != null) { if (eventShiftParticipationType.IsBold == true) { cevm.EventTextClasses.Add("bold_text"); } if (eventShiftParticipationType.IsAbsence == true) { cevm.EventTextClasses.Add("absence_text"); } cevm.TooltipText = $"participation = {eventShiftParticipationType.Name}"; } // derive the event title text color cevm.TitleColor = "#000000"; if (eventShiftParticipationType != null) { cevm.TitleColor = eventShiftParticipationType.ColorString; } // add the calendar event to the view model vm.CalendarEvents.Add(cevm); } } ViewData["EmployeeId"] = new SelectList(_context.Employees, "EmployeeId", "Name"); return(View(vm)); }
public async Task <IActionResult> Roaster(ShiftsPrintViewModel vm) { if (ModelState.IsValid) { // check if start date > end date if (vm.StartDate > vm.EndDate) { return(View(vm)); } //fetch the shift types List <ShiftType> shiftTypes = await _context.ShiftTypes.OrderBy(st => st.RoasterSequence).ToListAsync(); List <int> shiftTypeIds = shiftTypes.Select(st => st.ShiftTypeId).ToList(); //fetch the shift Participation types List <ShiftParticipationType> shiftPartTypes = await _context.ShiftParticipationTypes.ToListAsync(); List <int> shiftPartTypeIds = shiftPartTypes.Select(spt => spt.ShiftParticipationTypeId).ToList(); // set the view model shift Types vm.ShiftTypes = new List <string>(); foreach (ShiftType shiftType in shiftTypes) { vm.ShiftTypes.Add(shiftType.Name); } // initiaize the shift participations in view model vm.ShiftParticipations = new Dictionary <DateTime, List <List <Tuple <string, ShiftParticipationType> > > >(); for (DateTime dt = vm.StartDate; dt <= vm.EndDate; dt = dt.AddDays(1)) { vm.ShiftParticipations.Add(dt, new List <List <Tuple <string, ShiftParticipationType> > >()); foreach (var sType in shiftTypes) { vm.ShiftParticipations[dt].Add(new List <Tuple <string, ShiftParticipationType> >()); } } // get the shift participations from db List <ShiftParticipation> shiftParticipations = await _context.ShiftParticipations.Include(sp => sp.Shift).Include(sp => sp.Employee).Where(sp => sp.Shift.ShiftDate >= vm.StartDate && sp.Shift.ShiftDate <= vm.EndDate).ToListAsync(); // assign the fetched shift participations to the vm foreach (ShiftParticipation shiftPart in shiftParticipations.OrderBy(sp => sp.ParticipationSequence)) { DateTime shiftDate = shiftPart.Shift.ShiftDate; ShiftParticipationType participationType = null; int shiftTypeIndex = -1; int shiftPartTypeIndex = -1; try { shiftTypeIndex = shiftTypeIds.FindIndex(sTId => sTId == shiftPart.Shift.ShiftTypeId); shiftPartTypeIndex = shiftPartTypeIds.FindIndex(sPTId => sPTId == shiftPart.ShiftParticipationTypeId); } catch (Exception) { continue; } if (shiftPartTypeIndex != -1) { participationType = shiftPartTypes[shiftPartTypeIndex]; } vm.ShiftParticipations[shiftPart.Shift.ShiftDate][shiftTypeIndex].Add(new Tuple <string, ShiftParticipationType>(shiftPart.Employee.Name, participationType)); } // get all the shift objects for comments List <Shift> shifts = await _context.Shifts.Where(s => s.ShiftDate >= vm.StartDate && s.ShiftDate <= vm.EndDate).OrderBy(s => s.ShiftDate).ToListAsync(); // assign the fetched shift comments to the vm vm.ShiftComments = new List <Tuple <DateTime, string, string> >(); foreach (Shift shift in shifts) { if (shift.Comments != null && shift.Comments != "") { DateTime shiftDate = shift.ShiftDate; int shiftTypeIndex = -1; try { shiftTypeIndex = shiftTypeIds.FindIndex(sTId => sTId == shift.ShiftTypeId); } catch (Exception) { continue; } vm.ShiftComments.Add(new Tuple <DateTime, string, string>(shift.ShiftDate, shiftTypes[shiftTypeIndex].Name, shift.Comments)); } } } return(View(vm)); }
public async Task <CalendarDTO> Handle(GetEmployeeCalendarByIdQuery request, CancellationToken cancellationToken) { CalendarDTO vm = new CalendarDTO(); if (request.StartDate > request.EndDate) { return(vm); } // get the shift participations from db List <ShiftParticipation> empShiftParts = await _context.ShiftParticipations .Include(sp => sp.ShiftParticipationType) .Include(sp => sp.Shift) .ThenInclude(sp => sp.ShiftType) .Where(sp => sp.EmployeeId == request.EmployeeId && sp.Shift.ShiftDate >= request.StartDate && sp.Shift.ShiftDate <= request.EndDate) .ToListAsync(); // create calendar events from the fetched server information vm.CalendarEvents = new List <CalendarEventDTO>(); foreach (ShiftParticipation shiftPart in empShiftParts) { CalendarEventDTO ce = new CalendarEventDTO(); ShiftType evntShiftType = shiftPart.Shift.ShiftType; ShiftParticipationType evntShiftPartType = shiftPart.ShiftParticipationType; // derive the event title based on shift type ce.EventTitle = evntShiftType.Name; // derive the event date ce.ShiftDate = shiftPart.Shift.ShiftDate; // derive the event title css style ce.TitleBgColor = evntShiftType.ColorString; ce.EventTextClasses = new List <string> { "default_evnt_title" }; if (evntShiftPartType.IsBold == true) { ce.EventTextClasses.Add("bold_text"); } if (evntShiftPartType.IsAbsence == true) { ce.EventTextClasses.Add("absence_text"); } ce.TooltipText = $"participation = {evntShiftPartType.Name}"; // derive the event title text color ce.TitleColor = "#000000"; if (evntShiftPartType != null) { ce.TitleColor = evntShiftPartType.ColorString; } // add the calendar event to the view model vm.CalendarEvents.Add(ce); } return(vm); }
public async Task <ShiftRosterDTO> Handle(GetShiftRosterQuery request, CancellationToken cancellationToken) { // TODO refine code ShiftRosterDTO vm = new ShiftRosterDTO(); if (request.StartDate > request.EndDate) { return(vm); } //fetch the shift types List <ShiftType> shiftTypes = await _context.ShiftTypes.OrderBy(st => st.ShiftSequence).ToListAsync(); List <int> shiftTypeIds = shiftTypes.Select(st => st.Id).ToList(); //fetch the shift Participation types List <ShiftParticipationType> shiftPartTypes = await _context.ShiftParticipationTypes.ToListAsync(); List <int> shiftPartTypeIds = shiftPartTypes.Select(spt => spt.Id).ToList(); // set the view model shift Types vm.ShiftTypes = new List <string>(); foreach (ShiftType shiftType in shiftTypes) { vm.ShiftTypes.Add(shiftType.Name); } // initiaize the shift participations in view model vm.ShiftParticipations = new Dictionary <DateTime, List <List <Tuple <string, ShiftParticipationType> > > >(); for (DateTime dt = request.StartDate; dt <= request.EndDate; dt = dt.AddDays(1)) { vm.ShiftParticipations.Add(dt, new List <List <Tuple <string, ShiftParticipationType> > >()); foreach (var sType in shiftTypes) { vm.ShiftParticipations[dt].Add(new List <Tuple <string, ShiftParticipationType> >()); } } // get the shift participations from db List <ShiftParticipation> shiftParticipations = await _context.ShiftParticipations.Include(sp => sp.Shift).Include(sp => sp.Employee).Where(sp => sp.Shift.ShiftDate >= request.StartDate && sp.Shift.ShiftDate <= request.EndDate).ToListAsync(); // assign the fetched shift participations to the vm foreach (ShiftParticipation shiftPart in shiftParticipations.OrderBy(sp => sp.ParticipationSequence)) { DateTime shiftDate = shiftPart.Shift.ShiftDate; ShiftParticipationType participationType = null; int shiftTypeIndex = -1; int shiftPartTypeIndex = -1; try { shiftTypeIndex = shiftTypeIds.FindIndex(sTId => sTId == shiftPart.Shift.ShiftTypeId); shiftPartTypeIndex = shiftPartTypeIds.FindIndex(sPTId => sPTId == shiftPart.ShiftParticipationTypeId); } catch (Exception) { continue; } if (shiftPartTypeIndex != -1) { participationType = shiftPartTypes[shiftPartTypeIndex]; } vm.ShiftParticipations[shiftPart.Shift.ShiftDate.Date][shiftTypeIndex].Add(new Tuple <string, ShiftParticipationType>(shiftPart.Employee.DisplayName, participationType)); } // get all the shift objects for comments List <Shift> shifts = await _context.Shifts.Where(s => s.ShiftDate >= request.StartDate && s.ShiftDate <= request.EndDate).OrderBy(s => s.ShiftDate).ToListAsync(); // assign the fetched shift comments to the vm vm.ShiftComments = new List <Tuple <DateTime, string, string> >(); foreach (Shift shift in shifts) { if (shift.Comments != null && shift.Comments != "") { DateTime shiftDate = shift.ShiftDate; int shiftTypeIndex = -1; try { shiftTypeIndex = shiftTypeIds.FindIndex(sTId => sTId == shift.ShiftTypeId); } catch (Exception) { continue; } vm.ShiftComments.Add(new Tuple <DateTime, string, string>(shift.ShiftDate, shiftTypes[shiftTypeIndex].Name, shift.Comments)); } } return(vm); }