Exemplo n.º 1
0
        public IActionResult CreateDocument(int domainId,
                                            [FromBody] DocumentForCreationDto document)
        {
            if (document == null || document.Name == "")
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_cvERepository.domainEntityExists(domainId))
            {
                return(NotFound());
            }


            var finalDocument = Mapper.Map <Entities.Document>(document);

            _cvERepository.AddDocumentForDomainEntity(domainId, finalDocument);

            if (!_cvERepository.Save())
            {
                return(StatusCode(500, "A problem occured while handling your request"));
            }

            var CreatedDocumentToReturn = Mapper.Map <Models.DocumentDto>(finalDocument);

            return(CreatedAtRoute("GetDocument", new
            {
                domainId = domainId,
                id = CreatedDocumentToReturn.Id
            }, CreatedDocumentToReturn));
        }
        public async Task <IActionResult> AddDocumentForUser(int userId,
                                                             [FromForm] DocumentForCreationDto docForCreationDto)
        {
            // [FromForm]PhotoForCreationDto = PhotoForCreationDto)

            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                int DocumentUploadeeId = userId;
                int DocumentUploaderId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);
                var checkIfUploaderSelectedUploadee = await _repo.GetSelect(DocumentUploaderId, DocumentUploadeeId);

                var checkIfUploadeeSelectedUploader = await _repo.GetSelect(DocumentUploadeeId, DocumentUploaderId);

                if (checkIfUploadeeSelectedUploader != null && checkIfUploaderSelectedUploadee == null)
                {
                    return(BadRequest("You cannot upload photos until you accept user's request"));
                }
                else if (checkIfUploadeeSelectedUploader == null && checkIfUploaderSelectedUploadee == null)
                {
                    return(Unauthorized());
                }
            }

            var userFromRepo = await _repo.getUser(userId);

            var file = docForCreationDto.File;

            var imageUploadResult = new ImageUploadResult();
            var videoUploadResult = new VideoUploadResult();

            // var uploadResult = new RawUploadResult();


            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    if (file.ContentType.Contains("image"))
                    {
                        var uploadParams = new ImageUploadParams()
                        {
                            File           = new FileDescription(file.Name, stream),
                            Transformation = new Transformation()
                                             .Width(500).Height(500).Crop("fill").Gravity("face")
                        };
                        imageUploadResult          = _cloudinary.Upload(uploadParams);
                        docForCreationDto.Url      = imageUploadResult.Uri.ToString();
                        docForCreationDto.PublicId = imageUploadResult.PublicId;
                    }
                    if (file.ContentType.Contains("video"))
                    {
                        var uploadParams = new VideoUploadParams()
                        {
                            File            = new FileDescription(file.Name, stream),
                            EagerTransforms = new List <Transformation>()
                            {
                                new Transformation().Width(300).Height(300)
                                .Crop("pad").AudioCodec("none"),
                                new Transformation().Width(160).Height(100)
                                .Crop("crop").Gravity("south").AudioCodec("none")
                            }
                        };
                        videoUploadResult          = _cloudinary.Upload(uploadParams);
                        docForCreationDto.Url      = videoUploadResult.Uri.ToString();
                        docForCreationDto.PublicId = videoUploadResult.PublicId;
                    }

                    // var uploadParams = new RawUploadParams() {
                    //     File = new FileDescription(file.Name, stream),
                    //     // Transformation = new Transformation()
                    //     //   .Width(500).Height(500).Crop("fill").Gravity("face")
                    // };

                    // uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            // docForCreationDto.Url = uploadResult.Uri.ToString();
            // docForCreationDto.PublicId = uploadResult.PublicId;
            docForCreationDto.Type = file.ContentType;

            var document = _mapper.Map <Document>(docForCreationDto);

            // if (!userFromRepo.Documents.Any(u => u.IsMain))
            //     photo.IsMain = true;

            userFromRepo.Documents.Add(document);

            //var photoToReturn = _mapper.Map<PhotosForReturnDto>(photo);

            if (await _repo.SaveAll())
            {
                var docToReturn = _mapper.Map <DocumentForReturnDto>(document);
                return(CreatedAtRoute("GetDocument", new { id = document.id }, docToReturn));
            }

            return(BadRequest("Could not add the photo"));
        }