public async Task <HouseholdDto> AddHousehold(HouseholdDto householdDto)
        {
            var household = householdDto.ToHousehold();

            _context.Household.Add(household);
            await _context.SaveChangesAsync();

            return(new HouseholdDto(household));
        }
Exemplo n.º 2
0
        public async Task DeleteFamilyMember(int id, int familyMemberId)
        {
            // TODO: refactor to FamilyMemberBC
            var familyMember = await _context.FamilyMember.FirstOrDefaultAsync(p => p.Id == familyMemberId && p.HouseholdId == id);

            if (familyMember == null)
            {
                throw new NotFoundException($"Family member Id {familyMemberId} of Household Id {id} not found.");
            }

            _context.FamilyMember.Remove(familyMember);
            await _context.SaveChangesAsync();
        }
        public async Task <HouseholdDto> AddFamilyMember(int id, FamilyMemberDto familyMemberDto)
        {
            var household = await _context.Household
                            .Include(p => p.FamilyMembers)
                            .FirstOrDefaultAsync(x => x.Id == id);

            FamilyMember spouse = null;

            if (familyMemberDto.Spouse != null)
            {
                spouse = await _familyMemberBc.GetFamilyMember(familyMemberDto.Spouse);

                if (spouse == null)
                {
                    throw new NotFoundException($"Spouse Id {familyMemberDto.Spouse} not found");
                }
            }

            var familyMember = familyMemberDto.ToFamilyMember();

            household.FamilyMembers.Add(familyMember);
            _context.Entry(household).State = EntityState.Modified;
            _householdBc.ComputeHouseholdStatistics(household);
            await _context.SaveChangesAsync();

            // spouse to be updated
            if (spouse != null)
            {
                spouse.Spouse = familyMember.Id;
                _context.Entry(spouse).State = EntityState.Modified;

                await _context.SaveChangesAsync();
            }

            return(new HouseholdDto(household));
        }