// POST: api/RenovationGroups
 public HttpResponseMessage Post([FromBody]RenovationGroup group)
 {
     var repo = new UnitRenovationGroupRepository(new ConstructionEntities());
     var translated = new RenovationGroupBuilder().Translate(new List<RenovationGroup> { group });
     repo.Save(translated);
     return Request.CreateResponse(HttpStatusCode.OK, group);
 }
        public void Context()
        {
            var newGrp = new UnitRenoGrp
            {
                hProp = 1,
                RenoGrpName = "when_saving_new",
                RenoGrpDesc = "Test description",
                LeadContractor = "Test Lead Contractor"
            };

            var newGroups = new List<UnitRenoGrp> { newGrp };

            Func<UnitRenoGrp, bool> expression =
                (existingGrp => existingGrp.RenoGrpName == newGrp.RenoGrpName &&
                    existingGrp.hProp == newGrp.hProp &&
                    existingGrp.RenoGrpDesc == newGrp.RenoGrpDesc &&
                    existingGrp.LeadContractor == newGrp.LeadContractor &&
                    existingGrp.ArchiveFlag == newGrp.ArchiveFlag);

            _entities = new ConstructionEntities();

            //remove existing context before test run
            var existing = _entities.UnitRenoGrps.Where(expression).ToList();

            if (existing.Any())
            {
                existing.ForEach(rg =>
                {
                    _entities.UnitRenoGrps.Attach(rg);
                    _entities.UnitRenoGrps.Remove(rg);
                });

                _entities.SaveChanges();
            }

            _target = new UnitRenovationGroupRepository(_entities);
            _target.Save(newGroups);
            _result = _target.Retrieve(expression).ToList()[0];
        }
        public void Context()
        {
            _newGrp = new UnitRenoGrp
            {
                hProp = 23,
                RenoGrpName = "when_saving_existing",
                RenoGrpDesc = "Test description",
                LeadContractor = "Test Lead Contractor"
            };

            _entities = new ConstructionEntities();
            _target = new UnitRenovationGroupRepository(_entities);

            Func<UnitRenoGrp, bool> existingEntityExpression = (existingGrp =>
                existingGrp.RenoGrpName == _newGrp.RenoGrpName &&
                existingGrp.hProp == _newGrp.hProp &&
                existingGrp.RenoGrpDesc == _newGrp.RenoGrpDesc &&
                existingGrp.LeadContractor == _newGrp.LeadContractor);

            //remove existing data context
            var existing = _entities.UnitRenoGrps.Where(existingEntityExpression).ToList();

            if (existing.Any())
            {
                existing.ForEach(rg =>
                {
                    _entities.UnitRenoGrps.Attach(rg);
                    _entities.UnitRenoGrps.Remove(rg);
                });

                _entities.SaveChanges();
            }

            //create a new entity to update
            _entities.UnitRenoGrps.Add(_newGrp);
            _entities.SaveChanges();

            var saveTarget = _entities.UnitRenoGrps.Where(existingEntityExpression).ToList()[0];
            saveTarget.ArchiveFlag = true;
            var newGroups = new List<UnitRenoGrp> { saveTarget };

            //update the entity
            _target.Save(newGroups);

            _result = _entities.UnitRenoGrps.Where(existingEntityExpression).ToList()[0];
        }