Ejemplo n.º 1
0
        public async Task <IActionResult> BlockPost(int idpost, string reasonBlockPost, bool isreport = false)
        {
            if (reasonBlockPost == null)
            {
                return(NotFound());
            }
            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       = reasonBlockPost;
                poststatus.Status       = 3;
                poststatus.ModifiedDate = DateTime.Now;
                _context.Post_Status.Add(poststatus);
            }
            try
            {
                if (isreport)
                {
                    if (!checkIsReadRepost(idpost))
                    {
                        StatusMessage = "Error Khóa bài đăng không thành công";
                        return(RedirectToAction(nameof(DetailReportPost), new { id = idpost }));
                    }
                }
                await _context.SaveChangesAsync();

                post.Status = poststatus.ID_PostStatus;
                _context.Post.Attach(post);
                _context.Entry(post).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
                StatusMessage = "Khóa bài đăng thành công";
            }
            catch
            {
                StatusMessage = "Error Khóa bài đăng không thành công";
            }
            if (isreport)
            {
                return(RedirectToAction(nameof(ListReportPost)));
            }
            return(RedirectToAction(nameof(Index)));
        }
        public async Task <IActionResult> Create([Bind("ID_Post,ID_Account,PostTime,PostType,Tittle,Size,Project,Price,RealEstateType,Description,Status")] Post post, string id
                                                 , int district, int ward, int street, string diachi, bool alley, bool nearSchool, bool nearAirport, bool nearHospital, bool nearMarket, List <IFormFile> images, string descriptiondetail, int bathroom,
                                                 int bedroom, int yard, int floor, int province)
        {
            var user = await _userManager.GetUserAsync(User);

            post.PostTime   = DateTime.Now;
            post.ID_Account = user.Id;
            Post a = post;

            _context.Post.Add(post);
            await _context.SaveChangesAsync();

            var completePost = new CompletePost();
            var builder      = new PostBuilder();

            completePost.Builder = builder;


            completePost.buildFullPost(user.Id, post.ID_Post
                                       , district, ward, street, diachi, alley, nearSchool, nearAirport, nearHospital, nearMarket, images, descriptiondetail, bathroom,
                                       bedroom, yard, floor, province, user.IsAdmin, post.Project);

            try
            {
                StatusMessage = "Đăng bài thành công!";
                return(RedirectToAction("Index", "Post", new { area = "ManagePosts" }));
            }
            catch (Exception e)
            {
                string m = e.Message;
                StatusMessage = "Error Đăng bài không thành công";
            }


            ViewData["PostType"]       = new SelectList(_context.Post_Type, "ID_PostType", "Description");
            ViewData["Project"]        = new SelectList(_context.project, "id", "_name");
            ViewData["RealEstateType"] = new SelectList(_context.RealEstate_Type, "ID_RealEstateType", "Description");
            ViewData["IDAccount"]      = user.Id;
            ViewData["Province"]       = new SelectList(_context.province.OrderBy(p => p._name), "id", "_name");
            return(View(post));
        }
        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));
        }