示例#1
0
 public void Put(int id, [FromBody] UpdateTurbineDto dto)
 {
     _turbineService.Update(id, dto);
 }
        public void Update(int id, UpdateTurbineDto dto)
        {
            var turbine = _turbineRepository.FindById(id);

            if (turbine == null)
            {
                throw new Exception("Turbine was not found");
            }

            if (dto.SerialNum != null)
            {
                turbine.SerialNum = dto.SerialNum;
            }

            if (dto.ProdMW > 0)
            {
                turbine.ProdMW = dto.ProdMW;
            }

            if (dto.Cost > 0)
            {
                turbine.Cost = dto.Cost;
            }

            if (dto.InstallCost > 0)
            {
                turbine.InstallCost = dto.InstallCost;
            }

            if (dto.InstallDate != DateTime.MinValue)
            {
                turbine.InstallDate = dto.InstallDate;
            }

            if (dto.LastServDate != DateTime.MinValue)
            {
                turbine.LastServDate = dto.LastServDate;
            }

            if (dto.NextServDate != DateTime.MinValue)
            {
                turbine.NextServDate = dto.NextServDate;
            }

            if (dto.LastYearServDate != DateTime.MinValue)
            {
                turbine.LastYearServDate = dto.LastYearServDate;
            }

            if (dto.NextYearServDate != DateTime.MinValue)
            {
                turbine.NextYearServDate = dto.NextYearServDate;
            }

            if (dto.TurbineTypeId != turbine.TurbineType.Id)
            {
                var turbineType = _turbineTypeRepository.FindById(dto.TurbineTypeId);

                if (turbineType != null)
                {
                    turbine.TurbineType = turbineType;
                }
            }

            if (dto.OperatorId != turbine.Operator.Id)
            {
                var operatorDbItem = _operatorRepository.FindById(dto.OperatorId);

                if (operatorDbItem != null)
                {
                    turbine.Operator = operatorDbItem;
                }
            }

            if (turbine.Farm != null && dto.FarmId != turbine.Farm.Id)
            {
                var farm = _farmRepository.FindById(dto.FarmId);

                if (farm != null)
                {
                    turbine.Farm = farm;
                }
            }

            if (dto.AddressId != turbine.Address.Id)
            {
                var address = _addressRepository.FindById(dto.AddressId);

                if (address != null)
                {
                    turbine.Address = address;
                }
            }

            if (turbine.Image != null && dto.ImageId != turbine.Image.Id)
            {
                var image = _appImageRepository.FindById(dto.ImageId);

                if (image != null)
                {
                    turbine.Image = image;
                }
            }

            _turbineRepository.Update(turbine);
        }