public async Task <Unit> Handle(AcceptOrderCommand request, CancellationToken cancellationToken)
        {
            var order = _context.Order.FirstOrDefault(x => x.Id == request.OrderId);

            if (order == null)
            {
                throw new NotFoundException(nameof(order), request.OrderId.ToString());
            }

            if (order.AcceptedByUserId.HasValue && order.AcceptedByUserId != Guid.Empty)
            {
                throw new AlreadyAcceptedException(nameof(order), request.OrderId, order.AcceptedByUserId.Value);
            }

            var user = _context.User.FirstOrDefault(x => x.Id == request.UserId);

            if (user == null)
            {
                throw new NotFoundException(nameof(user), request.UserId.ToString());
            }

            order.AcceptedByUserId = request.UserId;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CreatePaymentCommand request, CancellationToken cancellationToken)
        {
            var paymentId = await _paymentService.MakePayment(new PaymentDto
            {
                Description = request.Description,
                Currency    = request.Currency,
                Email       = request.Email,
                Amount      = request.Amount,
                Token       = request.Token
            }, _paymentService.SecretKey);


            var entity = new Domain.Entities.Payment
            {
                Description       = request.Description,
                Amount            = request.Amount,
                Currency          = request.Currency,
                ExternalPaymentId = paymentId
            };

            await _context.Payment.AddAsync(entity, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 3
0
        public async Task <Unit> Handle(CreateAddressCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.User.FirstOrDefaultAsync(u =>
                                                               u.Id == request.UserId, cancellationToken);

            if (user == null)
            {
                throw new NotFoundException(nameof(user), request.UserId.ToString());
            }

            if (request.PrimaryAddress)
            {
                _context.Address.Where(x =>
                                       x.UserId == request.UserId &&
                                       x.PrimaryAddress)
                .ToList().ForEach(y => y.PrimaryAddress = false);
            }

            var entity = new Domain.Entities.Address
            {
                UserId         = request.UserId,
                City           = request.City,
                PhoneNumber    = request.PhoneNumber,
                Postcode       = request.Postcode,
                Street         = request.Street,
                PrimaryAddress = request.PrimaryAddress,
            };

            _context.Address.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task <Unit> Handle(CompleteOrderCommand request, CancellationToken cancellationToken)
        {
            var order = _context.Order
                        .SingleOrDefault(o => o.Id == request.OrderId);

            if (order == null)
            {
                throw new NotFoundException(nameof(order), request.OrderId.ToString());
            }

            order.Complete = request.Complete;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
        public async Task<Unit> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            if (request.Id == null || request.Id == Guid.Empty) throw new ArgumentException("ID cannot be null");

            var entity = new Domain.Entities.User
            {
                Id = request.Id,
                Email = request.Email,
                PhoneNumber = request.PhoneNumber,
                FirstName = request.FirstName,
                LastName = request.LastName
            };

            _context.User.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return Unit.Value;
        }
Exemplo n.º 6
0
        public async Task <Unit> Handle(SetPrimaryAddressCommand request, CancellationToken cancellationToken)
        {
            var address = _context.Address.FirstOrDefault(x =>
                                                          x.Id == request.Id &&
                                                          x.UserId == request.UserId);

            if (address == null)
            {
                throw new NotFoundException(nameof(address), request.Id.ToString());
            }

            var currentPrimaryAddress = _context.Address.Where(x =>
                                                               x.UserId == request.UserId &&
                                                               x.PrimaryAddress)
                                        .ToList();

            currentPrimaryAddress.ForEach(x => x.PrimaryAddress = false);

            address.PrimaryAddress = request.IsPrimary;

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Exemplo n.º 7
0
        public async Task <Unit> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.User.FirstOrDefaultAsync(u => u.Id == request.CreatedByUserId, cancellationToken);

            if (user == null)
            {
                throw new NotFoundException(nameof(user), request.CreatedByUserId.ToString());
            }

            var address = await _context.Address.FirstOrDefaultAsync(a => a.Id == request.AddressId, cancellationToken);

            if (address == null)
            {
                throw new NotFoundException(nameof(address), request.AddressId.ToString());
            }

            var entity = new Domain.Entities.Order
            {
                Address      = address,
                CreatedBy    = request.CreatedByUserId,
                Description  = request.Description,
                Registration = request.Registration,
                Make         = request.Make,
                Model        = request.Model,
                Trim         = request.Trim,
                Tyre         = request.Tyre,
                Year         = request.Year,
                Availability = _mapper.Map <IEnumerable <Availability> >(request.Availability)
            };

            _context.Order.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }