Example #1
0
        public async Task <ActivityProfileDocument> Handle(CreateActivityProfileCommand request, CancellationToken cancellationToken)
        {
            var activity = (ActivityEntity)await _mediator.Send(MergeActivityCommand.Create(request.ActivityId));

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

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

            //return profile;
            return(_mapper.Map <ActivityProfileDocument>(profile));
        }
        public async Task <ISubStatementEntity> Handle(CreateSubStatementCommand request, CancellationToken cancellationToken)
        {
            var subStatement = _mapper.Map <SubStatementEntity>(request.SubStatement);

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

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

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

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

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

            var objType = subStatement.Object.ObjectType;

            if (objType == EntityObjectType.Activity)
            {
                subStatement.Object.Activity = (ActivityEntity)await _mediator.Send(MergeActivityCommand.Create((IActivity)request.SubStatement.Object));
            }
            else if (objType == EntityObjectType.Agent || objType == EntityObjectType.Group)
            {
                subStatement.Object.Agent = (AgentEntity)await _mediator.Send(MergeActorCommand.Create((IAgent)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);
        }
Example #3
0
        public async Task <ActivityStateDocument> Handle(CreateStateDocumentCommand request, CancellationToken cancellationToken)
        {
            ActivityEntity activity = (ActivityEntity)await _mediator.Send(MergeActivityCommand.Create(request.ActivityId), cancellationToken);

            AgentEntity agent = (AgentEntity)await _mediator.Send(MergeActorCommand.Create(request.Agent), cancellationToken);

            var state = new ActivityStateEntity(request.Content, request.ContentType)
            {
                StateId      = request.StateId,
                Activity     = activity,
                Agent        = agent,
                Registration = request.Registration
            };

            _context.ActivityStates.Add(state);
            await _context.SaveChangesAsync(cancellationToken);

            return(_mapper.Map <ActivityStateDocument>(state));
        }
        /// <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)
        {
            if (request.Statement.Id.HasValue)
            {
                int count = await _context.Statements.CountAsync(x => x.StatementId == request.Statement.Id, cancellationToken);

                if (count > 0)
                {
                    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)
            {
                request.Statement.Authority = _authorityContext.Authority;
            }
            else
            {
                // TODO: Validate authority
            }

            StatementEntity newStatement = _mapper.Map <StatementEntity>(request.Statement);

            newStatement.Verb = (VerbEntity)await _mediator.Send(MergeVerbCommand.Create(request.Statement.Verb), cancellationToken).ConfigureAwait(false);

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

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

            if (newStatement.Context != null)
            {
                var context = newStatement.Context;
                if (context.Instructor != null)
                {
                    context.Instructor = (AgentEntity)await _mediator.Send(MergeActorCommand.Create(request.Statement.Context.Instructor), cancellationToken);
                }
                if (context.Team != null)
                {
                    context.Team = (AgentEntity)await _mediator.Send(MergeActorCommand.Create(request.Statement.Context.Team), cancellationToken);
                }
            }

            var objType = newStatement.Object.ObjectType;

            if (objType == EntityObjectType.Activity)
            {
                newStatement.Object.Activity = (ActivityEntity)await _mediator.Send(MergeActivityCommand.Create((IActivity)request.Statement.Object));
            }
            else if (objType == EntityObjectType.Agent || objType == EntityObjectType.Group)
            {
                newStatement.Object.Agent = (AgentEntity)await _mediator.Send(MergeActorCommand.Create((IAgent)request.Statement.Object));
            }
            else if (objType == EntityObjectType.SubStatement)
            {
                newStatement.Object.SubStatement = (SubStatementEntity)await _mediator.Send(CreateSubStatementCommand.Create((ISubStatement)request.Statement.Object));
            }
            else if (objType == EntityObjectType.StatementRef)
            {
                // It's already mapped from automapper
                // TODO: Additional logic should be performed here
            }

            newStatement.FullStatement = request.Statement.ToJson();

            _context.Statements.Add(newStatement);

            await _mediator.Publish(StatementAdded.Create(newStatement), cancellationToken);

            return(newStatement.StatementId);
        }