Ejemplo n.º 1
0
        public IActionResult UpdatePackingType()
        {
            try
            {
                var packingType = JsonSerializer.Deserialize <PackingType>(HttpContext.Request.Form["PackingType"]);
                var validator   = new ParamValidator();
                validator.ValidateNull(packingType.Name,
                                       General.Messages_.NullInputMessages_.GeneralNullMessage("نام"))
                .ValidateNull(packingType.Price,
                              General.Messages_.NullInputMessages_.GeneralNullMessage("قیمت"))
                .ValidateNull(packingType.Id,
                              General.Messages_.NullInputMessages_.GeneralNullMessage("آیدی"));

                var photo     = HttpContext.Request.Form.Files.GetFile("Photo");
                var video     = HttpContext.Request.Form.Files.GetFile("Video");
                var photopath = "";
                var videopath = "";
                if (photo != null)
                {
                    var uploadFileStatus = FileManeger.FileUploader(photo, 1, "PackingTypeImages");
                    if (uploadFileStatus.Status == 200)
                    {
                        photopath = uploadFileStatus.Path;
                    }
                    else
                    {
                        throw new BusinessException(uploadFileStatus.Path, 100);
                    }
                }

                if (video != null)
                {
                    var uploadFileStatus1 = FileManeger.FileUploader(video, 2, "PackingTypeVideo");
                    if (uploadFileStatus1.Status == 200)
                    {
                        videopath = uploadFileStatus1.Path;
                    }
                    else
                    {
                        throw new BusinessException(uploadFileStatus1.Path, 100);
                    }
                }


                var packing = _repository.PackingType.FindByCondition(c => c.Id == packingType.Id)
                              .Include(c => c.PackingTypeImage).FirstOrDefault();

                if (packing == null)
                {
                    throw new BusinessException(XError.GetDataErrors.NotFound());
                }

                packing.MuserId = DateTime.Now.Ticks;
                packing.Mdate   = ClaimPrincipalFactory.GetUserId(User);

                if (photopath != "")
                {
                    var toBeDeletedImage = packing.PackingTypeImage.FirstOrDefault(c => c.FileType == 2);
                    if (toBeDeletedImage != null)
                    {
                        _repository.PackingTypeImage.Delete(toBeDeletedImage);
                    }

                    var packingtypeImge = new PackingTypeImage
                    {
                        ImageFileUrl = photopath,
                        Cdate        = DateTime.Now.Ticks,
                        CuserId      = ClaimPrincipalFactory.GetUserId(User),
                        FileType     = 1
                    };
                    packing.PackingTypeImage.Add(packingtypeImge);
                }

                if (photopath != "")
                {
                    var toBeDeletedVideo = packing.PackingTypeImage.FirstOrDefault(c => c.FileType == 1);
                    if (toBeDeletedVideo != null)
                    {
                        _repository.PackingTypeImage.Delete(toBeDeletedVideo);
                    }
                    var packingtypeVideo = new PackingTypeImage
                    {
                        ImageFileUrl = videopath,
                        Cdate        = DateTime.Now.Ticks,
                        CuserId      = ClaimPrincipalFactory.GetUserId(User),
                        FileType     = 2
                    };
                    packing.PackingTypeImage.Add(packingtypeVideo);
                }

                _repository.PackingType.Update(packing);
                _repository.Save();
                _logger.LogData(MethodBase.GetCurrentMethod(), packing.Id, null);
                return(Ok(packing.Id));
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod());
                return(BadRequest(e.Message));
            }
        }
Ejemplo n.º 2
0
        public IActionResult InsertPackingType()
        {
            try
            {
                var packingType = JsonSerializer.Deserialize <PackingType>(HttpContext.Request.Form["PackingType"]);
                var validator   = new ParamValidator();
                validator.ValidateNull(packingType.Name,
                                       General.Messages_.NullInputMessages_.GeneralNullMessage("نام"))
                .ValidateNull(packingType.Price,
                              General.Messages_.NullInputMessages_.GeneralNullMessage("قیمت"));

                var photo     = HttpContext.Request.Form.Files.GetFile("Photo");
                var video     = HttpContext.Request.Form.Files.GetFile("Video");
                var photopath = "";
                var videopath = "";
                if (photo != null)
                {
                    var uploadFileStatus = FileManeger.FileUploader(photo, 1, "PackingTypeImages");
                    if (uploadFileStatus.Status == 200)
                    {
                        photopath = uploadFileStatus.Path;
                    }
                    else
                    {
                        throw new BusinessException(uploadFileStatus.Path, 100);
                    }
                }

                if (video != null)
                {
                    var uploadFileStatus1 = FileManeger.FileUploader(video, 2, "PackingTypeVideo");
                    if (uploadFileStatus1.Status == 200)
                    {
                        videopath = uploadFileStatus1.Path;
                    }
                    else
                    {
                        throw new BusinessException(uploadFileStatus1.Path, 100);
                    }
                }


                packingType.Cdate   = DateTime.Now.Ticks;
                packingType.CuserId = ClaimPrincipalFactory.GetUserId(User);

                if (photopath != "")
                {
                    var packingtypeImge = new PackingTypeImage
                    {
                        ImageFileUrl = photopath,
                        Cdate        = DateTime.Now.Ticks,
                        CuserId      = ClaimPrincipalFactory.GetUserId(User),
                        FileType     = 2
                    };
                    packingType.PackingTypeImage.Add(packingtypeImge);
                }

                if (photopath != "")
                {
                    var packingtypeVideo = new PackingTypeImage
                    {
                        ImageFileUrl = videopath,
                        Cdate        = DateTime.Now.Ticks,
                        CuserId      = ClaimPrincipalFactory.GetUserId(User),
                        FileType     = 1
                    };
                    packingType.PackingTypeImage.Add(packingtypeVideo);
                }

                _repository.PackingType.Create(packingType);
                _repository.Save();
                _logger.LogData(MethodBase.GetCurrentMethod(), packingType.Id, null);
                return(Ok(packingType.Id));
            }
            catch (Exception e)
            {
                _logger.LogError(e, MethodBase.GetCurrentMethod());
                return(BadRequest(e.Message));
            }
        }