Skip to content

Commit

Permalink
fix: 내가 좋아요를 눌렀는지 여부를 가져올 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ProdMoon committed Aug 4, 2024
1 parent c3429c1 commit 830ab24
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Controllers/Api/DliibDir/DliibController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class DliibController(DliibService dliibService, UserService userService)
[HttpGet]
public async Task<ActionResult<IEnumerable<DliibDto>>> GetDliibs()
{
return Ok(await dliibService.GetAllDliibDtos());
return Ok(await dliibService.GetAllDliibDtos(User.Identity?.Name));
}

[HttpGet("my")]
Expand All @@ -30,7 +30,7 @@ public async Task<ActionResult<IEnumerable<DliibDto>>> GetMyDliibs()
[HttpGet("{id}")]
public async Task<ActionResult<DliibDto>> GetDliib(int id)
{
return Ok(await dliibService.GetDliibDto(id));
return Ok(await dliibService.GetDliibDto(id, User.Identity?.Name));
}

[HttpPut]
Expand Down
2 changes: 2 additions & 0 deletions Dtos/DliibDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class DliibDto
public string Content { get; set; } = null!;
public int Likes { get; set; }
public int Dislikes { get; set; }
public bool IsLiked { get; set; }
public bool IsDisliked { get; set; }
public DateTime CreatedAt { get; set; } = DateTime.UtcNow.AddHours(9);
public string? AuthorNickName { get; set; }
}
20 changes: 10 additions & 10 deletions Repositories/DliibLikeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace DliibApi.Repositories;

public class DliibLikeRepository(AppDbContext db)
{
public async Task<int> Like(int dliibId, string userName)
public async Task<int> Like(int dliibId, string userId)
{
var dliib = await db.Dliibs.FirstOrDefaultAsync(x => x.Id == dliibId);
var user = await db.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == userName);
var dliib = await db.Dliibs.FindAsync(dliibId);
var user = await db.Users.FindAsync(userId);
if (dliib == null || user == null)
{
return 0;
Expand All @@ -32,10 +32,10 @@ public async Task<int> CancelLike(int id)
return await db.SaveChangesAsync();
}

public async Task<int> Dislike(int dliibId, string userName)
public async Task<int> Dislike(int dliibId, string userId)
{
var dliib = await db.Dliibs.FirstOrDefaultAsync(x => x.Id == dliibId);
var user = await db.Users.FirstOrDefaultAsync(x => x.NormalizedUserName == userName);
var dliib = await db.Dliibs.FindAsync(dliibId);
var user = await db.Users.FindAsync(userId);
if (dliib == null || user == null)
{
return 0;
Expand All @@ -59,18 +59,18 @@ public async Task<int> CancelDislike(int id)
return await db.SaveChangesAsync();
}

public async Task<int?> GetLikeId(int dliibId, string userName)
public async Task<int?> GetLikeId(int dliibId, string userId)
{
return await db.DliibLikes
.Where(x => x.Dliib.Id == dliibId && x.User.NormalizedUserName == userName)
.Where(x => x.Dliib.Id == dliibId && x.User.Id == userId)
.Select(x => x.Id)
.FirstOrDefaultAsync();
}

public async Task<int?> GetDislikeId(int dliibId, string userName)
public async Task<int?> GetDislikeId(int dliibId, string userId)
{
return await db.DliibDislikes
.Where(x => x.Dliib.Id == dliibId && x.User.NormalizedUserName == userName)
.Where(x => x.Dliib.Id == dliibId && x.User.Id == userId)
.Select(x => x.Id)
.FirstOrDefaultAsync();
}
Expand Down
23 changes: 4 additions & 19 deletions Repositories/DliibRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,23 @@ namespace DliibApi.Repositories;

public class DliibRepository(AppDbContext db, IMapper mapper)
{
public async Task<IEnumerable<DliibDto>> GetAllDliibDtos()
public async Task<IEnumerable<Dliib>> GetAllDliibs()
{
var dliibs = await db.Dliibs
return await db.Dliibs
.Include(x => x.Author)
.Include(x => x.Likes)
.Include(x => x.Dislikes)
.OrderByDescending(x => x.Id)
.ToListAsync();

return mapper.Map<List<DliibDto>>(dliibs);
}

public async Task<IEnumerable<Dliib>> GetAllDliibs()
public async Task<Dliib?> GetDliib(int id)
{
return await db.Dliibs
.Include(x => x.Author)
.Include(x => x.Likes)
.Include(x => x.Dislikes)
.OrderByDescending(x => x.Id)
.ToListAsync();
}

public async Task<DliibDto> GetDliibDto(int id)
{
var dliib = await db.Dliibs.FindAsync(id);

return mapper.Map<DliibDto>(dliib);
}

public async Task<Dliib?> GetDliib(int id)
{
return await db.Dliibs.FindAsync(id);
.FirstOrDefaultAsync(x => x.Id == id);
}

public async Task<IEnumerable<DliibDto>> GetUserDliibDtos(string userId)
Expand Down
8 changes: 4 additions & 4 deletions Services/DliibLikeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public async Task ToggleLike(int dliibId, string userName)
{
var userId = (await userRepository.GetUserByName(userName))?.Id;
var likeId = await dliibLikeRepository.GetLikeId(dliibId, userId);
if (likeId != null)
if (likeId > 0)
{
await dliibLikeRepository.CancelLike(likeId.Value);
return;
}

var dislikeId = await dliibLikeRepository.GetDislikeId(dliibId, userId);
if (dislikeId != null)
if (dislikeId > 0)
{
await dliibLikeRepository.CancelDislike(dislikeId.Value);
}
Expand All @@ -29,14 +29,14 @@ public async Task ToggleDislike(int dliibId, string userName)
{
var userId = (await userRepository.GetUserByName(userName))?.Id;
var dislikeId = await dliibLikeRepository.GetDislikeId(dliibId, userId);
if (dislikeId != null)
if (dislikeId > 0)
{
await dliibLikeRepository.CancelDislike(dislikeId.Value);
return;
}

var likeId = await dliibLikeRepository.GetLikeId(dliibId, userId);
if (likeId != null)
if (likeId > 0)
{
await dliibLikeRepository.CancelLike(likeId.Value);
}
Expand Down
39 changes: 35 additions & 4 deletions Services/DliibService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,23 @@ namespace DliibApi.Services;

public class DliibService(DliibRepository dliibRepository, UserRepository userRepository, IMapper mapper)
{
public async Task<IEnumerable<DliibDto>> GetAllDliibDtos()
public async Task<IEnumerable<DliibDto>> GetAllDliibDtos(string? userName)
{
return await dliibRepository.GetAllDliibDtos();
var dliibs = await dliibRepository.GetAllDliibs();
var dliibDtos = mapper.Map<List<DliibDto>>(dliibs);
if (userName == null)
{
return dliibDtos;
}

var user = await userRepository.GetUserByName(userName);
foreach (var dliibDto in dliibDtos)
{
dliibDto.IsLiked = dliibs.Where(x => x.Id == dliibDto.Id).Any(x => x.Likes.Any(y => y.User == user));
dliibDto.IsDisliked = dliibs.Where(x => x.Id == dliibDto.Id).Any(x => x.Dislikes.Any(y => y.User == user));
}

return dliibDtos;
}

public async Task<IEnumerable<DliibDto>> GetUserDliibDtos(string userName)
Expand All @@ -19,9 +33,26 @@ public async Task<IEnumerable<DliibDto>> GetUserDliibDtos(string userName)
return await dliibRepository.GetUserDliibDtos(userId);
}

public async Task<DliibDto> GetDliibDto(int id)
public async Task<DliibDto> GetDliibDto(int id, string? userName)
{
return await dliibRepository.GetDliibDto(id);
var dliib = await dliibRepository.GetDliib(id);
if (dliib == null)
{
return null;
}

var dliibDto = mapper.Map<DliibDto>(dliib);

if (userName == null)
{
return dliibDto;
}

var user = await userRepository.GetUserByName(userName);
dliibDto.IsLiked = dliib.Likes.Any(x => x.User == user);
dliibDto.IsDisliked = dliib.Dislikes.Any(x => x.User == user);

return dliibDto;
}

public async Task<Dliib?> GetDliib(int id)
Expand Down

0 comments on commit 830ab24

Please sign in to comment.