Пример #1
0
        public async Task <IActionResult> LikeUser(int id, int recipientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _repo.GetLike(id, recipientId);

            if (like != null)
            {
                return(BadRequest("You already like this user"));
            }

            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }

            like = new Like
            {
                LikerId = id,
                LikeeId = recipientId
            };

            _repo.Add <Like>(like);

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

            return(BadRequest("Failed to like user"));
        }
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            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("Could not find 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 on save");
        }
Пример #3
0
        public async Task <IActionResult> CreateMessage(int userId,      // metodo per inviare un messaggio
                                                        MessageForCreationDto messageForCreationDto)
        {
            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("Impossibile trovare utente"));
            }

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

            _repo.Add(message);

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


            throw new Exception("Creazione messaggio fallita in salvataggio");
        }
Пример #4
0
        public async Task <IActionResult> LikeUser(int id, int recipientId) // recipient sono i destinatari
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _repo.GetLike(id, recipientId);

            if (like != null)
            {
                return(BadRequest("Hai già apprezzato questo utente"));
            }


            if (await _repo.GetUser(recipientId) == null)
            {
                return(NotFound());
            }

            like = new Like
            {
                LikerId = id,
                LikeeId = recipientId
            };

            _repo.Add <Like>(like);

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

            return(BadRequest("Impossibile apprezzare"));
        }
Пример #5
0
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            //We are fetching this sender info becasue we want the senderPhotoUrl.
            // It is getting set with the help of AutoMaaper where the values of URL and knownAs is getting set automatically
            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);

            //Added by saurabh --start--
            // We can use this method of just fetching back the photo and assigning it to messageToReturn
            // But we do not do that since AUto Mapper as a special magic which automatically sets matching parmaters
            // from other objects. in Our case recipentPhotoUrl, recipientKnowAs, SenderPhotoUrl, SenderKnownAs.
            //var photo = await _repo.GetPhoto(userId);

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

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

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                //Added by saurabh
                //messageToReturn.SenderPhotoUrl = photo.Url;
                return(CreatedAtRoute("GetMessage",
                                      new { userId = userId, id = message.Id }, messageToReturn));
            }

            throw new System.Exception("Creating the message failed on save");
        }