public async Task <int> addAddress(AddressEntity address, string id) { var user = (from i in db.Users where i.userID == id select i).FirstOrDefault(); user.Address = address; db.Update(user); return(await db.SaveChangesAsync()); }
public async Task <int> setAmount(string id, int amount) { var query = (from i in db.Wallets where i.userID == id select i).FirstOrDefault(); query.Amount += amount; db.Update(query); return(await db.SaveChangesAsync()); }
public async Task <IActionResult> Put(int id, [FromBody] DailyExpense value) { if (value == null) { return(BadRequest()); } var spentItem = await _context.Wallet.FindAsync(id); if (spentItem == null) { _logger.LogInformation($"Daily Expense for {id} does not exists!!"); return(NotFound()); } //Assign Values // Use AutoMapper if more properties spentItem.Amount = value.Amount; spentItem.Description = value.Description; spentItem.SpentOn = value.SpentOn; _context.Update(spentItem); await SaveAsync(); return(NoContent()); }
public async Task <IActionResult> PutExchange(int userid, Exchange exchange) { if (!Enum.IsDefined(typeof(CurrencyEnum), exchange?.InputCurrency) || !Enum.IsDefined(typeof(CurrencyEnum), exchange?.OutputCurrency)) { return(BadRequest()); } var user = await _context.Users.Include(c => c.Currencies).Where(i => i.Id == userid).FirstOrDefaultAsync(); if (user is null || !user.Currencies.Any(x => x.CurrencyCode.Equals(exchange.InputCurrency)) || user.Currencies.Where(x => x.CurrencyCode.Equals(exchange.InputCurrency)).First().Ammount < exchange.Ammount) { return(NotFound()); } var client = new RestClient($"https://api.exchangeratesapi.io/latest?symbols={exchange.OutputCurrency}&base={exchange.InputCurrency}"); var request = new RestRequest(Method.GET); IRestResponse response = await client.ExecuteAsync(request); var rateModel = JsonConvert.DeserializeObject <Rate>(response.Content); user.Currencies.First(x => x.CurrencyCode.Equals(exchange.InputCurrency)).Ammount -= exchange.Ammount; var rate = ParseStr(rateModel.rates.FirstOrDefault().Value); if (!user.Currencies.Any(x => x.CurrencyCode.Equals(exchange.OutputCurrency))) { user.Currencies.Add(new Currency() { CurrencyCode = exchange.OutputCurrency, Ammount = Convert.ToDecimal((double)exchange.Ammount * rate) }); } else { user.Currencies.First(x => x.CurrencyCode.Equals(exchange.OutputCurrency)).Ammount += Convert.ToDecimal((double)exchange.Ammount * rate); } _context.Update(user); await _context.SaveChangesAsync(); return(Ok(response.Content)); }
public async Task <int> updateUser(UserEntity user) { var _user = (from i in db.Users where i.userID == user.userID select i).FirstOrDefault(); _user.FirstName = user.FirstName; _user.LastName = user.LastName; db.Update(_user); return(await db.SaveChangesAsync()); }