public async Task VoteCard(AudienceVoteUICommand command) { var client = _clientFactory.CreateClient("spring.festival.card.api"); var voteJson = new FormUrlEncodedContent(command.GetKeyValuePairs()); using var httpResponse = await client.PostAsync($"/api/audiences", voteJson); httpResponse.EnsureSuccessStatusCode(); }
public async Task <ActionResult> VoteCard(AudienceVoteUICommand command) { if (!ModelState.IsValid) { var errorMessages = new List <KeyValuePair <string, string> >(); foreach (var key in ModelState.Keys) { errorMessages.AddRange(ModelState[key].Errors .Select(error => new KeyValuePair <string, string>(key, error.ErrorMessage))); } return(BadRequest(errorMessages)); } await _audienceService.Vote(command); return(NoContent()); }
public async Task Vote(AudienceVoteUICommand command) { var audienceForVote = _mapper.Map <Audience>(command); var card = _cardRepository.Get(command.CardId); if (card == null) { throw new Exception("the card does not exist!"); } var audiences = await _audienceRepository.GetAll(); var audience = audiences.FirstOrDefault(x => x.PhoneNumber == audienceForVote.PhoneNumber); if (audience != null && audience.CardId != audienceForVote.CardId) { throw new Exception("每一位观众只能对一个节目进行投票!"); } audienceForVote.Time = audience?.Time + 1 ?? 1; if (audienceForVote.Time > 3) { throw new Exception("每一位观众最多能投三次票!"); } if (audience == null) { await _audienceRepository.Add(audienceForVote); } else { audienceForVote.Id = audience.Id; await _audienceRepository.Edit(audienceForVote); } }