Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ActionResult LikeMethod(string userId, int postId)
- {
- var userLike = _context.Publications.FirstOrDefault(u => u.UserId == userId);
- if (ModelState.IsValid)
- {
- if (userLike != null)
- {
- userLike.Like = userLike.Like + 1;
- userLike.UserId = userId;
- userLike.Id = postId;
- _context.Update(userLike);
- _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- }
- return View();
- }
- public ActionResult SubscriptionMethod(string userId, int postId)
- {
- var userLike = _context.Publications.FirstOrDefault(u => u.UserId == userId);
- if (ModelState.IsValid)
- {
- if (userLike != null)
- {
- userLike.Subscription = userLike.Subscription + 1;
- userLike.UserId = userId;
- userLike.Id = postId;
- _context.Update(userLike);
- _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- }
- return View();
- }
- [HttpPost]
- public async Task<IActionResult> Comment(int postId, string userId, string content, Comment comment)
- {
- var publ = _context.Publications.FirstOrDefault(c => c.Id == comment.PostId);
- if (publ != null) comment.ImageUrl = publ.ImageUrl;
- if (ModelState.IsValid)
- {
- var user = await _userManager.GetUserAsync(User);
- var comm = new Comment
- {
- UserId = userId,
- PostId = postId,
- Content = content,
- CommentDate = DateTime.Now
- };
- comm.UserId = user.Id;
- _context.Add(comm);
- await _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- return View();
- }
- <table>
- <tbody>
- <div class="row">
- <div class="col-lg-12">
- <a onclick="openCommentForm(@Model.Id)" style="cursor: pointer; color: darkslategray;">Оставить комментарий</a>
- </div>
- <div class="col-md-12" id="postdiv-@Model.Id" style="display: none; color: darkslategray;">
- <textarea id="posttext-@Model.Id"></textarea> <br/>
- <input type="button" id="postComment" onclick="sendComment('@Model.Id', '@Model.UserId')" value="Отправить"/>
- </div>
- </div>
- </tbody>
- </table>
- <script>
- function openCommentForm(postId) {
- $('#postdiv-' + postId).show();
- }
- function sendComment(postId, userId) {
- $.ajax({
- url: '@Url.Action("Comment", "Comments")',
- type: 'POST',
- data: { 'postId': postId, 'userId': userId, 'content': $('#posttext-' + postId).val() },
- success: function(data) {
- setTimeout(function() {
- alert('Ваш комментарий ' + '(' + $('#posttext-' + postId).val() + ')' + ' добавлен!!!',
- data)
- },
- 500);
- },
- error: function() {
- console.log('Ошибка во время отправки комментария', this);
- }
- });
- }
- </script>
- <div id="postdiv-@Model.Id">
- <form>
- <input id="LikeId" type="button" value="Like" class="btn btn-default" onclick="sendLike('@Model.UserId', '@Model.Id')"
- style="color: aliceblue; background-color: darksalmon;"/>
- <input id="DisLikeId" type="button" value="Dislike" class="btn btn-default" onclick="sendDislike('@Model.UserId', '@Model.Id')"
- style="color: aliceblue; background-color: darksalmon;"/>
- <input id="subscribers-@Model.User" type="button" value="Subscribers" class="btn btn-default" onclick="SubScribers('@Model.UserId', '@Model.Id')"
- style="color: aliceblue; background-color: cadetblue;"/>
- <input id="unsubscribers-@Model.User" type="button" value="UnSubscribers" class="btn btn-default" onclick="UnSubScribers('@Model.UserId', '@Model.Id')"
- style="color: aliceblue; background-color: cadetblue;"/>
- </form>
- </div>
- <script>
- function SubScribers(userId, postId) {
- $.ajax({
- url: '@Url.Action("SubscriptionMethod", "Publications")',
- type: 'POST',
- data: { 'postId': postId, 'userId': userId },
- success: function(data) {
- setTimeout(function() {
- alert('Вы подписались :)', data)},
- 500);
- },
- error: function() {
- console.log('Ошибка во время отправки комментария', this);
- }
- });
- }
- </script>
- <script>
- function sendLike(userId, postId) {
- $.ajax({
- url: '@Url.Action("LikeMethod", "Publications")',
- type: 'POST',
- data: { 'postId': postId, 'userId': userId },
- success: function(data) {
- setTimeout(function() {
- alert('Вы поставили Like!', data)
- },
- 500);
- },
- error: function() {
- console.log('Ошибка во время отправки комментария', this);
- }
- });
- }
- </script>
- public class Publication
- {
- public int Id { get; set; }
- [Display(Name = "Изображение")] public string ImageUrl { get; set; }
- [Display(Name = "Описание")] public string Description { get; set; }
- [Display(Name = "Нравиться!")] public int Like { get; set; }
- [Display(Name = "Подписка!")] public int Subscription { get; set; }
- public string UserId { get; set; }
- public ApplicationUser User { get; set; }
- [Display(Name = "Количество комментариев")]
- public int ComentCount { get; set; }
- public List<Comment> CommentsList { get; set; }
- }
- public class Comment
- {
- [Key] public int CommentId { get; set; }
- public string UserId { get; set; }
- public ApplicationUser User { get; set; }
- public int PostId { get; set; }
- public Publication Post { get; set; }
- public string Content { get; set; }
- public DateTime CommentDate { get; set; }
- [Display(Name = "Изображение")] public string ImageUrl { get; set; }
- }
- [HttpPost]
- [ValidateAntiForgeryToken]
- public async Task<IActionResult> Create([Bind("CommentId,PostId,CommentDate")] Comment comment)
- {
- var publ = _context.Publications.FirstOrDefault(c => c.Id == comment.PostId);
- if (publ != null) comment.ImageUrl = publ.ImageUrl;
- if (ModelState.IsValid)
- {
- _context.Add(comment);
- await _context.SaveChangesAsync();
- return RedirectToAction(nameof(Index));
- }
- return View(comment);
- }
- // GET: Comments/Details/5
- public async Task<IActionResult> Details(int id, PublicationVM model)
- {
- ViewBag.Comment = _context.Comments.Where(c => c.PostId == id);
- if (id == null)
- {
- return NotFound();
- }
- var comment = await _context.Comments
- .Include(c => c.Post)
- .Include(c => c.User)
- .SingleOrDefaultAsync(m => m.CommentId == id);
- if (comment == null)
- {
- return NotFound();
- }
- return View(comment);
- }
- <td>
- <img src="~/@Model.ImageUrl" width="700" height="450"/>
- </td>
- public class FileUploadService
- {
- public FileUploadService()
- {
- }
- public async void Upload(string path, string fileName, IFormFile file)
- {
- Directory.CreateDirectory(path);
- using (var stream = new FileStream(Path.Combine(path, fileName), FileMode.Create))
- {
- await file.CopyToAsync(stream);
- }
- }
- }
Add Comment
Please, Sign In to add comment