Exemple #1
0
        protected override async Task <CompleteModel> Process(SignUpCommand request, CancellationToken cancellationToken)
        {
            var principal = request.Principal;

            var updateCommand = new EntityUpsertCommand <Guid, SignUpUpdateModel, SignUpReadModel>(principal, request.Id, request.InstructorSignUp);
            var updateModel   = await _mediator.Send(updateCommand, cancellationToken);

            foreach (var signupTopic in request.InstructorSignUpTopics)
            {
                var signupTopicCommand = new EntityUpsertCommand <Guid, SignUpTopicUpdateModel, SignUpTopicReadModel>(principal, signupTopic.Id, signupTopic);
                var signupTopicModel   = await _mediator.Send(signupTopicCommand, cancellationToken);

                var topicId = signupTopicModel.TopicId;

                var topicReadCommand = new EntityIdentifierQuery <Guid, TopicUpdateModel>(principal, topicId);
                var topicModel       = await _mediator.Send(topicReadCommand, cancellationToken);

                topicModel.InstructorSlots = signupTopic.TopicInstructorSlots;

                var topicUpdateCommand = new EntityUpdateCommand <Guid, TopicUpdateModel, TopicReadModel>(principal, topicId, topicModel);
                var updatedModel       = await _mediator.Send(topicUpdateCommand, cancellationToken);
            }

            return(new CompleteModel());
        }
        protected virtual async Task <TReadModel> UpsertCommand(TKey id, TUpdateModel updateModel, CancellationToken cancellationToken = default)
        {
            var command = new EntityUpsertCommand <TKey, TUpdateModel, TReadModel>(User, id, updateModel);
            var result  = await Mediator.Send(command, cancellationToken);

            return(result);
        }
    protected override async Task <TReadModel> Process(EntityUpsertCommand <TKey, TUpdateModel, TReadModel> request, CancellationToken cancellationToken)
    {
        if (request is null)
        {
            throw new ArgumentNullException(nameof(request));
        }

        var dbSet = DataContext
                    .Set <TEntity>();

        var keyValue = new object[] { request.Id };

        // find entity to update by message id, not model id
        var entity = await dbSet
                     .FindAsync(keyValue, cancellationToken)
                     .ConfigureAwait(false);

        // create entity if not found
        if (entity == null)
        {
            entity    = new TEntity();
            entity.Id = request.Id;

            // apply create metadata
            if (entity is ITrackCreated createdModel)
            {
                createdModel.Created   = request.Activated;
                createdModel.CreatedBy = request.ActivatedBy;
            }

            await dbSet
            .AddAsync(entity, cancellationToken)
            .ConfigureAwait(false);
        }

        // copy updates from model to entity
        Mapper.Map(request.Model, entity);

        // apply update metadata
        if (entity is ITrackUpdated updateEntity)
        {
            updateEntity.Updated   = request.Activated;
            updateEntity.UpdatedBy = request.ActivatedBy;
        }

        // save updates
        await DataContext
        .SaveChangesAsync(cancellationToken)
        .ConfigureAwait(false);

        // return read model
        var readModel = await Read(entity.Id, cancellationToken).ConfigureAwait(false);

        return(readModel);
    }
    protected override async Task <TReadModel> Process(EntityUpsertCommand <string, TUpdateModel, TReadModel> request, CancellationToken cancellationToken)
    {
        if (request is null)
        {
            throw new ArgumentNullException(nameof(request));
        }
        if (!CosmosKey.TryDecode(request.Id, out var id, out var partitionKey))
        {
            throw new InvalidOperationException("Invalid Cosmos Key format");
        }

        var entity = await Repository
                     .FindAsync(id, partitionKey, cancellationToken)
                     .ConfigureAwait(false);

        // create entity if not found
        if (entity == null)
        {
            entity = new TEntity {
                Id = id
            };

            // apply create metadata
            if (entity is ITrackCreated createdModel)
            {
                createdModel.Created   = request.Activated;
                createdModel.CreatedBy = request.ActivatedBy;
            }
        }

        // copy updates from model to entity
        Mapper.Map(request.Model, entity);

        // apply update metadata
        if (entity is ITrackUpdated updateEntity)
        {
            updateEntity.Updated   = request.Activated;
            updateEntity.UpdatedBy = request.ActivatedBy;
        }

        // save updates
        var savedEntity = await Repository
                          .UpdateAsync(entity, cancellationToken)
                          .ConfigureAwait(false);

        // return read model
        var model = Mapper.Map <TReadModel>(savedEntity);

        return(model);
    }
        public async Task Upsert()
        {
            var key = ObjectId.GenerateNewId().ToString();

            var mediator = ServiceProvider.GetService <IMediator>();

            mediator.Should().NotBeNull();

            var mapper = ServiceProvider.GetService <IMapper>();

            mapper.Should().NotBeNull();

            // Update Entity
            var updateModel = Generator.Default.Single <TaskUpdateModel>();

            updateModel.Title       = "Upsert Test";
            updateModel.Description = "Insert " + DateTime.Now.Ticks;
            updateModel.StatusId    = StatusConstants.NotStarted.Id;
            updateModel.TenantId    = TenantConstants.Test.Id;

            var upsertCommandNew = new EntityUpsertCommand <string, TaskUpdateModel, TaskReadModel>(MockPrincipal.Default, key, updateModel);
            var upsertResultNew  = await mediator.Send(upsertCommandNew).ConfigureAwait(false);

            upsertResultNew.Should().NotBeNull();
            upsertResultNew.Id.Should().Be(key);

            // Get Entity by Key
            var identifierQuery  = new EntityIdentifierQuery <string, TaskReadModel>(MockPrincipal.Default, key);
            var identifierResult = await mediator.Send(identifierQuery).ConfigureAwait(false);

            identifierResult.Should().NotBeNull();
            identifierResult.Title.Should().Be(updateModel.Title);

            // update model
            updateModel.Description = "Update " + DateTime.Now.Ticks;

            // Upsert again, should be update
            var upsertCommandUpdate = new EntityUpsertCommand <string, TaskUpdateModel, TaskReadModel>(MockPrincipal.Default, key, updateModel);
            var upsertResultUpdate  = await mediator.Send(upsertCommandUpdate).ConfigureAwait(false);

            upsertResultUpdate.Should().NotBeNull();
            upsertResultUpdate.Description.Should().NotBe(upsertResultNew.Description);
        }
        public async Task Upsert()
        {
            var id  = ObjectId.GenerateNewId().ToString();
            var key = CosmosKey.Encode(id, id);

            var mediator = ServiceProvider.GetService <IMediator>();

            mediator.Should().NotBeNull();

            var mapper = ServiceProvider.GetService <IMapper>();

            mapper.Should().NotBeNull();

            // Update Entity
            var updateModel = Generator.Default.Single <AuditUpdateModel>();

            updateModel.Username = "******";
            updateModel.Content  = "Insert " + DateTime.Now.Ticks;

            var upsertCommandNew = new EntityUpsertCommand <string, AuditUpdateModel, AuditReadModel>(MockPrincipal.Default, key, updateModel);
            var upsertResultNew  = await mediator.Send(upsertCommandNew).ConfigureAwait(false);

            upsertResultNew.Should().NotBeNull();

            // Get Entity by Key
            var identifierQuery  = new EntityIdentifierQuery <string, AuditReadModel>(MockPrincipal.Default, key);
            var identifierResult = await mediator.Send(identifierQuery).ConfigureAwait(false);

            identifierResult.Should().NotBeNull();
            identifierResult.Username.Should().Be(updateModel.Username);

            // update model
            updateModel.Content = "Update " + DateTime.Now.Ticks;

            // Upsert again, should be update
            var upsertCommandUpdate = new EntityUpsertCommand <string, AuditUpdateModel, AuditReadModel>(MockPrincipal.Default, key, updateModel);
            var upsertResultUpdate  = await mediator.Send(upsertCommandUpdate).ConfigureAwait(false);

            upsertResultUpdate.Should().NotBeNull();
            upsertResultUpdate.Content.Should().NotBe(upsertResultNew.Content);
        }
        protected override async Task <TReadModel> Process(EntityUpsertCommand <TKey, TUpdateModel, TReadModel> request, CancellationToken cancellationToken)
        {
            var dbSet = DataContext
                        .Set <TEntity>();

            var keyValue = new object[] { request.Id };

            // find entity to update by message id, not model id
            var entity = await dbSet
                         .FindAsync(keyValue, cancellationToken)
                         .ConfigureAwait(false);

            // create entity if not found
            if (entity == null)
            {
                entity    = new TEntity();
                entity.Id = request.Id;

                await dbSet
                .AddAsync(entity, cancellationToken)
                .ConfigureAwait(false);
            }

            // copy updates from model to entity
            Mapper.Map(request.Model, entity);

            // save updates
            await DataContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            // return read model
            var readModel = await Read(entity.Id, cancellationToken)
                            .ConfigureAwait(false);

            return(readModel);
        }