示例#1
0
        public async Task <ActionResult> EditPhoto(int?id)
        {
            if (id == null || id < 1)
            {
                return(RedirectToAction("Index"));
            }

            try
            {
                Product_PhotosViewModel vm = new Product_PhotosViewModel();

                int productId = (id ?? 1);
                Dictionary <string, object> result = new Dictionary <string, object>();
                result = await GetProductFromDB(productId, "EditPhoto");

                if (result["ViewModel"] == null)
                {
                    return(View("Error", result["ErrObj"]));
                }
                else
                {
                    vm = result["ViewModel"] as Product_PhotosViewModel;
                }

                return(View(vm));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in editing Product photo!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }
示例#2
0
        public async Task <ActionResult> DeletePhoto(int selectedProductId, int selectedPhotoId)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    HttpResponseMessage response = await _photoService.DeletePhoto(selectedProductId, selectedPhotoId);

                    var responseData = response.Content.ReadAsStringAsync().Result;
                    if (!response.IsSuccessStatusCode)
                    {
                        ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                        object cauThongBao = "Error in Delete Photo!<br /> Reason: " + resObj.Message;
                        return(View("Error", cauThongBao));
                    }

                    // Delete Photo successfully
                }

                // Get Product from Database
                Product_PhotosViewModel vm = new Product_PhotosViewModel();

                Dictionary <string, object> result = new Dictionary <string, object>();
                result = await GetProductFromDB(selectedProductId, "DeletePhoto");

                if (result["ViewModel"] == null)
                {
                    return(View("Error", result["ErrObj"]));
                }
                else
                {
                    vm = result["ViewModel"] as Product_PhotosViewModel;
                }

                return(View("EditPhoto", vm));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in Delete Photo!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }
示例#3
0
        public async Task <Dictionary <string, object> > GetProductFromDB(int productId, string action)
        {
            Dictionary <string, object> result = new Dictionary <string, object>();
            object cauThongBao = null;

            HttpResponseMessage response = await _productService.GetProduct(productId);

            var responseData = response.Content.ReadAsStringAsync().Result;

            if (!response.IsSuccessStatusCode)
            {
                ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                if (action == "EditPhoto")
                {
                    cauThongBao = "Error in finding Product to edit photo!<br /> Reason: " + resObj.Message;
                }
                else if (action == "Delete")
                {
                    cauThongBao = "Error in finding Product to delete!<br /> Reason: " + resObj.Message;
                }
                else if (action == "EditProduct")
                {
                    cauThongBao = "Error in finding Product to edit!<br /> Reason: " + resObj.Message;
                }
                else if (action == "SetMainPhoto")
                {
                    cauThongBao = "Error in finding Product to Set main Photo!<br /> Reason: " + resObj.Message;
                }
                else if (action == "DeletePhoto")
                {
                    cauThongBao = "Error in finding Product to Delete Photo!<br /> Reason: " + resObj.Message;
                }

                result.Add("ErrObj", cauThongBao);
                result.Add("ViewModel", null);
            }
            else
            {
                Product productFromDb = new Product();
                productFromDb = JsonConvert.DeserializeObject <Product>(responseData);
                if (productFromDb == null)
                {
                    cauThongBao = $"Cannot find Product with Id (" + productId.ToString() + ").";

                    result.Add("ErrObj", cauThongBao);
                    result.Add("ViewModel", null);
                }
                else
                {
                    Product_PhotosViewModel vm = new Product_PhotosViewModel();

                    List <Photo> photosList = new List <Photo>();
                    photosList.AddRange(productFromDb.Photos);

                    // View Model
                    vm.Product = productFromDb;
                    vm.Photos  = photosList;

                    result.Add("ErrObj", null);
                    result.Add("ViewModel", vm);
                }
            }

            return(result);
        }
示例#4
0
        public async Task <ActionResult> EditPhoto(HttpPostedFileBase newPhotoFile, int id)
        {
            try
            {
                Product_PhotosViewModel vm = new Product_PhotosViewModel();
                ViewBag.ErrMsg = "";

                if (newPhotoFile != null && newPhotoFile.ContentLength > 0)
                {
                    // Validate File extension
                    string        extensionsList    = "png,jpg,jpeg,gif";
                    List <string> allowedExtensions = extensionsList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
                    bool          isValid           = allowedExtensions.Any(y => newPhotoFile.FileName.EndsWith(y));

                    ViewBag.ErrMsg = isValid ? "" : "'" + newPhotoFile.FileName + "' file is not supported. Please select only Supported Files .png | .jpg";

                    // Validate file size
                    int maxContentLength = 1024 * 1024 * 2; //2 MB
                    if (newPhotoFile.ContentLength > maxContentLength)
                    {
                        ViewBag.ErrMsg = "Your file is too large, maximum allowed size is: " + maxContentLength + " MB";
                    }

                    // Upload Photo
                    if (ViewBag.ErrMsg == "")
                    {
                        // Upload Photo via API
                        HttpResponseMessage response = await _photoService.UploadNewPhoto(id, newPhotoFile);

                        var responseData = response.Content.ReadAsStringAsync().Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            ResponseObj resObj = JsonConvert.DeserializeObject <ResponseObj>(responseData);

                            object cauThongBao = "Error in uploading Photo!<br /> Reason: " + resObj.Message;
                            return(View("Error", cauThongBao));
                        }

                        // Upload Photo successfully
                        ViewBag.SuccessMsg = newPhotoFile.FileName + " has been uploaded successfully!";
                    }
                }
                else
                {
                    ViewBag.ErrMsg = "Please select file or your selected file with empty content.";
                }

                // Get Product from Database
                Dictionary <string, object> result = new Dictionary <string, object>();
                result = await GetProductFromDB(id, "EditPhoto");

                if (result["ViewModel"] == null)
                {
                    return(View("Error", result["ErrObj"]));
                }
                else
                {
                    vm = result["ViewModel"] as Product_PhotosViewModel;
                }

                return(View("EditPhoto", vm));
            }
            catch (Exception ex)
            {
                object cauThongBao = $"Error in uploading new Photo!<br /> Reason: {ex.Message}";
                return(View("Error", cauThongBao));
            }
        }