public async Task<HttpResponseMessage> Post(Vote model) { #if DEBUG await Database.VoteIdea(model); return Request.CreateResponse(HttpStatusCode.OK); #else //using (var client = new HttpClient()) //{ // var url = GetUrl("/users"); // var response = await client.GetAsync(url); // var content = await response.Content.ReadAsStringAsync(); // dynamic users = JsonConvert.DeserializeObject<dynamic>(content); // if (users == null || users.Items == null || users.Items.Count == 0) // return; // model.VoterId = users.Items.First.Id; // var responsePost = await client.PostAsJsonAsync(GetUrl("/vote"), model); // string x = await responsePost.Content.ReadAsStringAsync(); //} return null; #endif }
public static async Task<User> AddVote(Vote newVote) { var user = await GetUserByIdAsync(newVote.VoterId); var idea = _ideas.First(i => i.Id == newVote.IdeaId); if (newVote.Points == 0) { var vote = idea.Votes.First(v => v.VoterId == newVote.VoterId); idea.Votes.Remove(vote); user.AvailablePoints += vote.Points; } else { if (await HasVoted(newVote.IdeaId, newVote.VoterId)) return user; if (idea.Votes == null) idea.Votes = new List<Vote>(); newVote.CreationDate = DateTime.Now; idea.Votes.Add(newVote); user.AvailablePoints -= newVote.Points; } return user; }
public static async Task VoteIdea(Vote vote) { if (null == vote) return; var idea = _ideas.FirstOrDefault(i => i.Id == vote.IdeaId); if (null == idea) return; idea.Votes = idea.Votes ?? new List<Vote>(); vote.CreationDate = DateTime.Now; idea.Votes.Add(vote); }
public async Task<IHttpActionResult> Vote(Vote newVote) { #if DEBUG var user = await Database.AddVote(newVote); return Ok(user); #else var url = "/vote/"; if (newVote.Points < 1) url += "unvote/"; using (var client = new HttpClient()) { var responsePost = await client.PostAsJsonAsync(GetUrl(url), newVote); var result = await responsePost.Content.ReadAsStringAsync(); if (!responsePost.IsSuccessStatusCode) return InternalServerError(new Exception(result)); } url = "/users/" + newVote.VoterId; var user = await FetchData<User>(url); if (null == user) return NotFound(); return Ok(user); #endif }