Example #1
0
        public async Task <OperationResult> Create(Product_Dto model)
        {
            var item = await GetProductByID(model.Product_Cate_ID, model.Product_ID);

            var data = _mapper.Map <Product>(model);

            try
            {
                if (item == null)
                {
                    _productRepository.Add(data);
                    await _productRepository.Save();

                    operationResult = new OperationResult {
                        Success = true, Message = "Product was successfully added."
                    };
                }
                else
                {
                    operationResult = new OperationResult {
                        Success = false, Message = "Product was exists."
                    };
                }
            }
            catch (Exception ex)
            {
                operationResult = new OperationResult {
                    Success = false, Message = ex.ToString()
                };
            }
            return(operationResult);
        }
Example #2
0
        public async Task <IActionResult> Create([FromForm] Product_Dto model)
        {
            if (model.Images != null)
            {
                foreach (var item in model.Images)
                {
                    var filename   = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
                    var randomGiud = Guid.NewGuid().ToString();

                    var    check         = Path.GetExtension(filename);
                    var    uploadPicture = model.Product_Cate_ID + "_" + model.Product_Name + "_" + randomGiud + check;
                    string folder        = _webHostEnvironment.WebRootPath + "\\uploaded\\images";

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    string filePath = Path.Combine(folder, uploadPicture);

                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        item.CopyTo(fs);
                        fs.Flush();
                    }
                    model.FileImages += uploadPicture + ";";
                }
            }
            if (model.Videos != null)
            {
                foreach (var item in model.Videos)
                {
                    var filename   = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
                    var randomGiud = Guid.NewGuid().ToString();

                    var    check       = Path.GetExtension(filename);
                    var    uploadVideo = model.Product_Cate_ID + "_" + model.Product_Name + "_" + randomGiud + check;
                    string folder      = _webHostEnvironment.WebRootPath + "\\uploaded\\video";

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    string filePath = Path.Combine(folder, uploadVideo);

                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        item.CopyTo(fs);
                        fs.Flush();
                    }
                    model.FileVideos += uploadVideo + ";";
                }
            }
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            var data = await _productService.Create(model);

            return(Ok(data));
        }
Example #3
0
        public async Task <IActionResult> ChangeStatus(Product_Dto model)
        {
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            model.Status      = !model.Status;
            var data = await _productService.Update(model);

            return(Ok(data));
        }
        public async Task <IActionResult> ChangeStatus(Product_Dto model)
        {
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            model.Status      = !model.Status;
            var result = await _productService.Update(model);

            if (result.Success)
            {
                await _hubContext.Clients.All.LoadDataProduct();
            }
            return(Ok(result));
        }
Example #5
0
        public async Task <IActionResult> ChangeIsSale(Product_Dto model)
        {
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            model.IsSale      = !model.IsSale;
            if (model.IsSale == false)
            {
                model.Time_Sale      = 0;
                model.From_Date_Sale = null;
                model.To_Date_Sale   = null;
            }
            var data = await _productService.Update(model);

            return(Ok(data));
        }
        public async Task <IActionResult> Update([FromForm] Product_Dto model)
        {
            model.Content = model.Content == "null" ? null : model.Content;

            // Delete images, videos old
            var images = await _productRepository.FindAll(x => x.Product_Cate_ID == model.Product_Cate_ID &&
                                                          x.Product_ID == model.Product_ID &&
                                                          x.Product_Name == model.Product_Name).Select(x => x.FileImages).Distinct().FirstOrDefaultAsync();

            var videos = await _productRepository.FindAll(x => x.Product_Cate_ID == model.Product_Cate_ID &&
                                                          x.Product_ID == model.Product_ID &&
                                                          x.Product_Name == model.Product_Name).Select(x => x.FileVideos).Distinct().FirstOrDefaultAsync();

            if (!string.IsNullOrEmpty(images))
            {
                _dropzoneService.DeleteFileUpload(images, "\\uploaded\\images\\product");
            }
            if (!string.IsNullOrEmpty(videos))
            {
                _dropzoneService.DeleteFileUpload(videos, "\\uploaded\\video\\product");
            }

            // Add images, videos
            model.FileImages = null;
            model.FileVideos = null;

            if (model.Images != null)
            {
                model.FileImages = await _dropzoneService.UploadFiles(model.Images, model.Product_Cate_ID + "_" + model.Product_ID + "_", "\\uploaded\\images\\product");
            }
            if (model.Videos != null)
            {
                model.FileVideos = await _dropzoneService.UploadFiles(model.Videos, model.Product_Cate_ID + "_" + model.Product_ID + "_", "\\uploaded\\video\\product");
            }

            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            var result = await _productService.Update(model);

            if (result.Success)
            {
                await _hubContext.Clients.All.LoadDataProduct();
            }
            return(Ok(result));
        }
        public async Task <IActionResult> ChangeIsSale(Product_Dto model)
        {
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            model.IsSale      = !model.IsSale;
            if (model.IsSale == false)
            {
                model.Time_Sale      = 0;
                model.From_Date_Sale = null;
                model.To_Date_Sale   = null;
            }
            var result = await _productService.Update(model);

            if (result.Success)
            {
                await _hubContext.Clients.All.LoadDataProduct();
            }
            return(Ok(result));
        }
Example #8
0
        public async Task <OperationResult> Update(Product_Dto model)
        {
            var data = _mapper.Map <Product>(model);

            try
            {
                _productRepository.Update(data);
                await _productRepository.Save();

                operationResult = new OperationResult {
                    Success = true, Message = "Product was successfully updated."
                };
            }
            catch (Exception ex)
            {
                operationResult = new OperationResult {
                    Success = false, Message = ex.ToString()
                };
            }
            return(operationResult);
        }
        public async Task <IActionResult> Create([FromForm] Product_Dto model)
        {
            if (model.Images != null)
            {
                model.FileImages = await _dropzoneService.UploadFiles(model.Images, model.Product_Cate_ID + "_" + model.Product_ID + "_", "\\uploaded\\images\\product");
            }
            if (model.Videos != null)
            {
                model.FileVideos = await _dropzoneService.UploadFiles(model.Videos, model.Product_Cate_ID + "_" + model.Product_ID + "_", "\\uploaded\\video\\product");
            }
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            model.Content     = model.Content == "null" ? null : model.Content;
            var result = await _productService.Create(model);

            if (result.Success)
            {
                await _hubContext.Clients.All.LoadDataProduct();
            }
            return(Ok(result));
        }
Example #10
0
        public async Task <IActionResult> Update([FromForm] Product_Dto model)
        {
            if (model.Content == "null")
            {
                model.Content = null;
            }
            if (model.FileImages == "null")
            {
                model.FileImages = "";
            }
            if (model.FileVideos == "null")
            {
                model.FileVideos = "";
            }

            // Delete images, videos old
            var images = _productRepository.FindAll(x => x.Product_Cate_ID == model.Product_Cate_ID &&
                                                    x.Product_ID == model.Product_ID &&
                                                    x.Product_Name == model.Product_Name).Select(x => x.FileImages).Distinct().FirstOrDefault();

            var videos = _productRepository.FindAll(x => x.Product_Cate_ID == model.Product_Cate_ID &&
                                                    x.Product_ID == model.Product_ID &&
                                                    x.Product_Name == model.Product_Name).Select(x => x.FileVideos).Distinct().FirstOrDefault();

            if (!string.IsNullOrEmpty(images))
            {
                string[] listResult = images.Split(";");
                foreach (var item in listResult)
                {
                    if (item != "")
                    {
                        string folder   = _webHostEnvironment.WebRootPath + "\\uploaded\\images";
                        string filePath = Path.Combine(folder, item);

                        // kiểm tra file cũ có không, nếu có thì xóa đi
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                    }
                }
            }
            if (!string.IsNullOrEmpty(videos))
            {
                string[] listResult = videos.Split(";");
                foreach (var item in listResult)
                {
                    if (item != "")
                    {
                        string folder   = _webHostEnvironment.WebRootPath + "\\uploaded\\video";
                        string filePath = Path.Combine(folder, item);

                        // kiểm tra file cũ có không, nếu có thì xóa đi
                        if (System.IO.File.Exists(filePath))
                        {
                            System.IO.File.Delete(filePath);
                        }
                    }
                }
            }

            // Add images, videos
            if (model.Images != null)
            {
                model.FileImages = "";
                foreach (var item in model.Images)
                {
                    var filename   = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
                    var randomGiud = Guid.NewGuid().ToString();

                    var    check         = Path.GetExtension(filename);
                    var    uploadPicture = model.Product_Cate_ID + "_" + model.Product_Name + "_" + randomGiud + check;
                    string folder        = _webHostEnvironment.WebRootPath + "\\uploaded\\images";

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    string filePath = Path.Combine(folder, uploadPicture);

                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        item.CopyTo(fs);
                        fs.Flush();
                    }
                    model.FileImages += uploadPicture + ";";
                }
            }
            if (model.Videos != null)
            {
                model.FileVideos = "";
                foreach (var item in model.Videos)
                {
                    var filename   = ContentDispositionHeaderValue.Parse(item.ContentDisposition).FileName.Trim('"');
                    var randomGiud = Guid.NewGuid().ToString();

                    var    check       = Path.GetExtension(filename);
                    var    uploadVideo = model.Product_Cate_ID + "_" + model.Product_Name + "_" + randomGiud + check;
                    string folder      = _webHostEnvironment.WebRootPath + "\\uploaded\\video";

                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }
                    string filePath = Path.Combine(folder, uploadVideo);

                    using (FileStream fs = System.IO.File.Create(filePath))
                    {
                        item.CopyTo(fs);
                        fs.Flush();
                    }
                    model.FileVideos += uploadVideo + ";";
                }
            }
            model.Update_By   = User.FindFirst(ClaimTypes.Name).Value;
            model.Update_Time = DateTime.Now;
            var data = await _productService.Update(model);

            return(Ok(data));
        }