public async Task <Either <ExceptionResponse, GetAnimalByDeviceIdentifierQueryResponse> > Handle( GetAnimalByDeviceIdentifierQuery request, CancellationToken cancellationToken) { GetAnimalByDeviceIdentifierQueryResponse response; using (var context = _animalContextFactory.CreateDbContext(new string[0])) { IQueryable <Device> deviceQuery; if (!string.IsNullOrEmpty(request.NlisId)) { deviceQuery = context.Devices .Include(x => x.DeviceDefinition) .Include(x => x.DeviceAssignment).ThenInclude(p => p.Animal) .Where(da => da.NLISID == request.NlisId); } else { deviceQuery = context.Devices .Include(x => x.DeviceDefinition) .Include(x => x.DeviceAssignment).ThenInclude(p => p.Animal) .Where(da => da.RFID == request.RfId); } var entity = await deviceQuery.FirstOrDefaultAsync(); Data.Domains.Animal.Animal animal = null; if (entity != null && entity.DeviceAssignment.Any()) { animal = entity.DeviceAssignment.FirstOrDefault(devAssgn => devAssgn.ReplacementDate == null)?.Animal; } else { return(ExceptionResponse.With( ErrorMessage: $"Associated Animal's data is not found. AnimalId = {request.NlisId ?? request.RfId}", HttpStatusCode: HttpStatusCode.NotFound)); } var deviceAssignedToAnimal = new DeviceAssignedToAnimal { Species = entity.DeviceDefinition.SpeciesID, IsPostBreederDevice = entity.DeviceDefinition.IsPostBreederDevice, NLISID = entity.NLISID, RFID = entity.RFID, AssignedToPropertyIdentifierID = entity.AssignedToPropertyIdentifierID, AssignmentDate = entity.AssignmentDate, DeviceID = entity.DeviceID, ExcludedDate = entity.ExcludedDate, ExcludedReasonID = entity.ExcludedReasonID, IssueToPropertyIdentifierID = entity.IssueToPropertyIdentifierID, CreatedRequestID = entity.CreatedRequestID, LastModifiedRequestID = entity.LastModifiedRequestID }; response = new GetAnimalByDeviceIdentifierQueryResponse(deviceAssignedToAnimal); } return(response); }
private async Task <Option <DeviceAssignedToAnimal> > GetDeviceByCompositeKey(AnimalContext context, string deviceCompositekey) { var device = await context.Devices .Include(x => x.DeviceDefinition) .Include(x => x.DeviceAssignment).ThenInclude(p => p.Animal) .Where(da => da.NLISID == deviceCompositekey || da.RFID == deviceCompositekey) .FirstOrDefaultAsync(); if (device == null) { return(Option <DeviceAssignedToAnimal> .None); } Data.Domains.Animal.Animal animal = null; if (device.DeviceAssignment.Any()) { animal = device.DeviceAssignment.FirstOrDefault(devAssgn => devAssgn.ReplacementDate == null) ?.Animal; //TODO:MF this criteria should be considered } var deviceAssignmentValue = new DeviceAssignedToAnimal { Species = device.DeviceDefinition.SpeciesID, IsPostBreederDevice = device.DeviceDefinition.IsPostBreederDevice, Animal = animal, DeviceAssignment = device.DeviceAssignment.FirstOrDefault(da => da.ReplacementDate == null), NLISID = device.NLISID, RFID = device.RFID, AssignedToPropertyIdentifierID = device.AssignedToPropertyIdentifierID, AssignmentDate = device.AssignmentDate, DeviceID = device.DeviceID, ExcludedDate = device.ExcludedDate, ExcludedReasonID = device.ExcludedReasonID, IssueToPropertyIdentifierID = device.IssueToPropertyIdentifierID }; return(Option <DeviceAssignedToAnimal> .Some(deviceAssignmentValue)); }
private Data.Domains.Animal.Animal AssignNewAnimalToDevice(AnimalContext context, DeviceAssignedToAnimal deviceDto, int?currentPropertyId, DateTime?latestTransferDate, long?requestId) { var animal = context.Animals.Add(new AnimalMemento(0, DateTime.Now, deviceDto.Species, currentPropertyId ?? deviceDto.AssignedToPropertyIdentifierID, requestId ?? 0)); var deviceAssignment = new DeviceAssignment { DeviceID = deviceDto.DeviceID, AssignmentDate = deviceDto.AssignmentDate ?? DateTime.Now, CreatedRequestID = requestId ?? 0 }; context.DeviceAssignments.Add(deviceAssignment); // todo: refactor after finishing memento //animal.Entity.DeviceAssignment.Add(deviceAssignment); return(Data.Domains.Animal.Animal.FromMemento(animal.Entity)); }
public GetAnimalByDeviceIdentifierQueryResponse(DeviceAssignedToAnimal deviceAssignedToAnimal, RuleResultModel[] ruleResults = null) : base(ruleResults) { DeviceAssignedToAnimal = deviceAssignedToAnimal; }