public async Task<ActionResult> Create(Post model) { using (var userManager = UserManager) { if (!ModelState.IsValid) { return View(model); } var user = await userManager.FindByIdAsync(CurrentUserId); var teacher = _teacherRepo.GetTeacher(user.Teacher.TeacherId); model.Slug = model.Title.MakeUrlFriendly(); // TODO: МЫ ХРАНИМ ВРЕМЯ В UTC model.Created = DateTime.Now; model.Blog = teacher.Blog; try { _blogRepo.CreatePost(model); return RedirectToAction("index"); } catch (Exception e) { ModelState.AddModelError("key", e); return View(model); } } }
public void CreatePost(Post post) { post.Created = DateTime.UtcNow; _context.Posts.Add(post); _context.SaveChanges(); }
public async Task EditPost(int id, Post updatedPost) { var post = await _context.Posts.SingleOrDefaultAsync(p => p.PostId == id); if (post == null) { throw new KeyNotFoundException($"A post with the id of {id} does not exist in the data store."); } post.Modified = DateTime.UtcNow; post.PostId = updatedPost.PostId; post.Title = updatedPost.Title; post.Tags = updatedPost.Tags; await _context.SaveChangesAsync(); }
public async Task<ActionResult> Edit(int postId, Post model) { if (!ModelState.IsValid) { return View(model); } using (var userManager = UserManager) { var postAuthorId = model.Blog.TeacherId; var currentUserTeacherId = await _teacherRepo.GetCurrentUserTeacherIdAsync(CurrentUserId); if (postAuthorId != currentUserTeacherId) { return new HttpUnauthorizedResult(); } try { model.Modified = DateTime.Now; model.Slug = model.Title.MakeUrlFriendly(); await _blogRepo.EditPost(postId, model); return RedirectToAction("index", new { nameSlug = model.Blog.Teacher.NameSlug, postSlug = model.Slug }); } catch (KeyNotFoundException) { return HttpNotFound(); } catch (Exception e) { ModelState.AddModelError(string.Empty, e.Message); return View(model); } } }