public async Task <Guid> Handle(CreateClientCommandRequest request, CancellationToken cancellationToken)
        {
            var client = new Domain.Client(
                request.FirstName,
                request.LastName,
                request.PhoneNumber,
                request.Email,
                request.DocumentNumber,
                request.BirthDate,
                new Domain.Address(
                    request.Address.PostalCode,
                    request.Address.AddressLine,
                    request.Address.Number,
                    request.Address.Complement,
                    request.Address.Neighborhood,
                    request.Address.City,
                    request.Address.State));

            if (await _context.Clients.AnyAsync(x => x.DocumentNumber == client.DocumentNumber))
            {
                throw new BadRequestException("Document already exists");
            }

            await _context.Clients.AddAsync(client, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(client.Id);
        }
Esempio n. 2
0
        public async Task <bool> Handle(UpdateClientCommandRequest request, CancellationToken cancellationToken)
        {
            var getclient = await _context.Clients.FirstOrDefaultAsync(x => x.Id == request.Id);

            if (getclient == null)
            {
                throw new BadRequestException("Client already exists");
            }

            getclient.UpdateClient(request.FirstName, request.LastName, request.PhoneNumber, request.Email, request.DocumentNumber, request.BirthDate.Value);
            getclient.Address.UpdateAddress(request.Address.PostalCode, request.Address.AddressLine, request.Address.Number, request.Address.Complement, request.Address.Neighborhood, request.Address.City, request.Address.State);

            _context.Clients.Update(getclient);
            await _context.SaveChangesAsync(cancellationToken);

            return(true);
        }
Esempio n. 3
0
        public async Task <Guid> Handle(UpdateClientCommandRequest request, CancellationToken cancellationToken)
        {
            var client = _context.Clients.Where(x => x.Id == request.Id).FirstOrDefault();

            if (client == null)
            {
                throw new BadRequestException("Client not found");
            }

            client.UpdateData(request.FirstName, request.LastName, request.PhoneNumber, request.Email, request.DocumentNumber, request.BirthDate);
            client.UpdateAddress(request.Address.PostalCode, request.Address.AddressLine, request.Address.Number, request.Address.Complement, request.Address.Neighborhood, request.Address.City, request.Address.State);

            _context.SetModifiedState(client);
            await _context.SaveChangesAsync(cancellationToken);

            return(client.Id);
        }