Guest User

Untitled

a guest
Nov 18th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. namespace PCServer.Data
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5.  
  6. public partial class Post: IEntity
  7. {
  8. public int Id { get; set; }
  9. public string Title { get; set; }
  10. public string Message { get; set; }
  11. public DateTime Date { get; set; }
  12.  
  13. public virtual Post ParentPost { get; set; }
  14. public virtual AspNetUser Author { get; set; }
  15. }
  16. }
  17.  
  18. public partial class Post
  19. {
  20. //This class "do" something, like adding a post or deleting a post
  21. public static class Do
  22. {
  23. public static void AddPost(ref ApplicationDbContext context, string postMessage)
  24. {
  25. //Create a post
  26. Post p = new Post();
  27. p.Title = "This is an example!";
  28. p.message = postMessage;
  29. p.Date = DateTime.UtcNow;
  30.  
  31. //Adding to context
  32. BaseService.Add(post, out context);
  33. }
  34.  
  35. public static void DeletePost(ref ApplicationDbContext context, int postId)
  36. {
  37. PostRepository postRepo = new PostRepository(context);
  38.  
  39. postRepo.GetById(postId);
  40.  
  41. //Removing from context
  42. BaseService.Remove(post, out context);
  43. }
  44. }
  45.  
  46. //This class "Get" something, like all posts
  47. public static class Get
  48. {
  49. public static void GetPosts()
  50. {
  51. using(ApplicationDbContext context = new ApplicationDbContext())
  52. {
  53. PostRepository postRepo = new PostRepository(context);
  54. return postRepo.GetAllPosts();
  55. }
  56. }
  57. }
  58.  
  59. //This class "Set" something, like title of the post or the post itself maybe
  60. public static class Set
  61. {
  62. public static void Title(ref ApplicationDbContext context, int postId, string title)
  63. {
  64. PostRepository postRepo = new PostRepository(context);
  65. Post post = postRepo.GetById(postId);
  66. post.Title = title;
  67.  
  68. BaseService.Update(post, out context);
  69. }
  70.  
  71. public static void ChangePost(ref ApplicationDbContext context, int postId, Post post)
  72. {
  73. PostRepository postRepo = new PostRepository(context);
  74. Post dbPost = postRepo.GetById(postId);
  75. dbPost = post;
  76.  
  77. BaseService.Update(dbPost, out context);
  78. }
  79. }
  80. }
  81.  
  82. ApplicationDbContext c = new ApplicationDbContext();
  83.  
  84. Post.Do.AddPost(ref c,"Hi!");
  85. IEnumerable<Post> posts = Post.Get.GetPosts();
  86.  
  87. Post.Set.Title(ref c,100,"Changing title!");
  88.  
  89. await BaseService.CommitAsync<Post>(c);
Add Comment
Please, Sign In to add comment