Exemple #1
0
        // End of Pre-Load

        public async Task CreateIngredientAsync(string name, byte primary)
        {
            var ingredient = new Ingredient()
            {
                Name    = name.ToLower(),
                Primary = primary
            };

            await dbContext.Ingredients.AddAsync(ingredient);

            await dbContext.SaveChangesAsync();
        }
Exemple #2
0
        public async Task CityNotificationAsync(string barName, string cityName)
        {
            var users = await dbContext.Users.Where(p => p.City.ToLower() == cityName.ToLower()).ToListAsync();

            foreach (var user in users)
            {
                var notification = new Notification()
                {
                    Name = "New Bar",
                    Text = $"A new bar called {barName} has opened in {cityName}",
                    User = user
                };
                await dbContext.Notifications.AddAsync(notification);

                await dbContext.SaveChangesAsync();
            }
        }
        //End of Pre-Load

        public async Task CreateCountryAsync(string countryName)
        {
            if (string.IsNullOrWhiteSpace(countryName))
            {
                throw new ArgumentNullException("Country name cannot be null or whitespace.");
            }
            if (!(await CheckIfCountryExistsAsync(countryName)))
            {
                var country1 = new Country()
                {
                    Name = countryName
                };
                await dbContext.Countries.AddAsync(country1);
                await dbContext.SaveChangesAsync();
            }
            else
                throw new ArgumentException("Country with that name already exists!");
        }
Exemple #4
0
        // End of Pre-Load

        public async Task AddBarAsync(string name, string address, string description, string countryName, string cityName, byte[] barCover)
        {
            if (String.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException("Bar name cannot be null or empty.");
            }
            if (String.IsNullOrWhiteSpace(address))
            {
                throw new ArgumentException("Address cannot be null or empty.");
            }
            if (!await countryService.CheckIfCountryExistsAsync(countryName))
            {
                await countryService.CreateCountryAsync(countryName);
            }

            if (!await cityService.CheckIfCityExistsAsync(cityName))
            {
                await cityService.CreateCityAsync(cityName, countryName);
            }

            var country = await countryService.GetCountryByNameAsync(countryName);

            var city = await cityService.GetCityByNameAsync(cityName);

            var bar = new Bar()
            {
                Name          = name,
                Address       = address,
                Description   = description,
                Country       = country,
                City          = city,
                AverageRating = 0,
                Hidden        = 0
            };
            await dbContext.Bars.AddAsync(bar);

            var barPhoto = new BarPhoto()
            {
                BarCover = barCover,
                Bar      = bar
            };
            await dbContext.BarPhotos.AddAsync(barPhoto);

            await dbContext.SaveChangesAsync();

            await nService.CityNotificationAsync(name, cityName);
        }
Exemple #5
0
        //End of Pre-Load

        public async Task CreateCityAsync(string cityName, string countryName)
        {
            if (String.IsNullOrWhiteSpace(cityName))
            {
                throw new ArgumentException("City name cannot be null or empty.");
            }
            if (!(await countryService.CheckIfCountryExistsAsync(countryName)))
            {
                await countryService.CreateCountryAsync(countryName);
            }

            var country = await countryService.GetCountryByNameAsync(countryName);

            var newCity = new City()
            {
                Name    = cityName,
                Country = country
            };
            await dbContext.Cities.AddAsync(newCity);

            await dbContext.SaveChangesAsync();
        }
        //End of Pre-Load

        public async Task CreateCocktailAsync(string name, string description, string[] primaryIngredients, string[] ingredients, byte[] photo)
        {
            if (primaryIngredients == null)
            {
                throw new ArgumentNullException("Primary ingredients list cannot be null.");
            }
            if (String.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("Cocktail name cannot be null or whitespace.");
            }
            var cocktail = new Cocktail()
            {
                Name          = name,
                Description   = description,
                AverageRating = 0,
                Hidden        = 0
            };
            await dbContext.Cocktails.AddAsync(cocktail);

            var cocktailPhoto = new CocktailPhoto()
            {
                CocktailCover = photo,
                Cocktail      = cocktail
            };
            await dbContext.CocktailPhotos.AddAsync(cocktailPhoto);

            await dbContext.SaveChangesAsync();

            foreach (var item in primaryIngredients.Where(p => !String.IsNullOrEmpty(p)))
            {
                if (!await iService.CheckIfIngredientExistsAsync(item, 1))
                {
                    await iService.CreateIngredientAsync(item, 1);
                }
                await AddIngredientToCocktailAsync(cocktail.Name, item.ToLower(), 1);
            }
            foreach (var item in (ingredients ?? new string[0]).Where(p => !String.IsNullOrEmpty(p)))
            {
                if (!await iService.CheckIfIngredientExistsAsync(item, 0))
                {
                    await iService.CreateIngredientAsync(item, 0);
                }
                await AddIngredientToCocktailAsync(cocktail.Name, item.ToLower(), 0);
            }
        }
Exemple #7
0
        //public async  Task<IList<User>> FindAllUsersAsync()
        //{
        //  List<User> users = await dbContext.Users.ToListAsync();
        //    return users;
        //}
        public async Task AddAccountAsync(string userName, string firstName, string lastName, string password, string accountType, string countryName, string cityName)
        {
            if (String.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException("Username cannot be null or empty.");
            }
            if (String.IsNullOrWhiteSpace(password) || password.Length < 6)
            {
                throw new ArgumentException("Password is required and must be at least 6 characters long.");
            }
            if (await this.dbContext.Users.AnyAsync(p => p.UserName == userName))
            {
                throw new ArgumentException("Username is already taken.");
            }

            var user = new User()
            {
                UserName      = userName,
                FirstName     = firstName,
                LastName      = lastName,
                Password      = hasher.Hash(password),
                AccountType   = accountType,
                AccountStatus = "Active",
                Country       = countryName,
                City          = cityName,
            };
            await dbContext.Users.AddAsync(user);

            var userPhoto = new UserPhoto()
            {
                User = user
            };
            await dbContext.UserPhotos.AddAsync(userPhoto);

            await dbContext.SaveChangesAsync();
        }