public TEntity Insert(TEntity entity) { var newEntity = _dbSet.Add(entity); _context.SaveChanges(); return(newEntity); }
public UserGetModel Register(RegisterPostModel registerInfo) { User existing = context.Users.FirstOrDefault(u => u.Username == registerInfo.Username); if (existing != null) { return(null); } User toAdd = new User { Email = registerInfo.Email, LastName = registerInfo.LastName, FirstName = registerInfo.FirstName, Password = ComputeSha256Hash(registerInfo.Password), Username = registerInfo.Username, UserUserRoles = new List <UserUserRole>(), CreatedAt = DateTime.Today }; var regularRole = context.UserRoles.FirstOrDefault(ur => ur.Name == UserRoles.Regular); context.Users.Add(toAdd); context.UserUserRoles.Add(new UserUserRole { User = toAdd, UserRole = regularRole, StartTime = DateTime.Now, EndTime = null, }); context.SaveChanges(); return(null); }
public void Post([FromBody] Task task) { task.DateClosed = null; task.DateAdded = DateTime.Now; context.Tasks.Add(task); context.SaveChanges(); }
public Task Create(Task task) { task.DateClosed = null; task.DateAdded = DateTime.Now; context.Tasks.Add(task); context.SaveChanges(); return(task); }
public Task Create(TaskPostModel task) { Task toAdd = TaskPostModel.ToTask(task); context.Tasks.Add(toAdd); context.SaveChanges(); return(toAdd); }
public UserRoleGetModel Create(UserRolePostModel userRolePostModel) { UserRole toAdd = UserRolePostModel.ToUserRole(userRolePostModel); context.UserRole.Add(toAdd); context.SaveChanges(); return(UserRoleGetModel.FromUserRole(toAdd)); }
public int Create(TaskGroupViewModel group) { var newGroup = _mapper.Map <TaskGroupEntity>(group); newGroup = _dbContext.TaskGroups.Add(newGroup).Entity; _dbContext.SaveChanges(); return(newGroup.Id); }
public Comment Create(CommentPostModel comment, int id) { Comment toAdd = CommentPostModel.ToComment(comment); Task task = context.Tasks.FirstOrDefault(tas => tas.Id == id); task.Comments.Add(toAdd); context.SaveChanges(); return(toAdd); }
public Taskk Create(TaskPostModel task, User addedBy) { Taskk toAdd = TaskPostModel.ToTask(task); toAdd.Owner = addedBy; context.Tasks.Add(toAdd); context.SaveChanges(); return(toAdd); }
public Comment Create(CommentPostDTO comment, User addedBy) { Comment commentAdd = CommentPostDTO.ToComment(comment); commentAdd.Owner = addedBy; context.Comments.Add(commentAdd); context.SaveChanges(); return(commentAdd); }
public int Create(UserTaskViewModel taskDetailsViewModel) { var newTask = _mapper.Map <UserTaskEntity>(taskDetailsViewModel); newTask = _dbContext.UserTasks.Add(newTask).Entity; _dbContext.SaveChanges(); return(newTask.Id); }
public IActionResult AddNewUser(UserInformation newUser) { if (ModelState.IsValid) { _context.UserInformation.Add(newUser); _context.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }
public Task Create(TaskPostDTO task, User addedBy) { Task taskAdd = TaskPostDTO.ToTask(task); task.DateClosed = null; task.DateAdded = DateTime.Now; taskAdd.Owner = addedBy; context.Tasks.Add(taskAdd); context.SaveChanges(); return(taskAdd); }
public IActionResult Post([FromBody] TodoTask item) { if (item == null) { return(BadRequest()); } _context.TasksTable.Add(item); _context.SaveChanges(); return(CreatedAtRoute("GetById", new { id = item.id }, item)); }
public bool Create(TasksManager entity) { try { context.TaskManager.Add(entity); context.SaveChanges(); return(true); } catch { return(false); } }
public void Delete_ShouldRemove_EntityFromDb() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: "Get_all_groups") .Options; using (var context = new TasksDbContext(options)) { context.TaskGroups.AddRange(new TaskGroupEntity { Name = "TestGroup1", UserTasks = new List <UserTaskEntity> { new UserTaskEntity { Name = "TestTask" } } }); context.SaveChanges(); var groupsDbManger = new TaskGroupDbManager(context, _mapper); groupsDbManger.Remove(1); } using (var context = new TasksDbContext(options)) { Assert.That(context.TaskGroups.Count(), Is.EqualTo(0)); Assert.That(context.UserTasks.Count(), Is.EqualTo(0)); context.Database.EnsureDeleted(); } }
public void Get_ShouldReturn_AllGroupsFromDb() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: "Get_all_groups") .Options; using (var context = new TasksDbContext(options)) { context.TaskGroups.AddRange(new TaskGroupEntity { Name = "TestGroup1", UserTasks = new List <UserTaskEntity> { new UserTaskEntity { Name = "TestTask" } } }, new TaskGroupEntity { Name = "TestGroup2" }); context.SaveChanges(); } using (var context = new TasksDbContext(options)) { var taskGroupManager = new TaskGroupDbManager(context, _mapper); var result = taskGroupManager.Get(); Assert.That(result.Count, Is.EqualTo(2)); Assert.That(result.Exists(group => group.Name == "TestGroup1" && group.Id == 1 && group.UserTasks.Count() == 1), Is.True); Assert.That(result.Exists(group => group.Name == "TestGroup2" && group.Id == 2 && !group.UserTasks.Any()), Is.True); context.Database.EnsureDeleted(); } }
public IActionResult MarkComplete(int id) { Tasks found = _context.Task.Find(id); if (found != null) { //change the things (we're changing the completed status) found.Completed = !found.Completed; //modify the state of this entry in the database _context.Entry(found).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.Update(found); _context.SaveChanges(); } return(RedirectToAction("TasksIndex", new { id = found.Id })); }
public UserRoleGetDTO Delete(int id) { var existing = context .UserRoles .FirstOrDefault(urole => urole.Id == id); if (existing == null) { return(null); } context.UserRoles.Remove(existing); context.SaveChanges(); return(UserRoleGetDTO.FromUserRole(existing)); }
public void GetById_ShouldReturn_CorrectGroupFromDb() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: "Get_group_by_id") .Options; using (var context = new TasksDbContext(options)) { context.TaskGroups.AddRange(new TaskGroupEntity { Name = "TestGroup1", UserTasks = new List <UserTaskEntity> { new UserTaskEntity { Name = "TestTask" } } }, new TaskGroupEntity { Name = "TestGroup2" }); context.SaveChanges(); } using (var context = new TasksDbContext(options)) { var taskGroupManager = new TaskGroupDbManager(context, _mapper); var result = taskGroupManager.Get(1); Assert.That(result.Id, Is.EqualTo(1)); Assert.That(result.Name, Is.EqualTo("TestGroup1")); Assert.That(result.UserTasks.ToList().Exists(t => t.Name == "TestTask"), Is.True); context.Database.EnsureDeleted(); } }
public void Create_ShouldAdd_NewTaskGroupEntityToDb() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: "Create_test") .Options; var expected = new TaskGroupEntity { Name = "TestGroup" }; using (var context = new TasksDbContext(options)) { context.TaskGroups.Add(expected); context.SaveChanges(); } using (var context = new TasksDbContext(options)) { Assert.That(context.TaskGroups.Count(), Is.EqualTo(1)); var groupFromDb = context.TaskGroups.First(); Assert.That(groupFromDb.Id, Is.Not.EqualTo(0)); Assert.That(groupFromDb.Name, Is.EqualTo(expected.Name)); context.Database.EnsureDeleted(); } }
public void CreateShouldReturnNullIfValidUserGetModel() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(CreateShouldReturnNullIfValidUserGetModel)) .Options; using (var context = new TasksDbContext(options)) { var validator = new RegisterValidator(); var usersService = new UsersService(context, validator, null, config); UserRole addUserRoleRegular = new UserRole { Name = "Regular", Description = "Creat pentru testare" }; context.UserRole.Add(addUserRoleRegular); context.SaveChanges(); var added1 = new TaskAgendaProj.ViewModels.RegisterPostModel { FirstName = "firstName1", LastName = "firstName1", UserName = "******", Email = "*****@*****.**", Password = "******" }; var userCreated = usersService.Create(added1); Assert.IsNull(userCreated); } }
public int Post([FromForm] Task task) { _dbContext.Tasks.Add(task); var taskId = _dbContext.SaveChanges(); return(taskId); }
public ActionResult DeleteConfirmed(int id) { TaskEntity executedTask = db.Tasks.Find(id); db.Tasks.Remove(executedTask); db.SaveChanges(); return(RedirectToAction("Index")); }
public IActionResult Delete(Task oldTask) { using (var db = new TasksDbContext()) { db.Tasks.Remove(oldTask); db.SaveChanges(); return(RedirectToAction("Index")); } }
public IActionResult Edit(Task newTask) { using (var db = new TasksDbContext()) { db.Tasks.Update(newTask); db.SaveChanges(); return(RedirectToAction("Index")); } }
public void GetUserRoleNameByIdShouldReturnUserRoleName() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnUserRoleName)) .Options; using (var context = new TasksDbContext(options)) { var userUserRolesService = new UserUserRolesService(null, context); User userToAdd = new User { Email = "*****@*****.**", LastName = "Marcus", FirstName = "Ana", Password = "******", RegistrationDate = DateTime.Now, UserUserRoles = new List <UserUserRole>() }; context.Users.Add(userToAdd); UserRole addUserRole = new UserRole { Name = "Newcomer", Description = "A new guy..." }; context.UserRoles.Add(addUserRole); context.SaveChanges(); context.UserUserRoles.Add(new UserUserRole { User = userToAdd, UserRole = addUserRole, StartTime = DateTime.Now, EndTime = null }); context.SaveChanges(); string userRoleName = userUserRolesService.GetUserRoleNameById(userToAdd.Id); Assert.AreEqual("Newcomer", userRoleName); Assert.AreEqual("Ana", userToAdd.FirstName); } }
public void GetByIdShouldReturnUserRole() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetByIdShouldReturnUserRole)) .Options; using (var context = new TasksDbContext(options)) { var userUserRolesService = new UserUserRoleService(null, context); User userToAdd = new User { Email = "*****@*****.**", LastName = "Ion", FirstName = "POpescu", Password = "******", DateAdded = DateTime.Now, UserUserRole = new List <UserUserRole>() }; context.Users.Add(userToAdd); UserRole addUserRole = new UserRole { Name = "Rol testare", Description = "Creat pentru testare" }; context.UserRole.Add(addUserRole); context.SaveChanges(); context.UserUserRole.Add(new UserUserRole { User = userToAdd, UserRole = addUserRole, StartTime = DateTime.Now, EndTime = null }); context.SaveChanges(); var userUserRoleGetModels = userUserRolesService.GetHistoryRoleById(1); Assert.IsNotNull(userUserRoleGetModels.FirstOrDefaultAsync(uur => uur.EndTime == null)); } }
public void GetUserRoleNameByIdShouldReturnUserRoleName() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(GetUserRoleNameByIdShouldReturnUserRoleName)) .Options; using (var context = new TasksDbContext(options)) { var userUserRolesService = new UserUserRoleService(null, context); User userToAdd = new User { Email = "*****@*****.**", LastName = "Ion", FirstName = "POpescu", Password = "******", DateAdded = DateTime.Now, UserUserRole = new List <UserUserRole>() }; context.Users.Add(userToAdd); UserRole addUserRole = new UserRole { Name = "Regular", Description = "Creat pentru testare" }; context.UserRole.Add(addUserRole); context.SaveChanges(); context.UserUserRole.Add(new UserUserRole { User = userToAdd, UserRole = addUserRole, StartTime = DateTime.Parse("2019-06-13T00:00:00"), EndTime = null }); context.SaveChanges(); string userRoleName = userUserRolesService.GetUserRoleNameById(1); Assert.AreEqual("Regular", userRoleName); } }
public LogInGetDTO Register(RegisterPostDTO registerInfo) { User existing = context.Users.FirstOrDefault(u => u.Username == registerInfo.Username); if (existing != null) { return(null); } context.Users.Add(new User { Email = registerInfo.Email, LastName = registerInfo.LastName, FirstName = registerInfo.FirstName, Password = ComputeSha256Hash(registerInfo.Password), Username = registerInfo.Username, UserRole = UserRole.Regular }); context.SaveChanges(); return(Authenticate(registerInfo.Username, registerInfo.Password)); }