/// <summary> /// Retrieves the batch from the database by search on the BatchID. If a negative ID is used it will throw an ArgumentException /// </summary> /// <param name="aID">A batch ID</param> /// <returns>Null if the batch ID doesn't exist, otherwise a Batch Object with the given ID</returns> public Vox.Auditing.Model.Batch GetByID(int aID) { if (aID <= 0) { throw new ArgumentException("Cannot search for a Batch Object with a non positive ID", "aID"); } Batch result = null; using (DataModel.VoxAuditingEntities context = new DataModel.VoxAuditingEntities(connectionString)) { var selectResult = from m in context.Batches where m.BatchID == aID select m; // Check the results. Can only have one result if (selectResult.Count() == 1) { DataModel.Batch selectedBatch = selectResult.First(); result = new Batch(selectedBatch.BatchID, selectedBatch.BatchNumber) { ClientID = selectedBatch.MessageClientID, StartDate = selectedBatch.StartDate, FinishDate = selectedBatch.FinishDate }; } } return(result); }
/// <summary> /// Updates an existing batch entity (Normally this will be to set the end date on the batch) /// </summary> /// <param name="entity">Batch to update</param> /// <returns>True if the batch was altered</returns> public bool Update(Vox.Auditing.Model.Batch entity) { if (entity == null) { throw new ArgumentNullException("entity"); } if (entity.ID == 0) { throw new InvalidBatchException("Cannot updated a batch which has not been added!"); } if (entity.ClientID == 0) { throw new InvalidBatchException("Cannot update a batch with no valid client!"); } bool updated = false; // Check if the batch is valid first using (DataModel.VoxAuditingEntities context = new DataModel.VoxAuditingEntities(connectionString)) { // Check that no existing batches are open for the current client ID var validBatch = from m in context.Batches where m.BatchID == entity.ID select m; if (validBatch.Count() == 1) { DataModel.Batch selectBatch = validBatch.First(); selectBatch.FinishDate = entity.FinishDate; updated = context.SaveChanges() != 0; } else { throw new InvalidBatchException(string.Format("Batch '{0}' does not exist", entity.Number)); } } return(updated); }