public Batch Index(int customerId, [FromBody] Collection<Image> images) { List<Image> initialisedImages = new List<Image>(); var newBatchId = GetModel().Batches.Select(b => b.ModelId).Max() + 1; var batch = new Batch(newBatchId, customerId, DateTime.Now); GetModel().Batches.Add(batch); foreach (var incomingImage in images.Members) { var newImage = new Image(customerId, incomingImage.Space, incomingImage.ModelId, DateTime.Now, incomingImage.Origin, incomingImage.InitialOrigin, 0, 0, incomingImage.MaxUnauthorised, null, null, null, true, null, incomingImage.Tags, incomingImage.String1, incomingImage.String2, incomingImage.String3, incomingImage.Number1, incomingImage.Number2, incomingImage.Number3, GetModel().ImageOptimisationPolicies.First().Id, GetModel().ThumbnailPolicies.First().Id); initialisedImages.Add(newImage); } GetModel().Images.AddRange(initialisedImages); GetModel().BatchImages.Add(batch.Id, initialisedImages.Select(im => im.Id).ToList()); return batch; }
private static List<Batch> CreateBatches(List<Image> images, Dictionary<string, List<string>> batchImages) { var r = new Random(); var batches = new List<Batch>(); int batchId = 100001; Batch currentBatch = null; int batchSize = -1; int counter = -1; int currentCustomer = -1; List<string> imagesInBatch = null; foreach (var image in images) { if (counter++ > batchSize || image.CustomerId != currentCustomer) { // save the old batch if (currentBatch != null) { batches.Add(currentBatch); batchImages.Add(currentBatch.Id, imagesInBatch); } // start a new batch currentCustomer = image.CustomerId; counter = 1; batchSize = r.Next(3, 10); currentBatch = new Batch(batchId++, image.CustomerId, image.Created.AddSeconds(-1)); imagesInBatch = new List<string>(); } imagesInBatch.Add(image.Id); image.Batch = currentBatch.Id; } batches.Add(currentBatch); batchImages.Add(currentBatch.Id, imagesInBatch); return batches; }