private async Task <HttpResponseMessage> SendExchangeRequest(ExchangeRequestModel exchangeModel, HttpClient client, string studentId) { exchangeModel.StudentId = studentId; var jsonModel = JsonConvert.SerializeObject(exchangeModel); var content = new StringContent(jsonModel, Encoding.UTF8, "application/json"); return(await client.PostAsync(ExchangeUri, content)); }
public async Task <IActionResult> ExchangeConfirm([FromBody] ExchangeRequestModel request) { try { var blockChangeRequest = new BlockChangeRequest(); blockChangeRequest.BlockFrom = BlockForExchangeModel.ConvertToBlock(request.BlockFrom); blockChangeRequest.BlockTo = BlockForExchangeModel.ConvertToBlock(request.BlockTo); blockChangeRequest.Status = ExchangeStatus.WaitingForExchange; blockChangeRequest.DateOfCreation = DateTime.Now; blockChangeRequest.StudentId = Guid.Parse(request.StudentId); var res = await _blockChangesService.AddAndFindMatch(blockChangeRequest); if (res != (null, null)) { var student1 = await _studentService.FindByIdAsync(res.Item1.StudentId); student1.Timetable.RemoveBlock(res.Item1.BlockFrom.BlockId); student1.Timetable.AddNewBlock(res.Item1.BlockTo.Clone()); await _studentService.UpdateStudentAsync(student1); var user1 = await _userService.GetUserByIdAsync(student1.UserId.ToString()); if (user1 == null) { string message = $"Cannot find user with ID {res.Item1.StudentId}."; _logger.LogError(message); return(NotFound(message)); } var student2 = await _studentService.FindByIdAsync(res.Item2.StudentId); student2.Timetable.RemoveBlock(res.Item2.BlockFrom.BlockId); student2.Timetable.AddNewBlock(res.Item2.BlockTo.Clone()); await _studentService.UpdateStudentAsync(student2); var user2 = await _userService.GetUserByIdAsync(student2.UserId.ToString()); if (user2 == null) { string message = $"Cannot find user with ID {res.Item2.StudentId}."; _logger.LogError(message); return(NotFound(message)); } string callbackUrl1 = new Uri(_baseUrl, $@"getStudentTimetable/{user1.Email}").ToString(); string callbackUrl2 = new Uri(_baseUrl, $@"getStudentTimetable/{user2.Email}").ToString(); if (!_emailService.SendConfirmationEmail(user1.Email, callbackUrl1, "ConfirmExchangeEmail")) { string message = $"Error when sending confirmation email to user {user1.Email}."; _logger.LogError(message); } if (!_emailService.SendConfirmationEmail(user2.Email, callbackUrl2, "ConfirmExchangeEmail")) { string message = $"Error when sending confirmation email to user {user2.Email}."; _logger.LogError(message); } return(Ok(res)); } return(Ok(null)); } catch { string message = $"Error when creating block change request"; _logger.LogError(message); return(ErrorResponse(message)); } }
public async Task <IActionResult> StartMonitorAsync([FromBody] ExchangeRequestModel model) { var user = await _dbContext.GetCurrentUserAsync(User); if (user == null) { return(NotFound(new { error = "User not found" })); } if (string.IsNullOrEmpty(user.TempAddress)) { return(BadRequest(new { error = "Not set temp address" })); } var item = _dbContext.ExchangeStatuses.FirstOrDefault(s => s.StartTx == model.Tx); if (item == null) { var rate = await GetRateAsync(model.Count); item = new ExchangeStatus { StartTx = model.Tx, CurrentTx = model.Tx, CurrentStep = 0, Rate = rate.rate, CreatedByUser = user, EthAmount = rate.amount.ToString(), TokenCount = model.Count }; await _dbContext.AddAsync(item); await _dbContext.SaveChangesAsync(); } //if (string.IsNullOrEmpty(user.ExchangerContract)) //{ // var rate = await GetRateAsync(model.Count); // if (string.IsNullOrEmpty(user.Wallet) || !await _contract.CheckWhitelistAsync(user.Wallet)) // { // return BadRequest(new { rate, user.Wallet, error = "Not in whitelist" }); // } // var addr = await _dbContext.Addresses.FindAsync(user.TempAddress); // string newContractAddr = await _contract.CreateExchangerAsync( // _crypto.Decrypt(addr.Exchanger.StringToByteArray()), // user.Wallet, // new HexBigInteger(rate.amount), // new HexBigInteger(rate.rate)); // var hash = await _contract.AddAddressToWhitelistAsync(newContractAddr); // await _contract.WaitReciept(hash); // user.ExchangerContract = newContractAddr; // await _dbContext.SaveChangesAsync(); // await _dbContext.AddAsync(new ExchangeStatus // { // StartTx = model.Tx, // CurrentTx = model.Tx, // CreatedByUser = user, // EthAmount = rate.amount.ToString(), // }); // user.TempAddress = null; // await _dbContext.SaveChangesAsync(); //} return(Ok(item.Id)); }