public void createRecipe_Test() { var listitemup = new RecipeInputDto { creatorId = 1, name = "Bulgur", description = "Boiled Bulgur fried with butter", rating = 9.8F, tag = "Turkish Cuisine" }; var listitem = new RecipeDto { id = 1, creatorId = 1, name = "Bulgur", description = "Boiled Bulgur fried with butter", rating = 9.8F, tag = "Turkish Cuisine" }; var tobereturned = new List <RecipeDto> { listitem }; _recipeRepository.Setup(x => x.CreateRecipe(listitemup)).Returns(tobereturned); _recipeRepository.Setup(x => x.GetRecipeById(1)).Returns(listitem); _recipeRepository.Setup(x => x.GetRecipes()).Returns(tobereturned); var tmp = _sut.ServiceCreateRecipe(listitemup); tmp.Should().BeEquivalentTo(tobereturned, options => options.ComparingByMembers <RecipeDto>());; }
private void sendEmailNotification(RecipeInputDto recipe, string tomail, string text) { try { string subject = "New " + recipe.name + " available at Cookify"; string body = text + ",\n" + "Cookify team"; string FromMail = "*****@*****.**"; string emailTo = tomail; MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); mail.From = new MailAddress(FromMail); mail.To.Add(emailTo); mail.Subject = subject; mail.Body = body; SmtpServer.Port = 25; SmtpServer.Credentials = new System.Net.NetworkCredential("*****@*****.**", "password-here"); SmtpServer.EnableSsl = true; SmtpServer.SendMailAsync(mail); } catch (System.Exception ex) { //Email does not exists hence it always throws the exception Console.WriteLine(ex.StackTrace); } }
public IEnumerable <RecipeDto> CreateRecipe(RecipeInputDto r) { var toAdd = new Recipe { CreatorId = r.creatorId, Name = r.name, Description = r.description, Rating = (decimal)r.rating, Tag = r.tag, }; _context.Add(toAdd); _context.SaveChanges(); foreach (var ingredient in r.Ingredients) { int?ingredientID = _context.Ingredients.Where(i => i.Name == ingredient.name).Select(x => x.Id).FirstOrDefault(); if (ingredientID != 0) { //link to recipe var RecipeIngredient = new RecipeIngredient(); RecipeIngredient.IngredientId = ingredientID; RecipeIngredient.RecipeId = toAdd.Id; _context.RecipeIngredients.Add(RecipeIngredient); _context.SaveChanges(); } } return(_context.Recipes.Select(x => x.AsDto()).ToList()); }
public IEnumerable <RecipeDto> ServiceCreateRecipe(RecipeInputDto recipe) { var recipes = _recipeRepository.CreateRecipe(recipe); //why would database return null?? // if(recipes == null) // { // return NotFound(); // } //some bussiness logic below foreach (var user in _userRepository.GetUsers()) { sendEmailNotification(recipe, user.email, recipe.name); } return(ServiceGetRecipes()); }
public ActionResult <IEnumerable <RecipeDto> > createRecipe(RecipeInputDto recipe) { //return what is going to be added to database for now //later we will do an insert on the database //send mail notification //var recipeJSON = Newtonsoft.Json.JsonConvert.SerializeObject(recipe); //System.Console.WriteLine(recipeJSON); //foreach(var user in _userRepository.GetUsers()) //{ // sendEmailNotification(recipe,user.email,recipeJSON); //} var recipes = _recipeService.ServiceCreateRecipe(recipe); if (recipes == null) { return(NotFound()); } return(Ok(recipes)); }