Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] IList <StaffDTO> value)
        {
            var cmd    = new InsertStaffCommand(value);
            var result = await _mediator.Send(cmd);

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public async Task <IEnumerable <Guid> > Handle(InsertStaffCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Call to InsertStaffHandler made to add staff record");

            using (var session = await _context.Client.StartSessionAsync())
            {
                try
                {
                    session.StartTransaction();
                    var         bulkOps = new List <WriteModel <Staff> >();
                    List <Guid> ids     = new List <Guid>();
                    foreach (var item in request.Models)
                    {
                        var department = await this._context.Departments.FindSync(session, Builders <Department> .Filter.Eq("Id", item.DepartmentId)).FirstOrDefaultAsync();

                        if (department == null)
                        {
                            _logger.LogInformation("Department in staff not found.");
                            throw new Exception("Department in staff not found.");
                        }

                        var model = _mapper.Map <Staff>(item);
                        ids.Add(model.Id);

                        model.CompanyId = department.CompanyId;
                        bulkOps.Add(new InsertOneModel <Staff>(model));
                    }
                    await this._context.Staffs.BulkWriteAsync(session, bulkOps).ConfigureAwait(false);

                    await session.CommitTransactionAsync();

                    _logger.LogInformation("Call to InsertStaffHandler completed.");

                    return(ids);
                }
                catch (Exception ex)
                {
                    await session.AbortTransactionAsync();

                    throw new Exception(ex.Message);
                }
            }
        }