コード例 #1
0
        public async Task <IActionResult> CreateAlbum([FromRoute] Guid artistId, [FromBody] AlbumForCreationDto albumToCreate)
        {
            var albumEntity = Mapper.Map <Album>(albumToCreate);

            albumEntity.Id       = Guid.NewGuid();
            albumEntity.ArtistId = artistId;

            _albumRepository.Create(albumEntity);

            if (!await _albumRepository.SaveChangesAsync())
            {
                throw new Exception($"adding album to artist {artistId} failed");
            }


            return(CreatedAtRoute("GetAlbum", new { artistId, id = albumEntity.Id }, albumEntity));
        }
コード例 #2
0
        public async Task <IActionResult> UploadAlbum(Guid userId, [FromForm] AlbumForCreationDto albumForCreationDto)
        {
            if (!userId.Equals(new Guid(User.FindFirstValue(ClaimTypes.NameIdentifier))))
            {
                return(Unauthorized());
            }

            User userFromRepo = await _userService.GetUser(userId);

            ImageUploadResult uploadResult = new ImageUploadResult();
            Album             album        = this._mapper.Map <Album>(albumForCreationDto);

            if (userFromRepo != null)
            {
                album.PhotographerId = userFromRepo.Id;
            }

            foreach (var image in albumForCreationDto.Files)
            {
                if (image.Length > 0)
                {
                    using (var stream = image.OpenReadStream())
                    {
                        var uploadParams = new ImageUploadParams()
                        {
                            File           = new FileDescription(image.Name, stream),
                            Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face"),
                            Folder         = "photome",
                        };

                        uploadResult = _cloudinary.Upload(uploadParams);
                    }
                }

                PhotoForCreationDto photoForCreationDto = new PhotoForCreationDto();
                photoForCreationDto.Url       = uploadResult.Url.ToString();
                photoForCreationDto.PublicId  = uploadResult.PublicId;
                photoForCreationDto.DateAdded = DateTime.UtcNow;
                var photo = _mapper.Map <Photo>(photoForCreationDto);

                if (!userFromRepo.Photos.Any(u => u.IsMain))
                {
                    photo.IsMain = true;
                }

                if (!album.Photos.Any(p => p.IsMain))
                {
                    album.ThumbnailPublicId = photo.PublicId;
                }

                userFromRepo.Photos.Add(photo);
                album.Photos.Add(photo);
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
            }

            if (await this._albumService.InsertAlbum(album))
            {
                return(CreatedAtRoute("", new { userId = userId }, album));
            }

            return(BadRequest("Could not add new album"));
        }