-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLikeController.cs
68 lines (57 loc) · 2.05 KB
/
LikeController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SeeSay.Models.Dto.Likes;
using SeeSay.Models.Entities;
using SeeSay.Services.Abstractions;
namespace SeeSay.Controllers;
[Route(template: "api/[controller]/[action]")]
[ApiController]
[Authorize]
public class LikeController : ControllerBase
{
private readonly ILikeRepository likeRepository;
private readonly IMapper mapper;
public LikeController(ILikeRepository likeRepository, IMapper mapper)
{
this.likeRepository = likeRepository;
this.mapper = mapper;
}
[HttpGet("{userId}/{postId:int}")]
public async Task<IActionResult> GetUserLikeAsync([FromRoute] string userId, [FromRoute] int postId)
{
var like = await likeRepository.GetUserLikeAsync(userId, postId);
return Ok(new { IsUserLikePost = like is not null });
}
[HttpGet("{postId:int}")]
public async Task<IActionResult> GetNumberOfLikesAsync([FromRoute] int postId)
{
var numberOfLikes = await likeRepository.GetNumberOfLikesAsync(postId);
return Ok(numberOfLikes);
}
[HttpGet("{userId}")]
public async Task<IActionResult> GetNumberOfUserLikes([FromRoute] string userId)
{
var numberOfUserLikes = await likeRepository.GetNumberOfUserLikesAsync(userId);
return Ok(numberOfUserLikes);
}
[HttpGet("{userId}")]
public async Task<IActionResult> GetUserLikes([FromRoute] string userId)
{
var likes = await likeRepository.GetUserLikesAsync(userId);
return Ok(likes);
}
[HttpPost]
public async Task<IActionResult> AddLike([FromBody] LikeDto likeDto)
{
var like = mapper.Map<LikeDto, Like>(likeDto);
await likeRepository.AddLikeAsync(like);
return Created($"Post/GetPost/{like.PostId}", like);
}
[HttpDelete("{userId}/{postId:int}")]
public async Task<IActionResult> DeleteLike([FromRoute] string userId, [FromRoute] int postId)
{
await likeRepository.DeleteLikeAsync(userId, postId);
return NoContent();
}
}