public async Task <Inspection> UpdateAsync(Inspection inspection, CancellationToken cancellationToken = default) { if (inspection.Id == null) { return(null); } // Add or update related entities SpeciesEntity speciesEntity = DbContext.Species.Update(Mapper.Map <SpeciesEntity>(inspection.Species)).Entity; // Retrieve existing user entity and nesting box. Updating these this way is not supported. NestingBoxEntity nestingBoxEntity = await DbContext.NestingBoxes.FindAsync(new object[] { inspection.NestingBox.Id }, cancellationToken).ConfigureAwait(false); if (nestingBoxEntity == null) { return(null); } UserEntity inspectedByUserEntity = inspection.InspectedByUser != null ? await DbContext.Users.FindAsync(new object[] { inspection.InspectedByUser.Id }, cancellationToken).ConfigureAwait(false) : null; // Get existing inspection entity InspectionEntity inspectionEntity = await Entities.FindAsync(new object[] { inspection.Id }, cancellationToken).ConfigureAwait(false); if (inspectionEntity == null) { return(null); } // Update values inspectionEntity.NestingBox = nestingBoxEntity; inspectionEntity.InspectionDate = inspection.InspectionDate.GetValueOrDefault(); inspectionEntity.InspectedByUser = inspectedByUserEntity; inspectionEntity.HasBeenCleaned = inspection.HasBeenCleaned.GetValueOrDefault(); inspectionEntity.Condition = inspection.Condition.GetValueOrDefault(); inspectionEntity.JustRepaired = inspection.JustRepaired.GetValueOrDefault(); inspectionEntity.Occupied = inspection.Occupied.GetValueOrDefault(); inspectionEntity.ContainsEggs = inspection.ContainsEggs.GetValueOrDefault(); inspectionEntity.EggCount = inspection.EggCount; inspectionEntity.ChickCount = inspection.ChickCount.GetValueOrDefault(); inspectionEntity.RingedChickCount = inspection.RingedChickCount.GetValueOrDefault(); inspectionEntity.AgeInDays = inspection.AgeInDays; inspectionEntity.FemaleParentBirdDiscovery = inspection.FemaleParentBirdDiscovery.GetValueOrDefault(); inspectionEntity.MaleParentBirdDiscovery = inspection.MaleParentBirdDiscovery.GetValueOrDefault(); inspectionEntity.Species = speciesEntity; inspectionEntity.Comment = inspection.Comment; // Save changes await DbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); return(Mapper.Map <Inspection>(inspectionEntity)); }
private static async Task ImportInspectionRecordAsync(NesteoDbContext dbContext, InspectionRecord inspectionRecord) { // Create collection for comments var comments = new List <string>(); if (!string.IsNullOrWhiteSpace(inspectionRecord.Comments)) { comments.AddRange(inspectionRecord.Comments.Split(',').Select(c => c.Trim()).Where(c => !string.IsNullOrEmpty(c))); } // Get nesting box entity NestingBoxEntity nestingBoxEntity = await dbContext.NestingBoxes.FindAsync(inspectionRecord.NestingBoxId).ConfigureAwait(false); if (nestingBoxEntity == null) { throw new InvalidCsvRecordException($"Nesting box {inspectionRecord.NestingBoxId} doesn't exist."); } // Ensure the species entity exists SpeciesEntity speciesEntity = null; if (!string.IsNullOrWhiteSpace(inspectionRecord.SpeciesName) && !new[] { "unbestimmt", "unbekannt" }.Contains(inspectionRecord.SpeciesName.ToLower())) { speciesEntity = await GetOrCreateEntityAsync(dbContext, species => species.Name == inspectionRecord.SpeciesName, () => new SpeciesEntity { Name = inspectionRecord.SpeciesName }).ConfigureAwait(false); } // Analyze date info DateTime inspectionDate = string.IsNullOrWhiteSpace(inspectionRecord.Date) ? throw new InvalidCsvRecordException("Inspection date not set.") : ParseDate(inspectionRecord.Date); // Map nesting box condition (Condition condition, bool justRepaired) = GetCondition(inspectionRecord.Condition); if (condition != Condition.Good) { comments.Add($"Kasten-Zustand: {inspectionRecord.Condition}"); } // Analyze ringing activity (ParentBirdDiscovery femaleParentBirdDiscovery, ParentBirdDiscovery maleParentBirdDiscovery, int ringedChickCount) = AnalyzeRingingActivity(inspectionRecord.RingedCount); // Create inspection dbContext.Inspections.Add(new InspectionEntity { NestingBox = nestingBoxEntity, InspectionDate = inspectionDate, InspectedByUser = null, HasBeenCleaned = GetYesNo(inspectionRecord.HasBeenCleaned), Condition = condition, JustRepaired = justRepaired, Occupied = GetYesNoWithUnknown(inspectionRecord.Occupied), ContainsEggs = !string.IsNullOrWhiteSpace(inspectionRecord.EggCount), EggCount = ParseNumberWithUnknown(inspectionRecord.EggCount), ChickCount = ParseNumberWithUnknown(inspectionRecord.ChickCount), RingedChickCount = ringedChickCount, AgeInDays = ParseNumberWithUnknown(inspectionRecord.ChickAges), FemaleParentBirdDiscovery = femaleParentBirdDiscovery, MaleParentBirdDiscovery = maleParentBirdDiscovery, Species = speciesEntity, ImageFileName = null, Comment = comments.Any() ? string.Join(", ", comments) : null, LastUpdated = inspectionDate }); }