internal static void InitializeDbForTests(CleanArchitectureDbContext db) { PredefinedData.InitializePolls(); db.Polls.AddRange(PredefinedData.Polls); db.SaveChanges(); }
public void Add_Poll_To_Database() { // In-memory database only exists while the connection is open var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); try { DbContextOptions <CleanArchitectureDbContext> options = new DbContextOptionsBuilder <CleanArchitectureDbContext>() .UseSqlite(connection) .Options; // Create the schema in the database using (var context = new CleanArchitectureDbContext(options)) { context.Database.EnsureCreated(); } string title = "test title"; Poll poll = this.GetDefaultPoll(title); // Run the test against one instance of the context using (var context = new CleanArchitectureDbContext(options)) { var pollGateway = new PollGateway(context); pollGateway.CreateAsync(poll); context.SaveChanges(); } // Use a separate instance of the context to verify correct data was saved to database using (var context = new CleanArchitectureDbContext(options)) { context.Polls.Count().Should().Be(1); context.Polls.Single().Title.Should().Be("test title"); } } finally { connection.Close(); } }
public async Task <ApiResponse> Handle(SignupCommand request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { if (request.RegisterId != null || request.RegisterId != Guid.Empty) { var ExistUser = _context.UserRegister.FirstOrDefault(x => x.UserName == request.UserName & x.IsActive == true); if (ExistUser == null) { var userexist = _context.User.FirstOrDefault(x => x.UserName == request.UserName); if (userexist == null) { UserRegister userRegister = new UserRegister { UserName = request.UserName, FirstName = request.FirstName, MiddleName = request.MiddleName, LastName = request.LastName, Email = request.Email, Password = request.Password, CreatedDate = DateTime.UtcNow, PhoneNo = request.PhoneNo, IsActive = true }; await _context.AddAsync(userRegister); //await _context.SaveChangesAsync(); var newUser = new ApplicationUser { UserName = request.UserName, FirstName = request.FirstName, LastName = request.LastName, PhoneNumber = request.PhoneNo, //RegistrationNumber = request.RegistrationNumber, //PhoneNumberConfirmed = true, UserType = (int)UserRole.User }; await _roleManager.CreateAsync(new IdentityRole(UserRoleName.User.ToString())); var objNew = await _userManager.CreateAsync(newUser, request.Password); var id = newUser.Id; await _userManager.AddClaimAsync(newUser, new Claim("Roles", UserRoleName.User.ToString())); await _userManager.AddToRoleAsync(newUser, UserRoleName.User.ToString()); User user = new User(); user.UserName = request.UserName; user.UserById = newUser.Id; user.PhoneNo = request.PhoneNo; //user.SSN = existrecord.SSN; user.isLoginFirstTime = true; user.Lat = 0; user.Long = 0; await _context.User.AddAsync(user); _context.SaveChanges(); //ExistUser.IsActive = true; //ExistUser.UpdatedDate = DateTime.UtcNow; //_context.UserRegister.Update(ExistUser); await _context.SaveChangesAsync(); response.Status = (int)Number.One; response.Message = ResponseMessage.Success; response.ResponseData = userRegister; } else { response.Status = (int)Number.One; response.Message = ResponseMessage.UserExist; } } else { response.Status = (int)Number.One; response.Message = ResponseMessage.UserExist; } } else { var existrecord = _context.UserRegister.FirstOrDefault(x => x.RegisterId == request.RegisterId); if (existrecord != null) { existrecord.FirstName = request.FirstName; existrecord.LastName = request.LastName; existrecord.MiddleName = request.MiddleName; //existrecord.EmailId = request.EmailId; existrecord.Password = RSAcrypotography.Encryption(request.Password); existrecord.UpdatedDate = DateTime.UtcNow; existrecord.PhoneNo = request.PhoneNo; //existrecord.OTP = number; //existrecord.OTPStartDateTime = DateTime.UtcNow; //existrecord.OTPEndDateTime = DateTime.UtcNow.AddMinutes(5); _context.Update(existrecord); await _context.SaveChangesAsync(); //SendOTPMessage.SendMessage(request.PhoneNo, number); response.Status = (int)Number.One; response.Message = ResponseMessage.Success; response.ResponseData = existrecord; } else { response.Status = (int)Number.Zero; response.Message = ResponseMessage.PhoneExist; } } } catch (Exception ex) { throw ex; //response.Status = (int)Number.Zero; //response.Message = ResponseMessage.Error; } return(response); }
public async Task <object> Handle(LoginQuery request, CancellationToken cancellationToken) { ApiResponse response = new ApiResponse(); try { var user = await _userManager.FindByNameAsync(request.UserName); if (user == null) { response.StatusCode = ResponseCode.BadRequest; response.Message = ResponseMessage.UserNotExist; return(response); } var result = await _signInManager.CheckPasswordSignInAsync(user, request.Password, false); if (!result.Succeeded) { throw new Exception("Could not create token"); } var userClaims = await _userManager.GetClaimsAsync(user); var roles = await _userManager.GetRolesAsync(user); userClaims.Add(new Claim(JwtRegisteredClaimNames.Sub, user.UserName)); userClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); string k = _configuration["JwtKey"]; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(k)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var expires = DateTime.Now.AddDays(Convert.ToDouble(_configuration["JwtExpireDays"])); var token = new JwtSecurityToken( issuer: _configuration.GetSection("JwtIssuerOptions:Issuer").Value, audience: _configuration.GetSection("JwtIssuerOptions:Audience").Value, claims: userClaims, expires: expires, signingCredentials: creds ); var userdetails = _context.User.FirstOrDefault(x => x.UserById == user.Id); UserLogin userloginmodel = new UserLogin(); userloginmodel.Token = new JwtSecurityTokenHandler().WriteToken(token); userloginmodel.IsFirstTimeLogin = userdetails.isLoginFirstTime; userloginmodel.UserId = userdetails.UserById; userloginmodel.UserRole = user.UserType; userloginmodel.FName = user.FirstName; userloginmodel.LName = user.LastName; userdetails.Lat = request.Lat; userdetails.Long = request.Lng; userdetails.Token = userloginmodel.Token; _context.User.Update(userdetails); _context.SaveChanges(); var firebasetoken = _context.storeDeviceTokens.FirstOrDefault(x => x.UserById == userdetails.UserById && x.TokenId == request.DiviceTokenId && x.IsDeleted == false); if (firebasetoken == null) { StoreDeviceToken diviceToken = new StoreDeviceToken { TokenId = request.DiviceTokenId, UserById = userdetails.UserById, CreatedById = userdetails.UserById, CreatedDate = DateTime.UtcNow, IsActive = true, IsDeleted = false }; _context.storeDeviceTokens.Add(diviceToken); _context.SaveChanges(); } response.StatusCode = ResponseCode.Ok; response.Message = ResponseMessage.Success; response.ResponseData = userloginmodel; } catch (Exception ex) { throw ex; //response.StatusCode = ResponseCode.BadRequest; //response.Message = ResponseMessage.InvalidPassword; } //var encryptor = new Encryptor(); //var encrypted = encryptor.Encrypt(Encoding.Default.GetBytes(JsonConvert.SerializeObject(response)), RNCryptorKey.Secretkey); //string encryptcode = Convert.ToBase64String(encrypted); //object responseObj = new //{ // responseData = encryptcode //}; return(response); }