Guest User

Untitled

a guest
Oct 26th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. public ActionResult LikeMethod(string userId, int postId)
  2. {
  3. var userLike = _context.Publications.FirstOrDefault(u => u.UserId == userId);
  4. if (ModelState.IsValid)
  5. {
  6. if (userLike != null)
  7. {
  8. userLike.Like = userLike.Like + 1;
  9. userLike.UserId = userId;
  10. userLike.Id = postId;
  11.  
  12. _context.Update(userLike);
  13. _context.SaveChangesAsync();
  14. return RedirectToAction(nameof(Index));
  15. }
  16. }
  17.  
  18. return View();
  19. }
  20.  
  21. public ActionResult SubscriptionMethod(string userId, int postId)
  22. {
  23. var userLike = _context.Publications.FirstOrDefault(u => u.UserId == userId);
  24. if (ModelState.IsValid)
  25. {
  26. if (userLike != null)
  27. {
  28. userLike.Subscription = userLike.Subscription + 1;
  29. userLike.UserId = userId;
  30. userLike.Id = postId;
  31.  
  32. _context.Update(userLike);
  33. _context.SaveChangesAsync();
  34. return RedirectToAction(nameof(Index));
  35. }
  36. }
  37.  
  38. return View();
  39. }
  40.  
  41. [HttpPost]
  42. public async Task<IActionResult> Comment(int postId, string userId, string content, Comment comment)
  43. {
  44. var publ = _context.Publications.FirstOrDefault(c => c.Id == comment.PostId);
  45. if (publ != null) comment.ImageUrl = publ.ImageUrl;
  46.  
  47. if (ModelState.IsValid)
  48. {
  49. var user = await _userManager.GetUserAsync(User);
  50. var comm = new Comment
  51. {
  52. UserId = userId,
  53. PostId = postId,
  54. Content = content,
  55. CommentDate = DateTime.Now
  56. };
  57. comm.UserId = user.Id;
  58. _context.Add(comm);
  59. await _context.SaveChangesAsync();
  60. return RedirectToAction(nameof(Index));
  61. }
  62.  
  63. return View();
  64. }
  65.  
  66. <table>
  67. <tbody>
  68. <div class="row">
  69. <div class="col-lg-12">
  70. <a onclick="openCommentForm(@Model.Id)" style="cursor: pointer; color: darkslategray;">Оставить комментарий</a>
  71. </div>
  72. <div class="col-md-12" id="postdiv-@Model.Id" style="display: none; color: darkslategray;">
  73. <textarea id="posttext-@Model.Id"></textarea> <br/>
  74. <input type="button" id="postComment" onclick="sendComment('@Model.Id', '@Model.UserId')" value="Отправить"/>
  75. </div>
  76. </div>
  77. </tbody>
  78. </table>
  79.  
  80. <script>
  81. function openCommentForm(postId) {
  82. $('#postdiv-' + postId).show();
  83. }
  84.  
  85. function sendComment(postId, userId) {
  86. $.ajax({
  87. url: '@Url.Action("Comment", "Comments")',
  88. type: 'POST',
  89. data: { 'postId': postId, 'userId': userId, 'content': $('#posttext-' + postId).val() },
  90. success: function(data) {
  91. setTimeout(function() {
  92. alert('Ваш комментарий ' + '(' + $('#posttext-' + postId).val() + ')' + ' добавлен!!!',
  93. data)
  94. },
  95. 500);
  96. },
  97. error: function() {
  98. console.log('Ошибка во время отправки комментария', this);
  99. }
  100. });
  101. }
  102. </script>
  103.  
  104. <div id="postdiv-@Model.Id">
  105. <form>
  106. <input id="LikeId" type="button" value="Like" class="btn btn-default" onclick="sendLike('@Model.UserId', '@Model.Id')"
  107. style="color: aliceblue; background-color: darksalmon;"/>
  108. <input id="DisLikeId" type="button" value="Dislike" class="btn btn-default" onclick="sendDislike('@Model.UserId', '@Model.Id')"
  109. style="color: aliceblue; background-color: darksalmon;"/>
  110. <input id="subscribers-@Model.User" type="button" value="Subscribers" class="btn btn-default" onclick="SubScribers('@Model.UserId', '@Model.Id')"
  111. style="color: aliceblue; background-color: cadetblue;"/>
  112. <input id="unsubscribers-@Model.User" type="button" value="UnSubscribers" class="btn btn-default" onclick="UnSubScribers('@Model.UserId', '@Model.Id')"
  113. style="color: aliceblue; background-color: cadetblue;"/>
  114. </form>
  115.  
  116. </div>
  117.  
  118.  
  119. <script>
  120. function SubScribers(userId, postId) {
  121. $.ajax({
  122. url: '@Url.Action("SubscriptionMethod", "Publications")',
  123. type: 'POST',
  124. data: { 'postId': postId, 'userId': userId },
  125. success: function(data) {
  126. setTimeout(function() {
  127. alert('Вы подписались :)', data)},
  128. 500);
  129. },
  130. error: function() {
  131. console.log('Ошибка во время отправки комментария', this);
  132. }
  133. });
  134. }
  135. </script>
  136.  
  137.  
  138. <script>
  139. function sendLike(userId, postId) {
  140. $.ajax({
  141. url: '@Url.Action("LikeMethod", "Publications")',
  142. type: 'POST',
  143. data: { 'postId': postId, 'userId': userId },
  144. success: function(data) {
  145. setTimeout(function() {
  146. alert('Вы поставили Like!', data)
  147. },
  148. 500);
  149. },
  150. error: function() {
  151. console.log('Ошибка во время отправки комментария', this);
  152. }
  153. });
  154. }
  155. </script>
  156.  
  157. public class Publication
  158. {
  159. public int Id { get; set; }
  160.  
  161. [Display(Name = "Изображение")] public string ImageUrl { get; set; }
  162. [Display(Name = "Описание")] public string Description { get; set; }
  163. [Display(Name = "Нравиться!")] public int Like { get; set; }
  164. [Display(Name = "Подписка!")] public int Subscription { get; set; }
  165.  
  166. public string UserId { get; set; }
  167. public ApplicationUser User { get; set; }
  168.  
  169. [Display(Name = "Количество комментариев")]
  170. public int ComentCount { get; set; }
  171.  
  172. public List<Comment> CommentsList { get; set; }
  173.  
  174. }
  175.  
  176. public class Comment
  177. {
  178. [Key] public int CommentId { get; set; }
  179.  
  180. public string UserId { get; set; }
  181. public ApplicationUser User { get; set; }
  182. public int PostId { get; set; }
  183. public Publication Post { get; set; }
  184. public string Content { get; set; }
  185. public DateTime CommentDate { get; set; }
  186.  
  187. [Display(Name = "Изображение")] public string ImageUrl { get; set; }
  188. }
  189.  
  190. [HttpPost]
  191. [ValidateAntiForgeryToken]
  192. public async Task<IActionResult> Create([Bind("CommentId,PostId,CommentDate")] Comment comment)
  193. {
  194. var publ = _context.Publications.FirstOrDefault(c => c.Id == comment.PostId);
  195. if (publ != null) comment.ImageUrl = publ.ImageUrl;
  196.  
  197.  
  198. if (ModelState.IsValid)
  199. {
  200. _context.Add(comment);
  201. await _context.SaveChangesAsync();
  202. return RedirectToAction(nameof(Index));
  203. }
  204.  
  205. return View(comment);
  206. }
  207.  
  208. // GET: Comments/Details/5
  209. public async Task<IActionResult> Details(int id, PublicationVM model)
  210. {
  211. ViewBag.Comment = _context.Comments.Where(c => c.PostId == id);
  212.  
  213. if (id == null)
  214. {
  215. return NotFound();
  216. }
  217. var comment = await _context.Comments
  218. .Include(c => c.Post)
  219. .Include(c => c.User)
  220. .SingleOrDefaultAsync(m => m.CommentId == id);
  221. if (comment == null)
  222. {
  223. return NotFound();
  224. }
  225. return View(comment);
  226. }
  227.  
  228. <td>
  229. <img src="~/@Model.ImageUrl" width="700" height="450"/>
  230. </td>
  231.  
  232. public class FileUploadService
  233. {
  234. public FileUploadService()
  235. {
  236.  
  237. }
  238. public async void Upload(string path, string fileName, IFormFile file)
  239. {
  240. Directory.CreateDirectory(path);
  241. using (var stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
  242. {
  243. await file.CopyToAsync(stream);
  244. }
  245. }
  246. }
Add Comment
Please, Sign In to add comment