Exemplo n.º 1
0
        public async Task <Either <ExceptionResponse, UpdateAnimalCommandResponse> > Handle(UpdateAnimalCommand request,
                                                                                            CancellationToken cancellationToken)
        {
            var commandParams = request.AnimalPayload;

            DeviceAssignedToAnimal deviceDto;

            Data.Domains.Animal.Animal existingAnimal = null;
            using (var context = _animalContextFactory.CreateDbContext(new string[0]))
            {
                deviceDto = await GetDeviceByCompositeKey(context, commandParams.DeviceIdentifier)
                            .Match(some => some, () => throw new InvalidOperationException("Device not found."));

                existingAnimal = deviceDto.Animal;
                if (existingAnimal == null)
                {
                    return(ExceptionResponse.With(
                               ErrorMessage: $"animal/{request.AnimalPayload.DeviceCompositeKey}",
                               HttpStatusCode: HttpStatusCode.NotFound));
                }

                existingAnimal.LastModifiedRequestID = request.RequestId;

                await context.SaveChangesAsync(request.RequestId);
            }
            return(new UpdateAnimalCommandResponse(existingAnimal));
        }
        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 ApplyPropertyRules(AnimalContext context, Data.Domains.Animal.Animal animal,
                                       long assignedToPropertyId,
                                       DateTime activationDate, long?requestId)
 {
     var activeRules = await context.PropertyAnimalStatusRules.Include(x => x.ProgramStatus)
                       .Include(x => x.ProgramStatus.Program)
                       .Where(r => r.PropertyIdentifierID == assignedToPropertyId &&
                              (r.RuleInactivationDate == null || r.RuleInactivationDate > DateTime.Now) &&
                              r.RuleActivationDate <= DateTime.Now)
                       .ToArrayAsync();
 }
        public async Task <Either <ExceptionResponse, CreateAnimalCommandResponse> > Handle(CreateAnimalCommand request,
                                                                                            CancellationToken cancellationToken)
        {
            var commandParams = request.AnimalPayload;

            DeviceAssignedToAnimal deviceDto;

            Data.Domains.Animal.Animal animal = null;

            using (var context = _animalContextFactory.CreateDbContext(new string[0]))
            {
                deviceDto = await GetDeviceByCompositeKey(context, commandParams.DeviceIdentifier)
                            .Match(some => some,
                                   () => new DeviceAssignedToAnimal());

                // todo MF: needs to be refactored
                if (deviceDto.DeviceID == 0)
                {
                    return(ExceptionResponse.With(ErrorMessage: "Device not found.", HttpStatusCode: HttpStatusCode.NotFound));
                }

                if (deviceDto.IssueToPropertyIdentifierID > 0)
                {
                    if (!deviceDto.AnimalAssignedToDevice)
                    {
                        animal = AssignNewAnimalToDevice(context, deviceDto, commandParams.CurrentPropertyIdentifierId,
                                                         commandParams.TransactionDate, request.RequestId);

                        await ApplyPropertyRules(context, animal, deviceDto.AssignedToPropertyIdentifierID, commandParams.TransactionDate,
                                                 request.RequestId);

                        await context.SaveChangesAsync(request.RequestId);
                    }
                    else
                    {
                        animal = deviceDto.Animal;
                        return(new CreateAnimalCommandResponse(animal));
                    }
                }
            }

            return(new CreateAnimalCommandResponse(animal));
        }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
 public UpdateAnimalCommandResponse(Data.Domains.Animal.Animal animal, RuleResultModel[] ruleResults = null) :
     base(ruleResults)
 {
     Animal = animal;
 }