Пример #1
0
        public static void Initialize(CoffeeContext context)
        {
            context.Database.EnsureCreated();

            if (context.Coffees.Any())
            {
                return;
            }
            var coffees = new CoffeeDto[]
            {
                new CoffeeDto {
                    name = "Cappuccino"
                },
                new CoffeeDto {
                    name = "Latte"
                }
            };

            foreach (CoffeeDto c in coffees)
            {
                context.Coffees.Add(new Entities.Coffee()
                {
                    Name = c.name
                });
                context.SaveChanges();
            }
        }
Пример #2
0
        public async Task <IActionResult> OrderCoffee(CoffeeDto dto)
        {
            string accessToken = await this.HttpContext.GetTokenAsync("access_token");

            HttpClient client = new HttpClient();

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);

            CoffeeInputDto inputDto = new CoffeeInputDto()
            {
                Coffee = dto.Coffee
            };

            StringContent content = new StringContent(JsonConvert.SerializeObject(inputDto), Encoding.UTF8, "application/json");

            HttpResponseMessage response = await client.PostAsync("http://localhost:5003/api/coffee", content);

            if (response.IsSuccessStatusCode)
            {
                string result = await response.Content.ReadAsStringAsync();

                return(View("OrderResult", new CoffeeResultDto()
                {
                    Result = result
                }));
            }
            else
            {
                ViewBag.Json = "403 - Forbidden";
                return(View("json"));
            }
        }
Пример #3
0
        public static CoffeeDto ToDto(this Coffee coffee)
        {
            coffee.CheckArgumentIsNull(nameof(coffee));
            var coffeeDto = new CoffeeDto(coffee.CoffeeDisplayId);

            coffeeDto.CoffeeName = coffee.CoffeeName;
            return(coffeeDto);
        }
        public async Task <CoffeeDto> GetCoffeeAsync(string name, CancellationToken ct)
        {
            Coffee coffeesEntity = await _uow.Coffees.GetCoffeeAsync(name, ct);

            CoffeeDto coffeesDto = _mapper.Mapper.Map <CoffeeDto>(coffeesEntity);

            return(coffeesDto);
        }
Пример #5
0
 public async Task <CoffeeDto> AddCoffeeAsync(CoffeeDto newCoffee)
 {
     using (var db = new CoffeBookContext())
     {
         var coffeeEntity = ConvertDtoToEntity(newCoffee, db);
         return(newCoffee);
     }
 }
Пример #6
0
 public async Task <IActionResult> Create([Bind("Id,Name,Value,Description,Energy")] CoffeeDto coffee)
 {
     if (ModelState.IsValid)
     {
         coffee.Id = Guid.NewGuid();
         await new APICoffee().Add(coffee);
         return(RedirectToAction(nameof(Index)));
     }
     return(View(coffee));
 }
Пример #7
0
 public async Task <CoffeeDto> UpdateCoffeeAsync(CoffeeDto updatedCoffee)
 {
     using (var db = new CoffeBookContext())
     {
         var coffee = db.Coffes.SingleOrDefault(x => x.Id == updatedCoffee.Id);
         coffee.Name    = updatedCoffee.Name;
         coffee.Picture = updatedCoffee.Picture;
         db.SaveChanges();
         return(new CoffeeDto(coffee));
     }
 }
Пример #8
0
        public static CoffeeDto ConvertFromCoffee(Coffee coffee)
        {
            CoffeeDto coffeeDto = new CoffeeDto
            {
                Id      = coffee.Id,
                Name    = coffee.Name,
                Picture = coffee.Picture
            };

            return(coffeeDto);
        }
Пример #9
0
        public static Coffee ConvertToCoffee(CoffeeDto coffeeType)
        {
            Coffee coffee = new Coffee
            {
                Id      = coffeeType.Id,
                Name    = coffeeType.Name,
                Picture = coffeeType.Picture
            };

            return(coffee);
        }
Пример #10
0
        public async Task <BlCallResult <RecipeDto> > AddRecipeToCoffee(CoffeeDto coffee, RecipeDto recipe)
        {
            Console.WriteLine("AddRecipeToCoffee enter");
            using (var db = new CoffeBookContext())
            {
                var coffeeFromDb = db.Coffes.SingleOrDefault(x => x.Id == coffee.Id);
                if (coffeeFromDb == null)
                {
                    try
                    {
                        await AddCoffeeAsync(coffee);

                        coffeeFromDb = db.Coffes.SingleOrDefault(x => x.Id == coffee.Id);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("AddRecipeToCoffee exception");
                        return(new BlCallResult <RecipeDto>(BlCallResult.BlResult.CoffeeError, e));
                    }
                }
                var recipeFromDb = GetRecipe(recipe.Id, db);
                if (recipeFromDb != null)
                {
                    recipeFromDb.CoffeeType = coffeeFromDb;
                }
                else
                {
                    try
                    {
                        var newRecipe = await AddRecipeAsync(recipe);

                        newRecipe.CoffeeType = coffee;
                        recipe = newRecipe;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("AddRecipeToCoffee exception");
                        return(new BlCallResult <RecipeDto>(BlCallResult.BlResult.RecipeError, e));
                    }
                }
                try
                {
                    db.SaveChanges();
                    Console.WriteLine("AddRecipeToCoffee exit");
                    return(new BlCallResult <RecipeDto>(await GetRecipeAsync(recipe.Id)));
                }
                catch (Exception e)
                {
                    Console.WriteLine("AddRecipeToCoffee exception");
                    return(new BlCallResult <RecipeDto>(BlCallResult.BlResult.DbError, e));
                }
            }
        }
Пример #11
0
        public async Task <IActionResult> Edit(Guid id, [Bind("Id,Name,Value,Description,Energy")] CoffeeDto coffee)
        {
            if (id != coffee.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                await new APICoffee().Edit(coffee);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(coffee));
        }
Пример #12
0
        private Coffee ConvertDtoToEntity(CoffeeDto coffeeDto, CoffeBookContext db)
        {
            var coffee = db.Coffes.SingleOrDefault(x => x.Id == coffeeDto.Id);

            if (coffee == null)
            {
                coffee = new Coffee
                {
                    Name    = coffeeDto.Name,
                    Picture = coffeeDto.Picture
                };
                db.Coffes.Add(coffee);
                db.SaveChanges();
                coffeeDto.Id = coffee.Id;
            }
            return(coffee);
        }
Пример #13
0
        public static Coffee ToEntity(this CoffeeDto coffeeDto, bool generatePrimaryKey = false)
        {
            coffeeDto.CheckArgumentIsNull(nameof(coffeeDto));

            var coffee = new Coffee()
            {
                CoffeeName      = coffeeDto.CoffeeName,
                ValidFrom       = coffeeDto.ValidFrom,
                ValidTo         = coffeeDto.ValidTo,
                CoffeeDisplayId = coffeeDto.CoffeeDisplayId
            };

            if (generatePrimaryKey)
            {
                coffee.CoffeeId = Guid.NewGuid();
            }

            return(coffee);
        }
Пример #14
0
        public async Task <(HttpStatusCode statusCode, string coffeeId)> CreateCoffee(AddCoffeeDto coffeeToAdd)
        {
            _logger.LogInformation($"Service-CreateCoffee-Executing CreateCoffee started at {DateTime.UtcNow}");

            var       coffeeId      = string.Empty;
            var       statusCode    = HttpStatusCode.Created;
            CoffeeDto cofeeToStrore = default(CoffeeDto);

            var coffeeSpec = new CoffeeWithAreasSpecification(coffeeToAdd.CoffeeName, false);

            var cofee = (await _coffeeRepository.ListAsync(coffeeSpec).ConfigureAwait(false)).FirstOrDefault();

            if (cofee != null)
            {
                _logger.LogInformation($"cofee with cofee name {coffeeToAdd.CoffeeName} already exists!!!");
                statusCode = HttpStatusCode.BadRequest;
            }
            else
            {
                Coffee cofeeEntity = await _coffeeRepository.GetMaxOfPrimaryKey();

                string newCoffeeDisplayId = cofeeEntity.GetNextPrimaryKey();

                cofeeToStrore            = new CoffeeDto(newCoffeeDisplayId);
                cofeeToStrore.CoffeeName = coffeeToAdd.CoffeeName;

                await _coffeeRepository.AddAsync(cofeeToStrore.ToEntity(true)).ConfigureAwait(false);

                await _coffeeRepository.SaveAllwithAudit().ConfigureAwait(false);

                statusCode = HttpStatusCode.OK;
                coffeeId   = newCoffeeDisplayId;
            }

            _logger.LogInformation($"Service-CreateCoffee-Executing CreateCoffee completed at {DateTime.UtcNow}");

            return(statusCode, coffeeId);
        }
Пример #15
0
 public async Task <BlCallResult <IList <RecipeDto> > > GetCoffeeRecipes(CoffeeDto coffeeDto)
 {
     Console.WriteLine("GetCoffeeRecipes enter");
     using (var db = new CoffeBookContext())
     {
         CoffeeDto coffee;
         try
         {
             coffee = await GetCoffeeAsync(coffeeDto.Id);
         }
         catch (Exception e)
         {
             return(new BlCallResult <IList <RecipeDto> >(BlCallResult.BlResult.CoffeeError, e));
         }
         var recipes    = db.Recipes.Include(x => x.CoffeeType).Where(x => x.CoffeeType.Id == coffee.Id);
         var recipeDtos = new List <RecipeDto>();
         foreach (var recipe in recipes)
         {
             recipeDtos.Add(new RecipeDto(recipe));
         }
         Console.WriteLine("GetCoffeeRecipes exit");
         return(new BlCallResult <IList <RecipeDto> >(recipeDtos));
     }
 }
Пример #16
0
        private void TestBL()
        {
            var dbHandler = new CoffeeBookDbHandlerFactory().GetDbHandler();

            //Coffee
            var coffeeNew = new CoffeeDto {
                Name = "GoodCoffee"
            };
            var coffeeAdded  = dbHandler.AddCoffeeAsync(coffeeNew).Result;
            var coffeeFromDb = dbHandler.GetCoffeeAsync(coffeeAdded.Id).Result;

            coffeeFromDb.Name = "Not GoodCoffee";
            var coffeeUpdated = dbHandler.UpdateCoffeeAsync(coffeeFromDb).Result;

            //Recipe
            var recipeNew = new RecipeDto
            {
                CoffeeType  = coffeeNew,
                Description = "Very good recipe",
                Name        = "Recipe for good coffee"
            };
            var recipeAdded  = dbHandler.AddRecipeAsync(recipeNew).Result;
            var recipeFromDb = dbHandler.GetRecipeAsync(recipeAdded.Id).Result;

            recipeFromDb.Name = "Useless Recipe";
            var recipeUpdated = dbHandler.UpdateRecipeAsync(recipeFromDb).Result;

            //RecipeBook
            var recipeBookNew = new RecipeBookDto
            {
                Description = "RBook fro user Feri",
                Name        = "FeriBook",
                Recipes     = new List <RecipeDto> {
                    recipeUpdated
                }
            };
            var recipeBookAdded  = dbHandler.AddRecipeBookAsync(recipeBookNew).Result;
            var recipeBookFromDb = dbHandler.GetRecipeBookAsync(recipeBookAdded.Id).Result;

            recipeBookFromDb.Recipes.Add(new RecipeDto
            {
                CoffeeType  = coffeeFromDb,
                Description = "Added Recipe",
                Name        = "Added"
            });
            var recipeBookUpdated = dbHandler.UpdateRecipeBookAsync(recipeBookFromDb).Result;

            //User
            var userNew = new UserDto
            {
                RecipeBooks = new List <RecipeBookDto> {
                    recipeBookUpdated
                },
                Name     = "Feri",
                Password = "******",
            };
            var userAdded  = dbHandler.AddUserAsync(userNew).Result;
            var userFromDb = dbHandler.GetUserAsync(userAdded.Id).Result;

            userFromDb.Name = "Galaktikus Johnny";
            userFromDb.RecipeBooks.RemoveAt(0);
            var userUpdated = dbHandler.UpdateUserAsync(userFromDb).Result;

            //GetAll
            var users = dbHandler.GetAllUsersAsync().Result;
            var rbs   = dbHandler.GetAllRecipeBooksAsync().Result;
            var rs    = dbHandler.GetAllRecipesAsync().Result;
            var cs    = dbHandler.GetAllCoffeesAsync().Result;

            //Delete
            dbHandler.DeleteUserAsync(userNew.Id);
            dbHandler.DeleteRecipeBookAsync(recipeBookNew.Id);
            dbHandler.DeleteRecipeAsync(recipeNew.Id);
            dbHandler.DeleteCoffeeAsync(coffeeNew.Id);

            var usersafterDelete       = dbHandler.GetAllUsersAsync().Result;
            var recipebooksafterDelete = dbHandler.GetAllRecipeBooksAsync().Result;
            var recipesafterDelete     = dbHandler.GetAllRecipesAsync().Result;
            var coffeesafterDelete     = dbHandler.GetAllCoffeesAsync().Result;
        }