示例#1
0
        public async Task <IActionResult> DeleteMessage(int id, int userId)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var messageFromRepo = await _repo.GetMessage(id);

            if (messageFromRepo.SenderId == userId)
            {
                messageFromRepo.SenderDeleted = true;
            }

            if (messageFromRepo.RecipientId == userId)
            {
                messageFromRepo.RecipientDeleted = true;
            }

            if (messageFromRepo.SenderDeleted && messageFromRepo.RecipientDeleted)
            {
                _repo.Delete(messageFromRepo);
            }

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

            throw new Exception("Error deleting the message");
        }
示例#2
0
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            // Check token ID
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            // Get the user from the Repo
            var userFromRepo = await _repo.GetUser(userId);

            // Need a var for the File
            var file = photoForCreationDto.File;

            // Need to store
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0)
            {
                // Filestream so we must dispose file in memory once we finish calling this method.
                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);
                }
            }

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

            // Mapping from the Dto
            var photo = _mapper.Map <Photo>(photoForCreationDto);

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

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                // Return from HTTP Post
                // Created a get request and modify the IRinkRepository
                var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo);
                return(CreatedAtRoute("GetPhoto", new { userId = userId, id = photo.Id }, photoToReturn));
            }

            return(BadRequest("We could not add the photo."));
            //next create the component for SPA
        }
示例#3
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            // Check token ID
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            // Get the user from the Repo
            var userFromRepo = await _repo.GetUser(id);

            // Update values from DTO and upate them on the repo user
            _mapper.Map(userForUpdateDto, userFromRepo);

            //Save changes

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

            throw new Exception($"Updatating user with {id} failed while saving.");
        }