public Employee GetEmployeeByLoginPassword(string login, string password)
        {
            string hashedPassword = this.HashPassword(password);

            Employee foundEmployee = null;
            string   role;

            using (MachineDbContext context = new MachineDbContext())
            {
                foundEmployee = context.Employees
                                .Include("Role")
                                .FirstOrDefault(x => x.login.Equals(login));
                role = foundEmployee.Role.name;
                if (foundEmployee is null)
                {
                    throw new Exception("Пользователя с таким логином не существует");
                }
            }

            bool isEqual = foundEmployee.password.ToUpper().Equals(hashedPassword.ToUpper());

            if (!isEqual)
            {
                throw new Exception("Неверный пароль");
            }

            return(foundEmployee);
        }
        public void CreateNewUser(Role role,
                                  Company company,
                                  string first_name,
                                  string second_name,
                                  string patronymic,
                                  string login,
                                  string password)
        {
            string hashedPassword = this.HashPassword(password);

            Employee foundUser = null;

            using (MachineDbContext context = new MachineDbContext())
            {
                foundUser = context.Employees.FirstOrDefault(x => x.login.Equals(login));
                if (!(foundUser is null))
                {
                    throw new Exception("Пользователь с таким логином существует");
                }

                Employee newUser = new Employee()
                {
                    Role        = role,
                    Company     = company,
                    first_name  = first_name,
                    second_name = second_name,
                    patronymic  = patronymic,
                    login       = login,
                    password    = password
                };

                context.Employees.Add(newUser);
                context.SaveChanges();
            }
        }
        public bool Authorize(string login, string password)
        {
            string hashedPassword = this.HashPassword(password);

            Employee foundUser = null;
            string   role;

            using (MachineDbContext context = new MachineDbContext())
            {
                foundUser = context.Employees.FirstOrDefault(x => x.login.Equals(login));
                role      = foundUser.Role.name;
                if (foundUser is null)
                {
                    throw new Exception("Пользователя с таким логином не существует");
                }
            }

            bool isEqual = foundUser.password.ToUpper().Equals(hashedPassword.ToUpper());

            if (!isEqual)
            {
                throw new Exception("Неверный пароль");
            }

            ApplicationData.Instance.Login = login;
            ApplicationData.Instance.Role  = role;
            return(true);
        }
Example #4
0
 // GET: Machines
 public ActionResult Add(Company company)
 {
     using (var db = new MachineDbContext())
     {
         db.Companies.Add(company);
         db.SaveChanges();
         return(Json(new { nane = "Alessandro" }));
     }
 }
Example #5
0
        public IEnumerable <Record> GetRecordsByCompany(int id_company)
        {
            List <Record> records;

            using (MachineDbContext context = new MachineDbContext())
            {
                records = context.Records.Where(x => x.id_company == id_company).ToList();
                //из всех записей мы отбираем только те, чьи ид равны заданному ид
            }

            return(records);
        }
Example #6
0
        public void AddRecord(Record r)
        {
            // List<Record> records;

            using (MachineDbContext context = new MachineDbContext())
            {
                if (!context.Records.Contains(r))
                {
                    context.Records.Add(r);
                    context.SaveChanges();
                }
            }
        }
Example #7
0
        public void FillDatabase(string[] args)
        {
            List <Record> records = new List <Record>();

            foreach (string line in args)
            {
                string[] lines  = line.Split(new[] { ';', ',' });
                Record   record = this.ParseRecordString(lines);
                records.Add(record);
            }

            using (MachineDbContext context = new MachineDbContext())
            {
                context.Records.AddRange(records);
                context.SaveChanges();
            }
        }
Example #8
0
        public void Test_CreateUser()
        {
            AutorizationService service = new AutorizationService();
            string login = "******";
            //string password = "******";

            Role adminRole = new Role()
            {
                name           = "admin",
                canEdit        = true,
                canEditCatalog = true,
                canGetData     = true,
                canEnter       = true,
            };

            using (MachineDbContext context = new MachineDbContext())
            {
                foreach (var employee in context.Employees)
                {
                    context.Employees.Remove(employee);
                }

                if (!(context.Roles.FirstOrDefault(x => x.name.Equals(adminRole.name)) is null))
                {
                    context.Roles.Add(adminRole);
                }

                context.SaveChanges();
            }

            //service.CreateNewUser(login, password, adminRole);

            using (MachineDbContext context = new MachineDbContext())
            {
                Assert.NotNull(context.Employees.FirstOrDefault(x => x.login.Equals(login)));
            }
        }
        public void Test_FillDatabase()
        {
            Random random = new Random();

            string[] args = new string[3];
            for (int i = 0; i < 3; i++)
            {
                string[] numbers = new string[176];
                for (int j = 0; j < numbers.Length; j++)
                {
                    numbers[j] = random.Next(1, 2).ToString();
                }
                args[i] = string.Join(",", numbers);
            }

            ImportService service = new ImportService();

            service.FillDatabase(args);

            using (MachineDbContext context = new MachineDbContext())
            {
                Assert.NotEmpty(context.Records);
            }
        }
        public async Task MachineRepositoryMethodAllTest()
        {
            //[6][1][0] DbContextOptions 개체 생성
            var options = new DbContextOptionsBuilder <MachineDbContext>()
                          .UseInMemoryDatabase(databaseName: "MachineApp").Options;

            //[6][1][1] AddAsync() Method Test
            using (var context = new MachineDbContext(options))
            //[6][1][1][1] DbContext 개체 생성 및 데이터 입력
            {
                //[?] Repository Object Creation
                var repository = new MachineRepository(context);
                var machine    = new Machine()
                {
                    Name = "[1] T7910", Created = DateTime.Now
                };
                await repository.AddMachineAsync(machine);

                await context.SaveChangesAsync(); //[!]
            }
            //[6][1][1][2] 제대로 입력되었는지 테스트
            using (var context = new MachineDbContext(options))
            {
                Assert.AreEqual(1, await context.Machines.CountAsync());
                var machine = await context.Machines.Where(m => m.Id == 1).SingleOrDefaultAsync();

                Assert.AreEqual("[1] T7910", machine.Name);
            }

            //[6][1][2] GetAllAsync() Method Test
            //[6][1][2][1] DbContext 개체 생성 및 추가 데이터 입력
            using (var context = new MachineDbContext(options))
            {
                await(new MachineRepository(context)).AddMachineAsync(
                    new Machine {
                    Name = "[2] Rugged Extreame", Created = DateTime.Now
                });
                context.Machines.Add(new Machine {
                    Name = "[3] Alienware Aurora R8", Created = DateTime.Now
                });
                await context.SaveChangesAsync(); //[!]
            }
            //[6][1][2][2] 제대로 출력되는지 테스트
            using (var context = new MachineDbContext(options))
            {
                var repository = new MachineRepository(context);
                var machines   = await repository.GetMachinesAsync(); //[?] 전체 레코드 가져오기

                Assert.AreEqual(3, machines.Count());                 // 현재까지 3개 테스트
            }

            //[6][1][3] GetByIdAsync() Method Test
            //[6][1][3][1] DbContext 개체 생성 및 추가 데이터 입력
            using (var context = new MachineDbContext(options))
            {
                await(new MachineRepository(context)).AddMachineAsync(
                    new Machine {
                    Name = "[4] Level 10 Limited Edtion", Created = DateTime.Now
                });
                await context.SaveChangesAsync(); //[!]
            }
            //[6][1][3][2] 제대로 출력되는지 테스트
            using (var context = new MachineDbContext(options))
            {
                var repository = new MachineRepository(context);
                var alienware  = await repository.GetMachineByIdAsync(3);

                Assert.IsTrue(alienware.Name.Contains("Alienware"));
                Assert.AreEqual("[3] Alienware Aurora R8", alienware.Name);
            }

            //[6][1][4] EditAsync() Method Test
            //[6][1][4][1] DbContext 개체 생성 및 추가 데이터 입력
            using (var context = new MachineDbContext(options))
            {
                await(new MachineRepository(context)).AddMachineAsync(
                    new Machine {
                    Name = "[5] Surface Pro", Created = DateTime.Now
                });
                await context.SaveChangesAsync(); //[!]
            }
            //[6][1][4][2] 제대로 수정되는지 테스트
            using (var context = new MachineDbContext(options))
            {
                var repository = new MachineRepository(context);
                var surface    = await repository.GetMachineByIdAsync(5);

                surface.Name = "[5] 서피스 프로";
                await repository.EditMachineAsync(surface);

                await context.SaveChangesAsync();

                var newSurface = await repository.GetMachineByIdAsync(5);

                Assert.AreEqual("[5] 서피스 프로", newSurface.Name);
            }

            //[6][1][5] DeleteAsync() Method Test
            //[6][1][5][1] DbContext 개체 생성 및 추가 데이터 입력
            using (var context = new MachineDbContext(options))
            {
                // Empty
            }

            //[6][1][5][2] 제대로 삭제되는지 테스트
            using (var context = new MachineDbContext(options))
            {
                var repository = new MachineRepository(context);
                await repository.DeleteMachineAsync(5);

                await context.SaveChangesAsync();

                Assert.AreEqual(4, await context.Machines.CountAsync());
                Assert.IsNull(await repository.GetMachineByIdAsync(5));
            }

            //[6][1][6] GetMachinesPageAsync() Method Test
            //[6][1][6][1] DbContext 개체 생성 및 추가 데이터 입력
            using (var context = new MachineDbContext(options))
            {
                // Empty
            }

            //[6][1][6][2] 2번째 페이지의 First 항목 체크
            using (var context = new MachineDbContext(options))
            {
                int pageIndex = 1;
                int pageSize  = 2;

                var repository = new MachineRepository(context);
                var machines   = await repository.GetMachinesPageAsync(pageIndex, pageSize); // 4, 3, [2, 1]

                Assert.AreEqual("[2] Rugged Extreame", machines.Records.FirstOrDefault().Name);
                Assert.AreEqual(4, machines.TotalRecords);
            }
        }