private Expression <Func <Contestant, bool> > ContestantSelector(ParsedOrder.Entrant parsedEntrant) { if (!parsedEntrant.DoB.HasValue) { return(c => c.Firstname == parsedEntrant.FirstName && c.Lastname == parsedEntrant.LastName); } else { var dob = parsedEntrant.DoB.Value; return(c => c.Firstname == parsedEntrant.FirstName && c.Lastname == parsedEntrant.LastName && (!c.DateofBirth.HasValue || c.DateofBirth == dob)); } }
private ParsedOrder Parse(EntryJson order) { // This catches people who only enter day and month for dob - they come through as // the current year - and people who want to cause us headaches... var validDateCutoff = DateTime.Now.AddYears(-4); var text = order.JsonText; if (text.StartsWith("\"")) { text = text.Substring(1, text.Length - 2) .Replace("\\", "") .Replace("\"event\"", "\"event_detail\""); // property name is reserved in C# } dynamic obj = JsonConvert.DeserializeObject(text); // One day it would be nice to have consistent JSON sent to me! var parsedOrder = new ParsedOrder(); parsedOrder.BookingId = obj.booking_id.Value; parsedOrder.EventCode = obj.event_id.Value; // This is a special case... if (parsedOrder.EventCode == "divingchess") { parsedOrder.EventCode = "CHDV"; } parsedOrder.BookingPrice = (decimal)obj.booking_price.Value; parsedOrder.BookingSpaces = int.Parse(obj.booking_spaces.Value); parsedOrder.BookingStatus = int.Parse(obj.booking_status.Value.ToString()); // should be integer 1, sometimes string "1" foreach (var o2 in obj.attendees) { // o2.Name is the mysterious number in json foreach (var entrant in o2.Value) { var parsedEntrant = new ParsedOrder.Entrant(); parsedEntrant.Title = entrant.title.Value; parsedEntrant.FirstName = TitleCase(entrant.first_name.Value); parsedEntrant.LastName = TitleCase(entrant.last_name.Value); parsedEntrant.Country = entrant.country_to_represent.Value; parsedEntrant.DoB = (entrant.date_of_birth != null) ? DateTime.Parse(entrant.date_of_birth.Value) : null; if (parsedEntrant.DoB > validDateCutoff) { parsedEntrant.DoB = null; } parsedOrder.Entrants.Add(parsedEntrant); // Override if necessary if (order.ManualDoB != null) { parsedEntrant.DoB = order.ManualDoB; } } } parsedOrder.Timestamp = obj.timestamp.Value; parsedOrder.EventId = obj.event_detail.id.Value; parsedOrder.EventName = obj.event_detail.name.Value; // MessageBox.Show(obj.attendees); /* \"attendees\":{\"31\":[{\"title\":\"Mr\",\"first_name\":\"Howard\",\"last_name\":\"Kenward\", * \"country_to_represent\":\"England\",\"date_of_birth\":\"1955-07-26\"}]}, \"timestamp\":1466096898,\"event\":{\"id\":\"31\",\"name\":\"Trench\"}}" */ if (parsedOrder.BookingSpaces != parsedOrder.Entrants.Count()) { throw new ParsingFailureException(string.Format("Order {0} has wrong number of entrants", parsedOrder.BookingId)); } return(parsedOrder); }