public async Task HandleVerb(StatementBase statement, IStatementBaseEntity newStatement, CancellationToken cancellationToken) { var verb = (VerbEntity)await _mediator.Send(UpsertVerbCommand.Create(statement.Verb), cancellationToken); newStatement.VerbId = verb.VerbId; newStatement.Verb = null; }
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); }
/// <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); }