public ActionResult Create(Room room) { if (ModelState.IsValid) { db.rooms.Add(room); db.SaveChanges(); return RedirectToAction("Index"); } return View(room); }
public static Boolean importRoomFile(String filename) { CsvFileDescription inputFileDescription = new CsvFileDescription { SeparatorChar = ',', FirstLineHasColumnNames = true }; CsvContext cc = new CsvContext(); HousingContext hdb = new HousingContext(); IEnumerable<RoomCSV> csvrooms = cc.Read<RoomCSV>(filename, inputFileDescription); List<String> campusNames = (from rcsv in csvrooms select rcsv.CampusName).Distinct().ToList(); List<Campus> campuses = hdb.campuses.Include(x => x.buildings).ToList(); List<String> buildingNames; List<Int32> floorNumbers; List<RoomCSV> csvRooms; Campus curCampus; Building curBuilding; Floor curFloor; foreach (String campusName in campusNames) { curCampus = campuses.Where(x => x.campusName == campusName).FirstOrDefault(); if (curCampus == null) { curCampus = hdb.campuses.Add(new Campus { campusName = campusName, buildings = new List<Building>() }); } buildingNames = ( from rcsv in csvrooms where rcsv.CampusName == campusName select rcsv.BuildingName ).Distinct().ToList(); foreach (String buildingName in buildingNames) { curBuilding = curCampus.buildings.Where(x => x.name == buildingName).FirstOrDefault(); if (curBuilding == null) { curBuilding = hdb.buildings.Add(new Building { campus = curCampus, floors = new List<Floor>(), name = buildingName }); } floorNumbers = ( from rcsv in csvrooms where rcsv.BuildingName == buildingName && rcsv.CampusName == campusName select rcsv.FloorNumber ).Distinct().ToList(); foreach (int floorNumber in floorNumbers) { curFloor = curBuilding.floors.Where(x => x.level == floorNumber).FirstOrDefault(); if (curFloor == null) { curFloor = hdb.floors.Add(new Floor { building = curBuilding, rooms = new List<Room>(), level = floorNumber }); } csvRooms = ( from rcsv in csvrooms where rcsv.CampusName == campusName && rcsv.BuildingName == buildingName && rcsv.FloorNumber == floorNumber select rcsv ).ToList(); foreach (RoomCSV room in csvRooms) { if (!curFloor.rooms.Where(x => x.roomNumber == room.RoomNumber).Any()) { Room r; if (room.Type == "Suite") { r = new Suite { floor = curFloor, roomNumber = room.RoomNumber, chaperonesAllowed = room.ChaperonesAllowed, studentsAllowed = room.StudentsAllowed }; } else { r = new Room { floor = curFloor, roomNumber = room.RoomNumber, chaperonesAllowed = room.ChaperonesAllowed, studentsAllowed = room.StudentsAllowed }; } hdb.rooms.Add(r); } } } } } hdb.SaveChanges(); List<RoomCSV> SuitesRooms = ( from rcsv in csvrooms where rcsv.SuiteFloorNumber != null select rcsv ).ToList(); foreach (RoomCSV r in SuitesRooms) { Suite curSuite = (Suite) (from suite in hdb.rooms where r.SuiteRoomNumber == suite.roomNumber && suite.floor.level == r.SuiteFloorNumber && suite.floor.building.name == r.SuiteBuildingName && suite.floor.building.campus.campusName == r.SuiteCampusName select suite).FirstOrDefault(); var curRoom = (from room in hdb.rooms where room.floor.level == r.FloorNumber && room.floor.building.name == r.BuildingName && room.floor.building.campus.campusName == r.CampusName && room.roomNumber == r.RoomNumber select room).FirstOrDefault(); if (curSuite.rooms == null) { curSuite.rooms = new List<Room>(); } curSuite.rooms.Add(curRoom); curSuite.chaperonesAllowed = 0; curSuite.studentsAllowed = 0; } hdb.SaveChanges(); return true; }
public ActionResult Edit(Room room) { if (ModelState.IsValid) { db.Entry(room).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(room); }