public IActionResult editblog([Bind( nameof(EditBlog.Id), nameof(EditBlog.Title), nameof(EditBlog.Text), nameof(EditBlog.datetime), nameof(EditBlog.imageFile), nameof(EditBlog.CategoryId) )] EditBlog model) { if (ModelState.IsValid) { try { model.Edit(); model.Response = new ResponseModel("Update Successfull", ResponseType.Success); return(RedirectToAction("index")); } catch (DuplicationException message) { model.Response = new ResponseModel(message.Message, ResponseType.Failure); } catch (Exception ex) { model.Response = new ResponseModel("Update Failed.", ResponseType.Failure); // error logger code } } return(View(model)); }
public IActionResult editblog(int id) { var model = new EditBlog(); model.Load(id); return(View(model)); }
public ActionResult EditSubmit(EditBlog data, int?blogId) { if (!string.IsNullOrWhiteSpace(data.Photo)) { var originalDirectory = new DirectoryInfo(string.Format("{0}MediaUpload\\", Server.MapPath(@"\"))); var pathString = Path.Combine(originalDirectory.ToString(), "BlogPhoto"); var ext = Path.GetExtension(data.Photo); var photoName = "temp" + GetOperation().UserId + ext; data.Photo = string.Format("{0}\\{1}", pathString, photoName); } var result = BlogLogic.Edit(data, GetOperation().UserId, blogId); return(Json(result)); }
public async Task <IActionResult> EditBlog(EditBlog editBlog) { string date = DateTime.Now.ToString("yyyy/MM/dd"); string sqlEscapeTitle = Regex.Replace(editBlog.title, "'", "''"); string sqlEscapeContent = Regex.Replace(editBlog.content, "'", "''"); string queryString = string.Format("UPDATE [BlogItem] SET Title = N'{0}', Content = N'{1}', DateModified = '{2}' WHERE ID = {3}", sqlEscapeTitle, sqlEscapeContent, date, editBlog.id); string connString = ConfigurationExtensions.GetConnectionString(configuration, "BlogAPI"); await using (SqlConnection connection = new(connString)) { SqlCommand command = new(queryString, connection); connection.Open(); command.ExecuteNonQuery(); connection.Close(); } return(Ok()); }
public IsSuccessResult <int> Edit(EditBlog data, int userId, int?blogId) { var log = GetLogger(); log.Debug("Title: {0}, Detail: {1}, Tag: {2}, UserId: {3}, BlogId: {4}, Catalog: {5}" , data.Title, data.Detail, data.Tag, userId, blogId, data.Catalog); var user = NiHonGoContext.Users .SingleOrDefault(r => r.Id == userId); if (user == null) { return(new IsSuccessResult <int>(ErrorCode.UserNotFound.ToString())); } if ((UserType)user.Type != UserType.Admin) { return(new IsSuccessResult <int>(ErrorCode.NoAuthentication.ToString())); } if (string.IsNullOrWhiteSpace(data.Title)) { return(new IsSuccessResult <int>(ErrorCode.BlogTitleIsNull.ToString())); } if (string.IsNullOrWhiteSpace(data.Detail)) { return(new IsSuccessResult <int>(ErrorCode.BlogDetailIsNull.ToString())); } if (string.IsNullOrWhiteSpace(data.Catalog)) { return(new IsSuccessResult <int>(ErrorCode.CatalogIsNull.ToString())); } var blog = NiHonGoContext.Blogs .SingleOrDefault(r => r.Id == blogId); var catalog = NiHonGoContext.Catalogs .SingleOrDefault(r => r.Display == data.Catalog); try { if (catalog == null) { catalog = new Grammer { Display = data.Catalog }; NiHonGoContext.Catalogs.Add(catalog); NiHonGoContext.SaveChanges(); } if (blog == null) { var currentDate = DateTime.UtcNow; blog = new Blog { Title = data.Title, Detail = data.Detail, UserId = userId, Tag = data.Tag, Catalog = catalog, CreateDate = currentDate, }; NiHonGoContext.Blogs.Add(blog); NiHonGoContext.SaveChanges(); if (!string.IsNullOrWhiteSpace(data.Photo)) { var oldPhotoName = data.Photo.Split('\\').Last(); var newPhotoName = blog.Id + Path.GetExtension(oldPhotoName); File.Move(data.Photo, data.Photo.Replace(oldPhotoName, newPhotoName)); blog.Photo = newPhotoName; NiHonGoContext.SaveChanges(); } } else { blog.Title = data.Title; blog.Detail = data.Detail; blog.Tag = data.Tag; blog.UserId = userId; blog.Catalog = catalog; NiHonGoContext.SaveChanges(); } return(new IsSuccessResult <int> { ReturnObject = blog.Id }); } catch (Exception ex) { log.Error(ex); return(new IsSuccessResult <int>(ErrorCode.InternalError.ToString())); } }
public async Task <IActionResult> Edit(int id, [FromServices] IHostingEnvironment env, EditBlog blog) { if (ModelState.IsValid) { string filename = string.Empty; if (blog.Blog_Img != null) { filename = Path.Combine("usersImg", Guid.NewGuid().ToString() + Path.GetExtension(blog.Blog_Img.FileName)); using (var stream = new FileStream(Path.Combine(env.WebRootPath, filename), FileMode.CreateNew)) { blog.Blog_Img.CopyTo(stream); } } try { Blog updataBlog = new Blog { Blog_Id = id, Blog_Title = blog.Blog_Title, Blog_Digest = blog.Blog_Digest, Blog_Context = blog.Blog_Context, Tally_Id = blog.Tally_Id, Blog_Time = DateTime.Now, Admin_Id = HttpContext.Session.GetInt32(AdminID).Value, Blog_Img = filename }; blogContext.Update(updataBlog); await blogContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CourseExists(blog.Blog_Id)) { return(NotFound()); } throw; } return(RedirectToAction(nameof(Index))); } PopulateDepartmentsDropDownList(); return(View(blog)); }