public async Task <GetOfferBasicInfoResponse> GetOfferBasicInfo( GetOfferBasicInfoRequest request, CallContext context = default) { var offer = await _offerRepository.GetByIdAsync(request.OfferId); if (offer == null) { return(new GetOfferBasicInfoResponse()); } var offerDto = new OfferDto { Id = offer.Id, Name = offer.Name, OwnerId = offer.OwnerId, AvailableStock = offer.AvailableStock, Price = offer.Price, Images = offer.Images?.Select(img => new ImageDto { Id = img.Id, Uri = img.Uri, IsMain = img.IsMain }) ?? new List <ImageDto>(), IsActive = offer.IsActive }; return(new GetOfferBasicInfoResponse { Offer = offerDto }); }
public async Task <CartItemDto> Handle(AddToCartCommand request, CancellationToken cancellationToken) { var offerId = Guid.Parse(request.OfferId); var offersServiceClient = _offersServiceClientFactory.Create(_endpointsConfig.Offers.Grpc.ToString()); var grpcRequest = new GetOfferBasicInfoRequest { OfferId = offerId }; var grpcResponse = await offersServiceClient.GetOfferBasicInfo(grpcRequest); var offer = grpcResponse.Offer; if (!offer.IsActive) { throw new CartsDomainException("Offer is not active"); } if (request.Quantity > offer.AvailableStock) { throw new CartsDomainException($"Quantity out of range. AvailableStock for offer {offer.Id} is {offer.AvailableStock}"); } var userId = _httpContext.User.Claims.ToTokenPayload().UserClaims.Id; if (userId == offer.OwnerId) { throw new CartsDomainException("Buying from himself/herself is illegal"); } var cart = await _cartRepository.GetOrCreateByUserIdAsync(userId); var imageUri = offer.Images.FirstOrDefault(x => x.IsMain)?.Uri; var cartItem = new CartItem(cart.Id, cart.UserId, Guid.Parse(request.OfferId), offer.OwnerId, offer.Name, offer.Price, request.Quantity, offer.AvailableStock, imageUri); cart.AddCartItem(cartItem); _cartRepository.Update(cart); await _cartRepository.UnitOfWork.SaveChangesAndDispatchDomainEventsAsync(cancellationToken); _logger.LogWithProps(LogLevel.Debug, "Offer added to cart", "OfferId".ToKvp(request.OfferId), "CartId".ToKvp(cart.Id), "CartItemId".ToKvp(cartItem.Id)); return(cartItem.ToDto()); }