Ejemplo n.º 1
0
        public async Task <ActionResult <ImageResourceModel> > GetImage(Guid id)
        {
            //Get Image from DB
            var image = await _context.Images.Where(i => i.ImageId == id && !i.IsDeleted).FirstOrDefaultAsync();

            if (image == null)
            {
                return(NotFound());
            }

            //Build image file url
            string imageurl = Path.Combine(URLPath, image.Path);

            imageurl = imageurl.Replace("\\", "/");

            //Create Image Resource Model
            ImageResourceModel image_ = new ImageResourceModel
            {
                ImageId = image.ImageId,
                Name    = image.Name,
                Path    = imageurl
            };

            return(image_);
        }
        public async Task <IActionResult> AddResource([FromBody] ImageResourceModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ErrorResponse.Create(ModelState.GetErrorMessage())));
            }

            if (model.Data.Length == 0)
            {
                return(BadRequest(ErrorResponse.Create($"Invalid {nameof(model.Data)} value")));
            }

            if (model.Data.Length > _settings.MaxFileSizeInMb * 1024 * 1024)
            {
                return(BadRequest(ErrorResponse.Create($"File size must be below {_settings.MaxFileSizeInMb} Mb")));
            }

            if (await _imageResourcesService.IsFileExistsAsync(model.Name))
            {
                return(BadRequest(ErrorResponse.Create($"File with name {model.Name} already exists")));
            }

            await _imageResourcesService.AddAsync(model.Name, model.Data);

            return(Ok());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <ImageResourceModel> > PostImage([FromForm] ImageBindingModel imageModel)
        {
            //Authorization
            string usertype = User.Claims.First(c => c.Type == "Role").Value;

            if (usertype.Equals(EntityConstants.Role_SuperAdmin) || usertype.Equals(EntityConstants.Role_Admin))
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }

                string uniqueName = null;
                if (imageModel.Image != null)
                {
                    //Check the availability of file sstoring folder
                    bool exists = System.IO.Directory.Exists(UploadPath);

                    if (!exists)
                    {
                        System.IO.Directory.CreateDirectory(UploadPath);
                    }

                    //Create unique file name
                    uniqueName = Guid.NewGuid().ToString() + "_" + imageModel.Image.FileName;
                    string filepath = Path.Combine(UploadPath, uniqueName);

                    //Upload
                    imageModel.Image.CopyTo(new FileStream(filepath, FileMode.Create));

                    //Create new Image Object
                    Image new_image = new Image()
                    {
                        Name = imageModel.Name,
                        Path = uniqueName
                    };
                    //Add to DB
                    _context.Images.Add(new_image);
                    await _context.SaveChangesAsync();

                    string imageurl = Path.Combine(URLPath, new_image.Path);
                    imageurl = imageurl.Replace("\\", "/");

                    ImageResourceModel image_ = new ImageResourceModel
                    {
                        ImageId = new_image.ImageId,
                        Name    = new_image.Name,
                        Path    = imageurl
                    };
                    return(Ok(image_));
                }


                return(BadRequest());
            }
            return(Unauthorized());
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Adds image resource
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='model'>
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <ErrorResponse> AddImageResourceAsync(this IResourcesAPI operations, ImageResourceModel model = default(ImageResourceModel), CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.AddImageResourceWithHttpMessagesAsync(model, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Adds image resource
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='model'>
 /// </param>
 public static ErrorResponse AddImageResource(this IResourcesAPI operations, ImageResourceModel model = default(ImageResourceModel))
 {
     return(operations.AddImageResourceAsync(model).GetAwaiter().GetResult());
 }
Ejemplo n.º 6
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));
        }
Ejemplo n.º 7
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_);
        }