public async Task <CollaboratorResponseDto> AddCollaborator(string email, int userId, CollaboratorRequestDto collaborator) { try { Collaborator modelCollaborator = _mapper.Map <Collaborator>(collaborator); if (email == modelCollaborator.email) { throw new FundooException(ExceptionMessages.SELF_COLLABORATE); } Collaborator collaboratorWithSameUser = await _collaboratorRepository.GetCollaboratorByEmail(modelCollaborator.email, modelCollaborator.NoteId); if (collaboratorWithSameUser != null) { throw new FundooException(ExceptionMessages.ALREADY_COLLABORATER); } Note note = await _noteRepository.GetNote(modelCollaborator.NoteId, userId); if (note == null) { throw new FundooException(ExceptionMessages.NO_SUCH_NOTE); } Message message = new EmailService.Message(new string[] { modelCollaborator.email }, "Added as collaborator", $"<h2>You have been added as collaborated by <p style='color:red'>" + email + "</p> To note <p style='color:green'>" + note.Title + "</p><h2>"); _mqServices.AddToQueue(message); return(_mapper.Map <CollaboratorResponseDto>(await _collaboratorRepository.AddCollaborator(email, modelCollaborator))); } catch (Microsoft.EntityFrameworkCore.DbUpdateException) { throw new FundooException(ExceptionMessages.NO_SUCH_USER); } }
private void MessageQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) { var msg = messageQueue.EndReceive(e.AsyncResult); EmailService.Message message = JsonConvert.DeserializeObject <EmailService.Message>(msg.Body.ToString()); _emailSender.SendEmail(message); }
public void AddToQueue(EmailService.Message message) { string mailInfo = JsonConvert.SerializeObject(message); messageQueue.Formatter = new BinaryMessageFormatter(); messageQueue.ReceiveCompleted += MessageQueue_ReceiveCompleted; messageQueue.Send(mailInfo); messageQueue.BeginReceive(); messageQueue.Close(); }
public async Task <ActionResult <UserDto> > Register(RegisterDto registerDto) { if (await UserExist(registerDto.Username)) { return(BadRequest("UserName already taken.")); } var user = _mapper.Map <AppUser>(registerDto); user.UserName = registerDto.Username.ToLower(); user.Email = registerDto.Email; var result = await _userManager.CreateAsync(user, registerDto.Password); if (!result.Succeeded) { return(BadRequest(result.Errors)); } var token = await _userManager.GenerateEmailConfirmationTokenAsync(user); string urlPath = ""; var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); if (env.ToLower() == "development") { urlPath = _config["returnPaths:ConfirmEmail"]; } else { urlPath = Environment.GetEnvironmentVariable("ReturnPaths:ConfirmEmail"); } var confirmationLink = URLBuilder.BuildUrl(urlPath, token, user.Id.ToString()); var message = new Message(new string[] { user.Email }, "Confirmation Email Link", URLBuilder.BuildContext(URLBuilder.MessageType.ConfirmEmail, confirmationLink, user.UserName), null); await _emailSender.SendEmailAsync(message); var roleResult = await _userManager.AddToRoleAsync(user, "Member"); if (!roleResult.Succeeded) { return(BadRequest(roleResult.Errors)); } return(new UserDto { Username = user.UserName, Token = await _tokenService.CreateToken(user), KnownAs = user.KnownAs, Gender = user.Gender }); }
public async Task <ActionResult> ForgotPassword(ForgotPasswordDto forgotPassword) { if (forgotPassword.Email == null) { return(BadRequest("Missing Email!")); } var user = await _userManager.Users .IgnoreQueryFilters() .Where(e => e.Email.ToLower() == forgotPassword.Email.ToLower()) .FirstOrDefaultAsync(); if (user == null) { return(Unauthorized("Username not Found")); } var token = await _userManager.GeneratePasswordResetTokenAsync(user); string urlPath = ""; if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT").ToLower() == "development") { urlPath = _config["returnPaths:PasswordChange"]; } else { urlPath = Environment.GetEnvironmentVariable("ReturnPaths:PasswordChange"); } var changePasswordLink = URLBuilder.BuildUrl(urlPath, token, user.Id.ToString()); var message = new Message(new string[] { user.Email }, "Reset Password link", URLBuilder.BuildContext(URLBuilder.MessageType.ResetPassword, changePasswordLink, user.UserName), null); await _emailSender.SendEmailAsync(message); return(Ok()); }