public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } ScheduledStream model = await _context.ScheduledStream .Include(x => x.Sessions) // Required for updating them. .SingleOrDefaultAsync(x => x.Id == ViewModel.Id); model.ApplyEditChanges(ViewModel); // TODO: Update the Sessions try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ScheduledStreamExists(ViewModel.Id)) { return(NotFound()); } throw; } return(RedirectToPage("./Details", new { id = ViewModel.Id })); }
private void CreateStreamSessions(ScheduledStream stream, DateTimeZone timeZone) { ZonedClock zonedClock = _clock.InZone(timeZone); LocalDate nextOfDay = zonedClock.GetCurrentDate() .With(DateAdjusters.Next(stream.DayOfWeek)); for (int i = 0; i < 52; i++) { LocalDateTime nextLocalStartDateTime = nextOfDay + stream.LocalStartTime; LocalDateTime nextLocalEndDateTime = nextOfDay + stream.LocalEndTime; var streamSession = new StreamSession { TzdbVersionId = DateTimeZoneProviders.Tzdb.VersionId, UtcStartTime = nextLocalStartDateTime .InZoneLeniently(timeZone) .ToInstant(), UtcEndTime = nextLocalEndDateTime.InZoneLeniently(timeZone).ToInstant(), }; stream.Sessions.Add(streamSession); nextOfDay = nextOfDay.PlusWeeks(1); } }
public void AddScheduledStreamToChannel(Channel channel, ScheduledStream stream) { channel.ScheduledStreams.Add(stream); var timeZone = DateTimeZoneProviders.Tzdb[channel.TimeZoneId]; CreateStreamSessions(stream, timeZone); }
public static ScheduledStreamEditModel ToEditViewModel(this ScheduledStream src) { return(new ScheduledStreamEditModel { Id = src.Id, DayOfWeek = src.DayOfWeek, LocalStartTime = TimePattern.Format(src.LocalStartTime), LocalEndTime = TimePattern.Format(src.LocalEndTime) }); }
public static void ApplyEditChanges(this ScheduledStream model, ScheduledStreamEditModel viewModel) { var parsedStart = TimePattern.Parse(viewModel.LocalStartTime); var parsedEnd = TimePattern.Parse(viewModel.LocalEndTime); model.DayOfWeek = viewModel.DayOfWeek; model.LocalStartTime = parsedStart.Value; model.LocalEndTime = parsedEnd.Value; }
public static ScheduledStreamViewModel ToViewModel(this ScheduledStream src) { return(new ScheduledStreamViewModel { Id = src.Id, DayOfWeek = src.DayOfWeek, LocalStartTime = TimePattern.Format(src.LocalStartTime), LocalEndTime = TimePattern.Format(src.LocalEndTime), TimeZoneName = TZNames.GetNamesForTimeZone(src.TimeZoneId, CultureInfo.CurrentUICulture.Name).Generic, ChannelId = src.ChannelId }); }
public async Task <IActionResult> OnPostAsync(int channelId) { ScheduledStream stream = StreamTime.ToModel(); Channel channel = await _crudRepository.Get <Channel>(channelId); stream.ChannelId = channelId; stream.TimeZoneId = channel.TimeZoneId; int?id = await _scheduledStreamService.AddScheduledStreamToChannel(stream); return(RedirectToPage("./Index", new { channelId })); }
public async Task <IActionResult> OnPostAsync(int channelId) { ScheduledStream stream = StreamTime.ToModel(); var channel = await _context.Channels .Include(x => x.ScheduledStreams) .SingleAsync(x => x.Id == channelId); _scheduledStreamService.AddScheduledStreamToChannel(channel, stream); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <int?> AddScheduledStreamToChannel(ScheduledStream stream) { using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection)) { int?id = await connection.InsertAsync(stream); stream.Id = id.Value; var timeZone = DateTimeZoneProviders.Tzdb[stream.TimeZoneId]; var sessions = CreateStreamSessions(stream, timeZone); await Task.WhenAll(sessions.Select(s => connection.InsertAsync(s))); return(id); } }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ScheduledStream model = await _crudRepository.Get <ScheduledStream>(id.Value); if (model == null) { return(NotFound()); } ScheduledStream = model.ToViewModel(); return(Page()); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ScheduledStream stream = await _crudRepository.Get <ScheduledStream>(id.Value); if (stream == null) { return(NotFound()); } ViewModel = stream.ToEditViewModel(); ChannelId = stream.ChannelId; return(Page()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } ScheduledStream model = await _crudRepository.Get <ScheduledStream>(ViewModel.Id); model.ApplyEditChanges(ViewModel); int updateCount = await _streamService.Update(model); if (updateCount == 0) { return(NotFound()); } return(RedirectToPage("./Details", new { id = ViewModel.Id })); }
private void TryAddScheduledStreams() { foreach (Channel channel in _channels) { for (int i = 0; i < 2; i++) { LocalTime localStartTime = new LocalTime(9, 0).PlusHours(_random.Next(0, 10)); var scheduledStream = new ScheduledStream { DayOfWeek = (IsoDayOfWeek)_random.Next(1, 8), LocalStartTime = localStartTime, LocalEndTime = localStartTime.PlusHours(_random.Next(1, 6)), }; _scheduledStreamService.AddScheduledStreamToChannel( channel, scheduledStream); } } _db.SaveChanges(); }
public async Task <int> Update(ScheduledStream stream) { using (IDbConnection connection = new SqlConnection(_dbSettings.DefaultConnection)) { int updateCount = await connection.UpdateAsync(stream); if (updateCount > 0) { await connection.DeleteListAsync <StreamSession>( new { ScheduledStreamId = stream.Id }); var timeZone = DateTimeZoneProviders.Tzdb[stream.TimeZoneId]; var sessions = CreateStreamSessions(stream, timeZone); await Task.WhenAll(sessions.Select(s => connection.InsertAsync(s))); } return(updateCount); } }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ScheduledStream scheduledStream = await _context.ScheduledStream .Include(x => x.Channel) // Required for "Back to List" link .FirstOrDefaultAsync(m => m.Id == id); if (scheduledStream == null) { return(NotFound()); } ViewModel = scheduledStream.ToEditViewModel(); ChannelId = scheduledStream.Channel.Id; return(Page()); }
public async Task<IActionResult> OnPostAsync(int channelId) { ScheduledStream stream = StreamTime.ToModel(); var channel = await _context.Channels .Include(x => x.ScheduledStreams) .SingleAsync(x => x.Id == channelId); channel.ScheduledStreams.Add(stream); var zone = DateTimeZoneProviders.Tzdb[channel.TimeZoneId]; var version = DateTimeZoneProviders.Tzdb.VersionId; ZonedClock zonedClock = _clock.InZone(zone); LocalDate today = zonedClock.GetCurrentDate(); LocalDate next = today.With(DateAdjusters.Next(stream.DayOfWeek)); for (int i = 0; i < 52; i++) { LocalDateTime nextLocalStartDateTime = next + stream.LocalStartTime; LocalDateTime nextLocalEndDateTime = next + stream.LocalEndTime; var streamSession = new StreamSession { TzdbVersionId = version, UtcStartTime = nextLocalStartDateTime.InZoneLeniently(zone).ToInstant(), UtcEndTime = nextLocalEndDateTime.InZoneLeniently(zone).ToInstant(), }; stream.Sessions.Add(streamSession); next = next.PlusWeeks(1); } await _context.SaveChangesAsync(); return RedirectToPage("./Index"); }
private static string GetStreamSummaryString(ScheduledStream stream) { return($"{stream.Title}\n{stream.Game}\n{stream.Start:HH:mm} - {stream.End:HH:mm} UTC"); }