/// <summary> /// Creates a new competition entrant. /// </summary> /// <param name="item"> /// The item. /// </param> /// <returns> /// The <see cref="HttpResponseMessage"/>. /// </returns> /// <remarks> /// POST competitions/enter /// </remarks> public HttpResponseMessage Post(PostEnterCompetition item) { var entrant = _mapper.Map<PostEnterCompetition, Entrant>(item); if (entrant.IsValid) { var competition = _competitionDataMapper.FindByCompetitionKey(item.CompetitionKey); competition.AddEntrant(entrant); _competitionDataMapper.Update(competition); return Request.CreateResponse(HttpStatusCode.Created, item); } return Request.CreateResponse(HttpStatusCode.BadRequest, entrant.ValidationErrors); }
public void Enter_Competition_Returns_OK_Status() { // #1 Create Competition var competition = new PostCompetition() { Answers = new[] { "Man Utd", "Man City", "Chelsea", "Arsenal" }, ClosingDate = DateTime.Now.AddMonths(1), CompetitionKey = RandomGenerator.GetRandomString(10), CorrectAnswerKey = 1, Question = "Who won the 2012 Premier League?" }; Assert.AreEqual( HttpStatusCode.Created, Client.PostAsJsonAsync(Resources.Competitions, competition).Result.StatusCode, "POSTCompetition not OK."); // #2 Enter Competition var enterCompetition = new PostEnterCompetition() { CompetitionKey = competition.CompetitionKey, ContactType = "Sms", Answer = "A", Contact = "00000111222", Source = "Sms" }; Assert.AreEqual( HttpStatusCode.Created, Client.PostAsJsonAsync(Resources.EnterCompetition, enterCompetition).Result.StatusCode, "POST EnterCompetition not OK."); // #3 Close Competition var response = Client.PutAsJsonAsync(string.Format("{0}/{1}", Resources.CloseCompetition, competition.CompetitionKey.ToLower()), new { }).Result; Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "PUT CloseCompetition not OK."); }