public async Task <long> Handle(AddGroupCommand request, CancellationToken cancellationToken)
        {
            var userId = contextProvider.GetUserId();
            var user   = await dbContext.Users.Include(u => u.Groups).SingleOrDefaultAsync(u => u.Id == userId);

            var newGroup = new Domain.Group
            {
                Name              = request.Name,
                GroupLanguage1    = request.LanguageFront,
                GroupLanguage2    = request.LanguageBack,
                User              = user,
                GroupCreationDate = dateTimeProvider.GetTime()
            };

            foreach (var card in request.Cards)
            {
                newGroup.Words.Add(
                    new Domain.Card
                {
                    Heads            = Domain.Side.New(card.Front.Value, card.Front.Example),
                    Tails            = Domain.Side.New(card.Back.Value, card.Back.Example),
                    Comment          = string.Empty,
                    Group            = newGroup,
                    IsVisible        = true,
                    WordCreationDate = dateTimeProvider.GetTime(),
                });
            }

            await dbContext.Groups.AddAsync(newGroup);

            await dbContext.SaveChangesAsync();

            return(newGroup.Id);
        }
Exemplo n.º 2
0
        public int Update(Domain.Group group)
        {
            string sql = @"UPDATE Groups SET Name=@Name, OfficeNo=@OfficeNo, XapiName=@XapiName, XapiPassword=@XapiPassword, XapiAddress=@XapiAddress, XapiPort = @XapiPort  IsPublic=@IsPublic,Description=@Description WHERE id=@id";

            using (var dbOperator = new DbOperator(Provider, ConnectionString))
            {
                dbOperator.AddParameter("Name", group.Name);
                dbOperator.AddParameter("OfficeNo", group.OfficeNo);
                dbOperator.AddParameter("XapiName", group.XapiName);
                dbOperator.AddParameter("XapiPassword", group.XapiPassword);
                dbOperator.AddParameter("XapiAddress", group.XapiAddress.Address);
                dbOperator.AddParameter("XapiPort", group.XapiAddress.Port);
                dbOperator.AddParameter("IsPublic", group.IsPublic);
                dbOperator.AddParameter("Description", group.Description);
                dbOperator.AddParameter("id", group.Id);
                return(dbOperator.ExecuteNonQuery(sql));
            }
        }
        public async Task OneToManyStudentGroupTestAsync()
        {
            var options = new DbContextOptionsBuilder <OlpContext>()
                          .UseInMemoryDatabase(databaseName: "UserGroupTest")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new OlpContext(options))
            {
                Domain.Group group = new Domain.Group()
                {
                    GroupId     = 1,
                    Name        = "PI15B",
                    Description = "make viko great again"
                };

                var groupService = new GroupService(context);
                await groupService.Create(group);

                Domain.Group fecthedGroup = await groupService.FindBy(x => x.Name == group.Name);

                Assert.AreEqual(fecthedGroup.Name, group.Name);
                Console.Write("Group  : " + group.Name + " successfully added");

                //user insert
                Domain.Student student = new Domain.Student()
                {
                    GroupId   = 1,
                    FirstName = "Daniel",
                    LastName  = "Zeimo",
                    Email     = "*****@*****.**"
                };

                var studentService = new StudentService(context);
                await studentService.Create(student);

                Domain.Student fetchedUser = await studentService.FindBy(x => x.FirstName == student.FirstName);

                Assert.AreEqual(fetchedUser.FirstName, student.FirstName);
                Assert.AreEqual(group.Name, fetchedUser.Group.Name);
                Console.Write("|User  : "******" successfully added");
                Console.Write("|User group : " + fetchedUser.Group.Name + " Description" + fetchedUser.Group.Description);
            }
        }
Exemplo n.º 4
0
        public async Task GroupAddTestAsync()
        {
            var options = new DbContextOptionsBuilder <OlpContext>()
                          .UseInMemoryDatabase(databaseName: "Add_writes_to_database_ADD")
                          .Options;

            // Run the test against one instance of the context
            using (var context = new OlpContext(options))
            {
                Domain.Group group = new Domain.Group()
                {
                    Name        = "Test1",
                    Description = "test desc1"
                };

                var service = new GroupService(context);
                await service.Create(group);

                Domain.Group fecthedGroup = await service.FindBy(x => x.Name == group.Name);

                Assert.AreEqual(fecthedGroup.Name, group.Name);
                Console.Write("Group was : " + group.Name + " successfully added");
            }
        }
        public async Task Init()
        {
            logger.LogInformation("Beginnin init");
            await dbContext.Database.EnsureCreatedAsync();

            logger.LogInformation("finish init");

            if (!options.Mocks)
            {
                return;
            }

            if (await dbContext.Users.AnyAsync(u => u.Name.Equals("testUser")))
            {
                return;
            }

            var random = new Random();

            var beginningDate = timeProvider.GetTime().AddDays(-7);

            Domain.User user = new Domain.User
            {
                Name          = "testUser",
                CreationDate  = timeProvider.GetTime().AddDays(-7),
                LastLoginDate = timeProvider.GetTime().AddDays(-1),
                Password      = encrypter.Md5Hash("testPassword"),
            };

            for (var i = 0; i < 6; i++)
            {
                var newLesson = new Lesson
                {
                    StartDate  = timeProvider.GetTime().AddDays(-i),
                    FinishDate = timeProvider.GetTime().AddDays(-i).AddMinutes(random.Next(20, 30)),
                    User       = user
                };
                user.Lessons.Add(newLesson);
            }

            for (var i = 0; i < 7; i++)
            {
                var newGroup = new Domain.Group
                {
                    GroupCreationDate = timeProvider.GetTime().AddDays(-i - 1),
                    GroupLanguage1    = 1,
                    GroupLanguage2    = 2,
                    Name = $"Test group {i + 1}",
                    User = user
                };
                user.Groups.Add(newGroup);

                for (var j = 0; j < random.Next(15, 20); j++)
                {
                    var newCard = new Domain.Card
                    {
                        WordCreationDate = timeProvider.GetTime().AddDays(-i - 1),
                        Comment          = "Test comment to word",
                        Group            = newGroup,
                        IsVisible        = true,
                        Heads            = new Side
                        {
                            Value   = $"Word {j + 1}",
                            Example = $"Simple sentance where Word {j + 1} is used.",
                            State   = new State
                            {
                                Drawer     = Drawer.Create(random.Next(0, 4)),
                                NextRepeat = timeProvider.GetTime().AddDays(random.Next(-4, 4))
                            }
                        },
                        Tails = new Side
                        {
                            Value   = $"Słowo {j + 1}",
                            Example = $"Proste zdanie gdzie słowo {j + 1} jest użyte.",
                            State   = new State
                            {
                                Drawer     = Drawer.Create(random.Next(0, 4)),
                                NextRepeat = timeProvider.GetTime().AddDays(random.Next(-4, 4))
                            }
                        },
                    };

                    newGroup.Words.Add(newCard);

                    for (var k = 0; k < random.Next(1, 7); k++)
                    {
                        var lesson    = user.Lessons[random.Next(user.Lessons.Count - 1)];
                        var newRepeat = new Repeat
                        {
                            Lesson       = lesson,
                            DateTime     = newCard.WordCreationDate.AddDays(random.Next(-7, 0)),
                            Result       = random.Next(-1, 1),
                            QuestionSide = random.Next(0, 1) > 0 ? QuestionSideEnum.Heads : QuestionSideEnum.Tails,
                            Word         = newCard
                        };
                        lesson.Repeats.Add(newRepeat);
                        newCard.Repeats.Add(newRepeat);
                    }
                }
            }

            dbContext.Users.Add(user);
            await dbContext.SaveChangesAsync();
        }