public WorkshopDto Update(CreateWorkshopDto input) { var cityQuery = _cityRepository.GetAll().Where(c => c.CityName == input.CityName); var city = cityQuery.FirstOrDefault(); if (city.IsNullOrDeleted()) { throw new ApplicationException("City不存在或输入错误"); } var factoryQuery = _factoryRepository.GetAll().Where(f => f.FactoryName == input.FactoryName); var factory = factoryQuery.FirstOrDefault(); if (factory.IsNullOrDeleted()) { throw new ApplicationException("Factory不存在或输入错误"); } var workshop = _workshopRepository.Get(input.Id); ObjectMapper.Map(input, workshop); workshop.Factory = factory; var result = _workshopRepository.Update(workshop); CurrentUnitOfWork.SaveChanges(); return(ObjectMapper.Map <WorkshopDto>(workshop)); }
public WorkshopDto Create(CreateWorkshopDto input) { var cityQuery = _cityRepository.GetAll().Where(c => c.CityName == input.CityName); var city = cityQuery.FirstOrDefault(); if (city.IsNullOrDeleted()) { throw new ApplicationException("City不存在或输入错误"); } var factoryQuery = _factoryRepository.GetAll().Where(f => f.FactoryName == input.FactoryName); var factory = factoryQuery.FirstOrDefault(); if (factory.IsNullOrDeleted()) { throw new ApplicationException("Workshop不存在或输入错误"); } var workshopQuery = _workshopRepository.GetAll().Where(w => w.WorkshopName == input.WorkshopName); if (workshopQuery.Any()) { throw new ApplicationException("WorkshopName重复"); } var entity = ObjectMapper.Map <Workshop>(input); entity.Factory = factory; var result = _workshopRepository.Insert(entity); CurrentUnitOfWork.SaveChanges(); return(ObjectMapper.Map <WorkshopDto>(result)); }