示例#1
0
        private async Task AddSampleSubscriptions(TestingContext testingContext, CranUser user)
        {
            ApplicationDbContext dbContext = testingContext.GetSimple <ApplicationDbContext>();
            IPrincipal           principal = testingContext.GetSimple <IPrincipal>();

            if (user == null)
            {
                user = await dbContext.CranUsers.FirstOrDefaultAsync(x => x.UserId == principal.Identity.Name);
            }


            dbContext.Notifications.Add(new NotificationSubscription()
            {
                Active   = true,
                Endpoint = "testendpoint",
                User     = user,
            });


            dbContext.Notifications.Add(new NotificationSubscription()
            {
                Active   = false,
                Endpoint = "testendpoint",
                User     = user,
            });

            await dbContext.SaveChangesAsync();
        }
示例#2
0
        public async Task <VotesDto> VoteAsync(VotesDto vote)
        {
            string userId = _securityService.GetUserId();
            Rating rating = _dbContext.Ratings
                            .Where(x => x.User.UserId == userId && x.Question.Id == vote.IdQuestion)
                            .SingleOrDefault();

            if (rating == null)
            {
                Question question = await _dbContext.FindAsync <Question>(vote.IdQuestion);

                CranUser user = await _userService.GetOrCreateCranUserAsync();

                rating = new Rating
                {
                    Question       = question,
                    User           = user,
                    QuestionRating = 0,
                };
                _dbContext.Ratings.Add(rating);
            }

            int r = vote.MyVote > 0 ? 1 : (vote.MyVote < 0 ? -1 : 0);

            rating.QuestionRating = r;
            await _dbContext.SaveChangesAsync();

            vote = await GetVoteAsync(vote.IdQuestion);

            return(vote);
        }
示例#3
0
 private UserInfoDto ToProfileDto(CranUser user)
 {
     return(new UserInfoDto
     {
         Name = user.UserId,
         IsAnonymous = user.IsAnonymous,
     });
 }
示例#4
0
        public async Task <bool> HasWriteAccess(int idUser)
        {
            CranUser cranUser = await _dbContext.FindAsync <CranUser>(idUser);

            //Security Check
            if (cranUser.UserId == _securityService.GetUserId() || _securityService.IsInRole(Roles.Admin))
            {
                return(true);
            }
            return(false);
        }
示例#5
0
        protected async Task <bool> HasWriteAccess(int idUser)
        {
            CranUser cranUser = await _context.FindAsync <CranUser>(idUser);

            //Security Check
            if (cranUser.UserId == GetUserId() || _currentPrincipal.IsInRole(Roles.Admin))
            {
                return(true);
            }
            return(false);
        }
示例#6
0
        public async Task RemoveCoureFromFavoritesAsync(CourseToFavoritesDto dto)
        {
            CranUser cranUser = await this.GetCranUserAsync();

            RelUserCourseFavorite rel = await _context.RelUserCourseFavorites.Where(x => x.User.Id == cranUser.Id && x.Course.Id == dto.CourseId)
                                        .FirstOrDefaultAsync();

            if (rel != null)
            {
                _context.Remove(rel);
                await this.SaveChangesAsync();
            }
        }
示例#7
0
        public async Task CreateUserAsync(UserInfoDto info)
        {
            CranUser user = await _context.CranUsers.Where(x => x.UserId == info.Name).SingleOrDefaultAsync();

            if (user == null)
            {
                user = new CranUser
                {
                    UserId      = info.Name,
                    IsAnonymous = info.IsAnonymous,
                };
                _context.CranUsers.Add(user);
                await SaveChangesAsync();
            }
        }
示例#8
0
        public async Task <int> AddCommentAsync(CommentDto vm)
        {
            CranUser cranUser = await _userService.GetOrCreateCranUserAsync();

            Question question = await _dbContext.FindAsync <Question>(vm.IdQuestion);

            Comment comment = new Comment()
            {
                Question    = question,
                User        = cranUser,
                CommentText = vm.CommentText,
            };

            _dbContext.Comments.Add(comment);
            await this._dbContext.SaveChangesAsync();

            return(comment.Id);
        }
示例#9
0
        public async Task <CranUser> GetOrCreateCranUserAsync()
        {
            string   userId         = _securityService.GetUserId();
            CranUser cranUserEntity = await _dbContext.CranUsers.Where(x => x.UserId == userId).SingleOrDefaultAsync();

            if (cranUserEntity == null)
            {
                cranUserEntity = new CranUser
                {
                    UserId      = userId,
                    IsAnonymous = true,
                };
                await _dbContext.AddAsync(cranUserEntity);
            }
            await _dbContext.SaveChangesAsync();

            return(cranUserEntity);
        }
示例#10
0
        protected async Task <CranUser> GetCranUserAsync()
        {
            string   userId         = GetUserId();
            CranUser cranUserEntity = await _context.CranUsers.Where(x => x.UserId == userId).SingleOrDefaultAsync();

            if (cranUserEntity == null)
            {
                cranUserEntity = new CranUser
                {
                    UserId      = userId,
                    IsAnonymous = true,
                };
                await _context.AddAsync(cranUserEntity);
            }
            await SaveChangesAsync();

            return(cranUserEntity);
        }
示例#11
0
        public async Task AddCourseToFavoritesAsync(CourseToFavoritesDto dto)
        {
            CranUser cranUser = await this.GetCranUserAsync();

            Course course = await _context.Courses.FindAsync(dto.CourseId);

            bool exists = await _context.RelUserCourseFavorites.AnyAsync(x => x.User.Id == cranUser.Id && x.Course.Id == course.Id);

            if (!exists)
            {
                RelUserCourseFavorite relUserCourseFavorite = new RelUserCourseFavorite()
                {
                    User   = cranUser,
                    Course = course,
                };
                _context.RelUserCourseFavorites.Add(relUserCourseFavorite);
                await SaveChangesAsync();
            }
        }
示例#12
0
        public async Task <CourseInstanceDto> StartCourseAsync(int courseId)
        {
            Course courseEntity = await _context.FindAsync <Course>(courseId);

            CranUser cranUserEntity = await GetCranUserAsync();

            await _dbLogService.LogMessageAsync($"Starting course {courseId}. Name: {courseEntity.Title}");

            CourseInstance courseInstanceEntity = new CourseInstance
            {
                User     = cranUserEntity,
                Course   = courseEntity,
                IdCourse = courseId,
            };

            await _context.AddAsync(courseInstanceEntity);

            await SaveChangesAsync();

            CourseInstanceDto result = await GetNextQuestion(courseInstanceEntity);

            return(result);
        }
示例#13
0
        public async Task <UserInfoDto> GetUserInfoAsync()
        {
            CranUser user = await GetCranUserAsync();

            return(ToProfileDto(user));
        }
示例#14
0
        public async Task <UserInfoDto> GetUserInfoAsync()
        {
            CranUser user = await _userService.GetOrCreateCranUserAsync();

            return(ToProfileDto(user));
        }
示例#15
0
        public void SetUpInMemoryDb(ApplicationDbContext context)
        {
            context.Database.EnsureDeleted();

            //Tags
            IList <Tag> tags = new List <Tag>();

            for (int i = 1; i <= 4; i++)
            {
                Tag tag = new Tag()
                {
                    Name        = $"Name{i}",
                    ShortDescDe = $"ShortDescDe{i}",
                    ShortDescEn = $"ShortDescEn{i}",
                    Description = $"Description{i}",
                    TagType     = TagType.Standard,
                };
                context.Tags.Add(tag);
                tags.Add(tag);
            }
            ;

            //add a non standard Tag
            Tag nonStandard = new Tag()
            {
                Name        = $"Deprecated",
                ShortDescDe = $"Veraltet",
                ShortDescEn = $"Deprecated",
                Description = $"Deprecated",
                TagType     = TagType.Warning,
            };

            context.Tags.Add(nonStandard);
            tags.Add(nonStandard);

            context.SaveChanges();

            //Questions
            for (int i = 1; i <= 10; i++)
            {
                CreateMockQuestion(context, i, tags);
            }

            //Courses
            Course course = new Course()
            {
                Description       = $"Description",
                Title             = $"Title",
                NumQuestionsToAsk = 3,
                Language          = Language.De,
            };

            context.Courses.Add(course);
            RelCourseTag relCourseTag = new RelCourseTag()
            {
                Course = course,
                Tag    = tags.First(),
            };

            context.RelCourseTags.Add(relCourseTag);

            //User
            CranUser cranUser = new CranUser()
            {
                IsAnonymous = false,
                UserId      = "testuser",
            };

            context.CranUsers.Add(cranUser);

            context.SaveChanges();
        }