예제 #1
0
        public TEntity Insert(TEntity entity)
        {
            var newEntity = _dbSet.Add(entity);

            _context.SaveChanges();
            return(newEntity);
        }
예제 #2
0
        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);
        }
예제 #3
0
 public void Post([FromBody] Task task)
 {
     task.DateClosed = null;
     task.DateAdded  = DateTime.Now;
     context.Tasks.Add(task);
     context.SaveChanges();
 }
예제 #4
0
 public Task Create(Task task)
 {
     task.DateClosed = null;
     task.DateAdded  = DateTime.Now;
     context.Tasks.Add(task);
     context.SaveChanges();
     return(task);
 }
예제 #5
0
        public Task Create(TaskPostModel task)
        {
            Task toAdd = TaskPostModel.ToTask(task);

            context.Tasks.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
예제 #6
0
        public UserRoleGetModel Create(UserRolePostModel userRolePostModel)
        {
            UserRole toAdd = UserRolePostModel.ToUserRole(userRolePostModel);

            context.UserRole.Add(toAdd);
            context.SaveChanges();
            return(UserRoleGetModel.FromUserRole(toAdd));
        }
예제 #7
0
        public int Create(TaskGroupViewModel group)
        {
            var newGroup = _mapper.Map <TaskGroupEntity>(group);

            newGroup = _dbContext.TaskGroups.Add(newGroup).Entity;
            _dbContext.SaveChanges();

            return(newGroup.Id);
        }
예제 #8
0
        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);
        }
예제 #9
0
        public Taskk Create(TaskPostModel task, User addedBy)
        {
            Taskk toAdd = TaskPostModel.ToTask(task);

            toAdd.Owner = addedBy;
            context.Tasks.Add(toAdd);
            context.SaveChanges();
            return(toAdd);
        }
예제 #10
0
        public Comment Create(CommentPostDTO comment, User addedBy)
        {
            Comment commentAdd = CommentPostDTO.ToComment(comment);

            commentAdd.Owner = addedBy;
            context.Comments.Add(commentAdd);
            context.SaveChanges();
            return(commentAdd);
        }
예제 #11
0
        public int Create(UserTaskViewModel taskDetailsViewModel)
        {
            var newTask = _mapper.Map <UserTaskEntity>(taskDetailsViewModel);

            newTask = _dbContext.UserTasks.Add(newTask).Entity;

            _dbContext.SaveChanges();

            return(newTask.Id);
        }
예제 #12
0
 public IActionResult AddNewUser(UserInformation newUser)
 {
     if (ModelState.IsValid)
     {
         _context.UserInformation.Add(newUser);
         _context.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View());
 }
예제 #13
0
        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);
        }
예제 #14
0
        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));
        }
예제 #15
0
 public bool Create(TasksManager entity)
 {
     try
     {
         context.TaskManager.Add(entity);
         context.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
예제 #16
0
        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();
            }
        }
예제 #17
0
        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();
            }
        }
예제 #18
0
        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 }));
        }
예제 #19
0
        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));
        }
예제 #20
0
        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();
            }
        }
예제 #21
0
        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();
            }
        }
예제 #22
0
        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);
            }
        }
예제 #23
0
        public int Post([FromForm] Task task)
        {
            _dbContext.Tasks.Add(task);
            var taskId = _dbContext.SaveChanges();

            return(taskId);
        }
예제 #24
0
        public ActionResult DeleteConfirmed(int id)
        {
            TaskEntity executedTask = db.Tasks.Find(id);

            db.Tasks.Remove(executedTask);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
예제 #25
0
 public IActionResult Delete(Task oldTask)
 {
     using (var db = new TasksDbContext())
     {
         db.Tasks.Remove(oldTask);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
예제 #26
0
 public IActionResult Edit(Task newTask)
 {
     using (var db = new TasksDbContext())
     {
         db.Tasks.Update(newTask);
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
예제 #27
0
        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);
            }
        }
예제 #28
0
        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));
            }
        }
예제 #29
0
        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);
            }
        }
예제 #30
0
        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));
        }