public IHttpActionResult AddPhotosForRoom(int id) { var keys = System.Web.HttpContext.Current.Request.Files.AllKeys; string path = AppDomain.CurrentDomain.BaseDirectory + "static\\Content\\img\\Rooms\\Room" + id + "\\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (var key in keys) { var picture = System.Web.HttpContext.Current.Request.Files[key]; string imgpath = path + picture.FileName; WebImage img = new WebImage(picture.InputStream); if (img.Width > 600 || img.Height > 350) { img.Resize(600, 350, false, true); } img.Save(imgpath); RoomPhoto roomPhoto = new RoomPhoto(); roomPhoto.PhotoLink = String.Format("../../static/Content/img/Rooms/Room{0}/{1}", id, picture.FileName); roomPhoto.RoomId = id; db.RoomsPhotos.Add(roomPhoto); db.SaveChanges(); } return(Ok()); }
public void CallConstructorAndFillProperties() { //Arrange var photosToRooms = new[] { new PhotoToRoom() }.AsEnumerable(); var roomPhoto = new RoomPhoto { Path = "Path", Name = "Name", Extension = "Extension", PhotosToRooms = photosToRooms }; //Act var path = roomPhoto.Path; var name = roomPhoto.Name; var extension = roomPhoto.Extension; //Assert Assert.Multiple(() => { Assert.IsTrue(path == "Path"); Assert.IsTrue(name == "Name"); Assert.IsTrue(extension == "Extension"); Assert.IsTrue(roomPhoto.PhotosToRooms.Count() == 1); }); }
public ActionResult DeleteRoom(int roomId, int sequencenumber) { if (IsUserLoggedIn()) { Models.MoveToLondon mtl = new Models.MoveToLondon(); List <Room> rooms = mtl.Rooms.Where(o => o.RoomSequence == sequencenumber).ToList(); List <int> roomids = new List <int>(); List <int> photoids = new List <int>(); List <string> photopaths = new List <string>(); foreach (Room room in rooms) { if (!roomids.Contains(room.ID)) { roomids.Add(room.ID); } List <RoomsPhotosBridge> bridges = room.RoomsPhotosBridges.ToList(); foreach (RoomsPhotosBridge bridge in bridges) { if (!photoids.Contains(bridge.PhotoId)) { photoids.Add(bridge.PhotoId); photopaths.Add(bridge.RoomPhoto.Path); } mtl.Entry(bridge).State = System.Data.Entity.EntityState.Deleted; } } mtl.SaveChanges(); mtl = null; mtl = new Models.MoveToLondon(); foreach (int id in roomids) { Room r = new Room(); r.ID = id; mtl.Entry(r).State = System.Data.Entity.EntityState.Deleted; } foreach (int id in photoids) { RoomPhoto rf = new RoomPhoto(); rf.ID = id; mtl.Entry(rf).State = System.Data.Entity.EntityState.Deleted; } //string serverpath = Server.MapPath(@"/"); //foreach (string path in photopaths) //{ //} mtl.SaveChanges(); return(RedirectToAction("roomslist")); } else { return(RedirectToAction("index")); } }
public bool AddImagesToRoom() { if (IsUserLoggedIn()) { bool isSavedSuccessfully = true; try { string Name = ""; RoomData rd = null; if (Session["RoomsData"] != null) { rd = Session["RoomsData"] as RoomData; } else { return(false); } foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; Name = file.FileName; if (file != null && file.ContentLength > 0) { string serverMapPath = Server.MapPath(@"/"); var originalDirectory = new DirectoryInfo(string.Format("{0}Images", serverMapPath)); string pathString = System.IO.Path.Combine(originalDirectory.ToString(), ""); string fileName1 = "image_" + DateTime.Now.ToString("dd_MM_hh_mm_ss") + Name; bool doessExists = System.IO.Directory.Exists(pathString); if (!doessExists) { System.IO.Directory.CreateDirectory(pathString); } var path = string.Format("{0}\\{1}", pathString, fileName1); file.SaveAs(path); string PathForDb = path.Remove(0, serverMapPath.Length); PathForDb = PathForDb.Replace(@"\", "/"); RoomPhoto rf = new RoomPhoto(); rf.Path = PathForDb; rd.RoomPhotos.Add(rf); } } } catch (Exception ex) { isSavedSuccessfully = false; } return(isSavedSuccessfully); } else { return(false); } }
public void SaveRoomPhoto(RoomPhoto roomphoto) { if (roomphoto.PhotoId == 0) { roomPhotoTable.InsertOnSubmit(roomphoto); } else { roomPhotoTable.Attach(roomphoto); roomPhotoTable.Context.Refresh(RefreshMode.KeepCurrentValues, roomphoto); } roomPhotoTable.Context.SubmitChanges(); }
public ActionResult EditRoom(RoomMessage roomMessage, string[] RoomFacility) { string fi = ""; foreach (var item in RoomFacility) { //可以自己写Insert语句(添加数据) fi = fi + "+" + item.ToString();//这样写方便调时看 } roomMessage.RoomFacility = fi; System.Web.HttpFileCollection file = System.Web.HttpContext.Current.Request.Files; var photocount = file.Count; string[] imgname = new string[photocount]; if (photocount > 0 && file[0].FileName != "") { for (int i = 0; i < photocount; i++) { if (file[i].FileName == "" || file[i].FileName == null) { imgname[i] = ""; } else { string guid = Guid.NewGuid().ToString(); string path = "~/images/Roomphoto/" + guid + file[i].FileName; file[i].SaveAs(Server.MapPath(path));//必须得是相对路径 imgname[i] = "/images/Roomphoto/" + guid + file[i].FileName; } } } RoomPhoto roomph = new RoomPhoto(); using (SmartRentalSystemEntities db = new SmartRentalSystemEntities()) { var hotmana = db.RoomMessage.Find(roomMessage.RoomID); if (roomMessage.RoomRemain >= (hotmana.RoomRemain - hotmana.RoomCount)) { if (imgname[0] != "" && imgname[0] != null) { for (int i = 0; i < photocount; i++) { if (i == 0) { roomph.PhotoName1 = imgname[i].ToString(); } if (i == 1) { roomph.PhotoName2 = imgname[i].ToString(); } if (i == 2) { roomph.PhotoName3 = imgname[i].ToString(); } if (i == 3) { roomph.PhotoName4 = imgname[i].ToString(); } if (i == 4) { roomph.PhotoName5 = imgname[i].ToString(); } if (i == 5) { roomph.PhotoName6 = imgname[i].ToString(); } if (i == 6) { roomph.PhotoName7 = imgname[i].ToString(); } } for (int s = photocount; s < 7; s++) { if (s == 0) { roomph.PhotoName1 = null; } if (s == 1) { roomph.PhotoName2 = null; } if (s == 2) { roomph.PhotoName3 = null; } if (s == 3) { roomph.PhotoName4 = null; } if (s == 4) { roomph.PhotoName5 = null; } if (s == 5) { roomph.PhotoName6 = null; } if (s == 6) { roomph.PhotoName7 = null; } } roomph.RoomPhotoID = (int)roomMessage.RoomPhotoID; db.Entry(roomph).State = EntityState.Modified; db.SaveChanges(); } hotmana.MattresID = roomMessage.MattresID; hotmana.RoomTypeID = roomMessage.RoomTypeID; hotmana.RoomName = roomMessage.RoomName; hotmana.RoomLayout = roomMessage.RoomLayout; hotmana.RoomFacility = roomMessage.RoomFacility; hotmana.RoomCount = roomMessage.RoomRemain - (hotmana.RoomRemain - hotmana.RoomCount); hotmana.RoomRemain = roomMessage.RoomRemain; hotmana.PrimeCost = roomMessage.PrimeCost; hotmana.RoomPrice = roomMessage.RoomPrice; var htole = db.HotelManag.Find(hotmana.HotelID); if (htole.HotelDiscount > hotmana.RoomPrice) { htole.HotelDiscount = hotmana.RoomPrice; } hotmana.Boolbreakfast = roomMessage.Boolbreakfast; //db.Entry(manag).State = EntityState.Modified; db.SaveChanges(); return(Content("<script>alert('修改成功');location.href = '/GHotelHome/RoomIndex';</script>")); } else { return(Content("<script>alert('修改失败,房间数量小于该房间已出售数量');location.href = '/GHotelHome/EditRoom?id=" + roomMessage.RoomID + "';</script>")); } } }
public System.Web.Mvc.ActionResult Index(RoomMessage roomMessage, string[] RoomFacility) { string fi = ""; foreach (var item in RoomFacility) { //可以自己写Insert语句(添加数据) fi = fi + "+" + item.ToString();//这样写方便调时看 } roomMessage.RoomFacility = fi; int hotelID = int.Parse(Session["HotelID"].ToString()); System.Web.HttpFileCollection file = System.Web.HttpContext.Current.Request.Files; var photocount = file.Count; string[] imgname = new string[photocount]; if (photocount > 0 && file[0].FileName != "") { for (int i = 0; i < photocount; i++) { if (file[i].FileName == "" || file[i].FileName == null) { imgname[i] = ""; } else { string guid = Guid.NewGuid().ToString(); string path = "~/images/Roomphoto/" + guid + file[i].FileName; file[i].SaveAs(Server.MapPath(path));//必须得是相对路径 imgname[i] = "/images/Roomphoto/" + guid + file[i].FileName; } } using (SmartRentalSystemEntities db = new SmartRentalSystemEntities()) { RoomPhoto roomph = new RoomPhoto(); for (int i = 0; i < photocount; i++) { if (i == 0) { roomph.PhotoName1 = imgname[i].ToString(); } if (i == 1) { roomph.PhotoName2 = imgname[i].ToString(); } if (i == 2) { roomph.PhotoName3 = imgname[i].ToString(); } if (i == 3) { roomph.PhotoName4 = imgname[i].ToString(); } if (i == 4) { roomph.PhotoName5 = imgname[i].ToString(); } if (i == 5) { roomph.PhotoName6 = imgname[i].ToString(); } if (i == 6) { roomph.PhotoName7 = imgname[i].ToString(); } } var bb = db.RoomPhoto.Add(roomph); db.SaveChanges(); int photoID = bb.RoomPhotoID; roomMessage.RoomPhotoID = photoID; roomMessage.RoomCount = roomMessage.RoomRemain; roomMessage.HotelID = hotelID; db.RoomMessage.Add(roomMessage); var htole = db.HotelManag.Find(hotelID); if (htole.HotelDiscount > roomMessage.RoomPrice) { htole.HotelDiscount = roomMessage.RoomPrice; } db.SaveChanges(); //if (db.SaveChanges()>0) //return Content("<script>alert('添加成功');location.href='/GHotelHome/Index'</script>"); } return(Content("<script>alert('添加成功');location.href = '/GHotelHome/Index';</script>")); } return(Content("<script>alert('添加失败');location.href = '/GHotelHome/Index';</script>")); }
private List <Room> GetListOfRooms() { Models.MoveToLondon mtl = new Models.MoveToLondon(); List <Room> rooms = new List <Models.Room>(); if (Convert.ToBoolean(Session["IsEnglishVersion"]) == true) { rooms = mtl.Rooms.Where(o => !o.IsFrench).ToList(); } else { rooms = mtl.Rooms.Where(o => o.IsFrench).ToList(); } return(rooms); string serverMapPath = Server.MapPath(@"/"); string filepath = Convert.ToBoolean(Session["IsEnglishVersion"]) ? serverMapPath + ConfigurationManager.AppSettings["EnglishRoomDataFilePath"].ToString() : serverMapPath + ConfigurationManager.AppSettings["FrenchRoomDataFilePath"].ToString(); string line = string.Empty; List <Room> roomsList = new List <Room>(); int roomsCounter = -1; bool isDescriptionStarted = false; string description = string.Empty; bool isPathsStarted = false; using (StreamReader sr = new StreamReader(filepath)) { while ((line = sr.ReadLine()) != null) { if (string.IsNullOrWhiteSpace(line)) { continue; } if (string.IsNullOrEmpty(line)) { continue; } if (line.StartsWith("***")) { Room room = new Room(); roomsList.Add(room); roomsCounter++; isPathsStarted = false; continue; } if (line.Trim().ToLower().StartsWith("title:")) { roomsList[roomsCounter].Title = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1]; continue; } if (line.Trim().ToLower().StartsWith("address:")) { roomsList[roomsCounter].Address = line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1]; continue; } if (line.Trim().ToLower().StartsWith("rentpermonth:")) { int rent = 0; int.TryParse(line.Trim().Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1], out rent); roomsList[roomsCounter].RentPerMonth = rent; continue; } if (line.Trim().ToLower().StartsWith("description:")) { isDescriptionStarted = true; description += line.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries)[1]; continue; } if (line.Trim().ToLower().StartsWith("photospaths:")) { isDescriptionStarted = false; roomsList[roomsCounter].Description = description; description = string.Empty; isPathsStarted = true; continue; } if (isDescriptionStarted) { description += "\n" + line; } if (isPathsStarted) { RoomPhoto rf = new RoomPhoto(); rf.Path = line.Trim(); //if (roomsList[roomsCounter].ListRoomPhoto == null) // roomsList[roomsCounter].ListRoomPhoto = new List<RoomPhoto>(); //roomsList[roomsCounter].ListRoomPhoto.Add(rf); } } } return(roomsList); }