Пример #1
0
        public async Task <IIdentifierResult> HandleAsync(ICreateOffer command, ICorrelationContext context)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            await Task.WhenAll(
                _posVerifier.AssertExists(command.PointOfSaleId),
                _productVerifier.AssertExists(command.ProductId),
                command.StockItemId != null?_stockItemVerifier.AssertExists(command.StockItemId.Value) : Task.CompletedTask
                );

            var offer = new Domain.Offer(command.Id, command.PointOfSaleId, command.ProductId, command.RecommendedPrice, command.StockItemId, command.ValidSince, command.ValidUntil);
            await _repository.AddAsync(offer);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("offer_already_exists", $"An offer with the ID '{command.Id}' already exists.");
            }


            await _busPublisher.Publish(new OfferCreated(offer.Id, offer.PointOfSaleId, offer.ProductId,
                                                         offer.RecommendedPrice, offer.StockItemId, offer.ValidSince, offer.ValidUntil));

            return(new IdentifierResult(offer.Id));
        }
Пример #2
0
 public async Task <ActionResult> NewOffer([FromBody] NewOffer newOffer)
 {
     if (ModelState.IsValid)
     {
         await _dataRepo.AddAsync(newOffer);                //_service.AddNewOfferAsync(newOffer);
     }
     return(Created($"api/offers/{newOffer.Title}", null)); //location header , don't return the model
 }
        public async Task <Unit> Handle(CreateOfferCommand request, CancellationToken cancellationToken)
        {
            var offer = _mapper.Map <Offer>(request);
            await _offerRepository.AddAsync(offer);

            await _offerRepository.UnitOfWork.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #4
0
 public async Task <dynamic> AddAsync(Offer item)
 {
     item.CreatedDate  = DateTime.Now;
     item.ModifiedDate = DateTime.Now;
     item.Status       = 1;
     return(new
     {
         succeeded = true,
         data = await _offerRepository.AddAsync(item)
     });
 }
        public async Task <OfferResponse> SaveAsync(Offer offer)
        {
            try
            {
                await _offerRepository.AddAsync(offer);

                await _unitOfWork.CompleteAsync();

                return(new OfferResponse(offer));
            }
            catch (Exception ex)
            {
                return(new OfferResponse($"An error ocurred while saving the offer: {ex.Message}"));
            }
        }
Пример #6
0
        public async Task <Unit> Handle(CreateOfferCommand request, CancellationToken cancellationToken)
        {
            var offer = new Offer(
                new OfferId(request.Id),
                new FacilityId(request.FacilityId),
                request.Name,
                request.Price,
                request.Currency,
                request.Duration
                );

            await repository.AddAsync(offer);

            await unitOfWork.CommitAsync();

            return(Unit.Value);
        }
        public async Task <Guid> Handle(CreateOfferDraftCommand request, CancellationToken cancellationToken)
        {
            var categoryId = Guid.Parse(request.CategoryId);
            var category   = await _categoryRepository.GetByIdAsync(categoryId);

            if (category == null)
            {
                throw new NotFoundException("Category");
            }

            var tokenPayload = _httpContextAccessor.HttpContext.User.Claims.ToTokenPayload();

            var offer = new Offer(
                ownerId: tokenPayload.UserClaims.Id,
                name: request.Name,
                description: request.Description,
                price: request.Price,
                totalStock: request.TotalStock,
                category: category
                );

            var keyValueInfos = _keyValueInfoExtractor.Extract(request.KeyValueInfos);

            offer.SetKeyValueInfos(keyValueInfos);

            var deliveryMethods = _deliveryMethodExtractor.Extract(request.DeliveryMethods);

            offer.SetDeliveryMethods(deliveryMethods);

            await _offerImagesProcessor.Process(offer, request.Images, request.ImagesMetadata);

            await _offerRepository.AddAsync(offer);

            await _offerRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync(cancellationToken);

            return(await Task.FromResult(offer.Id));
        }