Пример #1
0
    public TryAsync <Employee> Execute(EmployeeUpdateCommand command, CancellationToken token) =>
    command
    .Apply(EmployeeRecord.Map.Merge)
    .Apply(record =>
    {
        string identifier = record.Id.ToString();

        return(client
               .GetEmployeesContainer()
               .ReplaceItemAsync(
                   record, identifier,
                   new PartitionKey(identifier),
                   new ItemRequestOptions {
            IfMatchEtag = record.ETag
        }, token));
    })
    .Apply(TryAsync)
    .Map(CosmosResponse.Unwrap)
    .SelectMany(record => QueueMessageBuilder
                .ToQueueMessage(queueClient, new EmployeeUpdate
    {
        EmployeeId = command.EntityToUpdate.Id
    })
                .Apply(TryAsync),
                (record, _) => record)
    .Map(EmployeeRecord.Map.ToEmployee);
    public TryOptionAsync <EmployeeDetail> Execute(EmployeeDetailQuery query, CancellationToken token = default)
    {
        var iterator = client
                       .GetEmployeesContainer()
                       .GetItemQueryIterator <JObject>(requestOptions: new QueryRequestOptions
        {
            PartitionKey = new PartitionKey(query.EmployeeId.ToString())
        });

        return(async() =>
        {
            EmployeeRecord?employeeEntity = null;
            var payrollEntities = new List <EmployeePayrollRecord>();

            while (iterator.HasMoreResults)
            {
                var response = await iterator.ReadNextAsync(token);

                foreach (var item in response)
                {
                    string type = item.Value <string>("type") ?? "";

                    switch (type)
                    {
                    case nameof(EmployeeRecord):
                        var entity = item.ToObject <EmployeeRecord>();

                        if (entity is EmployeeRecord)
                        {
                            employeeEntity = entity;
                        }

                        continue;

                    case nameof(EmployeePayrollRecord):
                        var payroll = item.ToObject <EmployeePayrollRecord>();

                        if (payroll is EmployeePayrollRecord)
                        {
                            payrollEntities.Add(payroll);
                        }

                        continue;

                    default:
                        continue;
                    }
                }
            }

            if (employeeEntity is null)
            {
                return None;
            }

            return EmployeeRecord.Map.ToEmployeeDetails(employeeEntity, payrollEntities);
        });
    }
Пример #3
0
        public async Task <int> GetResourceCount(QueryDefinition query, CancellationToken token)
        {
            using var iterator = client.GetEmployeesContainer().GetItemQueryIterator <int>(query);

            if (!iterator.HasMoreResults)
            {
                return(0);
            }

            var response = await iterator.ReadNextAsync();

            return(response.Resource.First());
        }
 public TryAsync <Employee> Execute(EmployeeCreateCommand command, CancellationToken token) =>
 EmployeeRecord
 .Map
 .From(command.NewId, command.Employee)
 .Apply(record => client
        .GetEmployeesContainer()
        .CreateItemAsync(record, cancellationToken: token))
 .Apply(TryAsync)
 .Map(CosmosResponse.Unwrap)
 .SelectMany(record => QueueMessageBuilder
             .ToQueueMessage(queueClient, new EmployeeCreation
 {
     EmployeeId = command.NewId
 })
             .Apply(TryAsync),
             (record, _) => record)
 .Map(EmployeeRecord.Map.ToEmployee);
        public TryOptionAsync <Employee> Execute(EmployeeQuery query, CancellationToken token = default)
        {
            string identifier = query.EmployeeId.ToString();

            return(async() =>
            {
                try
                {
                    var record = await client
                                 .GetEmployeesContainer()
                                 .ReadItemAsync <EmployeeRecord>(identifier, new PartitionKey(identifier), cancellationToken: token);

                    return EmployeeRecord.Map.ToEmployee(record);
                }
                catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return None;
                }
            });
        }
Пример #6
0
        public TryAsync <EmployeePayroll> Execute(EmployeePayrollCreateCommand command, CancellationToken token)
        {
            var(employee, newPayrollId, newEmployeePayroll) = command;

            return(EmployeePayrollRecord
                   .Map
                   .From(employee, newPayrollId, newEmployeePayroll)
                   .Apply(record => client.GetEmployeesContainer().CreateItemAsync(record, cancellationToken: token))
                   .Apply(TryAsync)
                   .Map(CosmosResponse.Unwrap)
                   .SelectMany(_ => QueueMessageBuilder
                               .ToQueueMessage(queueClient, new EmployeePayrollCreation
            {
                EmployeeId = employee.Id,
                EmployeePayrollId = newPayrollId,
            })
                               .Apply(TryAsync),
                               (record, _) => record)
                   .Map(EmployeePayrollRecord.Map.ToEmployeePayroll));
        }
Пример #7
0
 public TryAsync <EmployeePayroll> Execute(EmployeePayrollUpdateCommand command, CancellationToken token) =>
 command.Apply(EmployeePayrollRecord.Map.Merge)
 .Apply(record => client
        .GetEmployeesContainer()
        .ReplaceItemAsync(
            record, record.Id.ToString(),
            new PartitionKey(record.PartitionKey),
            new ItemRequestOptions {
     IfMatchEtag = record.ETag
 }, token))
 .Apply(TryAsync)
 .Map(CosmosResponse.Unwrap)
 .SelectMany(record => QueueMessageBuilder
             .ToQueueMessage(queueClient, new EmployeePayrollUpdate
 {
     EmployeeId        = command.EntityToUpdate.EmployeeId,
     EmployeePayrollId = command.EntityToUpdate.Id,
 })
             .Apply(TryAsync),
             (record, _) => record)
 .Map(EmployeePayrollRecord.Map.ToEmployeePayroll);
        public TryOptionAsync <EmployeePayroll> Execute(EmployeePayrollQuery query, CancellationToken token = default)
        {
            var container = client.GetEmployeesContainer();

            var(employeeId, employeePayrollId) = query;

            return(async() =>
            {
                try
                {
                    var record = await container.ReadItemAsync <EmployeePayrollRecord>(
                        employeePayrollId.ToString(),
                        new PartitionKey(employeeId.ToString()),
                        cancellationToken : token);

                    return EmployeePayrollRecord.Map.ToEmployeePayroll(record);
                }
                catch (CosmosException ex) when(ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    return None;
                }
            });
        }
 public static IOrderedQueryable <TRecord> EmployeesQueryable <TRecord>(this CosmosClient client, string partitionKey) =>
 client.GetEmployeesContainer().GetItemLinqQueryable <TRecord>(requestOptions: new QueryRequestOptions
 {
     PartitionKey = new PartitionKey(partitionKey.ToLowerInvariant())
 });
 public static IOrderedQueryable <TRecord> EmployeesQueryable <TRecord>(this CosmosClient client) =>
 client.GetEmployeesContainer().GetItemLinqQueryable <TRecord>();