private decimal GetUserMonthlyAmmount(CurrencyPurchase purchase) { return(UnitOfWork.GenericRepository <CurrencyPurchase>() .GetQueryable(null) .Where(x => x.UserId == purchase.UserId && x.DateTime.Month == DateTime.Now.Month) .Sum(x => x.Amount)); }
public bool ValidatedLimit(CurrencyPurchase entity) { if (entity.IDExchangeCurrency == 1) { if (entity.Amount > 200) { return(true); } else { return(false); } } if (entity.IDExchangeCurrency == 2) { if (entity.Amount > 300) { return(true); } else { return(false); } } return(false); }
public async Task <ResponseHelper <CurrencyPurchase> > BuyCurrencyAsync(CurrencyPurchase purchase) { CurrencyQuotation currencyQuotation = await GetCurrencyQuotationAsync(purchase.ISOCode); purchase.Amount /= currencyQuotation.PurchaseRate; CurrencyApiSetting currencySetting = CurrenciesSettings.CurrenciesApiSettings .FirstOrDefault(c => c.ISOCode == purchase.ISOCode); decimal monthlyAmmout = GetUserMonthlyAmmount(purchase); List <string> errors = ValidatePurchase(purchase, currencySetting.Limit, monthlyAmmout); if (errors.Count > 0) { return(new ResponseHelper <CurrencyPurchase>(errors, statusCode: 400)); } else { purchase.DateTime = DateTime.Now; UnitOfWork.GenericRepository <CurrencyPurchase>().Add(purchase); await UnitOfWork.CompleteAsync(); return(new ResponseHelper <CurrencyPurchase>(purchase)); } }
public async Task <IActionResult> PutCurrencyPurchase(int id, CurrencyPurchase currencyPurchase) { if (id != currencyPurchase.ID) { return(BadRequest()); } _context.Entry(currencyPurchase).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CurrencyPurchaseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> purchaseCurrency(string userid, double amount, string iso) { CurrencyPurchase cp = new CurrencyPurchase(); var purchase = await cp.getCurrencyPurchase(userid, amount, iso); return(Ok(purchase)); }
public async Task <IActionResult> Add([FromBody] CurrencyPurchaseViewModel transactionViewModel) { CurrencyPurchase model = new CurrencyPurchase(); try { model.IDUser = transactionViewModel.IdUser; model.IDExchangeCurrency = transactionViewModel.IdExchangeCurrency; model.Amount = transactionViewModel.Amount; var data = await unitOfWork.Transaction.AddAsync(model); return(Ok(data)); } catch (WebException e) { throw e; } catch (CustomException e) { return(NotFound(e.Message)); } catch (Exception e) { throw e; } }
public async Task <ActionResult <CurrencyPurchase> > PostCurrencyPurchase(CurrencyPurchase currencyPurchase) { var rate = _currencyController.Currency(currencyPurchase.currencyCode).Result; List <string> value = ((Microsoft.AspNetCore.Mvc.ObjectResult)rate.Result).Value.ToString().Split(',').ToList(); if (value[0].Contains("not available")) { return(BadRequest("This currency is not available.")); } if (currencyPurchase.exchangeRate <= 0) { var value1 = String.Concat(value[0].Where(x => x == '.' || Char.IsDigit(x))); currencyPurchase.exchangeRate = Decimal.Parse(value1); } currencyPurchase.total = Math.Round(currencyPurchase.amount / currencyPurchase.exchangeRate, 2); currencyPurchase.date = DateTime.UtcNow; var limitCurrencyUser = from m in _context.CurrencyPurshaseItems select m; limitCurrencyUser = limitCurrencyUser.Where(s => s.userID.Equals(currencyPurchase.userID) & s.date.Month.Equals(currencyPurchase.date.Month) & s.currencyCode.Contains(currencyPurchase.currencyCode)); decimal totalCurrencyMonth = 0; if (limitCurrencyUser?.Any() == true) { totalCurrencyMonth = limitCurrencyUser.Sum(s => s.total); } var limitCurrency = from n in _exchangeRateContext.Currency select n; limitCurrency = limitCurrency.Where(t => t.ISOCode.Contains(currencyPurchase.currencyCode)); decimal currencyl = limitCurrency.First().Limit; if (currencyl > 0) { if ((currencyPurchase.total + totalCurrencyMonth) > currencyl) { return(BadRequest("You can't buy more than " + currencyl + " " + currencyPurchase.currencyCode + ", you already bought " + totalCurrencyMonth + " " + currencyPurchase.currencyCode + " this month.")); } } _context.CurrencyPurshaseItems.Add(currencyPurchase); await _context.SaveChangesAsync(); CreatedAtAction("GetCurrencyPurchase", new { id = currencyPurchase.ID }, currencyPurchase); return(Ok("You purchased " + currencyPurchase.total + " " + currencyPurchase.currencyCode)); }
public async Task <ActionResult <CurrencyPurchaseDTO> > GetPurchase(int id) { CurrencyPurchase purchase = await _curreciesService.GetPurchaseAsync(id); if (purchase == null) { return(NotFound()); } return(Ok(_mapper.Map <CurrencyPurchaseDTO>(purchase))); }
private List <string> ValidatePurchase(CurrencyPurchase purchase, decimal?limit, decimal monthlyAmmout) { List <string> errors = new List <string>(); if (limit != null && (purchase.Amount > limit || purchase.Amount + monthlyAmmout > limit)) { errors.Add("This transaction exceeds de limint amomunt for the currency."); } if (limit != null && monthlyAmmout > limit) { errors.Add($"The monthly amount for currency {purchase.ISOCode} has been exceeded."); } return(errors); }
public async Task <int> AddAsync(CurrencyPurchase entity) { var ServicesExtern = new Utilities(); ExchangeRate exchange_Rate = new ExchangeRate(); string err, Resultjson = string.Empty; ResultJson re = new ResultJson(); try { switch (entity.IDExchangeCurrency) { case 1: // AMERICAN DOLLAR (USD) var validated_Amount = ServicesExtern.ValidatedLimit(entity); if (validated_Amount) { err = "The amount entered should be minor or equal to $200 for American Dollar!"; re.Code = "404"; re.Message = err; Resultjson = JsonConvert.SerializeObject(re); throw new CustomException(Resultjson); } exchange_Rate = await ServicesExtern.GetExchangeRateAsync(entity.IDExchangeCurrency); break; case 2: // BRAZILIAN REAL (BRL) var validated_Amount2 = ServicesExtern.ValidatedLimit(entity); if (validated_Amount2) { err = "The amount entered should be minor or equal to $300 for Brazilian Real!"; re.Code = "404"; re.Message = err; Resultjson = JsonConvert.SerializeObject(re); throw new CustomException(Resultjson); } exchange_Rate = await ServicesExtern.GetExchangeRateAsync(entity.IDExchangeCurrency); var _4th = exchange_Rate.PurchasePrice / 4; exchange_Rate.PurchasePrice = _4th; exchange_Rate.SalePrice = 0; break; case 3: // CANADIAN DOLLAR (USD) err = "We dont have this kind of currency, we will have Canadian dollar in the future!"; re.Code = "404"; re.Message = err; Resultjson = JsonConvert.SerializeObject(re); throw new CustomException(Resultjson); default: // DOES NOT EXIST err = "This kind of currency does not exist in our database!"; re.Code = "404"; re.Message = err; Resultjson = JsonConvert.SerializeObject(re); throw new CustomException(Resultjson); } var subTotal = entity.Amount / exchange_Rate.PurchasePrice; using (var connection = new SqlConnection(configuration.GetConnectionString("DefaultConnection"))) { connection.Open(); var sqlTransaction = connection.BeginTransaction(); var para = new DynamicParameters(); para.Add("@UserID", entity.IDUser); para.Add("@DocumentID", 1); para.Add("@ExchangeCurrencyID", entity.IDExchangeCurrency); para.Add("@Description", "TRANSACTION OF CURRENCY PURCHASE"); para.Add("@Amount", entity.Amount); para.Add("@ExchangeRate", exchange_Rate.PurchasePrice); para.Add("@SubTotal", subTotal); int result = connection.Execute("sp_Insert_CurrencyPurchase", para, sqlTransaction, 0, System.Data.CommandType.StoredProcedure); if (result > 0) { sqlTransaction.Commit(); return(result); } else { sqlTransaction.Rollback(); return(0); } } } catch (WebException e) { throw e; } catch (CustomException e) { throw e; } catch (Exception e) { throw e; } }
public IActionResult UploadJsonToDb(int index, int quantity) { var path = _appEnvironment.WebRootPath + "\\jsons"; var files = Directory.GetFiles(path).Where(f => f.Contains(".json")).ToList(); for (int j = index; j < quantity + index; j++) { var text = string.Empty; if (files.Count > j) { text = System.IO.File.ReadAllText(files[j]); } else { break; } List <JsonFullObject> jsonModels = new List <JsonFullObject>(); jsonModels = JsonConvert.DeserializeObject <List <JsonFullObject> >(text); for (int i = 0; i < jsonModels.Count; i++) { switch (jsonModels[i].EventId) { case 1: var gl = new GameLaunch { Date = jsonModels[i].Date, EventId = jsonModels[i].EventId }; _db.GameLaunches.Add(gl); break; case 2: var fgl = new FirstGameLaunch { Date = jsonModels[i].Date, Gender = jsonModels[i].Parameters.Gender, Age = jsonModels[i].Parameters.Age, Country = jsonModels[i].Parameters.Country, EventId = jsonModels[i].EventId }; _db.FirstGameLaunches.Add(fgl); break; case 3: var ss = new StageStart { Date = jsonModels[i].Date, Stage = jsonModels[i].Parameters.Stage, EventId = jsonModels[i].EventId }; _db.StageStarts.Add(ss); break; case 4: var se = new StageEnd { Date = jsonModels[i].Date, Stage = jsonModels[i].Parameters.Stage, Win = jsonModels[i].Parameters.Win, Time = jsonModels[i].Parameters.Time, Income = jsonModels[i].Parameters.Income, EventId = jsonModels[i].EventId }; _db.StageEnds.Add(se); break; case 5: var ip = new IngamePurchase { Date = jsonModels[i].Date, Item = jsonModels[i].Parameters.Item, Price = jsonModels[i].Parameters.Price, EventId = jsonModels[i].EventId }; _db.IngamePurchases.Add(ip); break; case 6: var cp = new CurrencyPurchase { Date = jsonModels[i].Date, Name = jsonModels[i].Parameters.Name, Price = jsonModels[i].Parameters.Price, Income = jsonModels[i].Parameters.Income, EventId = jsonModels[i].EventId }; _db.CurrencyPurchases.Add(cp); break; } } } _db.SaveChanges(); return(RedirectToAction("Index")); }