private Client CreateModel(ClientBindingModel model, Client client, SchoolDataBase schoolDataBase)
        {
            if (model.Id.HasValue)
            {
                client.User.Login      = model.Login;
                client.User.Password   = model.Password;
                client.User.Name       = model.ClientName;
                client.User.Patronymic = model.ClientPatronymic;
                client.User.Surname    = model.ClientSurname;
                client.User.DateBirth  = model.DateBirth;
                schoolDataBase.SaveChanges();
                return(client);
            }
            User user = new User
            {
                Name       = model.ClientName,
                Surname    = model.ClientSurname,
                Patronymic = model.ClientPatronymic,
                DateBirth  = model.DateBirth,
                Login      = model.Login,
                Password   = model.Password,
            };

            schoolDataBase.Users.Add(user);
            schoolDataBase.SaveChanges();
            client.UserId = user.Id;
            return(client);
        }
        public void Initialize()
        {
            var options = new DbContextOptionsBuilder <SchoolDataBase>()
                          .UseInMemoryDatabase("Server=(localdb)\\mssqllocaldb;Database=StudentRepositoryTests;Trusted_Connection=True;ConnectRetryCount=0")
                          .Options;

            _context = new SchoolDataBase(options);

            students = new List <Student>()
            {
                new Student()
                {
                    Name = "Victor", Surname = "Dyshkant", Age = 19
                },
                new Student()
                {
                    Name = "Alla", Surname = "Dyshkant", Age = 18
                },
                new Student()
                {
                    Name = "Oleg", Surname = "Dyshkant", Age = 45
                }
            };


            _context.Students.AddRange(students);
            _context.SaveChanges();
            studentRepository = new StudentRepository(_context);
        }
        public void Update(PaymentBindingModel model)
        {
            using (var context = new SchoolDataBase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        var payment = context.Payments.FirstOrDefault(rec => rec.Id == model.Id);

                        if (payment == null)
                        {
                            throw new Exception("Кружок не найден");
                        }

                        CreateModel(model, payment);
                        context.SaveChanges();

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
        public void Initialize()
        {
            var options = new DbContextOptionsBuilder <SchoolDataBase>()
                          .UseInMemoryDatabase("Server=(localdb)\\mssqllocaldb;Database=GroupRepositoryTests;Trusted_Connection=True;ConnectRetryCount=0")
                          .Options;

            _context = new SchoolDataBase(options);

            groups = new List <Group>()
            {
                new Group()
                {
                    Name = "SE-121", Capacity = 20
                },
                new Group()
                {
                    Name = "SE-221", Capacity = 25
                },
                new Group()
                {
                    Name = "SE-321", Capacity = 30
                },
                new Group()
                {
                    Name = "SE-421", Capacity = 18
                }
            };


            _context.Groups.AddRange(groups);
            _context.SaveChanges();
        }
Beispiel #5
0
        public void Initialization()
        {
            mapper = new MapperConfiguration(conf =>
            {
                conf.AddProfile(new Mapping());
            }).CreateMapper();

            studentDTOs = new List <StudentDTO>()
            {
                new StudentDTO()
                {
                    Id = 1, Name = "Victor", Surname = "Dyshkant", Age = 19
                },
                new StudentDTO()
                {
                    Id = 2, Name = "Alla", Surname = "Dyshkant", Age = 17
                },
                new StudentDTO()
                {
                    Id = 3, Name = "Oleg", Surname = "Dyshkant", Age = 45
                }
            };

            var options = new DbContextOptionsBuilder <SchoolDataBase>()
                          .UseInMemoryDatabase("Server=(localdb)\\mssqllocaldb;Database=StudentServiceIntegrationTest;Trusted_Connection=True;ConnectRetryCount=0")
                          .Options;

            SchoolDataBase context = new SchoolDataBase(options);

            context.Students.AddRange(mapper.Map <IEnumerable <Student> >(studentDTOs));
            context.SaveChanges();
            EFUnitOfWork unit = new EFUnitOfWork(context);

            studentService = new StudentService(unit, mapper);
        }
 public void Insert(ClientBindingModel model)
 {
     using (var schoolDataBase = new SchoolDataBase())
     {
         schoolDataBase.Clients.Add(CreateModel(model, new Client(), schoolDataBase));
         schoolDataBase.SaveChanges();
     }
 }
Beispiel #7
0
        public void Initialization()
        {
            mapper = new MapperConfiguration(con =>
            {
                con.AddProfiles(new Profile[] { new Mapping() });
            }).CreateMapper();

            groupDTOs = new List <GroupDTO>()
            {
                new GroupDTO()
                {
                    Id = 1, Name = "SE-121", Capacity = 20
                },
                new GroupDTO()
                {
                    Id = 2, Name = "SE-221", Capacity = 25
                },
                new GroupDTO()
                {
                    Id = 3, Name = "SE-321", Capacity = 30
                },
                new GroupDTO()
                {
                    Id = 4, Name = "SE-421", Capacity = 18
                }
            };
            studentDTOs = new List <StudentDTO>()
            {
                new StudentDTO()
                {
                    Id = 1, Name = "Victor", Surname = "Dyshkant", Age = 19
                },
                new StudentDTO()
                {
                    Id = 2, Name = "Alla", Surname = "Dyshkant", Age = 17
                },
                new StudentDTO()
                {
                    Id = 3, Name = "Oleg", Surname = "Dyshkant", Age = 45
                }
            };

            var options = new DbContextOptionsBuilder <SchoolDataBase>()
                          .UseInMemoryDatabase("Server=(localdb)\\mssqllocaldb;Database=GroupServiceIntegrationTest;Trusted_Connection=True;ConnectRetryCount=0")
                          .Options;

            context = new SchoolDataBase(options);
            context.Groups.AddRange(mapper.Map <IEnumerable <Group> >(groupDTOs));
            context.Students.AddRange(mapper.Map <IEnumerable <Student> >(studentDTOs));
            context.SaveChanges();

            EFUnitOfWork unit = new EFUnitOfWork(context);

            groupService = new GroupService(unit, mapper, NSubstitute.Substitute.For <IEventPublisher>());
        }
        private Society CreateModel(SocietyBindingModel model, Society society, SchoolDataBase context)
        {
            society.SocietyName = model.SocietyName;
            society.AgeLimit    = model.AgeLimit;
            society.Sum         = model.Sum;
            society.ClientId    = model.ClientId;
            if (society.Id == 0)
            {
                society.DateCreate = DateTime.Now;
                context.Societies.Add(society);
                context.SaveChanges();
            }

            if (model.Id.HasValue)
            {
                var lessons = context.SocietyLessons
                              .Where(rec => rec.SocietyId == model.Id.Value)
                              .ToList();

                context.SocietyLessons.RemoveRange(lessons
                                                   .Where(rec => !model.Lessons.Contains(rec.LessonId))
                                                   .ToList());
                context.SaveChanges();

                foreach (var updateLesson in lessons)
                {
                    model.Lessons.Remove(updateLesson.LessonId);
                }
            }

            foreach (var lesson in model.Lessons)
            {
                context.SocietyLessons.Add(new SocietyLesson
                {
                    SocietyId = society.Id,
                    LessonId  = lesson
                });
                context.SaveChanges();
            }

            return(society);
        }
 public void Update(ClientBindingModel model)
 {
     using (var schoolDataBase = new SchoolDataBase())
     {
         var client = schoolDataBase.Clients.Include(rec => rec.User).FirstOrDefault(rec => rec.Id == model.Id);
         if (client == null)
         {
             throw new Exception("Клиент не найден");
         }
         CreateModel(model, client, schoolDataBase);
         schoolDataBase.SaveChanges();
     }
 }
        public void Delete(PaymentBindingModel model)
        {
            using (var context = new SchoolDataBase())
            {
                var payment = context.Payments.FirstOrDefault(rec => rec.Id == model.Id);

                if (payment == null)
                {
                    throw new Exception("Кружок не найден");
                }

                context.Payments.Remove(payment);
                context.SaveChanges();
            }
        }
        public void Delete(SocietyBindingModel model)
        {
            using (var context = new SchoolDataBase())
            {
                var society = context.Societies.FirstOrDefault(rec => rec.Id == model.Id);

                if (society == null)
                {
                    throw new Exception("Кружок не найден");
                }

                context.Societies.Remove(society);
                context.SaveChanges();
            }
        }
        public void Insert(SocietyBindingModel model)
        {
            using (var context = new SchoolDataBase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        CreateModel(model, new Society(), context);
                        context.SaveChanges();

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
        public void Insert(PaymentBindingModel model)
        {
            using (var context = new SchoolDataBase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        model.PaymentDate = DateTime.Now;
                        context.Payments.Add(CreateModel(model, new Payment()));
                        context.SaveChanges();

                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }