public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.GetUser(userId, false);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDto.RecipientId, true);

            if (recipient == null)
            {
                return(BadRequest("Could not found user..."));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);

                // TODO: CreatedAtRoute()
                return(CreatedAtRoute("GetMessage",
                                      new { controller = "Messages", userId, id = message.Id },
                                      messageToReturn));
            }


            throw new Exception("Creating the message failed on saved");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO)
        {
            //this line of code is added for the message photo display
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDTO.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDTO.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Colud not Found the user."));
            }


            var message = _mapper.Map <Message>(messageForCreationDTO);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDTO>(message);
                return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn));
            }
            throw new Exception("Creating the message failed. ");
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateUser(int id, UserUpdateDto userModel)
        {
            try
            {
                if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    return(Unauthorized());
                }

                var dbUser = await _datingrepo.GetUser(id);

                _mapper.Map(userModel, dbUser);

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

                throw new Exception($"Error updating user {userModel.UserID} !");
            }
            catch (Exception ex)
            {
                throw new Exception($"Error updating user {userModel.UserID} !");
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _datingRepo.GetUser(userId); // some random shit ? video 172 from course for blocking some automapper shit

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _datingRepo.GetUser(messageForCreationDto.RecipientId);

            if (recipient == null)
            {
                BadRequest("Could not find user");
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _datingRepo.Add(message);

            if (await _datingRepo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn));
            }

            return(BadRequest("Couldnt create message"));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> CreateMessage(int userId, MessageCreateDto messageCreateDto)
        {
            var sender = await _repo.GetUser(userId);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageCreateDto.SenderId = userId;
            var receipient = await _repo.GetUser(messageCreateDto.RecipientId);

            if (receipient == null)
            {
                return(BadRequest("receipient not found"));
            }
            var message = _mapper.Map <Message>(messageCreateDto);

            _repo.Add(message);
            var messageToReturn = _mapper.Map <MessageReturnDto>(message);


            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }
            throw new Exception("fail to creat message");
        }
        public async Task <IActionResult> AddPhotoForUser(int userId, [FromForm] PhotoForCreationDto photoForCreation)
        {
            // Checking the user id with params id
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId, true);

            var file = photoForCreation.File;

            // The result get from API
            ImageUploadResult uploadResult = new ImageUploadResult();

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

                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            // Saving info getting from cloud
            photoForCreation.Url      = uploadResult.Uri.ToString();
            photoForCreation.PublicId = uploadResult.PublicId;

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

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

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll())
            {
                var photoFromRepo = await _repo.GetPhoto(photo.Id);

                var photos = _mapper.Map <PhotoForReturnDto>(photoFromRepo);

                // TODO: CreatedAtRoute();
                return(Ok(photos));
            }

            return(BadRequest("Could not add the photo"));
        }
Ejemplo n.º 7
0
        public async Task<IActionResult> AddPhotoForUser(int userId, [FromForm]PhotoForCreationDto photoForCreationDto) {

            // authorization
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)){
                return Unauthorized();
            }

            // getting the user
            var userFromRepo = await _repo.GetUser(userId);

            // whatever the file we are trying to upload
            var file = photoForCreationDto.File;

            // clouindary methods
            var uploadResult = new ImageUploadResult();

            if (file.Length > 0) { // file is not empty
                using (var stream = file.OpenReadStream()) { // using since we are using memory and can be disposed that is the file that is in memry
                    // we are also transforming the photo for example if we send big photo
                    // we are transforming the photo to crop to small one and then cropping around the face
                    var uploadParams = new ImageUploadParams() {
                        File = new FileDescription(file.Name , stream), // combing the filename and file stream for upload params 
                        Transformation = new Transformation().Width(500).Height(500).Crop("fill").Gravity("face")
                    };

                    // we are uploading here and saving the result
                    uploadResult = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreationDto.Url = uploadResult.Uri.ToString();
            photoForCreationDto.PublicId = uploadResult.PublicId;
            // IMapper PhotosController.PublicId;

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

            // if user doesnt have photos we can set it as main photo
            if (!userFromRepo.Photos.Any(userFromRepo => userFromRepo.IsMain)) {
                photo.IsMain = true;
            }

            userFromRepo.Photos.Add(photo);

            if (await _repo.SaveAll()) {

                // we need to return that particular photo id so that only happens after save
                var photoToReturn = _mapper.Map<PhotoForReturnDto>(photo);
            
                // we are wrapping request qwith 201 and some other stuff as this reponse , GetPhoto in this case
                // the parameters for caretaedatroute are name of route , parameters dont know why wee are adding userid and object
                return CreatedAtRoute(nameof(GetPhoto), new { userId = userId, id = photo.Id }, photoToReturn);
            }
            return BadRequest("couldnt add the photo");
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> AddPhotoForUser(int userId,
                                                          [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            var file         = photoForCreationDto.File;
            var userFormrepo = await _repo.GetUser(userId);

            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);
                }
            }
            photoForCreationDto.PublicId = uploadResult.PublicId;



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

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

            if (!userFormrepo.Photos.Any(u => u.IsMain))
            {
                photo.IsMain = true;
            }
            userFormrepo.Photos.Add(photo);
            if (await _repo.SaveAll())
            {
                //need to return the header.
                //return Ok();


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

            return(BadRequest("Could not add the photo"));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> AddPhoto(int userid, [FromForm] PhotoDto photoReceived)
        {
            if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var dbUser = await _datingrepo.GetUser(userid);

            var file = photoReceived.file;

            var objUpload = new ImageUploadResult();

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

                    objUpload = _cloudinary.Upload(uploadParams);
                }
            }

            photoReceived.CreatedDate = DateTime.Now;
            photoReceived.Url         = objUpload.Uri.ToString();
            photoReceived.PublicID    = objUpload.PublicId;

            var photo = _mapper.Map <Photos>(photoReceived);

            if (dbUser.Photos.Count <= 0)
            {
                photo.IsMain = true;
            }

            dbUser.Photos.Add(photo);

            if (await _datingrepo.SaveAll())
            {
                var result = _mapper.Map <PhotoDto>(photo);
                return(CreatedAtRoute("getphoto", new { photoid = photo.Id }, result));
            }

            return(BadRequest("Error uploading photo"));
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDTO userForUpdateDTO)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id);

            _mapper.Map(userForUpdateDTO, userFromRepo);
            if (await _repo.SaveAll())
            {
                return(NoContent());
            }
            throw new Exception($"Updating user {id} failed on save");
        }
        public async Task <IActionResult> UpdateUser(int id, [FromBody] UserForUpdateDto updateDto)
        {
            // Checking if user is logged in who try to update profile
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(id, true);

            _mapper.Map(updateDto, userFromRepo);

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

            throw new Exception($"Updating user {id} failed on save");
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            // this is how we are checking if the user is the current user that has the token to the service
            // that is attampting to access this controller route
            // if token doesn match the user is not authorized.
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }
            // getting the user that we want to update
            var userFromRepo = await _datingRepo.GetUser(id);

            // mapping userforupdate to userfrom repo
            _mapper.Map(userForUpdateDto, userFromRepo); // this will check the profile and map userdto that we are getting and user from repo

            if (await _datingRepo.SaveAll())
            {
                return(NoContent()); // save successful so return nothing thats the appropriate return
            }
            throw new Exception($"Updating user {id} failed on save");
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> CreateMessage(int userid, MessageCreateDto messageParams)
        {
            try
            {
                var sender = await _datingrepo.GetUser(userid);

                if (sender.UserID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
                {
                    return(Unauthorized());
                }

                messageParams.SenderID = userid;

                var receiver = await _datingrepo.GetUser(messageParams.ReceiverID);

                if (receiver == null)
                {
                    return(BadRequest("Receiving user does not exists"));
                }

                var message = _mapper.Map <Messages>(messageParams);

                _datingrepo.Add(message);

                if (await _datingrepo.SaveAll())
                {
                    var returnMessage = _mapper.Map <MessageReturnDto>(message);
                    return(CreatedAtRoute("GetMessage", new { userid = userid, messageid = message.MessageID }, returnMessage));
                }

                throw new Exception("Error while creating message");
            }
            catch (Exception ex)
            {
                throw new Exception("Error while creating message");
            }
        }