Exemple #1
0
            public async Task <SubjectDto> Handle(Command message)
            {
                var subjectDto = message.SubjectDto;
                var customer   = _mapper.Map <SubjectDto, DicCustomer>(subjectDto);

                if (customer.Id == 0)
                {
                    throw new ValidationException("Unable to add power attorney!!!");
                }

                _customerUpdater.Update(customer);
                await _context.SaveChangesAsync();


                switch (message.OwnerType)
                {
                case Owner.Type.Request:
                {
                    var requestCustomer = _mapper.Map <SubjectDto, RequestCustomer>(subjectDto);
                    _context.RequestCustomers.Add(requestCustomer);
                    await _context.SaveChangesAsync();

                    var requestCustomerWithIncludes = await _context.RequestCustomers.Include(rc => rc.CustomerRole)
                                                      .Include(rc => rc.Customer).ThenInclude(c => c.Type)
                                                      .Include(rc => rc.Customer).ThenInclude(c => c.CustomerAttorneyInfos)
                                                      .SingleAsync(rc => rc.Id == requestCustomer.Id);

                    return(_mapper.Map <RequestCustomer, SubjectDto>(requestCustomerWithIncludes));
                }

                case Owner.Type.Contract:
                {
                    var contractCustomer = _mapper.Map <SubjectDto, ContractCustomer>(subjectDto);
                    _context.ContractCustomers.Add(contractCustomer);
                    await _context.SaveChangesAsync();

                    await _categoryIdentifier.IdentifyAsync(contractCustomer.ContractId);

                    var contractCustomerWithIncludes = await _context.ContractCustomers
                                                       .Include(cc => cc.CustomerRole)
                                                       .Include(cc => cc.Customer).ThenInclude(c => c.Type)
                                                       .Include(cc => cc.Customer).ThenInclude(c => c.CustomerAttorneyInfos)
                                                       .SingleAsync(cc => cc.Id == contractCustomer.Id);

                    return(_mapper.Map <ContractCustomer, SubjectDto>(contractCustomerWithIncludes));
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
Exemple #2
0
        public async Task <IActionResult> Update(Guid id, [FromBody] CustomerModel customerModel)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(new { Message = "Preenchimento invalido" }));
                }

                logger.LogInformation("Update");

                customerModel.SetId(id);
                await customerUpdater.Update(customerModel);

                return(Ok());
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Update");

                Console.WriteLine($"Update ==> {ex.Message}");
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
Exemple #3
0
            public async Task <SubjectDto> Handle(Command message)
            {
                var id         = message.SubjectDto.Id = message.Id;
                var subjectDto = message.SubjectDto;

                var customer = _mapper.Map <SubjectDto, DicCustomer>(subjectDto);

                _customerUpdater.Update(customer);
                await _context.SaveChangesAsync();

                try
                {
                    switch (message.OwnerType)
                    {
                    case Owner.Type.Request:
                    {
                        var requestCustomer = _context.RequestCustomers.Find(id);

                        if (requestCustomer == null)
                        {
                            throw new DataNotFoundException(nameof(RequestCustomer),
                                                            DataNotFoundException.OperationType.Update, (int)id);
                        }
                        _mapper.Map(subjectDto, requestCustomer);

                        await _context.SaveChangesAsync();

                        var requestCustomerWithIncludes = await _context.RequestCustomers.Include(rc => rc.CustomerRole)
                                                          .Include(rc => rc.Customer).ThenInclude(c => c.Type)
                                                          .Include(rc => rc.Customer).ThenInclude(c => c.CustomerAttorneyInfos)
                                                          .Include(rc => rc.Customer).ThenInclude(c => c.ContactInfos).ThenInclude(ci => ci.Type)
                                                          .SingleAsync(rc => rc.Id == requestCustomer.Id);

                        return(_mapper.Map <RequestCustomer, SubjectDto>(requestCustomerWithIncludes));
                    }

                    case Owner.Type.Contract:
                    {
                        var contractCustomer = _context.ContractCustomers.Find(id);

                        if (contractCustomer == null)
                        {
                            throw new DataNotFoundException(nameof(ContractCustomer),
                                                            DataNotFoundException.OperationType.Update, (int)id);
                        }
                        _mapper.Map(subjectDto, contractCustomer);

                        await _context.SaveChangesAsync();

                        await _categoryIdentifier.IdentifyAsync(contractCustomer.ContractId);

                        var contractCustomerWithIncludes = await _context.ContractCustomers.Include(rc => rc.CustomerRole)
                                                           .Include(rc => rc.Customer).ThenInclude(c => c.Type)
                                                           .Include(rc => rc.Customer).ThenInclude(c => c.CustomerAttorneyInfos)
                                                           .Include(rc => rc.Customer).ThenInclude(c => c.ContactInfos).ThenInclude(ci => ci.Type)
                                                           .SingleAsync(rc => rc.Id == contractCustomer.Id);

                        return(_mapper.Map <ContractCustomer, SubjectDto>(contractCustomerWithIncludes));
                    }

                    default:
                        throw new ArgumentOutOfRangeException();
                    }
                }
                catch (Exception e)
                {
                    throw new DatabaseException(e.InnerException?.Message ?? e.Message);
                }
            }
Exemple #4
0
        public async Task <IActionResult> Put(int id, Owner.Type ownerType, [FromBody] SubjectDto subjectDto)
        {
            SubjectDto subject;
            var        subjectId = subjectDto.Id = id;
            var        customer  = Mapper.Map <SubjectDto, DicCustomer>(subjectDto);

            _customerUpdater.Update(customer);

            switch (ownerType)
            {
            case Owner.Type.Request:
                var requestCustomer = await Executor.GetQuery <GetRequestCustomerByIdQuery>()
                                      .Process(q => q.ExecuteAsync(subjectId));

                if (requestCustomer == null)
                {
                    throw new DataNotFoundException(nameof(RequestCustomer),
                                                    DataNotFoundException.OperationType.Update, id);
                }

                Mapper.Map(subjectDto, requestCustomer);
                requestCustomer.Customer = null;
                await Executor.GetCommand <UpdateRequestCustomerCommand>()
                .Process(c => c.ExecuteAsync(requestCustomer));

                if (requestCustomer.CustomerRole.Code == DicCustomerRoleCodes.Correspondence)
                {
                    var correspondence = Executor.GetQuery <GetDicCustomerByIdQuery>()
                                         .Process(q => q.Execute(requestCustomer.CustomerId ?? 0));
                    correspondence.Address   = requestCustomer.Address;
                    correspondence.AddressEn = requestCustomer.AddressEn;
                    correspondence.AddressKz = requestCustomer.AddressKz;
                    await Executor.GetCommand <UpdateDicCustomerCommand>().Process(c => c.ExecuteAsync(correspondence));

                    //var request = await Executor.GetQuery<GetRequestByIdQuery>()
                    //    .Process(q => q.ExecuteAsync(requestCustomer.RequestId ?? 0));
                    //request.AddresseeId = correspondence.Id;
                    //await Executor.GetCommand<UpdateRequestCommand>().Process(c => c.ExecuteAsync(request));
                }

                var requestCustomerWithIncludes = await Executor.GetQuery <GetRequestCustomerByIdQuery>()
                                                  .Process(q => q.ExecuteAsync(requestCustomer.Id));

                subject = Mapper.Map <RequestCustomer, SubjectDto>(requestCustomerWithIncludes);
                break;

            case Owner.Type.Contract:
                var contractCustomer = await Executor.GetQuery <GetContractCustomerByIdQuery>()
                                       .Process(q => q.ExecuteAsync(subjectId));

                if (contractCustomer == null)
                {
                    throw new DataNotFoundException(nameof(ContractCustomer),
                                                    DataNotFoundException.OperationType.Update, id);
                }

                Mapper.Map(subjectDto, contractCustomer);
                contractCustomer.Customer = null;
                await Executor.GetCommand <UpdateContractCustomerCommand>()
                .Process(c => c.ExecuteAsync(contractCustomer));

                await _categoryIdentifier.IdentifyAsync(contractCustomer.ContractId);

                var contractCustomerWithIncludes = await Executor.GetQuery <GetContractCustomerByIdQuery>()
                                                   .Process(q => q.ExecuteAsync(contractCustomer.Id));

                subject = Mapper.Map <ContractCustomer, SubjectDto>(contractCustomerWithIncludes);
                break;

            case Owner.Type.ProtectionDoc:
                var protectionDocCustomer = await Executor.GetQuery <GetProtectionDocCustomerByIdQuery>()
                                            .Process(q => q.ExecuteAsync(subjectId));

                if (protectionDocCustomer == null)
                {
                    throw new DataNotFoundException(nameof(ProtectionDocCustomer),
                                                    DataNotFoundException.OperationType.Update, id);
                }

                Mapper.Map(subjectDto, protectionDocCustomer);
                protectionDocCustomer.Customer = null;
                await Executor.GetCommand <UpdateProtectionDocCustomerCommand>()
                .Process(c => c.ExecuteAsync(protectionDocCustomer));

                var protectionDocCustomerWithIncludes = await Executor.GetQuery <GetProtectionDocCustomerByIdQuery>()
                                                        .Process(q => q.ExecuteAsync(protectionDocCustomer.Id));

                subject = Mapper.Map <ProtectionDocCustomer, SubjectDto>(protectionDocCustomerWithIncludes);
                break;

            default:
                throw new NotImplementedException();
            }

            return(Ok(subject));
        }