예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
0
 public ActionResult Edit([Bind(Include = "event_ID,title,startDate,endDate,status,category,type,logoPath,description,location_ID")] Event @event)
 {
     if (ModelState.IsValid)
     {
         db.Entry(@event).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.location_ID = new SelectList(db.Locations, "location_ID", "city", @event.location_ID);
     return(View(@event));
 }
예제 #4
0
 public ActionResult Edit([Bind(Include = "user_ID,username,password,lastName,firstName,phoneHome,phoneCell,phoneOffice,foodPref,companyName,branch,additionalInfo,location_ID")] User user)
 {
     if (ModelState.IsValid)
     {
         db.Entry(user).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.location_ID = new SelectList(db.Locations, "location_ID", "city", user.location_ID);
     return(View(user));
 }
예제 #5
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();
         }
     }
 }
예제 #6
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();
                }
            }
        }