public async Task <IActionResult> Get(int id) { var aggregate = new ConstituentAggregate(); //aggregate.Constituent = await _repository.FindByID(id, _context.Constituents); //aggregate.Contacts = await _constituentRepository.GetContactsForConstituentId(id); var taskGetConstituent = _repository.FindByID(id, _context.Constituents); var taskGetContacts = _constituentRepository.GetContactsForConstituentId(id); var taskGetQuestionnaires = _constituentRepository.GetQuestionnaires(id); //var taskGetEntity = _entityRepository.GetEntity(id, 1); var taskList = new List <Task>() { taskGetConstituent, taskGetContacts, taskGetQuestionnaires, }; await Task.WhenAll(taskList.ToArray()); var response = new EntityResponse <ConstituentAggregate>(); aggregate.Constituent = taskGetConstituent.Result; aggregate.Contacts = taskGetContacts.Result; aggregate.Questionnaires = taskGetQuestionnaires.Result; //List<Entity> entities = taskGetEntity.Result; /*if (entities.Count > 0) * { * //var settings = new JsonSerializerSettings * //{ * // ContractResolver = new CamelCasePropertyNamesContractResolver() * //}; * //aggregate.JsonEntity = JsonConvert.DeserializeObject(entities[0].Json, settings); * aggregate.JsonEntity = JObject.Parse(entities[0].Json); * aggregate.JsonEntityId = entities[0].Id; * } * else * { * //var serializer = new JsonSerializer * //{ * // ContractResolver = new CamelCasePropertyNamesContractResolver() * //}; * //use same constituent if empty for now * aggregate.JsonEntity = JObject.FromObject(aggregate.Constituent); * }*/ response.Data = aggregate; return(Ok(response)); }
private ConstituentAggregate CreateTestAggregate() { var agg = new ConstituentAggregate(); var constituent = new Constituent(); var contact = new ConstituentContact(); constituent.FirstName = "Joe"; constituent.LastName = "Smith"; constituent.ConstituentId = 0; agg.Constituent = constituent; contact.ContactTypeId = 1; contact.ContactValue = _phoneNumber; agg.Contacts = new List <ConstituentContact>(); agg.Contacts.Add(contact); return(agg); }
public async Task <IActionResult> Post([FromBody] ConstituentAggregate aggregate) { var constituent = aggregate.Constituent; if (constituent.ConstituentId <= 0) { // constituent.ConstituentId = await _constituentRepository.GetNextConstituentId(); constituent = await _repository.Add(constituent, _context.Constituents); } else { constituent = await _repository.Update(constituent); } //contacts var contacts = aggregate.Contacts ?? new List <ConstituentContact>(); contacts.Select(c => { c.ConstituentId = constituent.ConstituentId; return(c); }).ToList(); _contactRepo.UpdateWithoutSave(contacts.Where(c => c.Id > 0).ToList(), _context.Contacts); _contactRepo.AddWithoutSave(contacts.Where(c => c.Id == 0).ToList(), _context.Contacts); await _contactRepo.Save(); //rebuild response var response = new EntityResponse <ConstituentAggregate>(); var returnAggregate = new ConstituentAggregate() { Constituent = constituent, Contacts = contacts, //JsonEntity = JObject.Parse(entity.Json), //JsonEntityId = entity.Id }; response.Data = returnAggregate; return(Ok(response)); }