public async Task <DealDetailsViewModel> GetById(int id, int userId) { var currentUser = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId); var deal = await _context.Deals .Include(x => x.Owner) .Include(x => x.PaymentPlan.Payments) .Include(x => x.PaymentPlan.BankTransactions).FirstOrDefaultAsync(x => x.Id == id); var result = new DealDetailsViewModel { CurrentUserId = userId, IsCurrentUserActive = currentUser.IsActive, DealOwner = Mapper.Map <UserModel>(deal.Owner), Offers = Mapper.Map <IList <OfferModel> >(deal.Offers), Deal = Mapper.Map <DealModel>(deal), PaymentPlanModel = Mapper.Map <PaymentPlanModel>(deal.PaymentPlan) }; if (deal.PaymentPlan != null) { var allPaidBefore = deal.PaymentPlan.Payments.Where(x => x.IsPaid).Select(x => x.Amount + x.Liability).Sum(); //some extra money before previous payment result.ExtraMoney = deal.PaymentPlan.BankTransactions.Select(x => x.Amount).Sum() - allPaidBefore; result.Payments = Mapper.Map <IList <PaymentModel> >(deal.PaymentPlan.Payments.CalculateLiability(result.ExtraMoney, deal)); } //save calculatrd liability await _context.SaveChangesAsync(); return(result); }
//public async Task<UserActivityViewModel> GetProfileViewModel(int currentUserId) //{ // var offers = await _context.Offers.Where(x => x.OffererId == currentUserId).ToListAsync(); // var deals = await _context.Deals.Where(x => x.OwnerId == currentUserId).ToListAsync(); // var user = await _context.Users.FirstAsync(x => x.Id == currentUserId); // return new UserActivityViewModel // { // Deals = Mapper.Map<List<DealModel>>(deals), // Offers = Mapper.Map<IList<OfferModel>>(offers), // IsCurrentUserActive = user.IsActive // }; //} public async Task ActivateUser(int userId) { var user = await _context.Users.FirstAsync(x => x.Id == userId); user.IsActive = true; user.Rating = Rating.AfterActivation; await _context.SaveChangesAsync(); }
public async Task <PaymentResult> LendMoney(VisaPaymentViewModel visaPaymentViewModel, int currentUserId) { var deal = await _context.Deals .FirstAsync(x => x.Id == visaPaymentViewModel.DealId); var recipient = deal.Owner; var sender = await _context.Users.FirstAsync(x => x.Id == currentUserId); if (!sender.Offers.Exists(x => x.IsApproved && x.DealId == deal.Id)) { throw new AccessViolationException(); } if (Math.Abs(deal.Amount - visaPaymentViewModel.PaymentCount) > NumericConstants.Eps) { return(PaymentResult.Error); } var result = _bankApi.DoWithVisa( new BankVisaTransaction { Amount = visaPaymentViewModel.PaymentCount, RecipientAccountNumber = recipient.CardNumber, SenderCardNumber = visaPaymentViewModel.CardNumber, SenderCcvCode = visaPaymentViewModel.CvvCode, SenderName = visaPaymentViewModel.Name, SenderValidBefore = visaPaymentViewModel.ValidBefore }); switch (result) { case BankResponse.Success: deal.DealStatus = DealStatus.InProgress; deal.PaymentPlan = GeneratePlan(deal); deal.CreditTransaction = GenerateCreditTransaction(sender, recipient, visaPaymentViewModel.PaymentCount); await _context.SaveChangesAsync(); return(PaymentResult.Success); case BankResponse.NotEnoughtMoney: return(PaymentResult.NotEnoughtMoney); default: return(PaymentResult.Error); } }
public async Task ApproveOffer(int offerId, int currentUserId) { var offer = await _context.Offers.SingleAsync(x => x.Id == offerId); if (offer.Deal.OwnerId != currentUserId) { throw new AccessViolationException("User try to approve the 2nd offer for one deal."); } if (offer.Deal.Offers.Any(x => x.IsApproved)) { throw new AccessViolationException("User try to approve offer for foreign deal."); } offer.IsApproved = true; var deal = offer.Deal; deal.DealStatus = DealStatus.WaitForApprove; await _context.SaveChangesAsync(); }