Ejemplo n.º 1
0
        public Blog_Post(DataRow myDR)
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
            if (myDR != null)
            {
                _postid           = myDR["PostID"].ToString();
                _userid           = myDR["UserID"].ToString();
                _ownership_userid = myDR["Ownership_UserID"].ToString();
                _username         = myDR["UserName"].ToString();
                _bloggroupid      = myDR["BlogGroupID"].ToString();
                _post_date        = myDR["Post_Date"].ToString();
                _post_modifydate  = myDR["Post_ModifyDate"].ToString();
                _post_title       = myDR["Post_Title"].ToString();
                _post_content     = myDR["Post_Content"].ToString();
                _post_status      = (Post_Status)StringEnum.Parse(typeof(Post_Status), myDR["Post_Status"].ToString(), true);
                _post_password    = myDR["Post_Password"].ToString();
                _view_count       = myDR["View_Count"].ToString();
                _comment_count    = myDR["Comment_Count"].ToString();

                _post_date_short = Convert.ToDateTime(_post_date).ToShortDateString();
            }
        }
        public async Task <IActionResult> ShownPost(int idpost)
        {
            var post = await _context.Post.FindAsync(idpost);

            Post_Status poststatus = new Post_Status();

            if (post != null)
            {
                poststatus.ID_Post = post.ID_Post;
                var user = await _userManager.GetUserAsync(User);

                poststatus.ID_Account   = user.Id;
                poststatus.Reason       = "";
                poststatus.Status       = 5;
                poststatus.ModifiedDate = DateTime.Now;
                _context.Post_Status.Add(poststatus);
            }

            try
            {
                await _context.SaveChangesAsync();

                post.Status = poststatus.ID_PostStatus;
                _context.Post.Attach(post);
                _context.Entry(post).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
                var Pending = Int32.Parse(HttpContext.Session.GetString("PedingPost")) + 1;
                HttpContext.Session.SetString("PedingPost", Pending.ToString());
                StatusMessage = "Hiện bài đăng thành công";
            }
            catch
            {
                StatusMessage = "Error Hiện bài đăng không thành công";
            }
            return(RedirectToAction(nameof(Index)));
        }
        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 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));
        }