public IActionResult UpdateProfile(string email, AccountManage profileInput, IFormFile Avatar) { CredentialManage credential = JsonConvert.DeserializeObject <CredentialManage>(HttpContext.Session.GetString(Constants.VM_MANAGE)); string token = credential.JwToken; AccountManage profile = GetApiAccountManage.GetAccountManages(credential.JwToken) .Select(p => new AccountManage() { Email = p.Email, AccountRoleId = p.AccountRoleId, FullName = profileInput.FullName, IsActivated = p.IsActivated, Avatar = p.Avatar, Address = profileInput.Address, Password = p.Password }).SingleOrDefault(p => p.Email == email); string accountImg = Encryptor.RandomString(12); string extension = Avatar != null?Path.GetExtension(Avatar.FileName) : ""; if (Avatar != null) { if (SlugHelper.CheckExtension(extension)) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images/avatar", accountImg + extension); using (var file = new FileStream(path, FileMode.Create)) { Avatar.CopyTo(file); } profile.Avatar = accountImg + extension; } else { ModelState.AddModelError("", Constants.EXTENSION_IMG_NOT_SUPPORT); return(Content(Constants.EXTENSION_IMG_NOT_SUPPORT)); } } using (HttpClient client = HelperClient.GetClient(token)) { client.BaseAddress = new Uri(Constants.BASE_URI); var putTask = client.PutAsJsonAsync <AccountManage>(Constants.ACCOUNT_MANAGE + "/" + profile.Email, profile); putTask.Wait(); var result = putTask.Result; return(View()); } }
public IActionResult EditProfile(UserProfile profile, IFormFile avatarFile) { if (ModelState.IsValid) { //get user current UserProfile user = GetApiUserProfile.GetUserProfiles().SingleOrDefault(p => p.UserProfileEmail == profile.UserProfileEmail); profile.AccountId = user.AccountId; profile.CustomerTypeId = user.CustomerTypeId; profile.UserProfileAvatar = user.UserProfileAvatar; if (avatarFile != null) { string extension = Path.GetExtension(avatarFile.FileName); if (SlugHelper.CheckExtension(extension)) { // delete img current var pathCurrent = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\avatar", profile.UserProfileAvatar); if (System.IO.File.Exists(pathCurrent)) { System.IO.File.Delete(pathCurrent); } string avatarName = Encryptor.RandomString(12); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images/avatar", avatarName + extension); using (var file = new FileStream(path, FileMode.Create)) { avatarFile.CopyTo(file); } profile.UserProfileAvatar = avatarName + extension; } else { ViewBag.error = "Kiểu file không hỗ trợ (jpg, png, ...)"; return(View("Index", profile)); } } CredentialModel credential = JsonConvert.DeserializeObject <CredentialModel>(HttpContext.Session.GetString("vm")); UserProfile update = UpdateProfile(profile); if (update != null) { // update sessionlogin credential.Profile = update; HttpContext.Session.SetObject("vm", credential); return(RedirectToAction("Index")); } else { ViewBag.error = "Cập nhật thất bại"; } } else { ViewBag.error = "Dữ liệu không thể để trống"; } return(View("Index", profile)); }
public IActionResult EditProduct(int id, Product product, IFormFile productFile) { CredentialManage credential = JsonConvert.DeserializeObject <CredentialManage>(HttpContext.Session.GetString(Constants.VM_MANAGE) != null ? HttpContext.Session.GetString(Constants.VM_MANAGE) : ""); string token = credential.JwToken; Product current = GetApiProducts.GetProducts().SingleOrDefault(p => p.ProductId == id); Product update = new Product() { ProductId = id, ProductName = product.ProductName, ProductDescription = product.ProductDescription, ProductPrice = product.ProductPrice, ProductDiscount = product.ProductDiscount, NumberOfPurchases = current.NumberOfPurchases, CategoryId = product.CategoryId, DistributorId = product.DistributorId, IsActivated = true, SlugName = SlugHelper.GetFriendlyTitle(product.ProductName), InitAt = current.InitAt }; // product img string productImg = Encryptor.RandomString(12); string extension = productFile != null?Path.GetExtension(productFile.FileName) : ""; if (productFile != null) { if (SlugHelper.CheckExtension(extension)) { update.ProductImage = productImg + extension; } else { ViewBag.error = Constants.EXTENSION_IMG_NOT_SUPPORT; return(View()); } } else { update.ProductImage = current.ProductImage; } using (HttpClient client = Common.HelperClient.GetClient(token)) { client.BaseAddress = new Uri(Common.Constants.BASE_URI); var postTask = client.PutAsJsonAsync <Product>(Constants.PRODUCT + "/" + update.ProductId, update); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { // save img if (productFile != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images/products", productImg + extension); using (var file = new FileStream(path, FileMode.Create)) { productFile.CopyTo(file); } } return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "Chỉnh sửa thất bại"); return(View()); } } }
public IActionResult Create(ProductCreateModel create, IFormFile productFile) { if (create.ProductDiscount < 0 || create.ProductDiscount > 1) { return(Content(Constants.DISCOUNT_INVALID)); } Product product = new Product() { ProductName = create.ProductName, SlugName = SlugHelper.GetFriendlyTitle(create.ProductName), ProductPrice = create.ProductPrice, ProductDiscount = create.ProductDiscount, DistributorId = create.DistributorId, CategoryId = create.CategoryId, ProductDescription = create.ProductDescription, InitAt = DateTime.Now, NumberOfPurchases = 0, IsActivated = false }; // product img string productImg = Encryptor.RandomString(12); string extension = productFile != null?Path.GetExtension(productFile.FileName) : ""; if (productFile != null) { if (SlugHelper.CheckExtension(extension)) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images/products", productImg + extension); using (var file = new FileStream(path, FileMode.Create)) { productFile.CopyTo(file); } product.ProductImage = productImg + extension; } else { ModelState.AddModelError("", Constants.EXTENSION_IMG_NOT_SUPPORT); return(Content(Constants.EXTENSION_IMG_NOT_SUPPORT)); } } else { ModelState.AddModelError("", Constants.IMG_REQUIRED); return(Content(Constants.IMG_REQUIRED)); } Product createdProduct = CreatedProduct(product); if (createdProduct != null) { string catName = GetApiCategories.GetCategories().SingleOrDefault(p => p.CategoryId == createdProduct.CategoryId).CategoryName; switch (catName) { case Constants.FOOD: CreateAmountFood(createdProduct.ProductId); break; case Constants.TOY: CreateAmountToys(createdProduct.ProductId); break; case Constants.COSTUME: CreateAmountCostume(createdProduct.ProductId); break; } return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }
public IActionResult Create(AccountManageDTO dto, IFormFile Avatar) { var obj = dto; if (dto.Password == null) { return(NoContent()); } AccountManage accountManage = new AccountManage() { FullName = dto.FullName, Address = dto.Address, Email = dto.Email, IsActivated = dto.IsActivated, Password = Encryptor.MD5Hash(dto.Password), AccountRoleId = GetApiAccountRoles.GetAccountRoles().SingleOrDefault(q => q.AccountRoleName == dto.AccountRoleName).AccountRoleId }; string accountImg = Encryptor.RandomString(12); string extension = Avatar != null?Path.GetExtension(Avatar.FileName) : ""; if (Avatar != null) { if (SlugHelper.CheckExtension(extension)) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images/avatar", accountImg + extension); using (var file = new FileStream(path, FileMode.Create)) { Avatar.CopyTo(file); } accountManage.Avatar = accountImg + extension; } else { ModelState.AddModelError("", Constants.EXTENSION_IMG_NOT_SUPPORT); return(Content(Constants.EXTENSION_IMG_NOT_SUPPORT)); } } else { accountManage.Avatar = "denyPaw.png"; } //account avatar CredentialManage credential = JsonConvert.DeserializeObject <CredentialManage>(HttpContext.Session.GetString(Constants.VM_MANAGE) != null ? HttpContext.Session.GetString(Constants.VM_MANAGE) : ""); string token = credential.JwToken; using (HttpClient client = HelperClient.GetClient(token)) { client.BaseAddress = new Uri(Common.Constants.BASE_URI); var postTask = client.PostAsJsonAsync <AccountManage>(Constants.ACCOUNT_MANAGE, accountManage); postTask.Wait(); var result = postTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync <Product>(); readTask.Wait(); } return(RedirectToAction(nameof(Index))); } }