Esempio n. 1
0
        public void Save(Region region)
        {
            using (var db = GetDbContext())
            {
                Entities.RegionEntity dbRegion;
                var timeStamp = DateTime.UtcNow;

                if (region.Id != Guid.Empty)
                {
                    dbRegion = db.Regions.SingleOrDefault(g => g.Id == region.Id);

                    if (dbRegion == null)
                    {
                        throw new Exception("Region could not be found");
                    }

                    dbRegion.Locations.Clear();
                }
                else
                {
                    dbRegion = new Entities.RegionEntity
                    {
                        Id        = Guid.NewGuid(),
                        CreatedBy = region.CreatedBy,
                        CreatedOn = timeStamp,
                        Locations = new Collection <Entities.LocationEntity>()
                    };

                    region.Id        = dbRegion.Id;
                    region.CreatedOn = dbRegion.CreatedOn;
                    db.Regions.Add(dbRegion);
                }


                dbRegion.Name      = region.Name;
                dbRegion.UpdatedBy = region.UpdatedBy;
                dbRegion.UpdatedOn = timeStamp;
                region.UpdatedOn   = dbRegion.UpdatedOn;

                // Add relations
                foreach (var location in region.Locations)
                {
                    var dbLocation = db.Locations.SingleOrDefault(l => l.Id == location.Id);
                    if (dbLocation != null)
                    {
                        dbRegion.Locations.Add(dbLocation);
                    }
                }

                db.SaveChanges();
            }
        }
Esempio n. 2
0
        private Region MapToRegion(Entities.RegionEntity dbRegion, bool includeLocations)
        {
            if (dbRegion == null)
            {
                return(null);
            }

            return(new Region
            {
                Id = dbRegion.Id,
                Name = dbRegion.Name,
                Locations = includeLocations ? dbRegion.Locations.Select(MapToLocation).ToList() : new List <Location>(),
                CreatedBy = dbRegion.CreatedBy,
                CreatedOn = dbRegion.CreatedOn,
                UpdatedBy = dbRegion.UpdatedBy,
                UpdatedOn = dbRegion.UpdatedOn
            });
        }