Example #1
0
        public async Task <ActionResult <IEnumerable <DishResourceModel> > > GetDishes()
        {
            //Get all dishes from DB
            var dish_list = await _context.Dishes.Include(i => i.Images).Include(i => i.Ratings).OrderByDescending(d => d.DateCreated).ToListAsync();

            List <DishResourceModel> All_Dishes = new List <DishResourceModel>();

            foreach (var dish in dish_list)
            {
                if (!dish.IsDeleted)
                {
                    //Create Image Resource Models from Image Objects
                    List <ImageResourceModel> All_Images = new List <ImageResourceModel>();
                    foreach (var image in dish.Images)
                    {
                        if (!image.IsDeleted)
                        {
                            string imageurl = Path.Combine(URLPath, image.Path);
                            imageurl = imageurl.Replace("\\", "/");

                            ImageResourceModel image_ = new ImageResourceModel
                            {
                                ImageId = image.ImageId,
                                Name    = image.Name,
                                Path    = imageurl
                            };
                            All_Images.Add(image_);
                        }
                    }
                    ;

                    decimal TotalRating = 0;

                    //Create Review Resource Models from review objects
                    List <RatingResourceModel> All_Ratings = new List <RatingResourceModel>();
                    foreach (var rating in dish.Ratings)
                    {
                        if (!rating.IsDeleted)
                        {
                            TotalRating += rating.Rate;

                            RatingResourceModel rating_ = new RatingResourceModel
                            {
                                RatingId  = rating.RatingId,
                                UserName  = rating.UserName,
                                UserEmail = rating.UserEmail,
                                Rate      = rating.Rate,
                                Review    = rating.Review
                            };
                            All_Ratings.Add(rating_);
                        }
                    }
                    ;

                    //Get shorter description with 50 chars
                    String description = dish.DishDescrition;
                    if (description.Length > 50)
                    {
                        description = dish.DishDescrition.Substring(0, 50);
                    }

                    //Calculate Average rating from all ratings
                    decimal DishTotalRating = 0;
                    if (TotalRating != 0)
                    {
                        DishTotalRating = TotalRating / All_Ratings.Count();
                    }

                    //Create Dish Resource Model
                    DishResourceModel dish_ = new DishResourceModel
                    {
                        DishId          = dish.DishId,
                        DishName        = dish.DishName,
                        DishDescription = description,
                        DishPrice       = dish.DishPrice,
                        DishTotalRating = DishTotalRating,
                        DateCreated     = dish.DateCreated,
                        DateModified    = dish.DateModified,

                        Images  = All_Images,
                        Ratings = All_Ratings
                    };
                    //Add to output array
                    All_Dishes.Add(dish_);
                }
            }
            ;


            return(Ok(All_Dishes));
        }
Example #2
0
        public async Task <ActionResult <DishResourceModel> > GetDish(Guid id)
        {
            //Get Dish object for given ID from DB
            var dish = await _context.Dishes.Include(i => i.Images).Include(i => i.Ratings).Where(d => d.DishId == id && !d.IsDeleted).FirstOrDefaultAsync();

            DishResourceModel dish_ = new DishResourceModel();

            if (dish == null)
            {
                return(NotFound());
            }
            else
            {
                //Create Image Resource Models from Image Objects
                List <ImageResourceModel> All_Images = new List <ImageResourceModel>();
                foreach (var image in dish.Images)
                {
                    if (!image.IsDeleted)
                    {
                        string imageurl = Path.Combine(URLPath, image.Path);
                        imageurl = imageurl.Replace("\\", "/");

                        ImageResourceModel image_ = new ImageResourceModel
                        {
                            ImageId = image.ImageId,
                            Name    = image.Name,
                            Path    = imageurl
                        };
                        All_Images.Add(image_);
                    }
                }
                ;

                //Create Rating Resource Models from Rating Objects
                decimal TotalRating = 0;
                List <RatingResourceModel> All_Ratings = new List <RatingResourceModel>();
                foreach (var rating in dish.Ratings)
                {
                    if (!rating.IsDeleted)
                    {
                        TotalRating += rating.Rate;
                        RatingResourceModel rating_ = new RatingResourceModel
                        {
                            RatingId  = rating.RatingId,
                            UserName  = rating.UserName,
                            UserEmail = rating.UserEmail,
                            Rate      = rating.Rate,
                            Review    = rating.Review
                        };
                        All_Ratings.Add(rating_);
                    }
                }
                ;

                //Calculate Average rating from all ratings
                decimal DishTotalRating = 0;
                if (TotalRating != 0)
                {
                    DishTotalRating = TotalRating / All_Ratings.Count();
                }


                //Create Dish Resource Model
                dish_ = new DishResourceModel
                {
                    DishId          = dish.DishId,
                    DishName        = dish.DishName,
                    DishDescription = dish.DishDescrition,
                    DishPrice       = dish.DishPrice,
                    DishTotalRating = DishTotalRating,
                    DateCreated     = dish.DateCreated,
                    DateModified    = dish.DateModified,

                    Images  = All_Images,
                    Ratings = All_Ratings
                };
            }

            return(dish_);
        }