/// <summary> /// The core upsert implementation. /// </summary> /// <param name="event">The event to be upserted.</param> /// <returns>The updated event.</returns> /// <exception cref="Veritema.Data.ScheduleException"> /// The event must start in the future. /// or /// The event must end after it starts. /// or /// The event must be at least 30 minutes in duration. /// </exception> private async Task <Event> UpsertKernelAsync(Event @event) { string connectionString = _resolver.Resolve(ConnectionStringName) .Match( Some: v => v, None: () => { throw new ConfigurationErrorsException($"Cannot load the configuration string with name {{{ConnectionStringName}}}"); } ); if (@event.StartUtc < DateTime.UtcNow) { throw new ScheduleException("The event must start in the future."); } if (@event.EndUtc < @event.StartUtc) { throw new ScheduleException("The event must end after it starts."); } if (@event.EndUtc < @event.StartUtc.AddMinutes(30)) { throw new ScheduleException("The event must be at least 30 minutes in duration."); } string sql = LoadScript("UpsertEvent.Sql"); using (var connection = new SqlConnection(connectionString)) { var record = new EventRecord { Id = @event.Id > -1 ? @event.Id : 0, Title = @event.Title, Details = @event.Description, End = new DateTimeOffset(@event.EndUtc), Start = new DateTimeOffset(@event.StartUtc), Confirmed = @event.Confirmed, StyleId = @event.Style.HasValue ? (int)@event.Style.Value : new int?(), LocationId = @event.Location != null ? @event.Location.Id : new int?(), Updated = DateTimeOffset.Now, TypeId = (char)@event.Type }; record = await connection.QueryFirstAsync <EventRecord>(sql, record); @event = new Event { Id = record.Id, Title = record.Title, Description = record.Details, EndUtc = record.End.UtcDateTime, StartUtc = record.Start.UtcDateTime, Confirmed = record.Confirmed, Style = record.StyleId.HasValue ? (MartialArtStyle)record.StyleId.Value : new MartialArtStyle?(), Location = record.LocationId.HasValue ? await _locationLoader.GetAsync(record.LocationId.Value) : null, Updated = record.Updated, Type = (EventType)record.TypeId }; } return(@event); }
public async Task <IHttpActionResult> Get(string eid) { IHttpActionResult result; long id; if (long.TryParse(eid, out id)) { var location = await _locationLoader.GetAsync(id); result = Ok(location); } else { result = NotFound(); } return(result); }
/// <summary> /// Convert the database representation to the abstraction. /// </summary> /// <param name="record">The database record.</param> /// <param name="location">The location record.</param> /// <returns>The <see cref="Event"/> representation.</returns> private async Task <Event> MapAsync(EventRecord record) { var @event = new Event { Id = record.Id, Description = record.Details, Title = record.Title, StartUtc = record.Start.UtcDateTime, EndUtc = record.End.UtcDateTime, Confirmed = record.Confirmed, Updated = record.Updated, Type = (EventType)record.TypeId, Style = record.StyleId.HasValue ? (MartialArtStyle)record.StyleId.Value : new MartialArtStyle?() }; if (record.LocationId.HasValue) { @event.Location = await _locationReader.GetAsync(record.LocationId.Value); } return(@event); }