//[ApiExplorerSettings(IgnoreApi= true)]
        public async Task <IActionResult> Post([FromForm] IFormFile Image, [FromRoute] string name)
        {
            Guid             userId           = Guid.Parse(sharedIdentityService.GetUserId);
            ProductCreateDto productCreateDto = new()
            {
                Image = Image,
                Name  = name
            };

            productCreateDto.CreatedUserId = userId;
            var imageUploadResponse = await imageService.UploadImageAsync(productCreateDto.Image);

            if (!imageUploadResponse.IsSuccessful)
            {
                logger.LogResponse(imageUploadResponse, "Ürün resmi yükleme başarısız");
                return(CreateResponseInstance(imageUploadResponse));
            }


            productCreateDto.ImageName = imageUploadResponse.Data;
            Product result = await productGenericService.AddAsync(productCreateDto);

            await productGenericService.Commit();

            Response <ProductDto> response = Response <ProductDto> .Success(mapper.Map <ProductDto>(result), StatusCodes.Status201Created);

            logger.LogInformation($"api/product/addproduct calling enpoint");
            logger.LogInformation($" adding product name='${productCreateDto.Name}'");
            return(CreateResponseInstance(response));
        }
Example #2
0
        public async Task ProductDepositRequestVerifyConditionAsync(VerifyDto dto)
        {
            if (dto.Verify.HasValue && dto.Verify.Value)
            {
                var entityDto = await genericProductDepositRequestService.GetByIdAsync <ProductDepositRequestDto>(dto.Id);

                var walletDto = await genericWalletService.GetByUserIdAsync <WalletDto>(entityDto.CreatedUserId);

                var productItem = await productItemService.GetByProductIdWithWalletIdAsync(walletDto.Id, entityDto.ProductId);

                if (productItem == null)
                {
                    productItem = await genericProductItemService.AddAsync(new ProductItemCreateDto()
                    {
                        CreatedUserId = entityDto.CreatedUserId,
                        ProductId     = entityDto.ProductId,
                        WalletId      = walletDto.Id,
                        Amount        = entityDto.Amount
                    });
                }
                else
                {
                    productItem.Amount      += entityDto.Amount;
                    productItem.UpdateUserId = dto.UpdateUserId;
                    var updateDto = mapper.Map <ProductItemUpdateDto>(productItem);
                    await genericProductItemService.UpdateAsync(updateDto);
                }
                await genericProductItemService.Commit();
            }
        }
Example #3
0
        public async Task DepositRequestVerifyConditionAsync(VerifyDto dto)
        {
            if (dto.Verify.HasValue && dto.Verify.Value)
            {
                var entityDto = await genericDepositRequestService.GetByIdAsync <DepositRequestDto>(dto.Id);

                var walletDto = await genericWalletService.GetByUserIdAsync <WalletDto>(entityDto.CreatedUserId);

                var exchangeResponse = await httpClient.GetFromJsonAsync <ExchangeResponse>("https://api.exchangerate.host/latest?base=TRY&symbols=USD,EUR,GBP,TRY");

                var ratio = exchangeResponse.Rates.GetMoney(entityDto.MoneyType);
                walletDto.Money += entityDto.Amount / ratio;
                var updateDto = mapper.Map <WalletUpdateDto>(walletDto);
                updateDto.UpdateUserId = dto.UpdateUserId;
                await genericWalletService.UpdateAsync(updateDto);

                await genericWalletService.Commit();
            }
        }
Example #4
0
        public async Task <IActionResult> CreateWallet()
        {
            Response <NoContent> response = null;
            var userId  = Guid.Parse(sharedIdentityService.GetUserId);
            var isExist = await walletService.WalletIsExistByUserIdAsync(userId);

            if (!isExist)
            {
                WalletCreateDto createWallet = new()
                {
                    CreatedUserId = userId,
                    UserId        = userId
                };

                var wallet = await walletGenericService.AddAsync(createWallet);

                await walletGenericService.Commit();

                if (wallet is null)
                {
                    response = Response <NoContent> .Fail(
                        statusCode : StatusCodes.Status500InternalServerError,
                        isShow : false,
                        path : "api/wallet/cratewallet",
                        errors : "Cüzdan oluşturulurken bir hata ile karşılaşıldı"
                        );

                    logger.LogResponse(response, "Cüzdan oluşturulamadı.");
                    return(CreateResponseInstance(response));
                }
            }
            response = Response <NoContent> .Success(StatusCodes.Status201Created);

            logger.LogResponse(response, "Cüzdan başarıyla oluşturuldu/getirildi.");
            return(CreateResponseInstance(response));
        }
    }
        public async Task <IActionResult> Post(GenaralCreateDto model)
        {
            Guid userId = Guid.Parse(sharedIdentityService.GetUserId);

            model.UserId = userId;
            Response <NoContent> response;

            try
            {
                IDTO dto = model.RequestType switch
                {
                    RequestType.Deposit => mapper.Map <CreateDepositRequest>(model),
                    RequestType.Product => mapper.Map <CreateProductDepositRequest>(model),
                    RequestType.NewType => mapper.Map <CreateNewTypeRequest>(model),
                    _ => throw new CustomException()
                };
                IEntityBase entity = null;
                switch (model.RequestType)
                {
                case RequestType.Deposit:
                    entity = await genericDepositRequestService.AddAsync(dto);

                    entity.CreatedUserId = userId;
                    await genericDepositRequestService.Commit();

                    break;

                case RequestType.Product:
                    entity = await genericProductRequestService.AddAsync(dto);

                    entity.CreatedUserId = userId;
                    await genericProductRequestService.Commit();

                    break;

                case RequestType.NewType:
                    entity = await genericNewTypeRequestService.AddAsync(dto);

                    entity.CreatedUserId = userId;
                    await genericNewTypeRequestService.Commit();

                    break;

                default:
                    throw new CustomException();
                }
                response = Response <NoContent> .Success(StatusCodes.Status201Created);

                logger.LogResponse(response, "Request başarıyla oluşturuldu ve eklendi.");
            }
            catch
            {
                response = Response <NoContent> .Fail(
                    statusCode : StatusCodes.Status400BadRequest,
                    isShow : true,
                    path : "[POST] api/request",
                    errors : "Desteklenmeyen istek tipi"
                    );

                logger.LogResponse(response, "Desteklenmeyen istek tipi.");
            }
            return(CreateResponseInstance(response));
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            await Task.Delay(5000, stoppingToken);

            while (!stoppingToken.IsCancellationRequested)
            {
                if (ScannerServiceState.IsScanned)
                {
                    var limitOptionRequests = (await limitOptionRequestGenericRepository.GetAllAsync()).Where(x => x.InProgress && !x.IsDeleted).ToList();
                    var marketItems         = (await genericMarketItemRepository.GetAllAsync()).Where(x => x.InProgress && !x.IsDeleted).ToList();
                    foreach (var marketItem in marketItems)
                    {
                        var requests = limitOptionRequests.Where(x => x.ProductId == marketItem.ProductId && x.UnitPrice == marketItem.UnitPrice).OrderBy(x => x.CreatedTime).ToList();
                        foreach (var request in requests)
                        {
                            if (!marketItem.InProgress)
                            {
                                break;
                            }
                            request.Wallet = await walletGenericRepository.GetByIdAsync(request.WalletId);

                            double satinalinabilecekitemsayisi = request.Wallet.Money / (marketItem.UnitPrice * (1 + ComissionRate));
                            double satinalinacaksayisi         = 0;
                            if (request.Amount <= satinalinabilecekitemsayisi)
                            {
                                if (request.Amount >= marketItem.Amount)
                                {
                                    satinalinacaksayisi = marketItem.Amount;
                                }
                                else
                                {
                                    satinalinacaksayisi = request.Amount;
                                }
                            }
                            else
                            {
                                satinalinacaksayisi = satinalinabilecekitemsayisi;
                            }


                            //Limit islemina aktarilan para ve adet sistemi guncellendi
                            request.Money       -= satinalinacaksayisi * (marketItem.UnitPrice * (1 + ComissionRate));
                            request.Amount      -= satinalinacaksayisi;
                            request.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                            if (request.Amount == 0)
                            {
                                request.InProgress = false;
                            }
                            await limitOptionRequestGenericRepository.UpdateAsync(request);

                            await limitOptionRequestGenericRepository.Commit();

                            //satan kullaniciya parayi ver
                            WalletUpdateDto sellerWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(marketItem.WalletId);

                            sellerWallet.Money       += satinalinacaksayisi * marketItem.UnitPrice;
                            sellerWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                            await genericWalletService.UpdateAsync(sellerWallet);

                            //Accounting UPDATE
                            WalletUpdateDto accountingWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(AccountingState.AccountingWalletId);

                            accountingWallet.Money       += satinalinacaksayisi * marketItem.UnitPrice * ComissionRate;
                            accountingWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                            await genericWalletService.UpdateAsync(accountingWallet);

                            await genericWalletService.Commit();

                            //kullanicinin hesabindaki urun adedini guncelle
                            var productItem = await productItemService.GetAsync(request.WalletId, request.ProductId);

                            productItem.Amount      += satinalinacaksayisi;
                            productItem.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                            await genericProductItemRepository.UpdateAsync(productItem);

                            await genericProductItemRepository.Commit();

                            //marketItem urun adedini guncelle
                            marketItem.Amount -= satinalinacaksayisi;
                            if (marketItem.Amount == 0)
                            {
                                marketItem.InProgress = false;
                            }
                            MarketItemUpdateDto marketItemUpdateDto = mapper.Map <MarketItemUpdateDto>(marketItem);
                            marketItemUpdateDto.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                            await genericMarketItemService.UpdateAsync(marketItemUpdateDto);

                            await genericMarketItemService.Commit();

                            //islem gecmisi olustur
                            TransactionCreateDto transaction = new()
                            {
                                Amount         = satinalinacaksayisi,
                                CreatedUserId  = Guid.Parse(UserStringInfo.SystemUserId),
                                ProductId      = request.ProductId,
                                UnitPrice      = marketItem.UnitPrice,
                                WalletIdBuyer  = request.WalletId,
                                WalletIdSeller = sellerWallet.Id
                            };
                            await genericTransactionService.AddAsync(transaction);

                            await genericTransactionService.Commit();

                            genericTransactionService.BeginTransaction();
                            genericMarketItemService.BeginTransaction();
                            genericProductItemRepository.BeginTransaction();
                            genericWalletService.BeginTransaction();
                            limitOptionRequestGenericRepository.BeginTransaction();
                        }
                    }
                    ScannerServiceState.IsScanned = false;
                }
            }
        }
    }
        public async Task <Response <NoContent> > BuyAsync(BuyModel buyModel)
        {
            Response <NoContent> response = null;

            DTO.Product.ProductTradeDto productTradeDto = await productService.GetProductByIdAsync(buyModel.ProductId);

            if (productTradeDto.Amount < buyModel.Amount)
            {
                response = Response <NoContent> .Fail(
                    statusCode : StatusCodes.Status400BadRequest,
                    isShow : true,
                    path : "[post] api/trade",
                    errors : "Piyasada bu miktarda ürün bulunmamaktadır"
                    );

                return(response);
            }

            List <MarketItemDto> marketItems = productTradeDto.MarketItems.Where(x => x.WalletId != buyModel.WalletId).OrderBy(x => x.UnitPrice).ToList();

            double count = 0;

            if (marketItems != null && marketItems.Any())
            {
                foreach (MarketItemDto marketItem in marketItems)
                {
                    if (count == buyModel.Amount)
                    {
                        break;
                    }
                    double buyToCount = buyModel.Amount - count;
                    double satinalinabilecekitemsayisi = buyModel.WalletDto.Money / (marketItem.UnitPrice * (1 + ComissionRate));
                    double satinalinacaksayisi         = 0;
                    if (buyToCount <= satinalinabilecekitemsayisi)
                    {
                        if (buyToCount >= marketItem.Amount)
                        {
                            satinalinacaksayisi = marketItem.Amount;
                        }
                        else if (marketItem.Amount > buyToCount)
                        {
                            satinalinacaksayisi = buyToCount;
                        }
                    }
                    else
                    {
                        satinalinacaksayisi = satinalinabilecekitemsayisi;
                    }

                    try
                    {
                        count += satinalinacaksayisi;
                        //Kullanicinin hesabindan satin alinacak urunun parasini dus
                        buyModel.WalletDto.Money -= satinalinacaksayisi * (marketItem.UnitPrice * (1 + ComissionRate));
                        await genericWalletService.UpdateAsync(new WalletUpdateDto()
                        {
                            Id           = buyModel.WalletDto.Id,
                            Money        = buyModel.WalletDto.Money,
                            UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId)
                        });

                        //satan kullaniciya parayi ver
                        WalletUpdateDto sellerWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(marketItem.WalletId);

                        sellerWallet.Money       += satinalinacaksayisi * marketItem.UnitPrice;
                        sellerWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                        await genericWalletService.UpdateAsync(sellerWallet);

                        //Accounting UPDATE
                        WalletUpdateDto accountingWallet = await genericWalletService.GetByIdAsync <WalletUpdateDto>(AccountingState.AccountingWalletId);

                        accountingWallet.Money       += satinalinacaksayisi * marketItem.UnitPrice * ComissionRate;
                        accountingWallet.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                        await genericWalletService.UpdateAsync(accountingWallet);

                        await genericWalletService.Commit();

                        //kullanicinin hesabindaki urun adedini guncelle
                        buyModel.ProductItem.Amount      += satinalinacaksayisi;
                        buyModel.ProductItem.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                        await genericProductItemRepository.UpdateAsync(buyModel.ProductItem);

                        await genericProductItemRepository.Commit();

                        //marketItem urun adedini guncelle
                        marketItem.Amount -= satinalinacaksayisi;
                        if (marketItem.Amount == 0)
                        {
                            marketItem.InProgress = false;
                        }
                        MarketItemUpdateDto marketItemUpdateDto = mapper.Map <MarketItemUpdateDto>(marketItem);
                        marketItemUpdateDto.UpdateUserId = Guid.Parse(UserStringInfo.SystemUserId);
                        await genericMarketItemService.UpdateAsync(marketItemUpdateDto);

                        await genericMarketItemService.Commit();

                        //islem gecmisi olustur
                        TransactionCreateDto transaction = new()
                        {
                            Amount         = satinalinacaksayisi,
                            CreatedUserId  = Guid.Parse(UserStringInfo.SystemUserId),
                            ProductId      = buyModel.ProductId,
                            UnitPrice      = marketItem.UnitPrice,
                            WalletIdBuyer  = buyModel.WalletId,
                            WalletIdSeller = sellerWallet.Id
                        };
                        await genericTransactionService.AddAsync(transaction);

                        await genericTransactionService.Commit();

                        response = Response <NoContent> .Success(StatusCodes.Status201Created);
                    }
                    catch
                    {
                        await genericWalletService.Commit(false);

                        await genericProductItemRepository.Commit(false);

                        await genericMarketItemService.Commit(false);

                        await genericTransactionService.Commit(false);

                        response = Response <NoContent> .Fail(
                            statusCode : StatusCodes.Status500InternalServerError,
                            isShow : true,
                            path : "[post] api/trade",
                            errors : "Satin alim gerceklesirken bir hata meydana geldi"
                            );
                    }
                }
            }

            if (response == null)
            {
                response = Response <NoContent> .Success(StatusCodes.Status201Created);
            }
            return(response);
        }