public async Task <IActionResult> CreateMessage(int userid, MessageForCreationDTO messageForCreationDTO) { var sender = await _repo.GetUser(userid); if (sender.Id != Convert.ToInt32(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()) { return(Ok(_mapper.Map <MessageToReturnDTO>(await _repo.GetMessage(message.Id)))); } throw new Exception("Could sent message."); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO model) { if (!ValidateAuthenticationUserId(userId)) { return(Unauthorized()); } var sender = await _repo.GetUser(userId); model.SenderId = sender.Id; model.Sender = sender; var recipient = await _repo.GetUser(model.RecipientId); model.RecipientId = recipient.Id; model.Recipient = recipient; if (recipient == null) { return(BadRequest("Could not find specified user")); } var message = _mapper.Map <Message>(model); _repo.Add(message); if (await _repo.SaveAll()) { var messageToReturn = _mapper.Map <MessageForReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } return(StatusCode(StatusCodes.Status502BadGateway, "There was a problem creating the message")); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO msgDTO) { //So autommaper maps automatically the objects inside MessageToReturnDTO User sender = await _repo.GetUser(userId); if (sender.ID != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } msgDTO.SenderId = userId; User recipient = await _repo.GetUser(msgDTO.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } Message m = _mapper.Map <Message>(msgDTO); _repo.Add(m); if (await _repo.SaveAll()) { var msgReturn = _mapper.Map <MessageToReturnDTO>(m); return(CreatedAtRoute("GetMessage", new { userId, msgId = m.Id }, msgReturn)); //return CreatedAtRoute("GetMessage", new { senderId, id = m.Id}, m); } throw new System.Exception("Error while creating message"); }
public async Task <IActionResult> CreateMessage(int userID, MessageForCreationDTO messageForCreationDTO) { var sender = await _datingRepo.GetUser(userID, false); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderID = userID; var recipient = await _datingRepo.GetUser(messageForCreationDTO.RecipientID, false); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDTO); _datingRepo.Add(message); if (await _datingRepo.SaveAll()) { // mapping it back so that we show only the DTO's information and not the // entire message/user information (i.e. with all the variables). // Also automapper automatically maps sender and receiver info in the MessageToReturnDTO var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { id = message.ID }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { if (userId != 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); var messageToReturn = _mapper.Map <MessageForCreationDTO>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }
public async Task <ActionResult> CreateMessage(int userId, MessageForCreationDTO model) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } model.SenderId = userId; var recipient = await _repo.GetUser(model.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(model); _repo.add(message); if (await _repo.SaveAll()) { var messagetoReturn = _mapper.Map <MessageForCreationDTO>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messagetoReturn)); } else { return(StatusCode(500, "Creating the message failed on save")); } }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _datingRepository.GetUser(userId, true); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderId = userId; var recipient = await _datingRepository.GetUser(messageForCreationDTO.RecipientId, false); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDTO); _datingRepository.Add(message); if (await _datingRepository.SaveAll()) { // recipient y sender se mapean a messageToReturn porque estan en memoria var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } return(BadRequest("Creating the message failed on save")); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO msgCreationDTO) { // checks if ID matches token id if (userId != int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier))) { return(Unauthorized()); } msgCreationDTO.SenderId = userId; var recipient = await _repo.GetUser(msgCreationDTO.RecipientId); if (recipient == null) { return(BadRequest("User doesn't exists")); } var message = _mapper.Map <Message>(msgCreationDTO); _repo.Add(message); var messageToReturn = _mapper.Map <MessageForCreationDTO>(message); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } // failed at saving message throw new Exception("Sending message failed"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _repository.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var recipient = await _repository.GetUser(messageForCreationDTO.RecipientId); messageForCreationDTO.SenderId = userId; if (recipient == null) { return(BadRequest("Não foi possível encontrar o recipiente com ID " + messageForCreationDTO.RecipientId)); } var message = _mapper.Map <Message>(messageForCreationDTO); _repository.Add(message); if (await _repository.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); Console.WriteLine(message.Id); return(CreatedAtRoute("GetMessage", new { userId, messageId = message.Id }, messageToReturn)); // atencao: no .NETCore 3.0 precisa fornecer tbm o userId } throw new System.Exception("Falha ao criar a mensagem"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _repository.GetUser(userId); if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderId = userId; var recipient = await _repository.GetUser(messageForCreationDTO.RecipientId); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDTO); _repository.Add(message); if (await _repository.SaveAll()) { var messageToReturn = _mapper.Map <MessageToReturnDto>(message); return(CreatedAtRoute("GetMessage", new { userId, id = message.Id }, messageToReturn)); } throw new Exception("Message creation failed"); //return BadRequest("Some error occured"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _repo.GetUser(userId); if (!this.CheckRequestAuthorization(sender.Id)) { 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."); }
public async Task <ActionResult> Create(MessageForCreationDTO model) { var result = await this.messages.CreateAsync(model); if (result.Failure) { return(BadRequest(result.Error)); } return(Created(nameof(this.Create), result.Data)); }
public async Task <Result <DateTime> > CreateAsync(MessageForCreationDTO model) { var result = await ValidateInputModel(model); if (result.Failure) { return(result.Error); } var message = this.mapper.Map <Message>(model); await this.data.AddAsync(message); await this.data.SaveChangesAsync(); return(message.SentOn); }
public IHttpActionResult SendMessage(MessageForCreationDTO model) { if (ModelState.IsValid) { var message = new Message { MessageHeader = model.MessageHeader, MessageBody = model.MessageBody, Email = model.Email, FullName = model.FullName }; _messageRepository.Add(message); _emailService.SendMail(model.FullName, model.Email, model.MessageHeader, $"{model.MessageBody} hakkında attığınız mesaj bize ulaştı.\n\nMesajınızı değerlendirilerek en kısa sürede size geri dönüş yapacaktır."); return(Ok("Messajınız gönderildi")); } return(BadRequest("Eksik bilgi doldurdunuz")); }
public IActionResult AnswerMessage(MessageForCreationDTO model) { if (ModelState.IsValid) { var message = new Message { MessageHeader = model.MessageHeader, MessageBody = model.Answer, Email = model.Email, FullName = model.FullName }; _messageService.Create(message); _emailService.SendMail(model.FullName, model.Email, model.MessageHeader, $"{model.Answer} "); return(Ok(Messages.MessageSend)); } return(BadRequest(Messages.ModelNullOrEmpty)); }
public IHttpActionResult AnswerMessage(MessageForCreationDTO model) { if (ModelState.IsValid) { var message = new Message { MessageHeader = model.MessageHeader, MessageBody = model.Answer, Email = model.Email, FullName = model.FullName }; _messageRepository.Add(message); _emailService.SendMail(model.FullName, model.Email, model.MessageHeader, $"{model.Answer} "); return(Ok("Messajınız gönderildi")); } return(BadRequest("Eksik bilgi doldurdunuz")); }
private async Task <Result> ValidateInputModel(MessageForCreationDTO model) { var adExists = await this.data.Ads.AnyAsync(a => a.Id == model.AdId); if (!adExists) { return("Обявата не е намерена"); } var recipientExists = await this.data.Users.AnyAsync(u => u.Id == model.RecipientId); if (!recipientExists) { return("Получателят не е намерен"); } return(true); }
public IActionResult SendMessage(MessageForCreationDTO model) { if (ModelState.IsValid) { var message = new Message { MessageHeader = model.MessageHeader, MessageBody = model.MessageBody, Email = model.Email, FullName = model.FullName }; _messageService.Create(message); _emailService.SendMail(model.FullName, model.Email, model.MessageHeader, $"{model.MessageBody} hakkında attığınız mesaj bize ulaştı.\n\nMesajınızı değerlendirilerek en kısa sürede size geri dönüş yapacaktır."); return(Ok(Messages.MessageSend)); } return(BadRequest(Messages.ModelNullOrEmpty)); }
public async Task <IActionResult> CreateMessage(string userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _repo.GetUser(userId); if (sender.ID != 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 the User")); } var message = _mapper.Map <Message>(messageForCreationDTO); try { _repo.CreateMessage(message); var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); // var userToReturn = _mapper.Map <UserForDetailedDTO>(sender); messageToReturn.SenderKnownAs = userToReturn.KnownAs; messageToReturn.SenderPhotoUrl = userToReturn.PhotoUrl; userToReturn = _mapper.Map <UserForDetailedDTO>(recipient); messageToReturn.RecipientKnownAs = userToReturn.KnownAs; messageToReturn.RecipientPhotoUrl = userToReturn.PhotoUrl; // return(CreatedAtRoute("GetMessage", new { id = message.ID }, messageToReturn)); } catch (Exception ex) { throw new Exception("Creating the message failed on Save"); } }
public async Task <IActionResult> CreateMessage(int userId, [FromBody] MessageForCreationDTO messageForCreationDTO) { var sender = await _repository.GetUser(userId, false); // Compare user id against root parameter, authorize the user var isCurrentUser = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value) == userId; if (sender == null || !isCurrentUser) { return(Unauthorized()); } if (messageForCreationDTO == null || messageForCreationDTO.Content == null) { return(BadRequest("Message cannot be empty")); } messageForCreationDTO.SenderId = userId; // Get the recipient and check if exists var recipient = await _repository.GetUser(messageForCreationDTO.RecipientId, false); if (recipient == null) { return(BadRequest("Could not find user")); } var message = _mapper.Map <Message>(messageForCreationDTO); // Add message to be saved _repository.Add(message); // If the message is saved then we get correct sender id from db if (await _repository.SaveAll()) { // we return message info tohether with senders and recipients photos to display on the client var messageToReturn = _mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { messageId = message.Id }, messageToReturn)); } return(BadRequest("Something went wrong")); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { // Check is user is making request from appropriate userId if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } messageForCreationDTO.SenderId = userId; // Check if recipient user exists User recipient = await _Repo.GetUser(messageForCreationDTO.RecipientId); if (recipient == null) { return(BadRequest("User not found")); } // Map the new message to the Message object and queue into entity Message message = _Mapper.Map <Message>(messageForCreationDTO); message.Recipient = recipient; message.Sender = await _Repo.GetUser(userId); _Repo.Add(message); // Execute save on the database and return a new route on the message if (await _Repo.SaveAll()) { // Map the message back to the DTO MessageToReturnDTO messageToReturn = _Mapper.Map <MessageToReturnDTO>(message); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn)); } throw new System.Exception("Message failed to save"); }
public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDTO messageForCreationDTO) { var sender = await _repo.GetOwnUser(userId); /*We do not need to load the sender * .. but we do it because onece it's loaded it's in the memory... * .. so AutoMapper does its magic and actually maps it correctnly from Message to MessageToReturnDTO*/ if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); //This method is checking if the ID of the user requesting this HttpPost is } //..is the same as userId (if they are different that means that somebody can pose / send messages as different users) messageForCreationDTO.SenderId = userId; var recipient = await _repo.GetOtherUser(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 { id = message.Id }, messageToReturn)); } throw new Exception("Creating the message failed on save"); }