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> Edit(int id, [Bind("Id,UserId,CurrentShiftId,WishShiftId,IsBroadcast,HasBeenSwitched")] SwitchRequest switchRequest) { if (id != switchRequest.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(switchRequest); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SwitchRequestExists(switchRequest.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CurrentShiftId"] = new SelectList(_context.Shift, "Id", "ShiftDate", switchRequest.CurrentShiftId); ViewData["UserId"] = new SelectList(_context.User, "Id", "Email", switchRequest.UserId); ViewData["WishShiftId"] = new SelectList(_context.Shift, "Id", "ShiftDate", switchRequest.WishShiftId); return(View(switchRequest)); }
public async Task <SwitchRequestViewModel> CreateSwitchRequest(Dictionary <string, string> sourceAndTarget, DiscordUser requestor) { var requestedBy = await _context.GuildMembers .FirstOrDefaultAsync(x => x.DiscordId == requestor.Id.ToString()) .ConfigureAwait(false); var sourceGuild = await _context.Guilds .FirstOrDefaultAsync(x => x.Name.ToLower() == sourceAndTarget["source"].ToLower()) .ConfigureAwait(false); var targetGuild = await _context.Guilds .FirstOrDefaultAsync(x => x.Name.ToLower() == sourceAndTarget["target"].ToLower()) .ConfigureAwait(false); var newSwitchRequest = new SwitchRequest() { RequestedBy = requestedBy, SourceGuild = sourceGuild, TargetGuild = targetGuild }; _context.SwitchRequests.Add(newSwitchRequest); await _context.SaveChangesAsync().ConfigureAwait(false); return(newSwitchRequest.MapFromEntity()); }
public IActionResult Switch(SwitchRequest request) { if (!this.ModelState.IsValid) { return(this.BadRequest()); } var application = this.applicationManager.Switch("chrome", null); return(this.Ok()); }
// GET: Requests/Create public IActionResult Create() { var user = _context.User.SingleOrDefault(s => s.Email.Equals(User.Identity.Name)); var switchRequest = new SwitchRequest(); switchRequest.UserId = user?.Id; switchRequest.PendingSwitches = new List <PendingSwitch>(); 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"); return(View(switchRequest)); }
/// <summary> /// Send the direction of the given switch towards the railway. /// This method is called on my worker thread. /// </summary> private void SendSwitchRequest(Address address, SwitchDirection direction, bool invertDirection) { Log.Trace("Send: SwitchRequest: address={0}, direction={1}", address.Value, direction); var msg = new SwitchRequest(); msg.Address = address.ValueAsInt - 1; msg.Direction = (direction == SwitchDirection.Straight); if (invertDirection) { msg.Direction = !msg.Direction; } msg.Output = true; msg.Execute(lb); }
public static SwitchRequestViewModel MapFromEntity(this SwitchRequest source) { if (source == null) { return(null); } return(new SwitchRequestViewModel { RequestedBy = source.RequestedBy.MapFromEntity(), SourceGuild = source.SourceGuild?.MapFromEntity(false), TargetGuild = source.TargetGuild.MapFromEntity(false), Status = source.Status.GetDescription() //IsApproved = source.IsApproved }); }
public async Task <IActionResult> PostSwitchRequestBroadcast([FromBody] BroadcastSwitchRequest broadcastSwitchRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } SwitchRequest switchRequest = new SwitchRequest(); switchRequest.HasBeenSwitched = false; switchRequest.UserId = broadcastSwitchRequest.RequestUserId; switchRequest.CurrentShiftId = broadcastSwitchRequest.RequesterShiftId; switchRequest.UserWishShiftId = null; switchRequest.IsBroadcast = true; switchRequest.RequestCreatedDate = DateTime.Now; // Add N pending switch to notify all users about your current-shift-date switch request switchRequest.PendingSwitches = new List <PendingSwitch>(); // await _context.User.ForEachAsync( await _context.User.Where(u => u.Id != switchRequest.UserId).ForEachAsync( u => { switchRequest.PendingSwitches.Add( new PendingSwitch() { UserId = u.Id, Date = null, Status = ScheduleApp.Web.Extensions.Constants.REQUEST_STATUS_NEW }); }); _context.Add(switchRequest); await _context.SaveChangesAsync(); return(Ok(true)); }
public virtual TReturn Visit(SwitchRequest msg, TData data) { return(default(TReturn)); }
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)); }