public async void Add_Post_Image(int ID_Post, string ID_User, List <IFormFile> images) { if (images.Count > 0 && images[0].Length > 0) { for (int i = 0; i < images.Count; i++) { var file = images[i]; if (file != null && images[i].Length > 0) { string fileName = Path.GetFileName(file.FileName); string extensionFileName = Path.GetExtension(fileName); if (fileName.Length - extensionFileName.Length > 40) { fileName = fileName.Substring(0, 40) + "-" + ID_User + "-" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("/", "") + extensionFileName; } else { fileName = fileName.Substring(0, fileName.Length - extensionFileName.Length) + "-" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("/", "") + extensionFileName; } var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images\posts", fileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } Post_Image pstImg = new Post_Image(); pstImg.url = fileName; pstImg.AddedDate = DateTime.Now; pstImg.ID_Post = ID_Post; DataProvider.Ins.db.Post_Image.Add(pstImg); DataProvider.Ins.db.SaveChanges(); } } } }
public ActionResult AddPost(PostData postData, string posttype, string project, string typeOfRealEstate, string bedroom, string bathroom, string floor, string alley, string direction) { using (var trans = db.Database.BeginTransaction()) { try { Employee poster = Session["Account_Censor"] as Employee; if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) { for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && Request.Files[i].ContentLength > 0) { string pic = Path.GetFileName(file.FileName); string extensionFileName = CommonFunction.getExtensionFileName(pic); pic = CommonFunction.hashSHA256(pic) + extensionFileName; string path = Path.Combine(Server.MapPath(Constants.POST_IMG_URL), pic); file.SaveAs(path); Post_Image pstImg = new Post_Image(); pstImg.url = pic; postData.post.Post_Image.Add(pstImg); } } } postData.post.Type1 = db.Type1.Single(n => n.PostType_ID.ToString() == posttype); postData.post.Project = db.Projects.SingleOrDefault(n => n.Project_ID.ToString() == project); postData.post.RealEstateType = db.RealEstateTypes.Single(n => n.RealEstateType_ID.ToString() == typeOfRealEstate); postData.post.Employee = db.Employees.Find(poster.Employee_ID); if (typeOfRealEstate == "1" && (bedroom != null || bathroom != null || floor != null || alley != null || direction != null)) { Detail detail = new Detail(); detail.Bedroom = Convert.ToInt32(bedroom); detail.Bathroom = Convert.ToInt32(bathroom); detail.Floor = Convert.ToInt32(floor); detail.Alley = alley == "1" ? true : false; detail.Direction = db.Directions.SingleOrDefault(n => n.Direction_ID.ToString() == direction); postData.post.Detail = detail; } db.Posts.Add(postData.post); db.SaveChanges(); Post_Status ps = new Post_Status(); ps.Status = db.Status.Find(2); ps.Employee = db.Employees.Find(poster.Employee_ID); ps.Reason = "This post was created by Employee ID: " + poster.Employee_ID; postData.post.Post_Status.Add(ps); db.SaveChanges(); trans.Commit(); TempData["addNewPost"] = "OK"; return(RedirectToAction("AddPost")); } catch (Exception ex) { Console.WriteLine(ex.ToString()); trans.Rollback(); return(View()); } } }
public ActionResult PostCustomer(CustomerPost post, HttpPostedFileBase fileUpload) { ViewBag.RealEstaleType = new SelectList(db.RealEstateTypes.ToList().OrderBy(n => n.Name), "RealEstateType_ID", "Name"); ViewBag.PostType = new SelectList(db.Type1.ToList().OrderBy(n => n.Name), "PostType_ID", "Name"); ViewBag.Direction = new SelectList(db.Directions.ToList().OrderBy(n => n.Direction_Name), "Direction_ID", "Direction_Name"); if (Session["Account"] == null || Session["Account"].ToString() == "") { return(RedirectToAction("Login", "Account")); } if (ModelState.IsValid) { Account cst = Session["Account"] as Account; var account = db.Accounts.Where(x => x.Account_ID == cst.Account_ID); Project project = new Project(); project.ProjectName = post.ProjectName; project.Location = post.LocationProject; project.Protential = post.Protential; Detail pst_detail = new Detail(); pst_detail.Floor = post.Floor; pst_detail.Bedroom = post.Bedroom; pst_detail.Bathroom = post.Bathroom; pst_detail.Direction = db.Directions.Find(post.Direction); Post pst = new Post(); var cust = db.Customers.Where(x => x.Account.Account_ID == cst.Account_ID).FirstOrDefault(); pst.Customer = cust; pst.Tittle = post.Tittle; pst.Price = post.Price; pst.Location = post.Location; pst.Area = post.Area; pst.Description = post.Description; pst.RealEstateType = db.RealEstateTypes.Find(post.RealEstaleType); pst.Type1 = db.Type1.Find(post.PostType); Post_Image pst_img = new Post_Image(); pst.Project = project; pst.Detail = pst_detail; //Lưu tên file var fileName = Path.GetFileName(fileUpload.FileName); //Lưu đường dẫn của file var path = Path.Combine(Server.MapPath("~/Images/Post"), fileName); if (System.IO.File.Exists(path)) { ViewBag.ThongBao = "Images already exists"; } else { fileUpload.SaveAs(path); } pst_img.url = fileUpload.FileName; Project_Image pr_img = new Project_Image(); var fileName1 = Path.GetFileName(fileUpload.FileName); var path1 = Path.Combine(Server.MapPath("~/Images/Project"), fileName1); if (System.IO.File.Exists(path1)) { ViewBag.ThongBao = "Images already exists"; } else { fileUpload.SaveAs(path1); } pr_img.url = fileUpload.FileName; db.Projects.Add(project); db.Posts.Add(pst); pst.Post_Image.Add(pst_img); project.Project_Image.Add(pr_img); db.Details.Add(pst_detail); db.SaveChanges(); ViewBag.ThongBao1 = "Post successful"; } return(View()); }
public async Task <IActionResult> Edit(int id, [Bind("ID_Post,ID_Account,PostTime,PostType,Tittle,Size,Project,Price,RealEstateType,Description,Status")] Post post, List <IFormFile> images, int district, int ward, int street , string diachi, bool alley, bool nearSchool, bool nearAirport, bool nearHospital, bool nearMarket, string descriptiondetail, int bathroom, int bedroom, int yard, int floor, int province) { if (id != post.ID_Post) { return(NotFound()); } if (images.Count > 0 && images[0].Length > 0) { for (int i = 0; i < images.Count; i++) { var file = images[i]; if (file != null && images[i].Length > 0) { string fileName = Path.GetFileName(file.FileName); string extensionFileName = Path.GetExtension(fileName); fileName = fileName.Substring(0, fileName.Length - extensionFileName.Length) + "-" + DateTime.Now.ToString().Replace(" ", "").Replace(":", "").Replace("/", "") + extensionFileName; var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\images\posts", fileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } Post_Image pstImg = new Post_Image(); pstImg.url = fileName; pstImg.AddedDate = DateTime.Now; pstImg.ID_Post = post.ID_Post; _context.Post_Image.Add(pstImg); } } } Post_Detail postdetail = _context.Post_Detail.Where(p => p.ID_Post == id).SingleOrDefault(); postdetail.Alley = alley; postdetail.Bathroom = bathroom; postdetail.Bedroom = bedroom; postdetail.Description = descriptiondetail; postdetail.Floor = floor; postdetail.NearAirport = nearAirport; postdetail.NearHospital = nearHospital; postdetail.NearMarket = nearMarket; postdetail.NearSchool = nearSchool; postdetail.Yard = yard; Post_Location post_Location = _context.Post_Location.Where(p => p.ID_Post == id).SingleOrDefault(); post_Location.DiaChi = diachi; if (street == 0) { post_Location.Duong_Pho = null; } else { post_Location.Duong_Pho = street; } if (ward == 0) { post_Location.Phuong_Xa = null; } else { post_Location.Phuong_Xa = ward; } if (district == 0) { post_Location.Quan_Huyen = null; } else { post_Location.Quan_Huyen = district; } post_Location.Tinh_TP = province; post_Location.DuAn = post.Project; var user = await _userManager.GetUserAsync(User); Post_Status post_Status = new Post_Status(); post_Status.ID_Account = user.Id; post_Status.ID_Post = post.ID_Post; post_Status.Reason = "Bài đăng được cập nhật. Xin đợi admin duyệt"; post_Status.Status = 5; post_Status.ModifiedDate = DateTime.Now; if (ModelState.IsValid) { try { _context.Update(post); _context.Update(postdetail); _context.Update(post_Location); _context.Post_Status.Add(post_Status); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(post.ID_Post)) { return(NotFound()); } else { throw; } } //return View(post); return(RedirectToAction(nameof(Details), new { id = id })); } int Province = post.Post_Location.SingleOrDefault().Tinh_TPNavigation.id; int District = post.Post_Location.SingleOrDefault().Quan_HuyenNavigation.id; int Ward = post.Post_Location.SingleOrDefault().Phuong_XaNavigation.id; int Street = post.Post_Location.SingleOrDefault().Duong_PhoNavigation.id; ViewData["ID_Account"] = user.Id; string nameuser = ""; if (user.IsAdmin == 1) { nameuser = "******" + _context.Admin.Where(p => p.Account_ID == user.Id).SingleOrDefault().FullName; } else { var cus = _context.Customer.Where(p => p.Account_ID == user.Id).SingleOrDefault(); nameuser = cus.FirstName + " " + cus.LastName; } ViewData["Name_Account"] = nameuser; ViewData["PostType"] = new SelectList(_context.Post_Type, "ID_PostType", "Description", post.PostType); ViewData["Project"] = new SelectList(_context.project, "id", "_name", post.Project); ViewData["RealEstateType"] = new SelectList(_context.RealEstate_Type, "ID_RealEstateType", "Description", post.RealEstateType); ViewData["Province"] = new SelectList(_context.province.OrderBy(p => p._name), "id", "_name", Province); ViewData["District"] = new SelectList(_context.district.OrderBy(p => p._name).Where(p => p._province_id == Province), "id", "_name", District); ViewData["Ward"] = new SelectList(_context.ward.OrderBy(p => p._name).Where(p => p._province_id == Province && p._district_id == District), "id", "_name", Ward); ViewData["Street"] = new SelectList(_context.street.OrderBy(p => p._name).Where(p => p._province_id == Province && p._district_id == District), "id", "_name", Street); return(View(post)); }