Beispiel #1
0
        public async Task <Unit> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            var hashedPassword = encrypter.Md5Hash(request.Password);

            if (await dbContext.Users.AnyAsync(u => u.Name.Equals(request.UserName) && u.Password.Equals(hashedPassword)))
            {
                throw new ApiException("User already exists", ErrorCode.RegisterUserAlreadyExists);
            }
            var newUser = userCreator.Create(request.UserName, hashedPassword);
            await dbContext.Users.AddAsync(newUser);

            await dbContext.SaveChangesAsync();

            return(Unit.Value);
        }
Beispiel #2
0
        public async Task <string> Handle(LoginCommnad request, CancellationToken cancellationToken)
        {
            var hashedPassword = encrypter.Md5Hash(request.Password);
            var user           = await dbContext.Users.SingleOrDefaultAsync(u =>
                                                                            u.Name.Equals(request.UserName) &&
                                                                            u.Password.Equals(hashedPassword));

            if (user == null)
            {
                throw new ApiException("User is not found", ErrorCode.LoginUserNotFound);
            }
            user.LastLoginDate = dateTimeProvider.GetTime();

            dbContext.Users.Update(user);
            await dbContext.SaveChangesAsync();

            return(authenticationService.Authenticate(user.Id, new string[0]));
        }
        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();
        }