Ejemplo n.º 1
0
        ////[NotMapped]
        ////public virtual Classroom Classroom { get; set; }
        ////[NotMapped]
        ////public string State { get; set; }

        /// <summary>
        /// Converts a Seat object into a Temporary Seat object
        /// </summary>
        /// <returns>A Temporary Seat Object</returns>
        public SeatTemp ToSeatTemp()
        {
            SeatTemp seatTemp = new SeatTemp();
            seatTemp.SeatId = this.SeatId;
            seatTemp.ClassroomId = this.ClassroomId;
            seatTemp.UserId = this.UserId;
            seatTemp.ConfigurationId = this.ConfigurationId;
            return seatTemp;
        }
        /// <summary>
        /// Processes the AJAX request that adds a Seat to a classroom
        /// </summary>
        /// <returns>An empty response</returns>
        public JsonResult AddSeats()
        {
            string json;
            int classroomId;
            IDictionary<string, string> response = new Dictionary<string, string>();
            using (var reader = new StreamReader(Request.InputStream))
            {
                json = reader.ReadToEnd();
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            dynamic parameters = serializer.Deserialize<object>(json);
            if (((IDictionary<string, object>)parameters).ContainsKey("NewSeats") && ((IDictionary<string, object>)parameters).ContainsKey("Session") && ((IDictionary<string, object>)parameters).ContainsKey("Classroom"))
            {
                if (int.TryParse(string.Empty + parameters["Classroom"] + string.Empty, out classroomId))
                {
                    string newSeats = parameters["NewSeats"];
                    string session = parameters["Session"];
                    if (newSeats != string.Empty)
                    {
                        string[] newUsers = newSeats.Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
                        if (newUsers.Count() > 0)
                        {
                            foreach (string newUser in newUsers)
                            {
                                string nu = newUser.ToLower();
                                User existingUser = this.db.Query<User>().Where(u => u.EmailAddress == nu).FirstOrDefault();
                                if (existingUser == null)
                                {
                                    User userToAdd = new User();
                                    userToAdd.EmailAddress = nu;
                                    userToAdd.IsInstructor = false;
                                    userToAdd.IsAdministrator = false;
                                    userToAdd.Password = PasswordHash.CreateHash("password");
                                    this.db.Add<User>(userToAdd);
                                    this.db.SaveChanges();
                                    existingUser = this.db.Query<User>().Where(u => u.EmailAddress == nu).FirstOrDefault();
                                }

                                SeatTemp existingSeat = this.db.Query<SeatTemp>().Where(s => (s.UserId == existingUser.UserId && s.SessionId == session)).FirstOrDefault();
                                if (existingSeat == null)
                                {
                                    SeatTemp seat = new SeatTemp() { UserId = existingUser.UserId, SessionId = session, TimeStamp = DateTime.Now, ClassroomId = classroomId };
                                    this.db.Add<SeatTemp>(seat);
                                    this.db.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }

            response.Add("Status", "Done");
            return this.Json(response);
        }