Skip to content

Commit

Permalink
Merge pull request #35 from LuongXuanNhat/12.ChatRealTime
Browse files Browse the repository at this point in the history
31 | Search post
  • Loading branch information
LuongXuanNhat authored Nov 21, 2023
2 parents f70fd9b + 734aff5 commit 8be707c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
8 changes: 1 addition & 7 deletions VNH.Application/DTOs/Catalog/Posts/PostFpkDto.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VNH.Application.DTOs.Catalog.Posts
namespace VNH.Application.DTOs.Catalog.Posts
{
public class PostFpkDto
{
Expand Down
2 changes: 2 additions & 0 deletions VNH.Application/Interfaces/Catalog/HashTags/IHashTag.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using VNH.Application.DTOs.Catalog.HashTags;
using VNH.Application.DTOs.Common.ResponseNotification;
using VNH.Domain;

namespace VNH.Application.Interfaces.Catalog.HashTags
Expand All @@ -7,5 +8,6 @@ public interface IHashTag
{
Task<List<TagDto>> GetAll();
Task AddTag(Tag tag);
Task<ApiResult<List<string>>> GetAllTag(int numberTag);
}
}
3 changes: 2 additions & 1 deletion VNH.Application/Interfaces/Catalog/Posts/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public interface IPostService
Task<List<ReportPostDto>> GetReport();
Task<ApiResult<bool>> GetLike(PostFpkDto postFpk);
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);
Task<ApiResult<List<PostResponseDto>>> GetMyPostSaved(string id);
Task<ApiResult<List<PostResponseDto>>> GetMyPost(string id);
}
}
25 changes: 25 additions & 0 deletions VNH.Infrastructure/Implement/Catalog/HashTags/TagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using System.Threading.Tasks;
using VNH.Application.DTOs.Catalog.HashTags;
using VNH.Application.DTOs.Common.ResponseNotification;
using VNH.Application.Interfaces.Catalog.HashTags;
using VNH.Domain;
using VNH.Infrastructure.Presenters.Migrations;
Expand Down Expand Up @@ -41,5 +42,29 @@ public async Task<List<TagDto>> GetAll()
}
return tagDtos;
}

public async Task<ApiResult<List<string>>> GetAllTag(int numberTag)
{
if (numberTag <= 0)
{
var tags = await _dataContext.Tags
.GroupBy(x => x.Name)
.Select(group => new { Name = group.Key, Count = group.Sum(t => 1) })
.OrderByDescending(tagName => tagName.Count)
.Select(group => group.Name)
.ToListAsync();
return new ApiSuccessResult<List<string>>(tags);
} else
{
var tags = await _dataContext.Tags
.GroupBy(x => x.Name)
.Select(group => new { Name = group.Key, Count = group.Sum(t => 1) })
.OrderByDescending(tagName => tagName.Count)
.Take(numberTag)
.Select(group => group.Name)
.ToListAsync();
return new ApiSuccessResult<List<string>>(tags);
}
}
}
}
46 changes: 33 additions & 13 deletions VNH.Infrastructure/Implement/Catalog/Posts/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,6 @@ public async Task<ApiResult<bool>> GetSave(PostFpkDto postFpk)
return new ApiSuccessResult<bool>(reuslt);
}

public async Task<ApiResult<List<string>>> GetTopTags(int numberTag)
{
var tags = await _dataContext.Tags
.GroupBy(x => x.Name)
.Select(group => new { Name = group.Key, Count = group.Sum(t => 1) })
.OrderByDescending(tagName => tagName.Count)
.Take(numberTag)
.Select(group => group.Name)
.ToListAsync();

return new ApiSuccessResult<List<string>>(tags);
}

public async Task<ApiResult<List<PostResponseDto>>> GetPostByTag(string tag)
{
var posts = await _dataContext.Posts
Expand Down Expand Up @@ -563,5 +550,38 @@ public async Task<ApiResult<string>> DeteleComment(string id)
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<string>();
}

public async Task<ApiResult<List<PostResponseDto>>> GetMyPostSaved(string id)
{
Guid userId = Guid.Parse(id);
var users = await _dataContext.User.ToListAsync();

var posts = await (
from postSave in _dataContext.PostSaves
join post in _dataContext.Posts on postSave.PostId equals post.Id
where postSave.UserId == userId
select post
).ToListAsync();

var result = new List<PostResponseDto>();
foreach (var item in posts)
{
var post = _mapper.Map<PostResponseDto>(item);
var userShort = users.First(x => x.Id == item.UserId);
if (userShort is not null)
{
post.UserShort.FullName = userShort.Fullname;
post.UserShort.Id = userShort.Id;
post.UserShort.Image = userShort.Image;
}
post.Image = item.Image;
result.Add(post);
}
return new ApiSuccessResult<List<PostResponseDto>>(result);
}
public Task<ApiResult<List<PostResponseDto>>> GetMyPost(string id)
{
throw new NotImplementedException();
}
}
}
11 changes: 8 additions & 3 deletions VNH.WebAPi/Controllers/HashTagController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public HashTagController(IHashTag hashTagService) {
[HttpGet]
public async Task<IActionResult> GetAll()
{
return Ok(await _hasgTagService.GetAll());
var result = await _hasgTagService.GetAllTag(0);
return result is null ? BadRequest(result) : Ok(result);
}
[HttpGet("TopTag")]
public async Task<IActionResult> GetTopTags(int numberTag)
{
var result = await _hasgTagService.GetAllTag(numberTag);
return result is null ? BadRequest(result) : Ok(result);
}


}
}
23 changes: 17 additions & 6 deletions VNH.WebAPi/Controllers/PostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ public async Task<IActionResult> GetSavePost([FromQuery] PostFpkDto postFpk)
var result = await _postService.GetSave(postFpk);
return result is null ? BadRequest(result) : Ok(result);
}
[HttpGet()]
[Authorize]
public async Task<IActionResult> GetMyPostSaved()
{
var id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
var result = await _postService.GetMyPostSaved(id);
return result is null ? BadRequest(result) : Ok(result);
}
[HttpGet("MyPost")]
[Authorize]
public async Task<IActionResult> GetMyPost()
{
var id = User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "";
var result = await _postService.GetMyPost(id);
return result is null ? BadRequest(result) : Ok(result);
}

[HttpPost("Save")]
[Authorize]
public async Task<IActionResult> Save([FromForm] PostFpkDto postFpk)
Expand All @@ -110,12 +127,6 @@ public async Task<IActionResult> GetReport()
var result = await _postService.GetReport();
return result is null ? BadRequest(result) : Ok(result);
}
[HttpGet("TopTag")]
public async Task<IActionResult> GetTopTags(int numberTag)
{
var result = await _postService.GetTopTags(numberTag);
return result is null ? BadRequest(result) : Ok(result);
}
[HttpGet("FindByTag")]
public async Task<IActionResult> GetPostByTag(string tag)
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 8be707c

Please sign in to comment.