private EventResponse CreateEventResponse(EventDbo dbo) { var response = new EventResponse() { Id = dbo.EventDboId, Data = new EventData(), Registrations = new RegistrationsResponse() { Data = new List <RegistrationResponse>() } }; response.Data.CopyFrom(dbo); bool canAddRegistrations = dbo.Deadline.HasValue ? dbo.Deadline > DateTime.Now.ToUniversalTime() : dbo.Start > DateTime.Now.ToUniversalTime(); bool canPatchEvent = dbo.Locked == false; if (dbo.Registrations != null) { foreach (var regDbo in dbo.Registrations) { response.Registrations.Data.Add(CreateRegistrationResponse(regDbo, canAddRegistrations, canPatchEvent)); } } response.AddHref(HrefType.SELF, "enroll/" + dbo.EventToken); if (dbo.SeriesToken.HasValue && dbo.SeriesToken != Guid.Empty) { response.AddHref(OpteaMateHrefType.BACK, "series/" + dbo.SeriesToken); } // DeleteEvent; also possible to delete past events response.AddHref(HrefType.DELETE, $"api/events/{dbo.EventDboId}"); if (canAddRegistrations) { // PostEventRegistration response.Registrations.AddHref(HrefType.POST, $"api/events/{dbo.EventDboId}/registrations"); } if (canPatchEvent) { // PatchEvent response.AddHref(HrefType.PATCH, $"api/events/{dbo.EventDboId}"); } return(response); }
public IActionResult PostEvent(EventData evt) { var evtDbo = new EventDbo(); evtDbo.CopyFrom(evt); evtDbo.EventToken = Guid.NewGuid(); using (var dbContextTransaction = _context.Database.BeginTransaction()) { _context.Events.Add(evtDbo); _context.SaveChanges(); dbContextTransaction.Commit(); } EventResponse evtDto = CreateEventResponse(evtDbo); return(CreatedAtAction(nameof(GetEvent), new { evtDbo.EventDboId }, evtDto)); }