Exemplo n.º 1
0
        public async Task <IActionResult> AddToUserArchive(int userId, ArchivedPhotoForCreationDto archivedPhotoForProfileDto)
        {
            //Determine if the user adding the photo is authorized:
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed
            {
                return(Unauthorized());
            }
            //Check if the photo is already archived for this user
            if (await _repo.ArchivedPhotoExists(userId, archivedPhotoForProfileDto.PhotoId))
            {
                return(BadRequest("Photo is already archived!"));
            }

            var userFromRepo = await _repo.GetUser(userId);

            var archivedPhoto = _mapper.Map <ArchivedPhoto>(archivedPhotoForProfileDto);

            userFromRepo.Archived.Add(archivedPhoto);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Could not add photo to archive"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetUser(int id)
        {
            var user = await _repo.GetUser(id);

            var userToReturn = _mapper.Map <UserForProfileDto>(user);

            return(Ok(userToReturn));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            //Determine if the user adding the photo is authorized:
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed
            {
                return(Unauthorized());
            }

            //Get our user
            var userFromRepo = await _repo.GetUser(userId);

            var file = photoForCreationDto.File;

            var uploadResult = new ImageUploadResult(); // to store the result from cloudinary

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream()) // use a 'using' statment to dispose of data read
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation().Width(600).Crop("scale") // scale our images to a decent viewable size
                    };
                    uploadResult = _cloudinary.Upload(uploadParams);                   // upload our file and get the link associated with that file
                };                                                                     // dispose of photo data in memory / close stream when done
            }

            photoForCreationDto.Url            = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId       = uploadResult.PublicId;
            photoForCreationDto.Author         = userFromRepo.Username;
            photoForCreationDto.AuthorPhotoUrl = userFromRepo.PhotoUrl;

            var photo = _mapper.Map <Photo>(photoForCreationDto);

            userFromRepo.Posts.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn));
            }

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