public async Task <IActionResult> PostSwitchRequestDirect([FromBody] DirectSwitchRequest directSwitchRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var pendingSwitch = new PendingSwitch(); pendingSwitch.UserId = directSwitchRequest.AcceptUserId; pendingSwitch.Date = directSwitchRequest.OfferedDate; pendingSwitch.Status = ScheduleApp.Web.Extensions.Constants.REQUEST_STATUS_NEW; var switchRequest = new SwitchRequest(); switchRequest.UserWishShiftId = directSwitchRequest.AcceptUserId; switchRequest.HasBeenSwitched = false; switchRequest.UserId = directSwitchRequest.RequestUserId; switchRequest.CurrentShiftId = directSwitchRequest.RequesterShiftId; switchRequest.WishShiftId = directSwitchRequest.AcceptorShiftId; switchRequest.IsBroadcast = false; switchRequest.RequestCreatedDate = DateTime.Now; // Add 1 pending switch to notify wish-user about your current-shift-date switch request switchRequest?.PendingSwitches?.Add(pendingSwitch); _context.Add(switchRequest); await _context.SaveChangesAsync(); return(Ok(true)); }
public async Task <IActionResult> Create([Bind("Id,UserId,CurrentShiftId,WishShiftId,RequestCreatedDate,PendingSwitches")] SwitchRequest vm) { var user = await _context.User.SingleOrDefaultAsync(m => m.Id == vm.UserId); if (user == null) { user = _context.User.SingleOrDefault(s => s.Email.Equals(User.Identity.Name)); if (user == null) { ModelState.AddModelError("NotFound", "User with given ID doesn't exist."); } } var currentShift = await _context.Shift.SingleOrDefaultAsync(m => m.Id == vm.CurrentShiftId); if (currentShift == null) { ModelState.AddModelError("NotFound", "Current shift with given ID doesn't exist."); } var wishShift = await _context.Shift.SingleOrDefaultAsync(m => m.Id == vm.WishShiftId); if (wishShift == null) { ModelState.AddModelError("NotFound", "Wish shift with given ID doesn't exist."); } var wishShiftUser = await _context.Schedule.Include(m => m.User).Where(s => s.ShiftId == wishShift.Id) .Select(s => s.User).SingleOrDefaultAsync(); if (wishShiftUser == null) { ModelState.AddModelError("NotFound", "Wish shift user with given ID doesn't exist."); } var pendingSwitch = await _context.PendingSwitch.SingleOrDefaultAsync(m => m.SwitchRequestId == vm.Id); if (pendingSwitch == null) { pendingSwitch = new PendingSwitch(); pendingSwitch.UserId = wishShiftUser?.Id; pendingSwitch.Date = currentShift?.ShiftDate; pendingSwitch.Status = ScheduleApp.Web.Extensions.Constants.REQUEST_STATUS_NEW; } //if (ModelState.IsValid) //{ // _context.Add(pendingSwitch); // await _context.SaveChangesAsync(); //} var switchRequest = new SwitchRequest() { IsBroadcast = false, HasBeenSwitched = false, CurrentShift = currentShift, WishShift = wishShift, UserWishShift = wishShiftUser, User = user, RequestCreatedDate = DateTime.Now }; // Add 1 pending switch to notify wish-user about your current-shift-date switch request switchRequest?.PendingSwitches?.Add(pendingSwitch); if (ModelState.IsValid) { _context.Add(switchRequest); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index), "Home")); } ViewData["CurrentShiftId"] = new SelectList(_context.Schedule.Include(s => s.User).Where(s => s.User.Email == User.Identity.Name).Select(s => s.Shift).OrderBy(s => s.ShiftDate), "Id", "ShiftDate"); ViewData["WishShiftId"] = new SelectList(_context.Schedule.Include(s => s.User).Where(s => s.User.Email != User.Identity.Name).Select(s => s.Shift).OrderBy(s => s.ShiftDate), "Id", "ShiftDate"); //ViewData["UserId"] = new SelectList(_context.User.Where(s => s.Email.Equals(User.Identity.Name)), "Id", "Email", switchRequest.UserId); return(View(vm)); }
public async Task <IActionResult> Broadcast([Bind("Id,UserId,CurrentShiftId,RequestCreatedDate,PendingSwitches")] SwitchRequest vm) { var user = await _context.User.SingleOrDefaultAsync(m => m.Id == vm.UserId); if (user == null) { user = _context.User.SingleOrDefault(s => s.Email.Equals(User.Identity.Name)); if (user == null) { ModelState.AddModelError("NotFound", "User with given ID doesn't exist."); } } var currentShift = await _context.Shift.SingleOrDefaultAsync(m => m.Id == vm.CurrentShiftId); if (currentShift == null) { ModelState.AddModelError("NotFound", "Current shift with given ID doesn't exist."); } // Add N pending switch to notify all users about your current-shift-date switch request var pendingSwitches = await _context.PendingSwitch.Where(m => m.SwitchRequestId == vm.Id).ToListAsync(); //if (pendingSwitches.Any()) var users = _context.User.Where(u => u.Email != user.Email && u.IsActive).ToList(); foreach (var u in users) { var pendingSwitch = new PendingSwitch(); pendingSwitch.UserId = u.Id; // for each valid users pendingSwitch.Date = currentShift?.ShiftDate; pendingSwitch.Status = ScheduleApp.Web.Extensions.Constants.REQUEST_STATUS_NEW; pendingSwitches.Add(pendingSwitch); } //if (ModelState.IsValid) //{ // _context.Add(pendingSwitch); // await _context.SaveChangesAsync(); //} var switchRequest = new SwitchRequest() { IsBroadcast = true, HasBeenSwitched = false, CurrentShift = currentShift, User = user, RequestCreatedDate = DateTime.Now }; // Add N pending switch to notify all users about your current-shift-date switch request foreach (var pendingSwitch in pendingSwitches) { switchRequest.PendingSwitches?.Add(pendingSwitch); } if (ModelState.IsValid) { _context.Add(switchRequest); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CurrentShiftId"] = new SelectList(_context.Schedule.Include(s => s.User).Where(s => s.User.Email == User.Identity.Name).Select(s => s.Shift).OrderBy(s => s.ShiftDate), "Id", "ShiftDate", vm.CurrentShiftId); //ViewData["UserId"] = new SelectList(_context.User.Where(s => s.Email.Equals(User.Identity.Name)), "Id", "Email", vm.UserId); return(View(vm)); }