public async Task<IActionResult> Index(ShopViewModel shopViewModel) { if (await _shopRepository.UserHasShopAsync(User)) { await _shopRepository.UpdateShopAsync(User, shopViewModel); } else { await _shopRepository.AddShopAsync(User, shopViewModel); } shopViewModel = _shopRepository.GetUserShop(User); return View(shopViewModel); }
public async Task UpdateShopAsync(ClaimsPrincipal user, ShopViewModel shopViewModel) { Shop shop = _dbContext.Shop.FirstOrDefault(x => x.UserId == user.GetUserId()); shop.Name = shopViewModel.Name; shop.ShortDesc = shopViewModel.ShortDesc; shop.City = shopViewModel.City; shop.Address = shopViewModel.Address; shop.Website = shopViewModel.Website; shop.Phone = shopViewModel.Phone; shop.Latitude = shopViewModel.Latitude; shop.Longitude = shopViewModel.Longitude; shop.InsertDate = DateTime.Now; shop.InsertUserId = user.GetUserId(); shop.UserId = user.GetUserId(); _dbContext.Shop.Update(shop); await _dbContext.SaveChangesAsync(); }
public async Task AddShopAsync(ClaimsPrincipal user, ShopViewModel shopViewModel) { Shop shop = new Shop(); var addressData = new AddressData { Address = shopViewModel.Address, City = shopViewModel.City.ToUpper(), Country = "Italy" }; var gls = new GoogleLocationService(); var latlong = gls.GetLatLongFromAddress(addressData); var latitude = latlong.Latitude; var longitude = latlong.Longitude; shop.Name = shopViewModel.Name; shop.ShortDesc = shopViewModel.ShortDesc; shop.City = shopViewModel.City; shop.Address = shopViewModel.Address; shop.Website = shopViewModel.Website; shop.Phone = shopViewModel.Phone; shop.Latitude = latitude; shop.Longitude = longitude; shop.InsertDate = DateTime.Now; shop.InsertUserId = user.GetUserId(); shop.UserId = user.GetUserId(); _dbContext.Shop.Add(shop); await _dbContext.SaveChangesAsync(); }