@@ -10,20 +10,20 @@ namespace MARKETPLACEAPI.Controllers;
1010[ ApiController ]
1111[ Produces ( "application/json" ) ]
1212[ Consumes ( "application/json" ) ]
13- [ Authorize ]
1413[ Route ( "api/project-likes/[controller]" ) ]
1514public class ProjectLikeController : ControllerBase
1615{
17- private readonly IProjectLikeService _projectLikeService ;
18- private readonly IProjectService _projectService ;
19- private readonly IMapper _mapper ;
20-
21- public ProjectLikeController ( IProjectLikeService projectLikeService , IProjectService projectService , IMapper mapper ) {
22- _projectLikeService = projectLikeService ;
23- _projectService = projectService ;
24- _mapper = mapper ;
16+ private readonly IProjectLikeService _projectLikeService ;
17+ private readonly IProjectService _projectService ;
18+ private readonly IMapper _mapper ;
19+
20+ public ProjectLikeController ( IProjectLikeService projectLikeService , IProjectService projectService , IMapper mapper )
21+ {
22+ _projectLikeService = projectLikeService ;
23+ _projectService = projectService ;
24+ _mapper = mapper ;
2525 }
26-
26+
2727
2828 [ HttpGet ]
2929 [ ProducesResponseType ( typeof ( IList < ProjectLike > ) , 200 ) ]
@@ -34,84 +34,94 @@ public async Task<IActionResult> Get() =>
3434 [ ProducesResponseType ( typeof ( ProjectLike ) , 200 ) ]
3535 public async Task < IActionResult > Get ( string id )
3636 {
37- var projectLike = await _projectLikeService . GetAsync ( id ) ;
37+ var projectLike = await _projectLikeService . GetAsync ( id ) ;
3838
39- if ( projectLike is null )
40- {
41- return NotFound ( ) ;
42- }
39+ if ( projectLike is null )
40+ {
41+ return NotFound ( ) ;
42+ }
4343
44- return Ok ( projectLike ) ;
44+ return Ok ( projectLike ) ;
4545 }
4646
4747 [ HttpPost ]
48+ [ Authorize ]
4849 public async Task < IActionResult > Post ( ProjectLikeCreateDto newProjectLike )
4950 {
50- var userId = HttpContext . Request . Headers [ "userId" ] . ToString ( ) ;
51- var existingProjectLike = await _projectLikeService . GetProjectLikeByUserIdAndProjectId (
52- userId , newProjectLike . projectId ) ;
51+ var userId = HttpContext . Request . Headers [ "userId" ] . ToString ( ) ;
52+ var existingProjectLike = await _projectLikeService . GetProjectLikeByUserIdAndProjectId (
53+ userId , newProjectLike . projectId ) ;
5354
54- if ( existingProjectLike is not null ) {
55- return Conflict ( "Project Like already exists for this user and project." ) ;
56- }
55+ if ( existingProjectLike is not null )
56+ {
57+ return Ok ( existingProjectLike ) ;
58+ }
5759
58- var projectLike = _mapper . Map < ProjectLike > ( newProjectLike ) ;
59- projectLike . userId = userId ;
60- var project = await _projectService . GetAsync ( projectLike . projectId ) ;
60+ var projectLike = _mapper . Map < ProjectLike > ( newProjectLike ) ;
61+ projectLike . userId = userId ;
62+ var project = await _projectService . GetAsync ( projectLike . projectId ) ;
6163
62- if ( project is null )
63- {
64- return NotFound ( ) ;
65- }
66- await _projectLikeService . CreateAsync ( projectLike ) ;
67-
64+ if ( project is null )
65+ {
66+ return NotFound ( ) ;
67+ }
68+ await _projectLikeService . CreateAsync ( projectLike ) ;
6869
69- project . noOfLikes += 1 ;
70- await _projectService . UpdateAsync ( projectLike . projectId , project ) ;
7170
72- return CreatedAtAction ( nameof ( Get ) , new { id = projectLike . projectLikeId } , projectLike ) ;
71+ project . noOfLikes += 1 ;
72+ await _projectService . UpdateAsync ( projectLike . projectId , project ) ;
73+
74+ return CreatedAtAction ( nameof ( Get ) , new { id = projectLike . projectLikeId } , projectLike ) ;
7375 }
7476
7577 [ HttpPatch ( "{id:length(24)}" ) ]
78+ [ Authorize ]
7679 public async Task < IActionResult > Update ( string id , ProjectLike updatedProjectLike )
7780 {
78- var projectLike = await _projectLikeService . GetAsync ( id ) ;
81+ var projectLike = await _projectLikeService . GetAsync ( id ) ;
7982
80- if ( projectLike is null )
81- {
82- return NotFound ( ) ;
83- }
83+ if ( projectLike is null )
84+ {
85+ return NotFound ( ) ;
86+ }
8487
85- updatedProjectLike . projectLikeId = projectLike . projectLikeId ;
88+ updatedProjectLike . projectLikeId = projectLike . projectLikeId ;
8689
87- await _projectLikeService . UpdateAsync ( id , updatedProjectLike ) ;
90+ await _projectLikeService . UpdateAsync ( id , updatedProjectLike ) ;
8891
89- return NoContent ( ) ;
92+ return NoContent ( ) ;
9093 }
9194
9295 [ HttpDelete ( "{id:length(24)}" ) ]
96+ [ Authorize ]
9397 public async Task < IActionResult > Delete ( string id )
9498 {
95- var projectLike = await _projectLikeService . GetAsync ( id ) ;
96- if ( projectLike is null )
97- {
98- return NotFound ( ) ;
99- }
100- var project = await _projectService . GetAsync ( projectLike . projectId ! ) ;
99+ var projectLike = await _projectLikeService . GetAsync ( id ) ;
100+ var userId = HttpContext . Request . Headers [ "userId" ] . ToString ( ) ;
101+ if ( projectLike is null )
102+ {
103+ return NoContent ( ) ;
104+ }
101105
102- if ( project is null )
103- {
104- return NotFound ( "Project not found." ) ;
105- }
106+ if ( projectLike . userId != userId )
107+ {
108+ return NoContent ( ) ;
109+ }
110+ var project = await _projectService . GetAsync ( projectLike . projectId ! ) ;
106111
107- project . noOfLikes -= 1 ;
108- await _projectService . UpdateAsync ( projectLike . projectId ! , project ) ;
112+ if ( project is null )
113+ {
114+ return NoContent ( ) ;
115+ }
116+
117+ project . noOfLikes -= 1 ;
118+ await _projectService . UpdateAsync ( projectLike . projectId ! , project ) ;
109119
110120
111- await _projectLikeService . RemoveAsync ( id ) ;
121+ await _projectLikeService . RemoveAsync ( id ) ;
112122
113123
114- return NoContent ( ) ;
124+ return NoContent ( ) ;
115125 }
116126
117127 [ HttpGet ( "get-by-projectid" ) ]
@@ -126,13 +136,14 @@ public async Task<IActionResult> GetByUserId([FromQuery] string userId) =>
126136
127137 [ HttpGet ( "get-by-userid-and-projectid" ) ]
128138 [ ProducesResponseType ( typeof ( ProjectLike ) , 200 ) ]
129- public async Task < IActionResult > GetByUserIdAndProjectId ( [ FromQuery ] string userId , [ FromQuery ] string projectId ) {
139+ public async Task < IActionResult > GetByUserIdAndProjectId ( [ FromQuery ] string userId , [ FromQuery ] string projectId )
140+ {
130141 var projectLike = await _projectLikeService . GetProjectLikeByUserIdAndProjectId ( userId , projectId ) ;
131142 if ( projectLike is null )
132143 {
133144 return NotFound ( ) ;
134145 }
135146 return Ok ( projectLike ) ;
136147 }
137-
148+
138149}
0 commit comments