public async Task <IActionResult> CreateMessage(int userId, MessageFromCreationDto messageFromCreationDto)
        {
            var sender = _repo.GetUser(userId);

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

            messageFromCreationDto.SenderId = userId;

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

            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }
            var message = _mapper.Map <Message>(messageFromCreationDto);

            _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");
        }
Пример #2
0
        public async Task <IActionResult> CreateMessage(int userId, MessageFromCreationDto messageFromCreationDto)
        {
            var sender = await _datingRepository.GetUser(userId);

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

            messageFromCreationDto.SenderId = userId;
            var recipient = await _datingRepository.GetUser(messageFromCreationDto.RecipientId);

            if (recipient == null)
            {
                return(BadRequest("Không tìm thấy người dùng"));
            }

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

            _datingRepository.Add(message);

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

            throw new Exception("Lỗi khi tạo tin nhắn");
        }
Пример #3
0
        public async Task <IActionResult> GreateMessage(int userId, [FromBody] MessageFromCreationDto messageFromCreationDto)
        {
            var sender = await _repo.GetUser(userId);

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

            messageFromCreationDto.SenderId = userId;
            var recipientId = await _repo.GetUser(messageFromCreationDto.reciptionId);

            if (recipientId == null)
            {
                return(BadRequest("Could Not Found User"));
            }

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

            _repo.Add(message);

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

            throw new Exception("Creation The Message Falied ");
        }