public async Task <Result <Unit> > Handle(CreateClassroomCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                 x.UserName == _userAccessor.GetCurrentUsername());

            // Create the classroom.
            var classroom = new Classroom {
                Id               = request.Id,
                Name             = request.Name,
                DiscussionsCount = 0,
                CreatedBy        = user
            };

            _context.Classrooms.Add(classroom);

            var student = new ApplicationUserClassroom(user.Id, classroom.Id, true, user, classroom);

            _context.ApplicationUserClassrooms.Add(student);

            var success = await _context.SaveChangesAsync() > 0;

            if (!success)
            {
                return(Result <Unit> .Failure("There was a problem saving changes."));
            }

            return(Result <Unit> .Success(Unit.Value));
        }
Ejemplo n.º 2
0
        // TODO: Need to check for expired tokens/invite links.
        // TODO: Need to update the 'Hits' column in the InviteLinks table whenever a token gets used successfully.
        public async Task <Result <Unit> > Handle(JoinClassroomCommand request, CancellationToken cancellationToken)
        {
            // Get the invite link and the related classroom id.
            var inviteLink = await _context.InviteLinks.SingleOrDefaultAsync(x =>
                                                                             x.Token == request.Token);

            // Get the classroom.
            var classroom = await _context.Classrooms.FindAsync(inviteLink.Classroom.Id);

            // No classroom was found with the given id.
            if (classroom is null)
            {
                Result <Unit> .Failure("Unable to find classroom.");
            }

            // Get the currently authorized user.
            var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                 x.UserName == _userAccessor.GetCurrentUsername());

            var student = await _context.ApplicationUserClassrooms
                          .SingleOrDefaultAsync(x => x.ClassroomId == classroom.Id &&
                                                x.ApplicationUserId == user.Id);

            // User is already attending this classroom.
            if (student != null)
            {
                return(Result <Unit> .Failure("User is already participating in this classroom."));
            }

            // Create a new entry in the UserClassroom table.
            student = new ApplicationUserClassroom(user.Id, classroom.Id, true, user, classroom);
            _context.ApplicationUserClassrooms.Add(student);

            // Save changes to the database.
            var success = await _context.SaveChangesAsync() > 0;

            // Return the final response.
            if (success)
            {
                return(Result <Unit> .Success(Unit.Value));
            }

            return(Result <Unit> .Failure("There was a problem saving changes."));
        }