Exemplo n.º 1
0
        public IActionResult ChangePassword(string password = "", string repassword = "******")
        {
            if (password != repassword)
            {
                TempData["Notice"] = "Lỗi: Mật khẩu nhập không khớp";
                return(RedirectToAction("ChangePassword"));
            }

            string username = HttpContext.Session.GetString("AdminSession");

            if (username == null)
            {
                return(LocalRedirect("~/Identity/Account/Login"));
            }

            RES.Data.DBModels.Admin admin = _context.Admin.Where(n => n.UserName == username).SingleOrDefault();

            if (admin == null)
            {
                return(NotFound());
            }

            admin.PasswordHash = HashPwdTool.GeneratePassword(password);

            _context.SaveChangesAsync();

            TempData["Notice"] = "Đổi mật khẩu thành công";
            return(RedirectToAction("ChangePassword"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("AdminId,UserName,PasswordHash")] RES.Data.DBModels.Admin admin)
        {
            if (ModelState.IsValid)
            {
                var adminDB = _context.Admin.Where(n => n.UserName == admin.UserName).SingleOrDefault();
                if (adminDB != null)
                {
                    TempData["Notice"] = "Lỗi: Tên tài khoản " + admin.UserName + " đã tồn tại.";
                    return(RedirectToAction("Create"));
                }
                admin.PasswordHash = HashPwdTool.GeneratePassword(admin.PasswordHash);
                _context.Add(admin);
                await _context.SaveChangesAsync();

                TempData["Notice"] = "Tạo admin " + admin.UserName + " thành công.";
                return(RedirectToAction(nameof(Index)));
            }
            return(View(admin));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Approve(string id = "")
        {
            if (id == "")
            {
                return(RedirectToAction("NewPost", "Home", new { Area = "Admin" }));
            }

            PostStatus ps = new PostStatus()
            {
                PostId         = id,
                Status         = 2,
                CensorshipTime = DateTime.Now,
                Reason         = "This post was approved by admin"
            };

            _context.Add(ps);
            await _context.SaveChangesAsync();

            TempData["Notice"] = "Duyệt tin thành công";

            return(RedirectToAction("NewPost", "Home", new { Area = "Admin" }));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("CustomerId,Firstname,LastName,Email,Address,PhoneNumber,AccountId,AvatarUrl,ModifiedDate")] Customer customer, IFormFile file = null)
        {
            if (ModelState.IsValid)
            {
                returnUrl = returnUrl ?? Url.Content("~/");

                var         email = User.Identity.Name;
                AspNetUsers user  = _context.AspNetUsers.Where(n => n.Email == email).Single();

                customer.Email        = email;
                customer.AccountId    = user.Id;
                customer.ModifiedDate = DateTime.Now;
                customer.CreatedDate  = DateTime.Now;

                if (file != null && file.Length > 0)
                {
                    // Add new image file
                    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\avatars", fileName);

                    using (var stream = new FileStream(path, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }

                    customer.AvatarUrl = fileName;
                }
                else
                {
                    customer.AvatarUrl = "noAvatar.png";
                }

                _context.Add(customer);
                await _context.SaveChangesAsync();

                HttpContext.Session.SetString("User_Name_Session", customer.LastName + " " + customer.Firstname);

                return(LocalRedirect(returnUrl));
            }

            return(View(customer));
        }
        public async Task <IActionResult> Index([Bind("InfoId,Email,Address,PhoneNumber,WorkingTime,Facebook,Twitter,Instagram,Pinterest,Linkedin")] Information information)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(information);
                    await _context.SaveChangesAsync();

                    TempData["Notice"] = "Lưu thông tin thành công";
                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!InformationExists(information.InfoId))
                    {
                        return(NotFound());
                    }
                }
            }

            TempData["Notice"] = "Lỗi: Không thể lưu thông tin, vui lòng thử lại sau.";
            return(View(information));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Create([Bind("PostId,PostType,PostTime,Tittle,Price,Location,Area,Project,Description,RealEstaleType,Detail,AuthorId,AuthorEmpId,Status")] Post post,
                                                 int bedroom     = 0, int bathroom     = 0, int floor    = 0, bool alley = false, int direction = 1, List <IFormFile> files = null,
                                                 string province = "", string district = "", string ward = "")
        {
            string      email = User.Identity.Name;
            AspNetUsers user  = _context.AspNetUsers.Where(n => n.Email == email).SingleOrDefault();

            if (user == null)
            {
                return(LocalRedirect("~/Identity/Account/Login?returnUrl=~/post/create"));
            }
            else
            {
                Customer customer = _context.Customer.Where(n => n.AccountId == user.Id).SingleOrDefault();

                if (customer == null)
                {
                    return(LocalRedirect("~/input-information?returnUrl=~/post/create"));
                }

                if (post.RealEstaleType == 1)
                {
                    post.DetailNavigation = new Detail()
                    {
                        Alley        = alley,
                        Bathroom     = bathroom,
                        Bedroom      = bedroom,
                        DirectionId  = direction,
                        Floor        = floor,
                        ModifiedDate = DateTime.Now
                    };
                }

                post.Location += ", " + ward + ", " + district + ", " + province;
                post.PostId    = CommonFunction.RemoveUnicode((post.Tittle + "-" + DateTime.Now.ToString().Replace(":", "").Replace("-", "").Replace(".", "").Replace(" ", ""))).ToLower().Replace(" ", "-");
                post.AuthorId  = customer.CustomerId;
                post.PostTime  = DateTime.Now;

                if (files.Count > 0 && files[0].Length > 0)
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        var file = files[i];

                        if (file != null && files[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);
                            }

                            PostImage pstImg = new PostImage();
                            pstImg.Url       = fileName;
                            pstImg.AddedDate = DateTime.Now;
                            post.PostImage.Add(pstImg);
                        }
                    }
                }



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

                TempData["Notice"] = "Tạo tin đăng thành công. Tin đăng đang chờ duyệt bởi admin.";

                return(RedirectToAction("Manager", "Post"));
            }

            //ViewData["Direction"] = new SelectList(_context.Direction, "DirectionId", "DirectionName");
            //ViewData["PostType"] = new SelectList(_context.Type, "PostTypeId", "Name", post.PostType);
            //ViewData["Project"] = new SelectList(_context.Project, "ProjectId", "Location", post.Project);
            //ViewData["RealEstaleType"] = new SelectList(_context.RealEstateType, "RealEstateTypeId", "Name", post.RealEstaleType);

            //return View(post);
        }
        public async Task <IActionResult> OnPostAsync(IFormFile file = null)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var user = await _userManager.GetUserAsync(User);

            var customer = _context.Customer.Where(n => n.AccountId == user.Id).SingleOrDefault() ?? new Customer();

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            customer.Firstname   = Input.Firstname;
            customer.LastName    = Input.LastName;
            customer.Address     = Input.Address;
            customer.PhoneNumber = Input.PhoneNumber;

            if (file != null && file.Length > 0)
            {
                // Delete old image file
                var oldPath = Directory.GetCurrentDirectory() + @"\wwwroot\images\avatars\" + customer.AvatarUrl;

                if (System.IO.File.Exists(oldPath) && customer.AvatarUrl != "noAvatar.png")
                {
                    System.IO.File.Delete(oldPath);
                }

                // Add new image file
                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\avatars", fileName);

                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }

                customer.AvatarUrl = fileName;
            }

            if (customer.CustomerId == 0)    // customer mới
            {
                if (customer.AvatarUrl == null || customer.AvatarUrl == "")
                {
                    customer.AvatarUrl = "noAvatar.png";
                }

                customer.AccountId   = user.Id;
                customer.Email       = user.UserName;
                customer.CreatedDate = DateTime.Now;

                _context.Customer.Add(customer);
            }

            await _context.SaveChangesAsync();

            HttpContext.Session.SetString("User_Name_Session", customer.LastName + " " + customer.Firstname);

            await _signInManager.RefreshSignInAsync(user);

            StatusMessage = "Thông tin đã cập nhật thành công";
            return(RedirectToPage());
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Edit(int id, [Bind("CustomerId,Firstname,LastName,Email,Address,PhoneNumber,AccountId,AvatarUrl,ModifiedDate")] Customer customer,
                                               IFormFile file = null)
        {
            if (id != customer.CustomerId)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (file != null && file.Length > 0)
                    {
                        // Delete old image file
                        var oldPath = Directory.GetCurrentDirectory() + @"\wwwroot\images\avatars\" + customer.AvatarUrl;

                        if (System.IO.File.Exists(oldPath) && customer.AvatarUrl != "noAvatar.png")
                        {
                            System.IO.File.Delete(oldPath);
                        }

                        // Add new image file
                        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\avatars", fileName);

                        using (var stream = new FileStream(path, FileMode.Create))
                        {
                            await file.CopyToAsync(stream);
                        }

                        customer.AvatarUrl = fileName;
                    }

                    _context.Update(customer);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CustomerExists(customer.CustomerId))
                    {
                        TempData["Notice"] = "Lỗi: Không thể lưu thông tin, vui lòng thử lại sau.";
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                TempData["Notice"] = "Lưu thông tin của " + customer.LastName + " " + customer.Firstname + " thành công";
                return(RedirectToAction(nameof(Index)));
            }

            return(View(customer));
        }