public void Handler(CreateWhisperCommand command) { int id = _whisperRepository.Insert(command.Whisper); User user = _userRepository.SelectUserByAccount(command.Whisper.Account); Whisper whisper = new Whisper( id, command.Whisper.Account, user.Username, command.Whisper.Content, command.Whisper.IsPassing, command.Whisper.CommentGuids, DateTime.Now); long length = _cacheClient.AddListTop(ConstantKey.CACHE_SQUARE_WHISPER, whisper).Result; int listLength = Convert.ToInt32(length); if (listLength > 12) { _cacheClient.listPop(ConstantKey.CACHE_SQUARE_WHISPER); } JsonSerializerSettings jsonSerializerSettings = new JsonContractResolver().SetJsonSerializerSettings(); List <Whisper> whispers = _cacheClient.ListRange <Whisper>(ConstantKey.CACHE_SQUARE_WHISPER, 0, 6, jsonSerializerSettings).GetAwaiter().GetResult(); Message message = new Message(); message.Data = whispers; _singalrContent.SendAllClientsMessage(message); }
public async Task Create(string content, string account, string username, string authorization) { Whisper whisper = new Whisper(); whisper.Account = account; whisper.Content = content; whisper.CreateTime = DateTime.Now; whisper = _whisperRepoistory.Insert(whisper); WhisperDTO whisperDTO = new WhisperDTO(); whisperDTO.Id = whisper.Id.ToString(); whisperDTO.Account = whisper.Account; whisperDTO.AccountName = username; whisperDTO.Content = content; whisperDTO.CreateDate = whisper.CreateTime.ToString("yyyy-MM-dd HH:mm"); long length = await _cacheClient.AddListTop(ConstantKey.CACHE_SQUARE_WHISPER, whisperDTO); int listLength = Convert.ToInt32(length); if (listLength > 12) { await _cacheClient.listPop(ConstantKey.CACHE_SQUARE_WHISPER); } List <WhisperDTO> whispers = await _cacheClient.ListRange <WhisperDTO>(ConstantKey.CACHE_SQUARE_WHISPER, 0, 6); List <WhisperDTO> whisperDTOs = new List <WhisperDTO>(); Dictionary <string, string> accountWithName = _userRepository.AccountWithName(whispers.Select(s => s.Account)); foreach (var item in whispers) { whisperDTO = new WhisperDTO(); whisperDTO.Id = item.Id.ToString(); whisperDTO.Account = item.Account; whisperDTO.AccountName = accountWithName[item.Account]; whisperDTO.Content = item.Content; whisperDTO.CreateDate = item.CreateDate; whisperDTOs.Add(whisperDTO); } HttpClient httpClient = _httpClientFactory.CreateClient(); httpClient.DefaultRequestHeaders.Add("Authorization", authorization); string jsonValue = JsonConvert.SerializeObject(whisperDTOs); string httpUrl = ConstantKey.GATEWAY_HOST + "/sms/singlar/whisper"; try { var response = await httpClient.PostAsync(httpUrl, new StringContent(jsonValue, Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode(); ApiResult apiResult = JsonConvert.DeserializeObject <ApiResult>(await response.Content.ReadAsStringAsync()); if (apiResult.Code != HttpStatusCode.SUCCESS) { throw new HttpRequestException(apiResult.Msg); } } catch (HttpRequestException ex) { LogUtils.LogError(ex, "WhisperService.Create", ex.Message); } }