public SportEventDTO addSportEvent(SportEventDTO sporteventVM)
        {
            using (var uow = new UnitOfWork())
            {
                var sportEventRepository = uow.GetRepository <SportEvent>();
                var adressRepository     = uow.GetRepository <Adress>();
                var sportRepository      = uow.GetRepository <Sport>();
                var accountRepository    = uow.GetRepository <Account>();

                AdressService  addressService = new AdressService();
                SportService   sportService   = new SportService();
                AccountService accountService = new AccountService();


                SportEventMapper sportEventMapper = new SportEventMapper();
                SportEvent       sportEvent       = sportEventMapper.MapFromDTO(sporteventVM);



                var queryAddress = adressRepository.FindBy(ad => ad.latitude == sportEvent.Adress.latitude |
                                                           ad.longitude == sportEvent.Adress.longitude);
                var existAd = queryAddress.FirstOrDefault();

                if (existAd != null)
                {
                    sportEvent.Adress = existAd;
                }


                var queryCategory = sportRepository.FindBy(sp => sp.Name == sportEvent.Category.Name);
                var existCategory = queryCategory.FirstOrDefault();

                if (existCategory != null)
                {
                    sportEvent.Category = existCategory;
                }


                var queryOwner = accountRepository.FindBy(ac => ac.UserName == sportEvent.Owner.UserName |
                                                          ac.Email == sportEvent.Owner.Email);
                var existOwner = queryOwner.FirstOrDefault();

                if (existOwner != null)
                {
                    sportEvent.Owner = existOwner;
                }

                sportEvent = sportEventRepository.Add(sportEvent);
                uow.SaveChanges();
                return(sportEventMapper.MapToDTO(sportEvent));
            }
        }
Пример #2
0
        public AccountDTO addAccount(AccountDTO accountVM)
        {
            using (var uow = new UnitOfWork())
            {
                var           accountRepository = uow.GetRepository <Account>();
                var           adressRepository  = uow.GetRepository <Adress>();
                AdressService addressService    = new AdressService();

                AccountMapper accountMaper = new AccountMapper();
                Account       account      = accountMaper.MapFromDTO(accountVM);

                var queryAccounts = accountRepository.FindBy(ac => ac.UserName == accountVM.UserName | ac.Email == accountVM.Email);
                var existAc       = queryAccounts.FirstOrDefault();

                if (existAc == null)
                {
                    var queryAddress = adressRepository.FindBy(ad => ad.latitude == accountVM.Adress.latitude |
                                                               ad.longitude == accountVM.Adress.longitude);
                    var existAd = queryAddress.FirstOrDefault();

                    if (existAd == null)
                    {
                        addressService.addAdress(accountVM.Adress);
                    }
                    else
                    {
                        account.Adress = existAd;
                    }
                }
                else
                {
                    return(null);
                }

                accountRepository.Add(account);
                uow.SaveChanges();

                return(accountMaper.MapToDTO(account));
            }
        }