Exemplo n.º 1
0
        public User CreateUser(User user, Location location)
        {
            User createdUser = null;

            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    var queriedLocation = db.Locations.Where(l =>
                                                             l.city.Equals(location.city, StringComparison.OrdinalIgnoreCase) &&
                                                             l.state.Equals(location.state, StringComparison.OrdinalIgnoreCase) &&
                                                             l.zip == location.zip && l.address.Equals(location.address, StringComparison.OrdinalIgnoreCase))
                                          .FirstOrDefault();

                    if (queriedLocation != null)
                    {
                        user.location_ID = queriedLocation.location_ID;
                    }
                    else
                    {
                        db.Locations.Add(location);
                        db.SaveChanges();
                        user.location_ID = location.location_ID;
                    }

                    createdUser = db.Users.Add(user);
                    db.SaveChanges();
                    tx.Commit();
                }
            }

            return(createdUser);
        }
Exemplo n.º 2
0
        public Event CreateEvent(Event newEvent, Location location)
        {
            Event createdEvent;

            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction(IsolationLevel.Unspecified))
                {
                    var queriedLocation = db.Locations.Where(l =>
                                                             l.city.Equals(location.city, StringComparison.OrdinalIgnoreCase) &&
                                                             l.state.Equals(location.state, StringComparison.OrdinalIgnoreCase) &&
                                                             l.zip == location.zip && l.address.Equals(location.address, StringComparison.OrdinalIgnoreCase))
                                          .FirstOrDefault();

                    if (queriedLocation != null)
                    {
                        newEvent.location_ID = queriedLocation.location_ID;
                    }
                    else
                    {
                        db.Locations.Add(location);
                        db.SaveChanges();
                        newEvent.location_ID = location.location_ID;
                    }

                    createdEvent = db.Events.Add(newEvent);
                    db.SaveChanges();
                    tx.Commit();
                }
            }

            return(createdEvent);
        }
Exemplo n.º 3
0
 public void UpdateEvent(Event theEvent, Location location)
 {
     using (var db = new EventRegEntities())
     {
         using (var tx = db.Database.BeginTransaction())
         {
             db.Entry(location).State = System.Data.Entity.EntityState.Modified;
             db.Entry(theEvent).State = System.Data.Entity.EntityState.Modified;
             db.SaveChanges();
             tx.Commit();
         }
     }
 }
Exemplo n.º 4
0
        public IQueryable <Event> GetAllEvents()
        {
            IQueryable <Event> events;

            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction(IsolationLevel.Unspecified))
                {
                    events = db.Events;
                    tx.Commit();
                }
            }
            return(events);
        }
Exemplo n.º 5
0
        public User GetUser(int userId)
        {
            User user = null;

            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    user = db.Users.Find(userId);
                    tx.Commit();
                }
            }

            return(user);
        }
Exemplo n.º 6
0
        public IEnumerable <User> GetAllUsersWithLocationInfo()
        {
            IEnumerable <User> users = null;

            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    users = db.Users.Include(u => u.Location).ToList();
                    tx.Commit();
                }
            }

            return(users);
        }
Exemplo n.º 7
0
        public void DeleteUser(int userId)
        {
            using (var db = new EventRegEntities())
            {
                using (var tx = db.Database.BeginTransaction())
                {
                    var userToBeDeleted = db.Users.Find(userId);
                    var numberOfUsersWithSameLocation = db.Users
                                                        .Include(u => u.Location)
                                                        .Where(u => u.location_ID == userToBeDeleted.location_ID)
                                                        .Count();

                    if (numberOfUsersWithSameLocation == 0)
                    {
                        var location = db.Locations.Find(userToBeDeleted.location_ID);
                        db.Locations.Remove(location);
                    }

                    db.Users.Remove(userToBeDeleted);
                    db.SaveChanges();
                    tx.Commit();
                }
            }
        }