예제 #1
0
        public CoctailRepositoryTests(DependencySetupFixtureForInMemory dependencySetupFixture)
        {
            _serviceProvide = dependencySetupFixture.ServiceProvider;
            _serviceScope   = _serviceProvide.CreateScope();

            _context           = _serviceScope.ServiceProvider.GetRequiredService <CoctailContext>();
            _coctailRepository = _serviceScope.ServiceProvider.GetRequiredService <ICoctailRepository>();

            _userManager = _serviceScope.ServiceProvider.GetRequiredService <UserManager <AppUser> >();
            _roleManager = _serviceScope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole <int> > >();

            _fixture = new Fixture().Customize(new AutoMoqCustomization());

            var coctailsData = Task.Run(() => File.ReadAllTextAsync(Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName, "DrinkerApiTests/SeedForTesting.json"))).Result;

            var options = new JsonSerializerOptions
            {
                NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString
            };

            var coctails = JsonSerializer.Deserialize <List <Coctail> >(coctailsData, options);

            foreach (var item in coctails)
            {
                item.IsAccepted = true;
                Task.Run(() => _context.Coctails.AddAsync(item)).Wait();
            }

            Task.Run(() => _context.SaveChangesAsync()).Wait();

            var roles = new List <IdentityRole <int> > {
                new IdentityRole <int> {
                    Name = "User"
                },
                new IdentityRole <int> {
                    Name = "Admin"
                }
            };

            foreach (var role in roles)
            {
                Task.Run(() => _roleManager.CreateAsync(role)).Wait();
            }

            var admin = new AppUser {
                UserName = "******", Email = "*****@*****.**"
            };
            var defaultUser = new AppUser {
                UserName = "******", Email = "*****@*****.**"
            };

            Task.Run(() =>
            {
                _userManager.CreateAsync(admin, "Pa$$w0rd");
                _userManager.CreateAsync(defaultUser, "Pa$$w0rd");

                _userManager.AddToRoleAsync(admin, "Admin");
                _userManager.AddToRoleAsync(defaultUser, "User");
            }).Wait();
        }
예제 #2
0
        public async Task <int> AddCoctail(CoctailToAdd coctailToAdd, int userId)
        {
            var coctail = new Coctail
            {
                Alcoholic    = coctailToAdd.Alcoholic,
                Category     = coctailToAdd.Category,
                DateModified = DateTime.Now.ToString(),
                Glass        = coctailToAdd.Glass,
                Name         = coctailToAdd.Name,
                Instructions = coctailToAdd.Instructions,
                IsAccepted   = false,
                Ingradients  = coctailToAdd.Ingradients.Select(x => new Ingredient {
                    Name = x.Name, Measure = x.Measure
                }).ToList(),
                UserId = userId
            };

            await _context.Coctails.AddAsync(coctail);

            if (await _context.SaveChangesAsync() > 0)
            {
                return(coctail.Id);
            }

            return(0);
        }