Example #1
0
 private void ConfigureData()
 {
     gateway = new Domain.Gateway()
     {
         Id           = 1,
         SerialNumber = "G1",
         IPV4Address  = "12.12.1.2",
         Name         = "G1"
     };
     gatewayDto = new GatewayDto()
     {
         Id           = 1,
         SerialNumber = "G1",
         IPV4Address  = "12.12.1.2",
         Name         = "G1"
     };
     gatewayDetailsDto = new GatewayDetailsDto()
     {
         Id           = 1,
         SerialNumber = "G1",
         IPV4Address  = "12.12.1.2",
         Name         = "G1",
         Devices      = new List <DeviceDto>()
     };
     mapper = new MapperConfiguration(c => c.AddProfile <AutoMapping>()).CreateMapper();
     gatewayRepositoryMock.Get(gateway.Id).Returns(Task <Domain.Gateway> .FromResult(gateway));
     gatewayRepositoryMock.Insert(gateway).Returns(Task <Domain.Gateway> .FromResult(gateway));
     gatewayRepositoryMock.Delete(gateway).Returns(Task <Domain.Gateway> .FromResult(gateway));
     service = new GatewayService(this.gatewayRepositoryMock, mapper);
 }
        public GatewayDto Create(CreateGatewayDto input)
        {
            var query       = _gatewayRepository.GetAllIncluding().Where(g => g.HardwareId == input.HardwareId || g.GatewayName == input.GatewayName);
            var gateway_old = query.FirstOrDefault();

            if ((query.Any()) && (query.FirstOrDefault().IsDeleted == true))
            {
                gateway_old.IsDeleted = false;
                var result_old = _gatewayRepository.Update(gateway_old);
                CurrentUnitOfWork.SaveChanges();
                return(ObjectMapper.Map <GatewayDto>(result_old));
            }

            if (query.Any() && gateway_old.IsDeleted == false)
            {
                throw new ApplicationException("网关已存在");
            }

            var workshopQuery = _workshopRepository.GetAllIncluding().Where(w => w.WorkshopName == input.WorkshopName)
                                .Where(w => w.Factory.FactoryName == input.FactoryName)
                                .Where(w => w.Factory.City.CityName == input.CityName);
            var workshop = workshopQuery.FirstOrDefault();

            if (workshop == null)
            {
                throw new ApplicationException("Workshop不存在");
            }

            var gatewayTypeQuery = _gatewayTypeRepository.GetAll().Where(gt => gt.TypeName == input.GatewayTypeName);
            var gatewayType      = gatewayTypeQuery.FirstOrDefault();

            if (gatewayType == null)
            {
                throw new ApplicationException("网关类型不存在");
            }
            var gateway = ObjectMapper.Map <Gateway>(input);

            gateway.Workshop    = workshop;
            gateway.GatewayType = gatewayType;
            var result = _gatewayRepository.Insert(gateway);

            CurrentUnitOfWork.SaveChanges();
            return(ObjectMapper.Map <GatewayDto>(result));
        }