public async Task <IEnumerable <Course> > Join(int courseId, string token)
        {
            var course = await _context.Courses
                         .Include(x => x.Tokens)
                         .SingleOrDefaultAsync(x => x.Id == courseId);

            if (course == null)
            {
                throw new UserException("CourseId and/or token wrong.", 404);
            }

            if (course.Tokens.Any(x => x.Active && x.Token == token))
            {
                if (course.Members == null)
                {
                    course.Members = new List <CourseMember>();
                }

                course.Members.Add(new CourseMember {
                    Course = course, CourseId = course.Id, MemberId = GetUserId()
                });
                await _context.SaveChangesAsync();

                return(await Get());
            }

            throw new UserException("CourseId and/or token wrong.", 404);
        }
Esempio n. 2
0
        public async Task <PushTokens> SaveToken(string userId, string token, string deviceId)
        {
            ILAUser user = await _context.Users.FindAsync(userId);

            PushTokens pushToken = await _context.PushTokens.Where(x => x.DeviceId == deviceId).SingleOrDefaultAsync(x => x.User.Id == userId);

            if (pushToken == null)
            {
                pushToken = new PushTokens {
                    DeviceId = deviceId, User = user, Token = token
                };
                await _context.PushTokens.AddAsync(pushToken);
            }
            else
            {
                pushToken.Token = token;
                pushToken.User  = user;
                _context.PushTokens.Update(pushToken);
            }

            await _context.SaveChangesAsync();

            pushToken.User = null;

            return(pushToken);
        }
        public async Task <Pause> PostPause(int lectureId)
        {
            Lecture lecture = await _context.Lectures
                              .Include(x => x.Pauses)
                              .ThenInclude(x => x.User)
                              .FirstOrDefaultAsync(x => x.Id == lectureId);

            ILAUser user = await _context.Users.FindAsync(GetUserId());

            if (lecture == null)
            {
                throw new UserException("Lecture not found", 404);
            }

            if (user == null)
            {
                throw new UserException("Internal server Error", 500);
            }

            Pause pause = new Pause
            {
                Lecture   = lecture,
                TimeStamp = DateTime.Now.ToUniversalTime(),
                User      = user
            };

            if (lecture.Pauses.Any(x => x.User.Id == GetUserId() && (pause.TimeStamp - x.TimeStamp).TotalSeconds < 120))
            {
                throw new UserException("You can pause ery 120 seconds only.", 481);
            }

            await _context.Pauses.AddAsync(pause);

            await _context.SaveChangesAsync();

            pause.User    = null;
            pause.Lecture = null;

            await _hubContext.Clients.Group(lectureId.ToString()).SendAsync("Pause", pause);

            return(pause);
        }
        public async Task <Question> PostQuestion(int lectureId, [FromBody] QuestionCreate model)
        {
            Lecture lecture = await _context.Lectures
                              .Where(x => x.Id == lectureId)
                              .Where(x => x.Course.Members.Any(y => y.MemberId == GetUserId()))
                              .Include(x => x.Questions)
                              .ThenInclude(x => x.Answers)
                              .SingleOrDefaultAsync();

            ILAUser user = await _context.Users.FindAsync(GetUserId());

            Question question = new Question
            {
                PointedQuestion = model.PointedQuestion,
                Lecture         = lecture,
                User            = user
            };

            await _context.Questions.AddAsync(question);

            await _context.SaveChangesAsync();

            question.User    = null;
            question.Lecture = null;
            return(question);
        }
        public async Task <IActionResult> Create([Bind("Id,Title,Description,Archived")] Course course)
        {
            if (ModelState.IsValid)
            {
                ILAUser user = await _context.Users.FindAsync(GetUserId());

                if (user == null)
                {
                    return(NotFound());
                }

                course.Owner = user;

                _context.Add(course);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(course));
        }
Esempio n. 6
0
        public async Task <IActionResult> Create([Bind("Id,Title,Description,Visible,Start,Stop,CourseId")]
                                                 Lecture lecture)
        {
            var course =
                await _context.Courses.FirstOrDefaultAsync(x => x.Id == lecture.CourseId && x.Owner.Id == GetUserId());

            if (course == null)
            {
                return(Forbid());
            }

            if (ModelState.IsValid)
            {
                _context.Add(lecture);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(lecture));
        }