Beispiel #1
0
        public async Task HandleContext(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken)
        {
            if (statement.Context != null)
            {
                newStatement.Context = _mapper.Map <ContextEntity>(statement.Context);
                ContextEntity context = newStatement.Context;
                if (context.Instructor != null)
                {
                    var instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(statement.Context.Instructor), cancellationToken);

                    context.InstructorId = instructor.AgentId;
                    context.Instructor   = null;
                }
                if (context.Team != null)
                {
                    var team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(statement.Context.Team), cancellationToken);

                    context.TeamId = team.AgentId;
                    context.Team   = null;
                }

                if (context.ContextActivities != null)
                {
                    foreach (var contextActivity in context.ContextActivities)
                    {
                        Iri id       = new Iri(contextActivity.Activity.Id);
                        var activity = await _mediator.Send(UpsertActivityCommand.Create(id), cancellationToken);

                        contextActivity.Activity   = null;
                        contextActivity.ActivityId = activity.ActivityId;
                    }
                }
            }
        }
Beispiel #2
0
        public async Task HandleObject(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken)
        {
            var objType = statement.Object.ObjectType;

            if (objType == ObjectType.Activity)
            {
                var activity = await _mediator.Send(UpsertActivityCommand.Create((Activity)statement.Object), cancellationToken);

                newStatement.ObjectType = EntityObjectType.Activity;
                newStatement.ObjectId   = activity.ActivityId;
            }
            else if (objType == ObjectType.Agent || objType == ObjectType.Group)
            {
                AgentEntity agent = await _mediator.Send(UpsertActorCommand.Create((Agent)statement.Object), cancellationToken);;
                newStatement.ObjectType = EntityObjectType.Agent;
                newStatement.ObjectId   = agent.AgentId;
            }
            else if (objType == ObjectType.SubStatement)
            {
                SubStatementEntity subStatement = await _mediator.Send(CreateSubStatementCommand.Create((SubStatement)statement.Object), cancellationToken);

                newStatement.ObjectType = EntityObjectType.SubStatement;
                newStatement.ObjectId   = subStatement.SubStatementId;
            }
            else if (objType == ObjectType.StatementRef)
            {
                StatementRef statementRef = (StatementRef)statement.Object;
                newStatement.ObjectType = EntityObjectType.StatementRef;
                newStatement.ObjectId   = statementRef.Id;
            }
        }
        public async Task <IActionResult> PostSingleState(
            [BindRequired, FromQuery] string stateId,
            [BindRequired, FromQuery] Iri activityId,
            [BindRequired, FromQuery] Agent agent,
            [BindRequired, FromBody] byte[] body,
            [BindRequired, FromHeader(Name = "Content-Type")] string contentType,
            [FromQuery] Guid?registration       = null,
            CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            AgentEntity storedAgent = await _mediator.Send(UpsertActorCommand.Create(agent));

            ActivityEntity activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create(activityId), cancellationToken);

            ActivityStateDocument stateDocument = await _mediator.Send(new GetActivityStateQuery()
            {
                StateId      = stateId,
                ActivityId   = activityId,
                AgentId      = storedAgent.AgentId,
                Registration = registration
            }, cancellationToken);

            if (stateDocument != null)
            {
                stateDocument = await _mediator.Send(new UpdateStateDocumentCommand()
                {
                    StateId      = stateId,
                    ActivityId   = activityId,
                    AgentId      = storedAgent.AgentId,
                    Content      = body,
                    ContentType  = contentType,
                    Registration = registration
                }, cancellationToken);
            }
            else
            {
                stateDocument = await _mediator.Send(new CreateStateDocumentCommand()
                {
                    StateId      = stateId,
                    Activity     = activity,
                    Agent        = storedAgent,
                    Content      = body,
                    ContentType  = contentType,
                    Registration = registration
                }, cancellationToken);
            }

            Response.Headers.Add(HeaderNames.ETag, $"\"{stateDocument.Tag}\"");
            Response.Headers.Add(HeaderNames.LastModified, stateDocument.LastModified?.ToString("o"));

            return(NoContent());
        }
        public async Task <ActivityProfileEntity> Handle(CreateActivityProfileCommand request, CancellationToken cancellationToken)
        {
            var activity = await _mediator.Send(UpsertActivityCommand.Create(request.ActivityId));

            var profile = new ActivityProfileEntity(request.Content, request.ContentType)
            {
                Key            = request.ProfileId,
                ActivityId     = activity.ActivityId,
                RegistrationId = request.Registration
            };

            _context.Documents.Add(profile);
            await _context.SaveChangesAsync(cancellationToken);

            return(profile);
        }
Beispiel #5
0
        public async Task <IDocument> PostSingleState(string stateId, Iri activityId, Agent agent, byte[] body, string contentType, Guid?registration = null, CancellationToken cancellationToken = default)
        {
            var activity = await mediator.Send(UpsertActivityCommand.Create(activityId));

            AgentEntity savedAgent = await mediator.Send(UpsertActorCommand.Create(agent), cancellationToken);

            var document = await mediator.Send(new CreateStateDocumentCommand()
            {
                StateId      = stateId,
                ActivityId   = activity.ActivityId,
                AgentId      = savedAgent.AgentId,
                Content      = body,
                ContentType  = contentType,
                Registration = registration,
            }, cancellationToken);

            return(mapper.Map <IDocument>(document));
        }
Beispiel #6
0
        public async Task <SubStatementEntity> Handle(CreateSubStatementCommand request, CancellationToken cancellationToken)
        {
            var subStatement = _mapper.Map <SubStatementEntity>(request.SubStatement);

            subStatement.Timestamp = subStatement.Timestamp ?? DateTimeOffset.UtcNow;

            subStatement.Verb = (VerbEntity)await _mediator.Send(UpsertVerbCommand.Create(request.SubStatement.Verb));

            subStatement.Actor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Actor));

            if (subStatement.Context != null)
            {
                var context = subStatement.Context;
                if (context.Instructor != null)
                {
                    context.Instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Context.Instructor), cancellationToken);
                }

                if (context.Team != null)
                {
                    context.Team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.SubStatement.Context.Team), cancellationToken);
                }
            }

            var objType = subStatement.Object.ObjectType;

            if (objType == EntityObjectType.Activity)
            {
                subStatement.Object.Activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create((Activity)request.SubStatement.Object));
            }
            else if (objType == EntityObjectType.Agent || objType == EntityObjectType.Group)
            {
                subStatement.Object.Agent = await _mediator.Send(UpsertActorCommand.Create((Agent)request.SubStatement.Object));
            }
            else if (objType == EntityObjectType.StatementRef)
            {
                // It's already mapped from automapper
                // TODO: Additional logic should be performed here
            }
            _context.SubStatements.Add(subStatement);

            return(subStatement);
        }
        public async Task DeepEqual()
        {
            var mediatorMock = new Mock <IMediator>();
            var activity1    = new Activity(@"{
                ""objectType"": ""Activity"",
                ""id"": ""http://www.example.com/verify/complete/34534"",
                ""definition"": {
                    ""type"": ""http://adlnet.gov/expapi/activities/meeting"",
                    ""name"": {
                        ""en-GB"": ""example meeting""
                    },
                    ""description"": {
                        ""en-GB"": ""An example meeting that happened on a specific occasion with certain people present.""
                    },
                    ""moreInfo"": ""http://virtualmeeting.example.com/345256"",
                    ""extensions"": {
                        ""http://example.com/profiles/meetings/extension/location"": ""X:\\\\meetings\\\\minutes\\\\examplemeeting.one"",
                        ""http://example.com/profiles/meetings/extension/reporter"": {
                            ""name"": ""Thomas"",
                            ""id"": ""http://openid.com/342""
                        }
                    }
                }
            }");
            var activity2    = new Activity(@"{
                ""objectType"": ""Activity"",
                ""id"": ""http://www.example.com/verify/complete/34534"",
                ""definition"": {
                    ""type"": ""http://adlnet.gov/expapi/activities/meeting"",
                    ""name"": {
                        ""en-US"": ""example meeting""
                    },
                    ""description"": {
                        ""en-US"": ""An example meeting that happened on a specific occasion with certain people present.""
                    },
                    ""moreInfo"": ""http://virtualmeeting.example.com/345256"",
                    ""extensions"": {
                        ""http://example.com/profiles/meetings/extension/location"": ""X:\\\\meetings\\\\minutes\\\\examplemeeting.one"",
                        ""http://example.com/profiles/meetings/extension/reporter"": {
                            ""name"": ""Thomas"",
                            ""id"": ""http://openid.com/342""
                        }
                    }
                }
            }");

            var upsertHandler = new UpsertActivityCommandHandler(_context, _mapper);

            var entity1 = await upsertHandler.Handle(UpsertActivityCommand.Create(activity1), CancellationToken.None);

            await _context.SaveChangesAsync();

            var entity2 = await upsertHandler.Handle(UpsertActivityCommand.Create(activity2), CancellationToken.None);

            entity2.Id.ShouldBe("http://www.example.com/verify/complete/34534");
            entity2.Definition.ShouldNotBeNull();

            entity2.Definition.Names.Count.ShouldBe(2);
            entity2.Definition.Names.ShouldContainKey("en-US");
            entity2.Definition.Names.ShouldContainKey("en-GB");

            entity2.Definition.Descriptions.Count.ShouldBe(2);
            entity2.Definition.Descriptions.ShouldContainKey("en-US");
            entity2.Definition.Descriptions.ShouldContainKey("en-GB");
        }
        /// <summary>
        /// Creates statement without saving to database
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns>Guid of the created statement</returns>
        public async Task <Guid> Handle(CreateStatementCommand request, CancellationToken cancellationToken)
        {
            // Prepare statement for mapping
            if (request.Statement.Id.HasValue)
            {
                bool any = await _context.Statements.AnyAsync(x => x.StatementId == request.Statement.Id, cancellationToken).ConfigureAwait(false);

                if (any)
                {
                    return(request.Statement.Id.Value);
                }
            }

            request.Statement.Stamp();

            // Ensure statement version and stored date
            request.Statement.Version = request.Statement.Version ?? ApiVersion.GetLatest().ToString();
            request.Statement.Stored  = request.Statement.Stored ?? DateTimeOffset.UtcNow;

            if (request.Statement.Authority == null)
            {
                // TODO: Map group?
                request.Statement.Authority = _mapper.Map <Agent>(_authorityContext.Authority);
            }
            else
            {
                // TODO: Validate authority
            }

            // Start mapping statement
            StatementEntity newStatement = new StatementEntity();

            newStatement.StatementId = request.Statement.Id.GetValueOrDefault();
            newStatement.Verb        = (VerbEntity)await _mediator.Send(UpsertVerbCommand.Create(request.Statement.Verb), cancellationToken).ConfigureAwait(false);

            newStatement.Actor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Actor), cancellationToken).ConfigureAwait(false);

            newStatement.Authority = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Authority), cancellationToken).ConfigureAwait(false);

            if (request.Statement.Context != null)
            {
                newStatement.Context = _mapper.Map <ContextEntity>(request.Statement.Context);
                ContextEntity context = newStatement.Context;
                if (context.Instructor != null)
                {
                    context.Instructor = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Context.Instructor), cancellationToken).ConfigureAwait(false);
                }
                if (context.Team != null)
                {
                    context.Team = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create(request.Statement.Context.Team), cancellationToken).ConfigureAwait(false);
                }
            }

            var objType = request.Statement.Object.ObjectType;

            newStatement.Object = new StatementObjectEntity();
            if (objType == ObjectType.Activity)
            {
                newStatement.Object.Activity = (ActivityEntity)await _mediator.Send(UpsertActivityCommand.Create((Activity)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.Agent || objType == ObjectType.Group)
            {
                newStatement.Object.Agent = (AgentEntity)await _mediator.Send(UpsertActorCommand.Create((Agent)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.SubStatement)
            {
                newStatement.Object.SubStatement = (SubStatementEntity)await _mediator.Send(CreateSubStatementCommand.Create((SubStatement)request.Statement.Object), cancellationToken).ConfigureAwait(false);
            }
            else if (objType == ObjectType.StatementRef)
            {
                newStatement.Object.StatementRef = _mapper.Map <StatementRefEntity>((StatementRef)request.Statement.Object);
            }

            if (request.Statement.Result != null)
            {
                newStatement.Result = _mapper.Map <ResultEntity>(request.Statement.Result);
            }
            newStatement.Stored        = request.Statement.Stored;
            newStatement.Timestamp     = request.Statement.Timestamp;
            newStatement.Version       = request.Statement.Version.ToString();
            newStatement.FullStatement = request.Statement.ToJson();

            _context.Statements.Add(newStatement);

            // await _context.SaveChangesAsync(cancellationToken);
            await _mediator.Publish(StatementAdded.Create(newStatement), cancellationToken).ConfigureAwait(false);

            if (request.Persist)
            {
                await _context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }

            return(newStatement.StatementId);
        }