Ejemplo n.º 1
0
        public IHttpActionResult PutSession(int id, Session session)
        {
            if (id != session.Id)
            {
                return(BadRequest());
            }

            var sessionToUpdate = db.Sessions
                                  .FirstOrDefault(s => s.Id == id);

            sessionToUpdate.Title              = session.Title;
            sessionToUpdate.Abstract           = session.Abstract;
            sessionToUpdate.SessionStartTime   = session.SessionStartTime;
            sessionToUpdate.SessionEndTime     = session.SessionEndTime;
            sessionToUpdate.VolunteersRequired = session.VolunteersRequired;

            sessionToUpdate.ActualSessionStartTime = session.ActualSessionStartTime;
            sessionToUpdate.ActualSessionEndTime   = session.ActualSessionEndTime;
            sessionToUpdate.Attendees10            = session.Attendees10;
            sessionToUpdate.Attendees50            = session.Attendees50;
            sessionToUpdate.Notes = session.Notes;


            db.SaveChanges();

            var rooms = db.Rooms.Where(r => r.SessionId == id).ToList();

            var deletedRooms =
                rooms.Where(x => !session.Rooms.Any(v => v.Name == x.Name)).ToList();
            var addedRooms =
                session.Rooms.Where(x => !rooms.Any(v => v.Name == x.Name)).ToList();

            //Do not need an update since there is only 1 field
            addedRooms.ForEach(x => _roomRepository.AddRoomToSession(x.Name, id));
            deletedRooms.ForEach(x => _roomRepository.DeleteRoomFromSession(x.Name, id));

            //User Check Ins
            var userCheckIns = db.UserCheckIns.Where(r => r.SessionId == id).ToList();

            var deleteduserCheckIns =
                userCheckIns.Where(x => !session.ProctorCheckIns.Any(v => v.UserId == x.UserId)).ToList();
            var addeduserCheckIns =
                session.ProctorCheckIns.Where(x => !userCheckIns.Any(v => v.UserId == x.UserId)).ToList();



            //Do not need an update since there is only 1 field
            addeduserCheckIns.ForEach(x => _userCheckInRepository.AddUserCheckInToSession(x.UserId, x.CheckInTime, id));
            deleteduserCheckIns.ForEach(x => _userCheckInRepository.DeleteUserCheckInFromSession(x.UserId, id));

            //foreach (var checkIn in session.ProctorCheckIns)
            //{
            //    var foundCheckIn = userCheckIns.FirstOrDefault(c => c.UserId == checkIn.UserId);
            //    foundCheckIn.CheckInTime = checkIn.CheckInTime;
            //    db.SaveChanges();
            //}

            return(StatusCode(HttpStatusCode.NoContent));
        }