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" })); }
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> 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)); }
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); }