// if EndDate is provided in params then a serie of weekly events is created // otherwise a single event is created public async Task <IEnumerable <ShowTimeViewModel> > Create(AddShowTimesParams addParams) { var entityEntries = new List <EntityEntry <ShowTime> >(); if (addParams.EndDate.HasValue) { for (DateTime date = addParams.StartDate; date.Date <= addParams.EndDate.Value.Date; date = date.AddDays(7)) { var entry = _context.ShowTimes.Add(new ShowTime { ShowID = addParams.ShowID, StartDateUtc = date.ToUniversalTime(), TicketsTotal = addParams.TicketsTotal }); entityEntries.Add(entry); } } else { var entry = _context.ShowTimes.Add(new ShowTime { ShowID = addParams.ShowID, StartDateUtc = addParams.StartDate.ToUniversalTime(), TicketsTotal = addParams.TicketsTotal }); entityEntries.Add(entry); } await _context.SaveChangesAsync(); return(entityEntries.Select(e => ToShowTimeViewModel(e.Entity)).ToList()); }
public async Task <IActionResult> CreateShowTime([FromBody] AddShowTimesParams addParams) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var created = await _showTimesService.Create(addParams); return(StatusCode((int)HttpStatusCode.Created, created)); }