Exemplo n.º 1
0
        private static void CreateFlavourCombinations(ShishaFlavoursDbContext context, IFlavourCombinationsService flavourCombinationsService, IFlavoursService flavoursService, UserManager <User> userManager, ILogger logger)
        {
            if (!context.FlavourCombinations.Any())
            {
                string adminUserId = userManager.FindByNameAsync("Admin").Result.Id;

                FlavourCombination combination = new FlavourCombination()
                {
                    Name        = "Sweet Mint",
                    Description = "A great combination of the most popular shisha flavours ever!",
                    DateAdded   = DateTime.Now,
                    Votes       = 20,
                    UserId      = adminUserId,
                    FlavourCombinationReferences = new List <FlavourCombinationReference>()
                };

                List <Flavour> flavours = new List <Flavour>()
                {
                    flavoursService.GetFlavourByName("Blueberry").Result,
                    flavoursService.GetFlavourByName("Mint").Result
                };

                flavourCombinationsService.CreateFlavourCombinationAsync(combination, flavours).Wait();
            }
            else
            {
                logger.LogInformation("Flavour combinations were already added");
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] FlavourCombinationCreateModel createModel)
        {
            IActionResult response = null;

            if (ModelState.IsValid)
            {
                FlavourCombination flavourCombo = new FlavourCombination()
                {
                    Name        = createModel.Name,
                    Description = createModel.Description,
                    DateAdded   = DateTime.Now,
                    Votes       = 0,
                    UserId      = createModel.UserId,
                    FlavourCombinationReferences = new List <FlavourCombinationReference>()
                };

                List <Flavour> flavours = new List <Flavour>();

                foreach (string flavourName in createModel.Flavours)
                {
                    Flavour flavour = await flavoursService.GetFlavourByName(flavourName);

                    flavours.Add(flavour);
                }

                ResultStatus status = await flavourCombinationsService.CreateFlavourCombinationAsync(flavourCombo, flavours);

                response = new JsonResult(status);
            }
            else
            {
                return(BadRequest(ModelState));
            }

            return(response);
        }