public async Task UpdateWallet(UpdateWalletDto model, long idUser) { try { var user = await _weGOPAYDbContext.User.Where(s => s.Id == idUser).FirstOrDefaultAsync(); var userId = user.UserId; var nUpdate = await _weGOPAYDbContext.Wallet.FindAsync(model.Id); if (nUpdate != null) { nUpdate.Id = model.Id; nUpdate.WalletCreationDate = DateTime.UtcNow; nUpdate.NairaBalance = GetNairaBalance(model.Currency, model.Amount, userId); nUpdate.EuroBalance = GetEuroBalance(model.Currency, model.Amount, userId); nUpdate.PoundBalance = GetPoundBalance(model.Currency, model.Amount, userId); nUpdate.USDBalance = GetDollarBalance(model.Currency, model.Amount, userId); nUpdate.YenBalance = GetYenBalance(model.Currency, model.Amount, userId); } ; _weGOPAYDbContext.Entry(nUpdate).State = EntityState.Modified; await _weGOPAYDbContext.SaveChangesAsync(); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> UpdateWallet(UpdateWalletDto walletDto) { if (!ModelState.IsValid) { return(BadRequest(ResponseMessage.Message("Invalid Model", ModelState, walletDto))); } var wallet = _walletRepository.GetWalletById(walletDto.WalletId); if (wallet == null) { return(BadRequest(ResponseMessage.Message("Unable to update wallet", "invalid wallet id", walletDto))); } var loggedInUserId = _walletRepository.GetUserId(); if (wallet.OwnerId != loggedInUserId) { return(BadRequest(ResponseMessage.Message("Invalid", "This wallet is not owned by you", walletDto))); } var updated = await _walletRepository.UpdateWallet(walletDto); if (!updated) { return(BadRequest(ResponseMessage.Message("Unable to update wallet", "error encountered while updating the wallet", walletDto))); } return(Ok(ResponseMessage.Message("Wallet successfully updated", null, walletDto))); }
public async Task UpdateWalletAsync(UpdateWalletDto dto) { if (_dbContext.Wallets.Any(w => w.Id == dto.WalletId) == false) { throw new HttpStatusException(404); } if (dto.Name == "") { throw new ValidationException(new() { { nameof(dto.Name), "Name can't be empty." } }); } List <int> userIds = await _dbContext.Users .Where(u => dto.SharedEmails.Contains(u.Email)) .Select(u => u.Id) .ToListAsync(); if (userIds.Count() < dto.SharedEmails.Count()) { throw new ValidationException(new() { { nameof(dto.SharedEmails), "Some/all emails are not associated with a user." } }); } List <int> currentUserIds = await _dbContext.Set <WalletAllowedUser>() .Where(wu => wu.WalletId == dto.WalletId) .Select(wu => wu.UserId) .ToListAsync(); var newUserAssociations = userIds .Where(uid => currentUserIds.Contains(uid) == false) .Select(uid => new WalletAllowedUser { WalletId = dto.WalletId, UserId = uid }); var deletedUserAssociations = currentUserIds .Where(uid => userIds.Contains(uid) == false) .Select(uid => new WalletAllowedUser { WalletId = dto.WalletId, UserId = uid }); _dbContext.Set <WalletAllowedUser>().RemoveRange(deletedUserAssociations); _dbContext.Set <WalletAllowedUser>().AddRange(newUserAssociations); var wallet = new Wallet { Id = dto.WalletId }; _dbContext.Attach(wallet); wallet.Name = dto.Name; await _dbContext.SaveChangesAsync(); }
public async Task <IActionResult> Update([FromBody] UpdateWalletDto updateWalletDto, string id) { var result = await _walletService.UpdateWallet(updateWalletDto, id, UserId); if (!result.Succedeed) { return(NotFound()); } return(NoContent()); }
public async Task UpdateWalletAsync(int userId, int walletId, UpdateWalletDto updateWalletDto) { var walletToUpdate = await ThrowIfNotExists(userId, walletId); await ThrowIfNameExists(userId, updateWalletDto.Name, walletToUpdate.WalletId); Mapper.Map(updateWalletDto, walletToUpdate); UnitOfWork.WalletRepository.Update(walletToUpdate); await UnitOfWork.CommitAsync(); }
public async Task <IActionResult> UpdateWallet([FromBody] UpdateWalletDto updateWallet, long idUser) { try { await _WalletService.UpdateWallet(updateWallet, idUser); return(Ok("Updated Successfully")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public async Task <Result> UpdateWallet(UpdateWalletDto updateWalletDto, string id, string userId) { var wallet = await _walletRepository.GetWithoutDependencies(id.ToDeobfuscated(), userId); if (wallet == null || !wallet.Role.HasAllPrivileges()) { return(Result <ExtendedWalletDto> .Failure()); } wallet.Wallet.Name = updateWalletDto.Name; wallet.Wallet.Category = updateWalletDto.Category.ToEnumValue <WalletCategory>(); var result = _walletRepository.Update(wallet.Wallet); return(result == 0 ? Result.Failure() : Result.Success()); }
/// <summary> /// /// </summary> /// <param name="updateWalletDto"></param> /// <returns></returns> public async Task <bool> UpdateWallet(UpdateWalletDto updateWalletDto) { var walletToUpdate = GetWalletById(updateWalletDto.WalletId); if (walletToUpdate.CurrencyId != updateWalletDto.CurrencyId) { var targetCode = _currencyRepository.GetCurrencyCode(updateWalletDto.CurrencyId); var sourceCode = _currencyRepository.GetCurrencyCode(walletToUpdate.CurrencyId); var newAmount = await CurrencyRate.ConvertCurrency(sourceCode, targetCode, walletToUpdate.Balance); walletToUpdate.CurrencyId = updateWalletDto.CurrencyId; walletToUpdate.Balance = newAmount ?? 0; } return(await UpdateWallet(walletToUpdate)); }