public string register() { JSONRegResponse response = new JSONRegResponse(); string registerJSONRaw = Request.Query.FirstOrDefault(p => p.Key == "registerData").Value; registerJSON = JsonSerializer.Deserialize <JSONDataReg>(registerJSONRaw); foreach (User users in db.Users) { // username is taken if (users.username == registerJSON.username) { return(JsonSerializer.Serialize <JSONRegResponse>(response)); } } response.login = true; db.AddRange( new User { id = 0, firstname = registerJSON.firstname, username = registerJSON.username, email = registerJSON.email, pass = registerJSON.pass, sex = registerJSON.sex, newsletter = ((registerJSON.newsletter == "news_accept")?true:false) } ); db.SaveChanges(); // Registration success; return(JsonSerializer.Serialize <JSONRegResponse>(response));; }
public async Task <IActionResult> Patch([FromBody] JsonPatchDocument <AppUser> patch) { var user = await _userContext.Users //.Include(u => u.Properties) .FirstOrDefaultAsync(u => u.Id == UserIdentity.UserId); //var originProperties = user.Properties; patch.ApplyTo(user); foreach (var property in user.Properties) { _userContext.Entry(property).State = EntityState.Detached; } var originProperties = await _userContext.UserProperties.AsNoTracking().Where(u => u.AppUserId == UserIdentity.UserId).ToListAsync(); var allProperties = originProperties.Union(user.Properties).Distinct(); var removedProperties = originProperties.Except(user.Properties); var newProperties = allProperties.Except(originProperties); _userContext.RemoveRange(removedProperties); _userContext.AddRange(newProperties); using (var trans = _userContext.Database.BeginTransaction()) { this.RaiseUserProfileChangeEvent(user);//发布用户属性变更的消息 _userContext.SaveChanges(); trans.Commit(); } return(Json(user)); }
public async Task Seed() { if (!_context.Users.Any()) { _context.AddRange(Users); await _context.SaveChangesAsync(); } }
public UserControllerTest() { _dbOptions = new DbContextOptionsBuilder <UserContext>() .UseInMemoryDatabase(databaseName: "in-memory") .Options; using (var dbContext = new UserContext(_dbOptions)) { dbContext.AddRange(UserContextDbSeed.GetData()); dbContext.SaveChanges(); } }
/// <summary> /// Sets up. /// </summary> // TODO there is a TestInitialize annotation for this. you should NOT call it directly private void SetUp() { // TODO no point in having this region #region Setup _userRepositoryMocked = new Mock <IUserRepository>(MockBehavior.Strict); var options = new DbContextOptionsBuilder <UserContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()) .Options; // TODO we mock everything else than the tested "unit" in unit tests _context = new UserContext(options); _usersController = new UsersController(_userRepositoryMocked.Object); _listOfUsers = GetUsers(); // TODO we do NOT manipulate the context in the unit tests _context.AddRange(_listOfUsers); _context.SaveChanges(); #endregion }
public void BulkAddUser(IEnumerable <User> users) { _ctx.AddRange(users); }