Skip to content

Commit

Permalink
#29.4 -> 30 | Update code docs, update tags code
Browse files Browse the repository at this point in the history
  • Loading branch information
LuongXuanNhat committed Nov 19, 2023
1 parent 81a0cd3 commit 0770741
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 51 deletions.
2 changes: 1 addition & 1 deletion VNH.Application/DTOs/Catalog/Posts/ReportPostDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class ReportPostDto
public string PostId { get; set; }
public Guid UserId { get; set; }
public Guid ReportId { get; set; }
public string Description { get; set; } = string.Empty;
public string? Description { get; set; } = string.Empty;
public DateTime ReportDate { get; set; } = DateTime.Now;
public bool Checked { get; set; } = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public interface IDocumentService

Task<ApiResult<bool>> GetSave(DocumentFpkDto docsFpk);

Task<ApiResult<bool>> AddOrRemoveSaveDocs(DocumentFpkDto docsFpk);
Task<ApiResult<int>> AddOrRemoveSaveDocs(DocumentFpkDto docsFpk);

}
}
9 changes: 6 additions & 3 deletions VNH.Application/Interfaces/Catalog/Posts/IPostService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using VNH.Application.DTOs.Catalog.Posts;
using VNH.Application.DTOs.Catalog.HashTags;
using VNH.Application.DTOs.Catalog.Posts;
using VNH.Application.DTOs.Common.ResponseNotification;

namespace VNH.Application.Interfaces.Posts
Expand All @@ -13,11 +14,13 @@ public interface IPostService
Task<ApiResult<string>> DeleteAdmin(string id);


Task<ApiResult<bool>> AddOrUnLikePost(PostFpkDto postFpk);
Task<ApiResult<bool>> AddOrRemoveSavePost(PostFpkDto postFpk);
Task<ApiResult<int>> AddOrUnLikePost(PostFpkDto postFpk);
Task<ApiResult<int>> AddOrRemoveSavePost(PostFpkDto postFpk);
Task<ApiResult<string>> ReportPost(ReportPostDto reportPostDto);
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);
}
}
3 changes: 1 addition & 2 deletions VNH.Application/Mappers/DocumentMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace VNH.Application.Mappers
{
public class DocumentMapper : Profile
{
public DocumentMapper
()
public DocumentMapper()
{
CreateMap<CreateDocumentDto, Document>()
.ForMember(dest => dest.FileName, opt => opt.Ignore());
Expand Down
3 changes: 0 additions & 3 deletions VNH.Domain/Entities/Post.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;
using VNH.Domain.Entities;

namespace VNH.Domain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public async Task<string> GetToken(User user)
var token = new JwtSecurityToken(_config["Tokens:Issuer"],
_config["Tokens:Issuer"],
claims,
expires: DateTime.Now.AddHours(3),
expires: DateTime.Now.AddDays(15),
signingCredentials: creds);

return new JwtSecurityTokenHandler().WriteToken(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using VNH.Infrastructure.Presenters.Migrations;
using Microsoft.EntityFrameworkCore;
using VNH.Application.DTOs.Catalog.Posts;
using Microsoft.Extensions.Hosting;

namespace VNH.Infrastructure.Implement.Catalog.Documents
{
Expand Down Expand Up @@ -178,9 +179,6 @@ public async Task<ApiResult<DocumentReponseDto>> Detail(string Id)
.UserId.ToString());
var documentResponse = _mapper.Map<DocumentReponseDto>(document);
documentResponse.FileName = document.FileName;



documentResponse
.UserShort = new()
{
Expand All @@ -189,7 +187,6 @@ public async Task<ApiResult<DocumentReponseDto>> Detail(string Id)
Image = user.Image
};

// _dataContext.Documents.Update(document);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<DocumentReponseDto>(documentResponse);
}
Expand Down Expand Up @@ -222,15 +219,16 @@ public async Task<ApiResult<bool>> GetSave(DocumentFpkDto docsFpk)
return new ApiSuccessResult<bool>(reuslt);
}

public async Task<ApiResult<bool>> AddOrRemoveSaveDocs(DocumentFpkDto docsFpk)
public async Task<ApiResult<int>> AddOrRemoveSaveDocs(DocumentFpkDto docsFpk)
{
var docs = await _dataContext.Documents.FirstOrDefaultAsync(x => x.SubId.Equals(docsFpk.DocumentId));
if (docs is null)
{
return new ApiErrorResult<bool>("Không tìm thấy bài viết");
return new ApiErrorResult<int>("Không tìm thấy bài viết");
}
var check = _dataContext.DocumentSaves.Where(x => x.DocumentId == docs.Id && x.UserId == Guid.Parse(docsFpk.UserId)).FirstOrDefault();
var mess = "";
var saveNumber = await _dataContext.DocumentSaves.Where(x => x.DocumentId == docs.Id).CountAsync();
if (check is null)
{
var save = new DocumentSave()
Expand All @@ -241,13 +239,13 @@ public async Task<ApiResult<bool>> AddOrRemoveSaveDocs(DocumentFpkDto docsFpk)
};
_dataContext.DocumentSaves.Add(save);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(true);
return new ApiSuccessResult<int>(saveNumber+1);
}
else
{
_dataContext.DocumentSaves.Remove(check);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(false);
return new ApiSuccessResult<int>(saveNumber-1);
}


Expand Down
87 changes: 60 additions & 27 deletions VNH.Infrastructure/Implement/Catalog/Posts/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,9 @@ public async Task<ApiResult<PostResponseDto>> Detail(string Id)
post.ViewNumber += 1;
var topic = await _dataContext.Topics.FirstAsync(x => x.Id == post.TopicId);
postResponse.TopicName = topic.Title;
postResponse.SaveNumber = await _dataContext.PostSaves.Where(x=>x.PostId.Equals(Id)).CountAsync();
postResponse.CommentNumber = await _dataContext.PostComments.Where(x => x.PostId.Equals(Id)).CountAsync();
postResponse.LikeNumber = await _dataContext.PostLikes.Where(x=>x.PostId.Equals(x.PostId)).CountAsync();
postResponse.SaveNumber = await _dataContext.PostSaves.Where(x=>x.PostId.Equals(post.Id)).CountAsync();
postResponse.CommentNumber = await _dataContext.PostComments.Where(x => x.PostId.Equals(post.Id)).CountAsync();
postResponse.LikeNumber = await _dataContext.PostLikes.Where(x=>x.PostId.Equals(post.Id)).CountAsync();

_dataContext.Posts.Update(post);
await _dataContext.SaveChangesAsync();
Expand All @@ -267,19 +267,6 @@ public async Task<ApiResult<List<PostResponseDto>>> GetAll()
post.UserShort.Id = userShort.Id;
post.UserShort.Image = userShort.Image;
}
var tags = await _dataContext.PostTags
.Where(x => x.PostId == item.Id)
.Join(
_dataContext.Tags,
postTag => postTag.TagId,
tag => tag.Id,
(postTag, tag) => tag)
.ToListAsync();
foreach (var tag in tags)
{
post.Tags.Add(_mapper.Map<TagDto>(tag));
}
post.TopicName = topics.Where(x => x.Id == item.TopicId).Select(x=>x.Title).First();
post.Image = item.Image;
result.Add(post);
}
Expand Down Expand Up @@ -323,15 +310,16 @@ public async Task<ApiResult<string>> DeleteAdmin(string id)
return new ApiSuccessResult<string>("Đã xóa bài viết");
}

public async Task<ApiResult<bool>> AddOrUnLikePost(PostFpkDto postFpk)
public async Task<ApiResult<int>> AddOrUnLikePost(PostFpkDto postFpk)
{
var post = await _dataContext.Posts.FirstOrDefaultAsync(x => x.SubId.Equals(postFpk.PostId));
if (post is null)
{
return new ApiErrorResult<bool>("Không tìm thấy bài viết");
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)
{
var like = new PostLike()
Expand All @@ -342,26 +330,27 @@ public async Task<ApiResult<bool>> AddOrUnLikePost(PostFpkDto postFpk)
};
_dataContext.PostLikes.Add(like);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(true);
return new ApiSuccessResult<int>(likeNumber+1);
} else
{
_dataContext.PostLikes.Remove(check);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(false);
return new ApiSuccessResult<int>(likeNumber-1);
}


}

public async Task<ApiResult<bool>> AddOrRemoveSavePost(PostFpkDto postFpk)
public async Task<ApiResult<int>> AddOrRemoveSavePost(PostFpkDto postFpk)
{
var post = await _dataContext.Posts.FirstOrDefaultAsync(x => x.SubId.Equals(postFpk.PostId));
if (post is null)
{
return new ApiErrorResult<bool>("Không tìm thấy bài viết");
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)
{
var save = new PostSave()
Expand All @@ -372,28 +361,28 @@ public async Task<ApiResult<bool>> AddOrRemoveSavePost(PostFpkDto postFpk)
};
_dataContext.PostSaves.Add(save);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(true);
return new ApiSuccessResult<int>(saveNumber+1);
}
else
{
_dataContext.PostSaves.Remove(check);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>(false);
return new ApiSuccessResult<int>(saveNumber-1);
}


}

public async Task<ApiResult<string>> ReportPost(ReportPostDto reportPostDto)
{
var postId = _dataContext.Posts.FirstOrDefault(x => x.SubId.Equals(reportPostDto.PostId));
if (postId == null)
var post = _dataContext.Posts.FirstOrDefault(x => x.SubId.Equals(reportPostDto.PostId));
if (post == null)
{
return new ApiErrorResult<string>("Bài viết không tồn tại");
}
var reportPost = _mapper.Map<PostReportDetail>(reportPostDto);
reportPost.Id = Guid.NewGuid();

reportPost.PostId = post.Id;
_dataContext.PostReportDetails.Add(reportPost);
await _dataContext.SaveChangesAsync();

Expand Down Expand Up @@ -429,5 +418,49 @@ public async Task<ApiResult<bool>> GetSave(PostFpkDto postFpk)
var reuslt = check != null;
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(10)
.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
.Where(post => _dataContext.PostTags
.Any(postTag => _dataContext.Tags
.Any(tagEntity => tagEntity.Name.ToLower().Contains(tag.ToLower()) && tagEntity.Id == postTag.TagId)
&& postTag.PostId == post.Id))
.ToListAsync();
if (posts.IsNullOrEmpty())
{
return new ApiSuccessResult<List<PostResponseDto>>(new List<PostResponseDto>());
}
var users = await _dataContext.User.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);
}
}
}
15 changes: 13 additions & 2 deletions VNH.WebAPi/Controllers/PostController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ 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)
{
var result = await _postService.GetPostByTag(tag);
return result is null ? BadRequest(result) : Ok(result);
}

}
}
6 changes: 3 additions & 3 deletions VNH.WebAPi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public static void Main(string[] args)
var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
}
//}
app.UseForwardedHeaders();
app.UseHttpLogging();

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 0770741

Please sign in to comment.