public async Task <IActionResult> CreateMessage(int userId, MessageForCreatingDto messageForCreatingDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreatingDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreatingDto.RecipientId); if (recipient == null) { return(BadRequest("Could not find the user")); } var message = _mapper.Map <Message>(messageForCreatingDto); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturnDto = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturnDto)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreatingDto messageForCreatingDto) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreatingDto.SenderId = userId; var recipient = await _repo.GetUser(messageForCreatingDto.RecipientId); await _repo.GetUser(messageForCreatingDto.SenderId); //just loading User object with sender data in to the memory if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreatingDto); _repo.Add(message); var messageToReturn = _mapper.Map <MessageToReturnDto>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); //after message in saved, go to GetMessage Action and get saved message } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreatingDto messageForCreatingDto) { var sender = await _repo.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreatingDto.SenderId = sender.Id; var recipent = await _repo.GetUser(messageForCreatingDto.RecipientId); if (recipent == null) { return(BadRequest("Could Not find User")); } var massage = _mapper.Map <Message>(messageForCreatingDto); _repo.Add <Message>(massage); // massage will return each and every object in the return so we convert it again into DTO to filter the response // MessageForCreatingDto if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(massage); return(CreatedAtRoute("GetMessage", new { id = massage.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreatingDto messageCreatingDto) { var sender = await _DatingRepository.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageCreatingDto.SenderId = userId; var recipient = await _DatingRepository.GetUser(messageCreatingDto.RecipientId); if (recipient == null) { return(NotFound("could not found")); } var message = _mapper.Map <Message>(messageCreatingDto); _DatingRepository.Add(message); if (await _DatingRepository.SaveAll()) { var MessageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, MessageToReturn)); } throw new Exception("Unable to send message"); }