Exemple #1
0
        public async Task <IActionResult> UpdatePartGuide([Required] int id, [FromForm] UpdatePartGuideDto updatedGuide)
        {
            var serviceResponse = await _guidesService.UpdatePartGuide(id, updatedGuide);

            return(StatusCode(serviceResponse.StatusCode, serviceResponse.ToControllerResponse()));
        }
        /// <inheritdoc />
        public async Task <ServiceResponse <int> > UpdatePartGuide(int partGuideId, UpdatePartGuideDto updatedGuide)
        {
            var(isEditable, accessResponse, partGuide) = await PartGuideIsEditable <int>(partGuideId);

            if (!isEditable)
            {
                return(accessResponse);
            }

            var serviceResponse = new ServiceResponse <int>();

            var name    = updatedGuide.Name;
            var content = updatedGuide.Content;
            var file    = updatedGuide.File;

            if (file != null && content != null)
            {
                serviceResponse.StatusCode = 400;
                serviceResponse.Message    = "You cannot provide file and content at the same time.";
                serviceResponse.MessageRu  = "Вы не можете заполнить поля file и content одновременно.";
                return(serviceResponse);
            }

            if (content != null)
            {
                if (!Uri.IsWellFormedUriString(content, UriKind.Absolute))
                {
                    serviceResponse.StatusCode = 400;
                    serviceResponse.Message    = "You should provide valid URL in content field.";
                    serviceResponse.MessageRu  = "Вы должны указать валидный URL в поле content.";
                    return(serviceResponse);
                }

                if (_fileManager.FileExists(partGuide.GuideId, partGuide.Content))
                {
                    _fileManager.DeleteFile(partGuide.GuideId, partGuide.Content);
                }

                partGuide.Content = content;
            }

            if (file != null)
            {
                var isPdf = file.ContentType == "application/pdf";
                var isZip = file.ContentType == "application/zip" || file.ContentType == "application/x-zip-compressed";
                if (!isPdf && !isZip)
                {
                    serviceResponse.StatusCode = 400;
                    serviceResponse.Message    = "You should provide PDF or ZIP file or no files at all.";
                    serviceResponse.MessageRu  = "Вы должны загрузить PDF или ZIP файл или не загружать файл вовсе.";
                    return(serviceResponse);
                }

                if (_fileManager.FileExists(partGuide.GuideId, file.FileName))
                {
                    if (file.FileName != partGuide.Content)
                    {
                        serviceResponse.StatusCode = 400;
                        serviceResponse.Message    = "File with this name already exists in this guide.";
                        serviceResponse.MessageRu  = "Файл с таким именем уже существует в данном гайде.";
                        return(serviceResponse);
                    }

                    await _fileManager.SaveFile(partGuide.GuideId, file.FileName, file);
                }

                if (_fileManager.FileExists(partGuide.GuideId, partGuide.Content))
                {
                    _fileManager.DeleteFile(partGuide.GuideId, partGuide.Content);
                }

                partGuide.Content = file.FileName;
                await _fileManager.SaveFile(partGuide.GuideId, file.FileName, file);
            }

            if (name != null)
            {
                partGuide.Name = name;
            }

            await _guidesRepository.UpdatePartGuide(partGuide);

            serviceResponse.Data      = partGuide.Id;
            serviceResponse.Message   = "Part guide is successfully updated.";
            serviceResponse.MessageRu = "Гайд детали успешно обновлен.";
            return(serviceResponse);
        }