public static void RegisterDeviceForPushNotifications(IRequestMessage<Registration> request, IResponseMessage response) { if (!string.IsNullOrEmpty(request.RequestObject.PushToken)) { using (Repository repository = new Repository()) { using (IDbTransaction transaction = repository.Connection.BeginTransaction()) { try { Pass pass = repository.GetPass( request.RouteValue<string>("passTypeIdentifier"), request.RouteValue<string>("serialNumber"), transaction); if (pass != null) { Registration registration = new Registration() { Created = DateTime.UtcNow, DeviceLibraryIdentifier = request.RouteValue<string>("deviceLibraryIdentifier"), LastUpdated = DateTime.UtcNow, PassId = pass.Id, PushToken = request.RequestObject.PushToken }; if (repository.CreateRegistration(registration, transaction)) { response.SetStatus(StatusCode.Created); } } else { response.SetStatus(StatusCode.NotFound); } transaction.Commit(); } catch { transaction.Rollback(); throw; } } } } else { response.SetStatus(StatusCode.BadRequest); } }
public bool CreateRegistration(Registration registration, IDbTransaction transaction = null) { const string ExistingSql = @"SELECT COUNT([Id]) FROM [Registration] WHERE [PassId] = @PassId AND [DeviceLibraryIdentifier] = @DeviceLibraryIdentifier;"; const string InsertSql = @"INSERT INTO [Registration]([Created],[DeviceLibraryIdentifier],[LastUpdated],[PassId],[PushToken]) VALUES(@Created,@DeviceLibraryIdentifier,@LastUpdated,@PassId,@PushToken); SELECT last_insert_rowid();"; if (0 < this.connection.Query<long>(ExistingSql, registration, transaction).First()) { registration.Id = this.connection.Query<long>(InsertSql, registration, transaction).First(); return true; } return false; }