public ActionResult Create([Bind(Include = "id,name,password,age,updatedDateTime")] Users users) { if (ModelState.IsValid) { db.usersList.Add(users); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(users)); }
public string UpdateScore([FromBody] System.Text.Json.JsonElement rawQuery) { string rawJson = rawQuery.ToString(); User scoreData = JsonConvert.DeserializeObject <User>(rawJson); error err = new error { Code = 0, Message = "Successfull" }; using (var context = new UsersDBContext()) { User user = context.Users.Where(x => x.Name == scoreData.Name).FirstOrDefault(); if (user == null) { err.Code = 1; err.Message = "Error update values, player doesnt exist"; } else { user.Score += scoreData.Score; user.Win += scoreData.Win; user.Lose += scoreData.Lose; context.SaveChanges(); } } string response = JsonConvert.SerializeObject(err); return(response); }
public Result Add(User userData) { Result resultado = ValidateData(userData); resultado.Action = "Inclusão de Usuário"; if (resultado.Inconsistencies.Count == 0 && _context.Users.Where( u => u.Email == userData.Email).Count() > 0) { resultado.Inconsistencies.Add( "Usuário já cadastrado"); } if (resultado.Inconsistencies.Count == 0) { _context.Users.Add(userData); _context.SaveChanges(); } return(resultado); }
public async Task <User> Handle(PutUserRequest request, CancellationToken cancellationToken) { var data = await DatabaseConnection.Users.AddAsync(new User() { FirstName = request.FirstName, LastName = request.LastName, Address = request.Address, DateOfBith = request.DateOfBith }, cancellationToken).ConfigureAwait(false); DatabaseConnection.SaveChanges(); return(data.Entity); }
public static int AddUserRole(UserRole role) { try { using (var db = new UsersDBContext()) { db.UserRoles.Add(role); return db.SaveChanges(); } } catch (Exception ex) { return -2; } }
public static int AssignRoleToUser(int UserID, int RoleID) { try { using (var db = new UsersDBContext()) { UsersRolesMapping mapTableObj = new UsersRolesMapping() { UserID = UserID, UserRoleID = RoleID, }; db.UsersRolesMappings.Add(mapTableObj); return db.SaveChanges(); } } catch (Exception ex) { return -2; } }
public static int DeleteRole(int id) { try { using (var db = new UsersDBContext()) { UserRole r = db.UserRoles.Single(role => role.UserRoleID == id); if (r != null) { db.UserRoles.Remove(r); return db.SaveChanges(); } else return 0; } } catch (Exception ex) { return -2; } }
public void BasicAddingUserTest() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new UsersDBContext(options)) { List <User> users = context.Users.Select(s => s).ToList(); Assert.AreEqual(1, users.Count); } }
public void TestDbValidationOfDataModel() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; try { // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "", LastName = "Test" }); context.SaveChanges(); } } catch (ValidationException ex) { Assert.IsTrue(ex.Message.Contains("The FirstName field is required")); } }
public string Post([FromBody] System.Text.Json.JsonElement rawQuery) { string rawJson = rawQuery.ToString(); error err = new error { Code = 0, Message = "Successfull" }; string response; User userData = JsonConvert.DeserializeObject <User>(rawJson); using (var context = new UsersDBContext()) { User user = context.Users.Where(x => x.Name == userData.Name).FirstOrDefault(); if (user != null) { //err.Code = 1; //err.Message = "Name already naken"; //Already have user } else { context.Users.Add(userData); context.SaveChanges(); } } response = JsonConvert.SerializeObject(err); //string jsonData = JsonConvert.SerializeObject(query); //Newtonsoft.Json.Linq.JObject //var aa = JsonConvert.DeserializeObject(value.ToString()); return(response); }
public async System.Threading.Tasks.Task GetUserIDTestAsync() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234" }); context.Add(new User() { Address = "142345678910111213141516171819a", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); var Service = new GetUserService(context); var cancelToken = new CancellationToken(); var response = await Service.Handle(new GetUserRequest() { Id = 1 }, cancelToken).ConfigureAwait(false); Assert.IsNotNull(response); Assert.IsTrue(response.Id == 1); } }
public Users Create(Users user, string password) { // validation if (string.IsNullOrWhiteSpace(password)) { throw new AppException("Password is required"); } if (_context.Users.Any(x => x.Username == user.Username)) { throw new AppException("Username \"" + user.Username + "\" is already taken"); } byte[] passwordHash, passwordSalt; CreatePasswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; _context.Users.Add(user); _context.SaveChanges(); return(user); }
public void BasicFindingCorrectByIdUserTest() { var options = new DbContextOptionsBuilder <UsersDBContext>() .UseInMemoryDatabase(databaseName: "User") .Options; // Insert seed data into the database using one instance of the context using (var context = new UsersDBContext(options)) { context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Correct", LastName = "Test1234" }); context.Add(new User() { Address = "12345", DateOfBith = DateTime.Now, FirstName = "Seed", LastName = "Test" }); context.SaveChanges(); } // Use a clean instance of the context to run the test using (var context = new UsersDBContext(options)) { var user = context.Users.FirstOrDefault(s => s.Id == 3); Assert.IsNotNull(user); Assert.IsFalse(string.IsNullOrWhiteSpace(user.FirstName)); Assert.IsFalse(string.IsNullOrWhiteSpace(user.LastName)); Assert.IsFalse(string.IsNullOrWhiteSpace(user.Address)); Assert.IsTrue(user.DateOfBith > DateTime.Now.AddDays(-1)); Assert.IsTrue(user.LastName.Equals("Test1234", StringComparison.InvariantCultureIgnoreCase)); } }
public static int UpdateRole(UserRole UpdatedRole) { using (var db = new UsersDBContext()) { UserRole r = db.UserRoles.Single(role => role.UserRoleID == UpdatedRole.UserRoleID); if (r != null) { r.UserRoleName = UpdatedRole.UserRoleName; r.UserRoleDescription = UpdatedRole.UserRoleDescription; return db.SaveChanges(); } else return 0; } }
public static int DeleteUser(int id) { try { using (var db = new UsersDBContext()) { User u = db.Users.Single(user => user.UserID == id); if (u != null) { db.Users.Remove(u); return db.SaveChanges(); } else return 0; } } catch (Exception ex) { return -2; } }
public static int UpdateUser(User UpdatedUser) { using (var db = new UsersDBContext()) { User u = db.Users.Single(user => user.UserID == UpdatedUser.UserID); if (u != null) { u.FirstName = UpdatedUser.FirstName; u.LastName = UpdatedUser.LastName; u.Password = UpdatedUser.Password; u.UserName = UpdatedUser.UserName; u.EmailAddress = UpdatedUser.EmailAddress; return db.SaveChanges(); } else return 0; } }
public bool Save() { return(_context.SaveChanges() >= 0); }
public static int RemoveRoleFromUser(int UserID, int RoleID) { try { using (var db = new UsersDBContext()) { if (db.UsersRolesMappings.Any(m => m.UserID == UserID & m.UserRoleID == RoleID)) { UsersRolesMapping mapObj = db.UsersRolesMappings.Single(m => m.UserID == UserID & m.UserRoleID == RoleID); db.UsersRolesMappings.Remove(mapObj); return db.SaveChanges(); } else return 0; } } catch (Exception ex) { return -2; } }