public IHttpActionResult PostChili(Chili chili) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.Chilis.Add(chili); try { db.SaveChanges(); } catch (DbUpdateException) { if (ChiliExists(chili.Id)) { return(Conflict()); } else { throw; } } return(CreatedAtRoute("DefaultApi", new { id = chili.Id }, chili)); }
public Taco(IngredientFactory ingredientFactory) { _poultry = ingredientFactory.GetPoultry(); _meat = ingredientFactory.GetMeat(); _vegetation = ingredientFactory.GetVegetation(); _cheese = ingredientFactory.GetCheese(); _guacomole = ingredientFactory.GetGuacomole(); _rice = ingredientFactory.GetRice(); _beans = ingredientFactory.GetBeans(); _creamCheese = ingredientFactory.GetCreamCheese(); _tomato = ingredientFactory.GetTomato(); _chili = ingredientFactory.GetChili(); }
public IHttpActionResult DeleteChili(Guid id) { Chili chili = db.Chilis.Find(id); if (chili == null) { return(NotFound()); } db.Chilis.Remove(chili); db.SaveChanges(); return(Ok(chili)); }
public IHttpActionResult PutChili(Guid id, Chili chili) { if (!User.Identity.IsAuthenticated) { return(Unauthorized()); } var userId = Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(User.Identity); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != chili.Id) { return(BadRequest()); } db.Entry(chili).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ChiliExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public IHttpActionResult JoinParty(PersonDTO personDto) { if (!User.Identity.IsAuthenticated) { return(Unauthorized()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userId = Guid.Parse(Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(User.Identity)); var party = db.Parties.Where(p => p.Code == personDto.PartyCode).FirstOrDefault(); var partyDto = new PartyDTO(); if (party == null) { ModelState.AddModelError("PartyCode", string.Format("Party Code '{0}' is not a valid party code. Confirm the party code with your party host.", personDto.PartyCode)); return(BadRequest(ModelState)); } partyDto.PartyName = party.Name; var person = db.People.Where(p => p.UserId == userId).FirstOrDefault(); if (db.PartyPersons.Where(p => p.PartyId == party.Id && p.PersonId == person.Id).FirstOrDefault() != null) { ModelState.AddModelError("PartyPerson", string.Format("You've already joined this party! Go to your Account Profile page to manage your info.", personDto.PartyCode)); return(BadRequest(ModelState)); } var partyPerson = new PartyPerson { Id = Guid.NewGuid(), PartyId = party.Id, PersonId = person.Id }; db.PartyPersons.Add(partyPerson); if (personDto.EnterChili) { var chef = new Chef { Id = Guid.NewGuid(), PartyPersonId = partyPerson.Id }; db.Chefs.Add(chef); var chiliNumber = 0; var numberOfChilis = db.Chilis.Count(c => c.PartyId == party.Id); if (numberOfChilis >= party.MaxChilis) { chiliNumber = numberOfChilis + 1; } else { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); while (db.Chilis.Where(c => c.Number == chiliNumber && c.PartyId == party.Id).FirstOrDefault() != null) { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); } } var chili = new Chili { Id = Guid.NewGuid(), ChefId = chef.Id, Name = personDto.ChiliName, Number = chiliNumber, PartyId = party.Id }; db.Chilis.Add(chili); partyDto.ChiliNumber = chiliNumber; } db.SaveChanges(); return(Ok(partyDto)); }
public IHttpActionResult HostParty(PartyDTO partyDto) { if (!User.Identity.IsAuthenticated) { return(Unauthorized()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userIdString = Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(User.Identity); var userId = Guid.Parse(userIdString); var person = db.People.Where(p => p.UserId == userId).FirstOrDefault(); var returnedPartyDto = new PartyDTO(); var partyCode = Utilities.Utilities.GeneratePartyCode(4); while (db.Parties.Where(p => p.Code == partyCode).FirstOrDefault() != null) { partyCode = Utilities.Utilities.GeneratePartyCode(4); } var party = new Party { Id = Guid.NewGuid(), Name = partyDto.PartyName, Date = partyDto.PartyDate, Code = partyCode, HostId = person.Id, MaxChilis = partyDto.MaxChilis }; db.Parties.Add(party); returnedPartyDto.PartyCode = partyCode; var partyPerson = new PartyPerson { Id = Guid.NewGuid(), PartyId = party.Id, PersonId = person.Id }; db.PartyPersons.Add(partyPerson); if (partyDto.EnterChili.HasValue && partyDto.EnterChili.Value) { var chef = new Chef { Id = Guid.NewGuid(), PartyPersonId = partyPerson.Id }; db.Chefs.Add(chef); var chiliNumber = 0; var numberOfChilis = db.Chilis.Count(c => c.PartyId == party.Id); if (numberOfChilis >= party.MaxChilis) { chiliNumber = numberOfChilis + 1; } else { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); while (db.Chilis.Where(c => c.Number == chiliNumber && c.PartyId == party.Id).FirstOrDefault() != null) { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); } } var chili = new Chili { Id = Guid.NewGuid(), ChefId = chef.Id, Name = partyDto.ChiliName, Number = chiliNumber, PartyId = party.Id }; db.Chilis.Add(chili); returnedPartyDto.ChiliNumber = chiliNumber; } db.SaveChanges(); return(Ok(returnedPartyDto)); }
public IHttpActionResult PostParty(PartyDTO partyDto) { if (!User.Identity.IsAuthenticated) { return(Unauthorized()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userIdString = Microsoft.AspNet.Identity.IdentityExtensions.GetUserId(User.Identity); var userId = Guid.Parse(userIdString); var person = db.People.Where(p => p.UserId == userId).FirstOrDefault(); if (person != null) { var party = db.Parties.Where(p => p.Id == partyDto.PartyId).FirstOrDefault(); if (party != null) { if (partyDto.PartyDate != null) { party.Date = partyDto.PartyDate; } if (partyDto.PartyName != null) { party.Name = partyDto.PartyName; } if (partyDto.MaxChilis > 0) { party.MaxChilis = partyDto.MaxChilis; } party.HasVotingStarted = partyDto.HasVotingStarted; // If votes haven't been tallied yet but the host initiated it, tally the results if (!party.HaveVotesBeenTallied && partyDto.HaveVotesBeenTallied) { var chiliPoints = new Dictionary <Guid, int>(); db.Votes.Where(v => db.Ballots.Where(b => db.PartyPersons.Where(pp => pp.PartyId == party.Id).Select(pp => pp.Id).Contains(b.VoterId)).Select(b => b.Id).Contains(v.BallotId)).GroupBy(v => v.ChiliId).ToList().ForEach(g => chiliPoints.Add(g.First().ChiliId, g.Sum(v => v.Points))); chiliPoints.Keys.ToList().ForEach(k => db.Chilis.Where(c => c.Id == k).FirstOrDefault().Votes = chiliPoints[k]); db.SaveChanges(); var partyChilis = db.Chilis.Where(c => c.PartyId == party.Id).OrderByDescending(c => c.Votes).ToList(); // Check for tie for first place var firstPlaceChilis = partyChilis.Where(pc => pc.Votes == partyChilis.Max(p => p.Votes)); if (firstPlaceChilis.Count() > 1) { // See if any one of the first place chilis has more first place votes (points == 3) var chilisAndTheirNumberOfFirstPlaceVotes = new Dictionary <Guid, int>(); var firstPlaceChiliIds = firstPlaceChilis.Select(f => f.Id); db.Votes.Where(v => firstPlaceChiliIds.Contains(v.ChiliId) && v.Points == 3).GroupBy(v => v.ChiliId).ToList().ForEach(g => chilisAndTheirNumberOfFirstPlaceVotes.Add(g.First().ChiliId, g.Count())); if (chilisAndTheirNumberOfFirstPlaceVotes.Count > 1) { // if there's more than one first place chili that got first place votes, see if one has more than others if (chilisAndTheirNumberOfFirstPlaceVotes.Count(c => c.Value == chilisAndTheirNumberOfFirstPlaceVotes.Max(ch => ch.Value)) > 1) { // If still tied, see if any one chili has more second place votes (points == 2) var chilisAndTheirNumberOfSecondPlaceVotes = new Dictionary <Guid, int>(); db.Votes.Where(v => firstPlaceChiliIds.Contains(v.ChiliId) && v.Points == 2).GroupBy(v => v.ChiliId).ToList().ForEach(g => chilisAndTheirNumberOfSecondPlaceVotes.Add(g.First().ChiliId, g.Count())); if (chilisAndTheirNumberOfSecondPlaceVotes.Count > 1) { // if there's more than one first place chili that got second place votes, see if one has more than others if (chilisAndTheirNumberOfSecondPlaceVotes.Count(c => c.Value == chilisAndTheirNumberOfSecondPlaceVotes.Max(ch => ch.Value)) > 1) { // If still tied, well there's a first place tie then partyChilis.Where(p => chilisAndTheirNumberOfSecondPlaceVotes.Where(c => c.Value == chilisAndTheirNumberOfSecondPlaceVotes.Max(ch => ch.Value)).Select(c => c.Key).Contains(p.Id)).ToList().ForEach(ch => ch.Place = 1); } else { partyChilis.Where(p => p.Id == chilisAndTheirNumberOfSecondPlaceVotes.OrderByDescending(c => c.Value).Select(c => c.Key).First()).First().Place = 1; } } else { partyChilis.Where(p => p.Id == chilisAndTheirNumberOfSecondPlaceVotes.OrderByDescending(c => c.Value).Select(c => c.Key).First()).First().Place = 1; } } else { partyChilis.Where(p => p.Id == chilisAndTheirNumberOfFirstPlaceVotes.OrderByDescending(c => c.Value).Select(c => c.Key).First()).First().Place = 1; } } else { partyChilis.Where(p => p.Id == chilisAndTheirNumberOfFirstPlaceVotes.OrderByDescending(c => c.Value).Select(c => c.Key).First()).First().Place = 1; } //Reorder partyChilis by place then by votes partyChilis = partyChilis.Where(c => c.PartyId == party.Id).OrderByDescending(c => c.Place).OrderByDescending(c => c.Votes).ToList(); } for (var i = 0; i < partyChilis.Count; i++) { if (partyChilis[i].Place == null) { partyChilis[i].Place = i + 1; } } } party.HaveVotesBeenTallied = partyDto.HaveVotesBeenTallied; var partyPerson = db.PartyPersons.Where(pp => pp.PartyId == party.Id && pp.PersonId == person.Id).FirstOrDefault(); if (!partyDto.IsHostUpdate) { if (partyPerson != null) { var chef = db.Chefs.Where(c => c.PartyPersonId == partyPerson.Id).FirstOrDefault(); if (chef != null) { var chili = db.Chilis.Where(c => c.ChefId == chef.Id && c.PartyId == party.Id).FirstOrDefault(); if (chili != null) { if (partyDto.EnterChili.HasValue && partyDto.EnterChili.Value) { if (chili != null) { chili.Name = partyDto.ChiliName; partyDto.ChiliNumber = chili.Number; } } else { db.Chilis.Remove(chili); db.Chefs.Remove(chef); } } } else if (partyDto.EnterChili.HasValue && partyDto.EnterChili.Value) { var newChef = new Chef { Id = Guid.NewGuid(), PartyPersonId = partyPerson.Id }; db.Chefs.Add(newChef); var chiliNumber = 0; var numberOfChilis = db.Chilis.Count(c => c.PartyId == party.Id); if (numberOfChilis >= party.MaxChilis) { chiliNumber = numberOfChilis + 1; } else { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); while (db.Chilis.Where(c => c.Number == chiliNumber && c.PartyId == party.Id).FirstOrDefault() != null) { chiliNumber = randomNumberGenerator.Next(1, party.MaxChilis + 1); } } var chili = new Chili { Id = Guid.NewGuid(), ChefId = newChef.Id, Name = partyDto.ChiliName, Number = chiliNumber, PartyId = party.Id }; db.Chilis.Add(chili); partyDto.ChiliNumber = chiliNumber; } } } db.SaveChanges(); } } return(Ok(partyDto)); }