//public IActionResult Index() //{ // return View(); //} public async Task <IActionResult> Detail(int id) { PastryDetailViewModel viewModel = await pastryService.GetPastryAsync(id); ViewData["Title"] = viewModel.Name; return(View(viewModel)); }
public async Task <PastryDetailViewModel> GetPastryAsync(int id) { logger.LogInformation("Pastry {id} requested", id); FormattableString query = $@"SELECT Id, Name, Description, Price, Currency, ImagePath, date(InsertDateTime) as InsertDateTime FROM Pastries WHERE Id={id}; SELECT i.Id, i.Name, c.Quantity, c.UoM FROM Ingredients i INNER JOIN Compositions c ON i.Id = c.IdI WHERE c.IdP = {id}"; DataSet dataSet = await db.QueryAsync(query); var pastryTable = dataSet.Tables[0]; if (pastryTable.Rows.Count != 1) { logger.LogWarning("Pastry {id} not found", id); throw new PastryNotFoundException(id); } var pastryRow = pastryTable.Rows[0]; var pastryDetailViewModel = PastryDetailViewModel.FromDataRow(pastryRow); var ingredientsDataTable = dataSet.Tables[1]; foreach (DataRow ingredientRow in ingredientsDataTable.Rows) { IngredientViewModel ingredientViewModel = IngredientViewModel.FromDataRow(ingredientRow); pastryDetailViewModel.Ingredients.Add(ingredientViewModel); } return(pastryDetailViewModel); }
//View that we can acces when we click on a pastry public IActionResult Details(int id) { //ViewModel that we use in order to show Pastry with Comments PastryDetailViewModel viewmodelfordetails = new PastryDetailViewModel(); //In the PastryDetailViewModel we have All the Pasteries with properties and All the Comments in a List viewmodelfordetails.Comments = new List <Comment>(); //In order to show the comments we need to know which Product where are showing comments for and setting the ViewModel for that Pastry Pastry pastry = repositoryofpastries.GetPastryById(id); viewmodelfordetails.PastryDetails = pastry; //Now we can access the Comments for that specific Pastry so we create a list that we are going to pass on to the ViewModel IEnumerable <Comment> ListOfComments; //Gets the Comments by the Pastry ID ListOfComments = repositoryofcomments.GetCommentByPastryId(id); //And passes on to the ViewModel List viewmodelfordetails.Comments = ListOfComments.ToList(); //Returns a View Model for View Detail return(View(viewmodelfordetails)); }