Skip to content

Commit

Permalink
#31 | Chat Realtime success
Browse files Browse the repository at this point in the history
  • Loading branch information
LuongXuanNhat committed Nov 20, 2023
1 parent 0770741 commit b27b491
Show file tree
Hide file tree
Showing 31 changed files with 797 additions and 624 deletions.
2 changes: 1 addition & 1 deletion VNH.Application/Common/Contants/SystemConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public static class SystemConstants
public const string BaseAddress = "BaseAddress";
// public const string UrlWeb = "https://vuanhpham25-001-site1.gtempurl.com/";
public const string UrlWeb = "https://localhost:7138/";
// public const string ConnectString = "Data Source=SQL5111.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;User Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai";
// public const string ConnectString = "Data Source=SQL5111.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;UserShort Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai";
public const string ConnectString = "Data Source=.;Initial Catalog=VietNamHistory_2;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
}
}
20 changes: 18 additions & 2 deletions VNH.Application/DTOs/Catalog/Posts/CommentPostDto.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
namespace VNH.Application.DTOs.Catalog.Posts
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations.Schema;
using VNH.Application.DTOs.Catalog.Users;

namespace VNH.Application.DTOs.Catalog.Posts
{
public class CommentPostDto
{
public Guid Id { get; set; } = Guid.NewGuid();
public Guid? UserId { get; set; }
public string PostId { get; set; } = String.Empty;
public Guid UserId { get; set; }
public UserShortDto? UserShort { get; set; }
public string Content { get; set; } = String.Empty;
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public List<SubCommentDto>? SubComment { get; set; }
}
public class SubCommentDto
{
public Guid Id { get; set; }
public string? Content { get; set; }
[Column(TypeName = "datetime")]
public DateTime? CreatedAt { get; set; }
[Column(TypeName = "datetime")]
public DateTime? UpdatedAt { get; set; }
public UserShortDto? UserShort { get; set; }
}
}
4 changes: 2 additions & 2 deletions VNH.Application/DTOs/Catalog/Posts/CreatePostDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class CreatePostDto
public string? Id { get; set; } = Guid.NewGuid().ToString();
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public IFormFile? Image { get; set; }
public IFormFile Image { get; set; }
public Guid TopicId { get; set; }
public List<string>? Tag { get; set; }
public List<string>? Tag { get; set; } = new List<string>();
}
}

2 changes: 1 addition & 1 deletion VNH.Application/DTOs/Catalog/Users/UserShortDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace VNH.Application.DTOs.Catalog.Users
public class UserShortDto
{
public Guid Id { get; set; }
public string FullName { get; set; }
public string FullName { get; set; } = string.Empty;
public string? Image { get; set; } = string.Empty;
}
}
4 changes: 4 additions & 0 deletions VNH.Application/Interfaces/Catalog/Posts/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public interface IPostService
Task<ApiResult<bool>> GetSave(PostFpkDto postFpk);
Task<ApiResult<List<string>>> GetTopTags(int numberTag);
Task<ApiResult<List<PostResponseDto>>> GetPostByTag(string tag);
Task<ApiResult<List<CommentPostDto>>> GetComment(string postId);
Task<ApiResult<List<CommentPostDto>>> CreateComment(CommentPostDto comment);
Task<ApiResult<List<CommentPostDto>>> UpdateComment(CommentPostDto comment);
Task<ApiResult<string>> DeteleComment(string id);
}
}
15 changes: 15 additions & 0 deletions VNH.Application/Mappers/CommentMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AutoMapper;
using VNH.Application.DTOs.Catalog.Posts;
using VNH.Domain;

namespace VNH.Application.Mappers
{
public class CommentMapper : Profile
{
public CommentMapper() {
CreateMap<PostComment, CommentPostDto>().ReverseMap();
CreateMap<PostSubComment, SubCommentDto>().ReverseMap();
}

}
}
3 changes: 0 additions & 3 deletions VNH.Domain/Entities/PostReportDetail.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace VNH.Domain
{
Expand Down
2 changes: 2 additions & 0 deletions VNH.Infrastructure/DependencyInjectionInfrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
services.AddScoped<IFileService, FileService>();
services.AddSignalR();




return services;
}
Expand Down
113 changes: 107 additions & 6 deletions VNH.Infrastructure/Implement/Catalog/Posts/PostService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Tokens;
using System.Globalization;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using VNH.Application.DTOs.Catalog.HashTags;
Expand All @@ -15,6 +16,7 @@
using VNH.Application.Interfaces.Posts;
using VNH.Domain;
using VNH.Domain.Entities;
using VNH.Infrastructure.Presenters;
using VNH.Infrastructure.Presenters.Migrations;

namespace VNH.Infrastructure.Implement.Catalog.Posts
Expand All @@ -25,16 +27,19 @@ public class PostService : IPostService
private readonly VietNamHistoryContext _dataContext;
private readonly IImageService _image;
private readonly IStorageService _storageService;
private readonly IHubContext<ChatSignalR> _commentHubContext;
private readonly IMapper _mapper;

public PostService(UserManager<User> userManager, IMapper mapper, IImageService image,
VietNamHistoryContext vietNamHistoryContext, IStorageService storageService)
VietNamHistoryContext vietNamHistoryContext, IStorageService storageService,
IHubContext<ChatSignalR> chatSignalR)
{
_userManager = userManager;
_mapper = mapper;
_image = image;
_dataContext = vietNamHistoryContext;
_storageService = storageService;
_commentHubContext = chatSignalR;
}
public async Task<ApiResult<PostResponseDto>> Create(CreatePostDto requestDto, string name)
{
Expand Down Expand Up @@ -253,7 +258,6 @@ public async Task<ApiResult<PostResponseDto>> Detail(string Id)
public async Task<ApiResult<List<PostResponseDto>>> GetAll()
{
var posts = await _dataContext.Posts.ToListAsync();
var topics = await _dataContext.Topics.ToListAsync();
var users = await _dataContext.User.ToListAsync();

var result = new List<PostResponseDto>();
Expand Down Expand Up @@ -318,7 +322,6 @@ public async Task<ApiResult<int>> AddOrUnLikePost(PostFpkDto postFpk)
return new ApiErrorResult<int>("Không tìm thấy bài viết");
}
var check = _dataContext.PostLikes.Where(x => x.PostId == post.Id && x.UserId == Guid.Parse(postFpk.UserId)).FirstOrDefault();
var mess = "";
var likeNumber = await _dataContext.PostLikes.Where(x => x.PostId == post.Id).CountAsync();
if (check is null)
{
Expand Down Expand Up @@ -349,7 +352,6 @@ public async Task<ApiResult<int>> AddOrRemoveSavePost(PostFpkDto postFpk)
return new ApiErrorResult<int>("Không tìm thấy bài viết");
}
var check = _dataContext.PostSaves.Where(x => x.PostId == post.Id && x.UserId == Guid.Parse(postFpk.UserId)).FirstOrDefault();
var mess = "";
var saveNumber = await _dataContext.PostSaves.Where(x => x.PostId == post.Id).CountAsync();
if (check is null)
{
Expand Down Expand Up @@ -425,7 +427,7 @@ public async Task<ApiResult<List<string>>> GetTopTags(int numberTag)
.GroupBy(x => x.Name)
.Select(group => new { Name = group.Key, Count = group.Sum(t => 1) })
.OrderByDescending(tagName => tagName.Count)
.Take(10)
.Take(numberTag)
.Select(group => group.Name)
.ToListAsync();

Expand Down Expand Up @@ -462,5 +464,104 @@ public async Task<ApiResult<List<PostResponseDto>>> GetPostByTag(string tag)
}
return new ApiSuccessResult<List<PostResponseDto>>(result);
}

public async Task<ApiResult<List<CommentPostDto>>> GetComment(string postId)
{
var post = await _dataContext.Posts.FirstAsync(x => x.SubId.Equals(postId));
if (post == null)
{
return new ApiSuccessResult<List<CommentPostDto>>();
}
var users = await _dataContext.User.ToListAsync();
var postComment = await _dataContext.PostComments.Where(x=>x.PostId.Equals(post.Id)).ToListAsync();
var postSubComment = await _dataContext.PostSubComments.ToListAsync();

var result = new List<CommentPostDto>();
foreach (var item in postComment)
{
var subComment = postSubComment.Where(x=>x.PreCommentId.Equals(item.Id)).ToList();
var comment = _mapper.Map<CommentPostDto>(item);
comment.UserShort = GetUserShort(users, item.UserId);
if(subComment.Count > 0)
{
comment.SubComment = GetSubComment(subComment, users);
}
result.Add(comment);
}
return new ApiSuccessResult<List<CommentPostDto>>(result);
}

private List<SubCommentDto>? GetSubComment(List<PostSubComment> subComment, List<User> users)
{
List<SubCommentDto> result = new();
foreach (var item in subComment)
{
var comment = _mapper.Map<SubCommentDto>(item);
comment.UserShort = GetUserShort(users, item.UserId);
result.Add(comment);
}
return result;
}

private UserShortDto? GetUserShort(List<User> users, Guid? IdUser)
{
return users
.Where(x => x.Id.Equals(IdUser))
.Select(x => new UserShortDto
{
Id = x.Id,
FullName = x.Fullname,
Image = x.Image
})
.FirstOrDefault();
}

public async Task<ApiResult<List<CommentPostDto>>> CreateComment(CommentPostDto comment)
{
var post = await _dataContext.Posts.FirstOrDefaultAsync(x=>x.SubId.Equals(comment.PostId));
if (post == null)
{
return new ApiErrorResult<List<CommentPostDto>>("Không tìm thấy bài đọc bạn bình luận, có thể nó đã bị xóa");
}
PostComment postComment = _mapper.Map<PostComment>(comment);
postComment.PostId = post.Id;
_dataContext.PostComments.Add(postComment);
await _dataContext.SaveChangesAsync();
var comments = await GetComment(comment.PostId);
await _commentHubContext.Clients.All.SendAsync("ReceiveComment", comments);

return comments;
}

public async Task<ApiResult<List<CommentPostDto>>> UpdateComment(CommentPostDto comment)
{
var postComment = await _dataContext.PostComments.FirstOrDefaultAsync(x => x.Id == comment.Id);
if (postComment == null)
{
return new ApiErrorResult<List<CommentPostDto>> ("Không tìm thấy bài đọc bạn bình luận!");
}
postComment.Content = comment.Content;
postComment.UpdatedAt = DateTime.Now;

_dataContext.PostComments.Update(postComment);
await _dataContext.SaveChangesAsync();

var comments = await GetComment(comment.PostId);
await _commentHubContext.Clients.All.SendAsync("ReceiveComment", comments);

return comments;
}

public async Task<ApiResult<string>> DeteleComment(string id)
{
var comment = await _dataContext.PostComments.FirstOrDefaultAsync(x=>x.Id.Equals(Guid.Parse(id)));
if (comment == null)
{
return new ApiErrorResult<string>("Không tìm thấy bình luận");
}
_dataContext.PostComments.Remove(comment);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>();
}
}
}
Loading

0 comments on commit b27b491

Please sign in to comment.