public async Task <UserServiceResponse> LoginAsync(UserLogin user) { var response = new UserServiceResponse(); var result = await this._accountRepository.LoginAsync(user).ConfigureAwait(false); if (!result.Succeeded) { return(response); } var loginUser = await this._accountRepository.FindUserByEmailAsync(user.EmailAddress).ConfigureAwait(true); if (loginUser != null) { response.IsValidUser = true; response.User = ProvideUserInfo(loginUser); } else { response.IsValidUser = false; } return(response); }
public IActionResult Update([FromBody] UserTemplate user) { try { var userGetId = _userRepository.GetById(user.Id); if (userGetId == null) { return(Ok(new ServiceResponse <UserServiceResponse, UserTemplate>(UserServiceResponse.NotFound))); } UserServiceResponse validation = Validate(user); if (validation != UserServiceResponse.Success) { return(Ok(new ServiceResponse <UserServiceResponse, UserTemplate>(validation))); } var userMap = _mapper.Map <User>(user); _userRepository.Update(userMap); return(Ok(new ServiceResponse <UserServiceResponse, UserTemplate>(UserServiceResponse.Success))); } catch (System.Exception ex) { _logger.Error($"User Update :{ex}"); return(BadRequest(new ServiceResponse <UserServiceResponse, UserTemplate>(UserServiceResponse.Exception))); } }
public async Task <UserServiceResponse> RegisterUserAsync(UserRegistration user) { var response = new UserServiceResponse(); var existingUser = await this._accountRepository.FindUserByEmailAsync(emailAddress : user.EmailAddress).ConfigureAwait(continueOnCapturedContext: false); if (existingUser != null) { return(response); } var result = await this._accountRepository.CreateUserAsync(user : user) .ConfigureAwait(continueOnCapturedContext: true); if (!result.Succeeded) { return(response); } response.IsValidUser = true; existingUser = await this._accountRepository.FindUserByEmailAsync(emailAddress : user.EmailAddress) .ConfigureAwait(continueOnCapturedContext: false); var emailMessage = EmailComposer.ComposeEmail(existingUser.UserName, existingUser.VerificationCode); EmailSender.SendEmail(@"*****@*****.**", existingUser.Email, @"testuser44", emailMessage); return(response); }
public UserServiceResponse InsertUser(User user) { var result = new UserServiceResponse(); using (TransactionScope scope = new TransactionScope()) { MyNextComicEntities context = null; try { context = new MyNextComicEntities(); context.Configuration.AutoDetectChangesEnabled = false; if (!(context.Users.Any(x => x.Username == user.UserName))) { var Id = context.Users.OrderByDescending(x => x.Id).Select(x => x.Id).First(); context.Users.Add(new Users() { Id = Id + 1, Username = user.UserName, Password = HashPassword(user.Password), Email = user.Email }); context.SaveChanges(); context.Dispose(); result.Success = true; result.ErrorMessage = ""; } else { context.Dispose(); result.Success = false; result.ErrorMessage = "El nombre de usuario ingresado no se encuentra disponible"; } } catch (DbEntityValidationException e) { result.Success = false; result.ErrorMessage = e.Message; } scope.Complete(); } return(result); }
public UserServiceResponse LogInUser(User user) { var result = new UserServiceResponse(); using (TransactionScope scope = new TransactionScope()) { MyNextComicEntities context = null; try { context = new MyNextComicEntities(); context.Configuration.AutoDetectChangesEnabled = false; var storedUser = context.Users.Where(x => x.Username == user.UserName).FirstOrDefault(); if (storedUser != null) { if (VerifyPassword(storedUser.Password, user.Password)) { result.Success = true; result.ErrorMessage = ""; } else { result.Success = false; result.ErrorMessage = "Contraseña inválida"; } } else { context.Dispose(); result.Success = false; result.ErrorMessage = "No se encontró ningun usuario con ese nombre"; } } catch (DbEntityValidationException e) { result.Success = false; result.ErrorMessage = e.Message; } scope.Complete(); } return(result); }
public IActionResult Add([FromBody] UserTemplate user) { try { UserServiceResponse validation = Validate(user); if (validation != UserServiceResponse.Success) { return(Ok(new ServiceResponse <UserServiceResponse, UserTemplate>(validation))); } user.Password = EncryptionDecryption.EncryptString(_config.Value.HashKey, user.Password); var userMap = _mapper.Map <User>(user); _userRepository.Add(userMap); return(Ok(new ServiceResponse <UserServiceResponse, UserTemplate>(UserServiceResponse.Success))); } catch (System.Exception ex) { _logger.Error($"User Add :{ex}"); return(BadRequest(new ServiceResponse <UserServiceResponse, UserTemplate>(UserServiceResponse.Exception))); } }
public async Task <UserServiceResponse> VerifyUserAsync(UserVerification user) { var response = new UserServiceResponse(); var loginUser = await this._accountRepository.FindUserByEmailAsync(user.UserName).ConfigureAwait(true); if (!loginUser.VerificationCode.Equals(user.VerificationCode)) { return(response); } loginUser.IsVerified = true; var result = await this._accountRepository.UpdateUserAsync(loginUser).ConfigureAwait(true); if (!result.Succeeded) { return(response); } response.IsValidUser = true; response.User = ProvideUserInfo(loginUser); return(response); }
public UserServiceResponse <HeadstoneUser> GetUsers(UserQueryRequest req, List <ServiceLogRecord> logRecords = null) { // Create the watch var sw = new Stopwatch(); sw.Start(); // Create a log record collection if necessary if (logRecords == null) { logRecords = new List <ServiceLogRecord>(); } // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "User query request received." }); // Create response var response = new UserServiceResponse <HeadstoneUser>(); #region [ Validate request ] // Check required data List <string> dataErrors = new List <string>(); if (dataErrors.Count > 0) { // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = dataErrors.Count + " error(s) found within the posted data! Terminating the process. Errors:" + String.Join(";", dataErrors) }); // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Error; response.Code = ((short)TenantServiceResponseCodes.Invalid_Request).ToString(); response.PreProcessingTook = sw.ElapsedMilliseconds; response.Message = "There are some erros with the incoming request data!"; response.Errors.AddRange(dataErrors); response.LogRecords = logRecords; return(response); } #endregion // Stop the timer sw.Stop(); // Set the pre-processing time and start the time response.PreProcessingTook = sw.ElapsedMilliseconds; sw.Start(); #region [ Envelope settings ] // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "Creating the envelope." }); // Create the including fields according to the envelope var includes = new List <string>(); if (!string.IsNullOrEmpty(req.Envelope)) { if (req.Envelope == "full") { } } #endregion #region [ Filters ] // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "Creating filters." }); #if NET452 // Check for filters Expression <Func <HeadstoneUser, bool> > filterPredicate = PredicateBuilder.New <HeadstoneUser>(true); // Add the filters if (req.UserIds.Any()) { filterPredicate = filterPredicate.And(u => req.UserIds.Contains(u.Id)); } if (req.Emails.Any()) { filterPredicate = filterPredicate.And(u => req.Emails.Contains(u.Email)); } #endif #endregion #region [ Service call ] // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "Calling the base service." }); // Create a default list var baseServiceResponse = new ServiceResponse <HeadstoneUser>(); #if NET452 // Make the query if (filterPredicate.Parameters.Count > 0) { baseServiceResponse = userServiceBase.GetIncluding(filterPredicate, includes.ToArray()); } else { baseServiceResponse = userServiceBase.GetAllIncluding(includes.ToArray()); } #endif if (baseServiceResponse.Type != ServiceResponseTypes.Success) { // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "There was an error while querying the users!" }); // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Error; response.Code = ((short)TenantServiceResponseCodes.General_Exception).ToString(); response.ServiceTook = sw.ElapsedMilliseconds; response.Message = "There was an error while querying the users!"; response.Errors.Add("There was an error while querying the users!"); response.LogRecords = logRecords; return(response); } // Set the result response.Result = baseServiceResponse.Result; // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "Users successfuly fetched." }); // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Success; response.Code = ((short)TenantServiceResponseCodes.Request_Successfuly_Completed).ToString(); response.ServiceTook = sw.ElapsedMilliseconds; response.Message = "Users successfuly fetched"; response.LogRecords = logRecords; #endregion return(response); }
public UserServiceResponse <HeadstoneUser> CreateUser(UserCreated ev, List <ServiceLogRecord> logRecords = null) { // Create the watch var sw = new Stopwatch(); sw.Start(); // Create a log record collection if necessary if (logRecords == null) { logRecords = new List <ServiceLogRecord>(); } // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "User creation request received." }); // Create a response object var response = new UserServiceResponse <HeadstoneUser>(); #region [ Validate request ] // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "User has the required permissions. Now validating the incoming data." }); // Check required data List <string> dataErrors = new List <string>(); if (String.IsNullOrEmpty(ev.AppKey)) { dataErrors.Add("No valid application key!"); } if (String.IsNullOrEmpty(ev.Environment)) { dataErrors.Add("No valid environment!"); } if (dataErrors.Count > 0) { // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = dataErrors.Count + " error(s) found within the posted data! Terminating the process. Errors:" + String.Join(";", dataErrors) }); // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Error; response.Code = ((short)TenantServiceResponseCodes.Invalid_Request).ToString(); response.PreProcessingTook = sw.ElapsedMilliseconds; response.Message = "There are some erros with the incoming request data!"; response.Errors.AddRange(dataErrors); response.LogRecords = logRecords; return(response); } #endregion #region [ Data manuplation ] #endregion // Stop the timer sw.Stop(); // Set the pre-processing time and start the time response.PreProcessingTook = sw.ElapsedMilliseconds; sw.Start(); #region [ Create wallet ] // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = "Creating the user." }); // Create the user var user = new HeadstoneUser() { Firstname = ev.Firstname, Lastname = ev.Lastname, Gender = ev.Gender, Birthdate = ev.DateOfBirth, Email = ev.Email, UserName = ev.Email, PhoneNumber = ev.PhoneNumber, MobileNumber = ev.MobileNumber, }; // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = string.Format("User created. UserToken:{0}; SessionId:{1}", ev.UserToken, ev.SessionId) }); #endregion #region [ Save user ] // Save the user var baseServiceResponse = userManager.CreateAsync(user).Result; if (!baseServiceResponse.Succeeded) { // Add log logRecords.Add(new ServiceLogRecord() { Type = "ERROR", TimeStamp = DateTime.Now, Body = "There was an error while saving the user!" }); // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Error; response.Code = ((short)TenantServiceResponseCodes.General_Exception).ToString(); response.ServiceTook = sw.ElapsedMilliseconds; response.Message = "There was an error while creating the user!"; response.Errors.Add("There was an error while creating the user!"); response.LogRecords = logRecords; return(response); } else { // Add log logRecords.Add(new ServiceLogRecord() { Type = "DEBUG", TimeStamp = DateTime.Now, Body = string.Format("User successfuly created. UserId:{0}; UserToken:{1}; SessionId:{2}", user.Id, ev.UserToken, ev.SessionId) }); // Add the new object to the result response.Result.Add(user); // Set the wallet id response.UserId = user.Id; } #endregion // Stop the sw sw.Stop(); response.Type = ServiceResponseTypes.Success; response.Code = ((short)TenantServiceResponseCodes.Request_Successfuly_Completed).ToString(); response.ServiceTook = sw.ElapsedMilliseconds; response.Message = string.Format("User successfuly created. UserId:{0}; UserToken:{1}; SessionId:{2}", user.Id, ev.UserToken, ev.SessionId); response.LogRecords = logRecords; return(response); }