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

            return(Ok(result));
        }
Esempio n. 2
0
        public async Task <APIResult> Insert([FromBody] InsertCompanyCommand command)
        {
            var id = await mediator.Send(command);

            return(new APIResult()
            {
                Data = new { id = (id > 0) ? id : (int?)null },
                Result = (id > 0) ? 0 : -1,
            });
        }
Esempio n. 3
0
        public async Task <ActionResult <Result> > AddCompany([FromBody] InsertCompanyCommand command)
        {
            var result = await Mediator.Send(command);

            if (result.IsSuccessed)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
Esempio n. 4
0
        public async Task <IEnumerable <Guid> > Handle(InsertCompanyCommand request, CancellationToken cancellationToken)
        {
            _logger.LogInformation("Call to InsertCompanyHandler made to add company record");

            using (var session = await _context.Client.StartSessionAsync())
            {
                try
                {
                    session.StartTransaction();
                    var         bulkOps = new List <WriteModel <Company> >();
                    List <Guid> ids     = new List <Guid>();
                    foreach (var item in request.Models)
                    {
                        var company = await this._context.Companies.FindSync(session, Builders <Company> .Filter.Eq("Name", item.Name)).FirstOrDefaultAsync();

                        if (company != null)
                        {
                            _logger.LogInformation("Company name should be unique.");
                            throw new Exception("Company name should be unique.");
                        }

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

                        bulkOps.Add(new InsertOneModel <Company>(model));
                    }
                    await this._context.Companies.BulkWriteAsync(session, bulkOps).ConfigureAwait(false);

                    await session.CommitTransactionAsync();

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

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

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