public async Task <IActionResult> GetOffer(int id)
        {
            Offer offer = await offerRepository.FindByIdAsync(id);

            if (offer == null)
            {
                return(NotFound());
            }

            return(new ObjectResult(offer));
        }
Beispiel #2
0
            public async Task <OfferVM> Handle(OfferByIdQuery request, CancellationToken cancellationToken)
            {
                var offerFromRepo = await _offersRepository.FindByIdAsync(request.OfferId);

                if (offerFromRepo == null)
                {
                    throw new OfferNotFoundException(request.OfferId);
                }

                var offerToReturn = _mapper.Map <OfferVM>(offerFromRepo);

                return(offerToReturn);
            }
Beispiel #3
0
            public async Task <string> Handle(UpdateOfferCommand request, CancellationToken cancellationToken)
            {
                var offerFromRepo = await _offerRepository.FindByIdAsync(request.OfferId);

                if (offerFromRepo == null)
                {
                    throw new OfferNotFoundException(request.OfferId);
                }

                offerFromRepo.UpdateOffer(request.Name, request.PhotoUrl, request.StartDate, request.EndDate);

                _offerRepository.Update(offerFromRepo);

                await _offerRepository.UnitOfWork.SaveEntitiesSeveralTransactionsAsync(cancellationToken);

                return(offerFromRepo.Id.ToString());
            }
Beispiel #4
0
            public async Task <MediatR.Unit> Handle(DeleteOfferCommand request, CancellationToken cancellationToken)
            {
                // get offer by id
                var offerFromRepo = await _offerRepository.FindByIdAsync(request.OfferId);

                if (offerFromRepo == null)
                {
                    throw new OfferNotFoundException(request.OfferId);
                }


                // we call delete offer to rase delete offer event to sync with algolia
                offerFromRepo.DeleteOffer();

                // update offer with the new unit deleted
                _offerRepository.Update(offerFromRepo);

                // save changes in the database and rase OfferUpdated event
                await _offerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

                return(MediatR.Unit.Value);
            }
            public async Task <string> Handle(AddProductToOfferCommand request, CancellationToken cancellationToken)
            {
                var offerFromRepo = await _offerRepository.FindByIdAsync(request.OfferId);

                if (offerFromRepo == null)
                {
                    throw new OfferNotFoundException(request.OfferId);
                }

                var productVM = await _mediator.Send(new ProductByIdQuery { ProductId = request.ProductId }, cancellationToken);

                var product = offerFromRepo.AddProductToOffer(productVM.Id, productVM.Name, productVM.Barcode, productVM.PhotoUrl, productVM.AvailableToSell, productVM.Brand.Name, productVM.ProductCategory.Name);

                foreach (var unit in productVM.Units)
                {
                    product.AddUnit(unit.Id, unit.Name, unit.Count, unit.ContentCount, unit.Price, unit.SellingPrice, unit.Weight, unit.IsAvailable);
                }

                _offerRepository.Update(offerFromRepo);

                await _offerRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

                return(offerFromRepo.Id.ToString());
            }