public SportEventDetailDTO editSportEvent(SportEventDetailDTO sportEventVM) { using (var uow = new UnitOfWork()) { var accountRepository = uow.GetRepository <Account>(); var addressRepository = uow.GetRepository <Adress>(); var sportRepository = uow.GetRepository <Sport>(); var sportEventRepository = uow.GetRepository <SportEvent>(); SportEventDetailMapper sportEventMapper = new SportEventDetailMapper(); SportEvent sportEvent = sportEventMapper.MapFromDTO(sportEventVM); AdressMapper addressMapper = new AdressMapper(); var queryaddress = addressRepository.FindBy(ad => ad.latitude == sportEventVM.Adress.latitude & ad.longitude == sportEventVM.Adress.longitude); Adress address = queryaddress.FirstOrDefault(); if (address == null) { addressRepository.Add(addressMapper.MapFromDTO(sportEventVM.Adress)); uow.SaveChanges(); } address = addressRepository.FindBy(ad => ad.latitude == sportEventVM.Adress.latitude & ad.longitude == sportEventVM.Adress.longitude).FirstOrDefault(); sportEvent.AdressId = address.Id; sportEvent.Adress = address; sportEvent.OwnerId = sportEventVM.Owner.Id; sportEvent.Owner.AdressId = sportEvent.Owner.Adress.Id; SportMapper sportMapper = new SportMapper(); var querysport = sportRepository.FindBy(sp => sp.Name == sportEventVM.Category.Name); Sport sport = querysport.FirstOrDefault(); if (sport == null) { sportRepository.Add(sportMapper.MapFromDTO(sportEventVM.Category)); uow.SaveChanges(); } sport = sportRepository.FindBy(sp => sp.Name == sportEventVM.Category.Name).FirstOrDefault(); sportEvent.Category = sport; sportEvent.CategoryId = sport.Id; sportEventRepository.Edit(sportEvent); uow.SaveChanges(); return(sportEventMapper.MapToDTO(sportEvent)); } }
public SportDTO addSport(SportDTO sportVM) { using (var uow = new UnitOfWork()) { var sportRepository = uow.GetRepository <Sport>(); var querySport = sportRepository.FindBy(sp => sp.Name == sportVM.Name); var exist = querySport.FirstOrDefault(); SportMapper sportMapper = new SportMapper(); if (exist == null) { sportRepository.Add(sportMapper.MapFromDTO(sportVM)); uow.SaveChanges(); var query = sportRepository.FindBy(s => s.Name == sportVM.Name); var sp = querySport.FirstOrDefault(); return(sportMapper.MapToDTO(sp)); } else { return(null); } } }