internal static StreetsBuildingItem Copy(StreetBuildingData item)
 {
     return(new StreetsBuildingItem()
     {
         ItemId = item.ItemId,
         BuildingNumber = item.BuildingNumber,
         DateCreated = item.DateCreated,
         Street = item.Street,
         TerritoyrCardId = item.TerritoryCardId
     });
 }
 public static StreetBuildingData GetStreetBuilding(int itemId)
 {
     using (var db = new StreetsBuildingDataContext()) {
         try {
             var s = db.StreetsBuildingItems.Single(r => r.ItemId == itemId);
             return(StreetBuildingData.Copy(s));
         } catch (InvalidOperationException) {
             throw new StreetBuildingItemDoesntExistException();
         }
     }
 }
 public static bool AddOrUpdate(ref StreetBuildingData st)
 {
     try {
         if (st.ItemId < 0)
         {
             return(AddNewStreetBuilding(ref st));
         }
         return(UpdateStreetBuilding(ref st));
     } catch (StreetBuildingItemDoesntExistException) {
         return(UpdateStreetBuilding(ref st));
     } catch (StreetBuildingAlreadyExistsException) {
         return(AddNewStreetBuilding(ref st));
     }
 }
        private static bool UpdateStreetBuilding(ref StreetBuildingData st)
        {
            using (var db = new StreetsBuildingDataContext()) {
                try {
                    var i    = st.ItemId;
                    var stEx = db.StreetsBuildingItems.Single(s => s.ItemId == i);
                    stEx.BuildingNumber  = st.BuildingNumber;
                    stEx.DateCreated     = st.DateCreated;
                    stEx.Street          = st.Street;
                    stEx.TerritoyrCardId = st.TerritoryCardId;

                    db.SubmitChanges();
                    return(true);
                } catch (InvalidOperationException) {
                    throw new StreetBuildingItemDoesntExistException();
                }
            }
        }
 private static bool AddNewStreetBuilding(ref StreetBuildingData st)
 {
     using (var db = new StreetsBuildingDataContext()) {
         var s = StreetBuildingData.Copy(st);
         var q = from x in db.StreetsBuildingItems
                 where x.TerritoyrCardId.Equals(s.TerritoyrCardId) &&
                 x.Street.Equals(s.Street) &&
                 x.BuildingNumber.Equals(s.BuildingNumber)
                 select x;
         if (q.Any())
         {
             throw new StreetBuildingAlreadyExistsException();
         }
         db.StreetsBuildingItems.InsertOnSubmit(s);
         db.SubmitChanges();
         st.ItemId = s.ItemId;
         return(st.ItemId >= 0);
     }
 }
 internal static StreetsBuildingItem Copy(StreetBuildingData item)
 {
     return new StreetsBuildingItem() {
         ItemId = item.ItemId,
         BuildingNumber = item.BuildingNumber,
         DateCreated = item.DateCreated,
         Street = item.Street,
         TerritoyrCardId = item.TerritoryCardId
     };
 }
 private static bool AddNewStreetBuilding(ref StreetBuildingData st)
 {
     using (var db = new StreetsBuildingDataContext()) {
         var s = StreetBuildingData.Copy(st);
         var q = from x in db.StreetsBuildingItems
                 where x.TerritoyrCardId.Equals(s.TerritoyrCardId) &&
                       x.Street.Equals(s.Street) &&
                       x.BuildingNumber.Equals(s.BuildingNumber)
                 select x;
         if (q.Any())
             throw new StreetBuildingAlreadyExistsException();
         db.StreetsBuildingItems.InsertOnSubmit(s);
         db.SubmitChanges();
         st.ItemId = s.ItemId;
         return st.ItemId >= 0;
     }
 }
        private static bool UpdateStreetBuilding(ref StreetBuildingData st)
        {
            using (var db = new StreetsBuildingDataContext()) {
                try {
                    var i = st.ItemId;
                    var stEx = db.StreetsBuildingItems.Single(s => s.ItemId == i);
                    stEx.BuildingNumber = st.BuildingNumber;
                    stEx.DateCreated = st.DateCreated;
                    stEx.Street = st.Street;
                    stEx.TerritoyrCardId = st.TerritoryCardId;

                    db.SubmitChanges();
                    return true;
                } catch (InvalidOperationException) {
                    throw new StreetBuildingItemDoesntExistException();
                }
            }
        }
        public static bool AddOrUpdate(ref StreetBuildingData st)
        {
            try {
                if (st.ItemId < 0) return AddNewStreetBuilding(ref st);
                return UpdateStreetBuilding(ref st);
            } catch (StreetBuildingItemDoesntExistException) {
                return UpdateStreetBuilding(ref st);
            } catch (StreetBuildingAlreadyExistsException) {
                return AddNewStreetBuilding(ref st);
            }

        }