Esempio n. 1
0
        public async Task <IActionResult> GetAudit(int id)
        {
            var auditFromRepo = await _repo.GetAudit(id);

            var auditToReturn = _mapper.Map <AuditForReturnDto>(auditFromRepo);

            return(Ok(auditToReturn));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddPhotoForAudit(int auditId,
                                                           [FromForm] PhotoAuditForCreationDto photoAuditForCreationDto)
        {
            var auditFromRepo = await _repo.GetAudit(auditId);

            var file = photoAuditForCreationDto.File;

            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoAuditForCreationDto.Url      = uploadResult.Uri.ToString();
            photoAuditForCreationDto.PublicId = uploadResult.PublicId;

            var photo = _mapper.Map <PhotoAudit>(photoAuditForCreationDto);

            if (!auditFromRepo.PhotoAudits.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }

            auditFromRepo.PhotoAudits.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoAuditToReturn = _mapper.Map <PhotoAuditForReturnDto>(photo);
                return(CreatedAtRoute("GetPhotoAudit", new { id = photo.Id }, photoAuditToReturn));
            }

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