Beispiel #1
0
        public ResultMessage Update(ProductsDto product, int id, IUserDto user)
        {
            var validationResult = _validator.Validate(product);

            if (!validationResult.IsValid)
            {
                return new ResultMessage
                       {
                           Status             = HttpStatusCode.BadRequest,
                           ValidationMessages = validationResult.GetErrorsList()
                       }
            }
            ;

            try
            {
                var oldProduct = _unitOfWork.ProductsRepository.GetById(id);

                if (oldProduct == null)
                {
                    return new ResultMessage
                           {
                               Status    = HttpStatusCode.NotFound,
                               ErrorCode = (int)ProductsErrorsCodeEnum.ProductsNotFoundError
                           }
                }
                ;

                if (!user.IsAdmin && oldProduct.CreatedBy != user.Id)
                {
                    return new ResultMessage
                           {
                               Status    = HttpStatusCode.NotFound,
                               ErrorCode = (int)ProductsErrorsCodeEnum.ProductsNotFoundError
                           }
                }
                ;

                oldProduct.Name = product.Name;
                if (product.ProfilePictureFile != null)
                {
                    oldProduct.ProfilePicture = _attachmentsManager.Save(new SavedFileDto
                    {
                        attachmentType = AttachmentTypesEnum.Products,
                        CanChangeName  = true,
                        File           = product.ProfilePictureFile
                    });
                }

                oldProduct.IsActive = product.IsActive = null;
                if (user.IsAdmin)
                {
                    oldProduct.IsActive = true;
                }

                oldProduct.IsSpecial   = product.IsSpecial;
                oldProduct.Price       = product.Price;
                oldProduct.ExpDate     = product.ExpDate;
                oldProduct.Description = product.Description;
                oldProduct.CategoryId  = product.CategoryId;
                oldProduct.PhoneNumber = product.PhoneNumber;

                oldProduct.UpdatedBy = user.Id;
                oldProduct.UpdatedAt = DateTime.Now;

                var sliderDto = new SliderDto
                {
                    attachmentType = AttachmentTypesEnum.Products,
                    Items          = product.UpdatedImages ?? new List <SliderItemDto>(),
                    SubFolderName  = oldProduct.SubFolderName,
                    ParentId       = id
                };

                //check profile picture
                if (sliderDto.Items.Count > 0)
                {
                    oldProduct.ProfilePicture = _sliderManager.GetProfilePicturePath(sliderDto, oldProduct.ProfilePicture);
                }


                _unitOfWork.ProductsRepository.Update(oldProduct);
                _unitOfWork.Commit();

                // update files
                if (sliderDto.Items.Count > 0)
                {
                    _sliderManager.Update(sliderDto);
                }

                return(new ResultMessage
                {
                    Status = HttpStatusCode.OK,
                    Data = oldProduct.Adapt <ProductsDto>()
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, string.Empty);
                return(new ResultMessage
                {
                    Status = HttpStatusCode.InternalServerError,
                    ErrorCode = (int)ProductsErrorsCodeEnum.ProductsUpdateError
                });
            }
        }
Beispiel #2
0
        public ResultMessage Update(ArticleAddDto article, int articleId, IUserDto user)
        {
            _logger.LogInformation($"START: Article.Update: Name:{article.Name}");

            var validationResult = _addValidator.Validate(article);

            if (!validationResult.IsValid)
            {
                _logger.LogInformation($"END: Article.Update: Id:{articleId} -- VALIDATION");
                return(new ResultMessage
                {
                    Status = HttpStatusCode.BadRequest,
                    ValidationMessages = validationResult.GetErrorsList()
                });
            }
            try
            {
                var articleData = _unitOfWork.ArticlesRepository.GetById(articleId);
                if (articleData == null || articleData.IsDraft)
                {
                    _logger.LogInformation($"END: Article.Update: Id:{articleId} -- NOTFOUND");
                    return(new ResultMessage
                    {
                        Status = HttpStatusCode.NotFound,
                    });
                }


                if (user.IsAdmin || (user.Id == articleData.CreatedBy && (!articleData.IsActive.HasValue || !articleData.IsActive.Value)))
                {
                    article.Adapt(articleData, typeof(ArticleAddDto), typeof(DBModels.Articles));
                    articleData.UpdatedAt = DateTime.Now;
                    articleData.UpdatedBy = user.Id;

                    var sliderDto = new SliderDto
                    {
                        attachmentType = AttachmentTypesEnum.Articles,
                        Items          = article.UpdatedImages ?? new List <SliderItemDto>(),
                        SubFolderName  = articleData.SubFolderName,
                        ParentId       = articleId
                    };

                    //check profile picture
                    if (sliderDto.Items.Count > 0)
                    {
                        articleData.ProfilePicture = _sliderManager.GetProfilePicturePath(sliderDto, article.ProfilePicture);
                    }

                    _unitOfWork.ArticlesRepository.Update(articleData);
                    _unitOfWork.Commit();

                    // update files
                    if (sliderDto.Items.Count > 0)
                    {
                        _sliderManager.Update(sliderDto);
                    }

                    _logger.LogInformation($"END: Article.Update: ID:{articleId} --SUCCESS");

                    return(new ResultMessage
                    {
                        Status = HttpStatusCode.OK
                    });
                }
                else
                {
                    _logger.LogInformation($"END: Article.Update: Id:{articleId} -- UNAuthorized");

                    return(new ResultMessage
                    {
                        Status = HttpStatusCode.Unauthorized
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, string.Empty);
                return(new ResultMessage
                {
                    Status = HttpStatusCode.InternalServerError
                });
            }
        }