Skip to content

Commit 5185c7c

Browse files
committed
Changed authorization for get requests
1 parent fb6b970 commit 5185c7c

File tree

14 files changed

+157
-99
lines changed

14 files changed

+157
-99
lines changed

Controllers/NftLikeController.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ namespace MARKETPLACEAPI.Controllers;
1010
[ApiController]
1111
[Produces("application/json")]
1212
[Consumes("application/json")]
13-
[Authorize]
1413
[Route("api/nftlikes/[controller]")]
1514
public class NftLikeController : ControllerBase
1615
{
1716
private readonly INftLikeService _nftLikeService;
1817
private readonly INftService _nftService;
1918
private readonly IMapper _mapper;
2019

21-
public NftLikeController(INftLikeService nftLikeService, INftService nftService, IMapper mapper) {
22-
_nftLikeService = nftLikeService;
23-
_nftService = nftService;
24-
_mapper = mapper;
25-
}
20+
public NftLikeController(INftLikeService nftLikeService, INftService nftService, IMapper mapper)
21+
{
22+
_nftLikeService = nftLikeService;
23+
_nftService = nftService;
24+
_mapper = mapper;
25+
}
2626

2727
[HttpGet]
2828
[ProducesResponseType(typeof(IList<NftLike>), 200)]
@@ -44,6 +44,7 @@ public async Task<IActionResult> Get(string id)
4444
}
4545

4646
[HttpPost]
47+
[Authorize]
4748
[ProducesResponseType(StatusCodes.Status201Created)]
4849
[ProducesResponseType(StatusCodes.Status400BadRequest)]
4950
[ProducesResponseType(StatusCodes.Status404NotFound)]
@@ -61,7 +62,7 @@ public async Task<IActionResult> Post(NftLikeCreateDto newNftLike)
6162

6263
if (nftLike != null)
6364
{
64-
return BadRequest("You already liked this NFT");
65+
return Ok(nftLike);
6566
}
6667

6768
var newLike = _mapper.Map<NftLike>(newNftLike);
@@ -76,6 +77,7 @@ public async Task<IActionResult> Post(NftLikeCreateDto newNftLike)
7677
}
7778

7879
[HttpPatch("{id:length(24)}")]
80+
[Authorize]
7981
public async Task<IActionResult> Update(string id, NftLike updatedNftLike)
8082
{
8183
var nftLike = await _nftLikeService.GetAsync(id);
@@ -93,19 +95,20 @@ public async Task<IActionResult> Update(string id, NftLike updatedNftLike)
9395
}
9496

9597
[HttpDelete("{id:length(24)}")]
98+
[Authorize]
9699
public async Task<IActionResult> Delete(string id)
97-
{
100+
{
98101
var userId = HttpContext.Request.Headers["userId"].ToString();
99102
var nftLike = await _nftLikeService.GetAsync(id);
100103

101104
if (nftLike is null)
102105
{
103-
return NotFound();
106+
return NoContent();
104107
}
105108

106109
if (nftLike.userId != userId)
107110
{
108-
return BadRequest("You can only delete your own likes");
111+
return NoContent();
109112
}
110113

111114
var nft = await _nftService.GetAsync(nftLike.nftId!);
@@ -138,7 +141,7 @@ public async Task<IActionResult> GetNftLikeByNftId([FromQuery] string nftId)
138141
public async Task<IActionResult> GetNftLikesByUserId([FromQuery] string userId) =>
139142
Ok(await _nftLikeService.GetNftLikesByUserId(userId));
140143

141-
144+
142145
[HttpGet("get-by-user-id-and-nft-id")]
143146
[ProducesResponseType(typeof(NftLike), 200)]
144147
public async Task<IActionResult> GetNftLikeByUserIdAndNftId([FromQuery] string userId, [FromQuery] string nftId)

Controllers/ProjectDetailController.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ namespace MARKETPLACEAPI.Controllers;
1010
[ApiController]
1111
[Produces("application/json")]
1212
[Consumes("application/json")]
13-
[Authorize]
1413
[Route("api/project-details/[controller]")]
1514
public class ProjectDetailController : ControllerBase
1615
{
1716
private readonly IProjectDetailService _projectDetailService;
1817
private readonly IMapper _mapper;
1918

20-
public ProjectDetailController(IProjectDetailService projectDetailService, IMapper mapper) {
19+
public ProjectDetailController(IProjectDetailService projectDetailService, IMapper mapper)
20+
{
2121
_projectDetailService = projectDetailService;
2222
_mapper = mapper;
2323
}
24-
24+
2525

2626
[HttpGet]
2727
[ProducesResponseType(typeof(IList<ProjectDetail>), 200)]
@@ -43,23 +43,25 @@ public async Task<IActionResult> Get(string id)
4343
}
4444

4545
[HttpPost]
46+
[Authorize]
4647
public async Task<IActionResult> Post(ProjectDetailCreateDto newProjectDetail)
4748
{
4849
var existingProjectDetail = await _projectDetailService.GetProjectDetailsByProjectId(newProjectDetail.projectId!);
4950

50-
// if (existingProjectDetail is not null)
51-
// {
52-
// return Conflict("Project Details already exists for this project.");
53-
// }
51+
if (existingProjectDetail is not null)
52+
{
53+
return Conflict("Project Details already exists for this project.");
54+
}
5455

55-
var projectDetail = _mapper.Map<ProjectDetail>(newProjectDetail);
56+
var projectDetail = _mapper.Map<ProjectDetail>(newProjectDetail);
5657

57-
await _projectDetailService.CreateAsync(projectDetail);
58+
await _projectDetailService.CreateAsync(projectDetail);
5859

5960
return CreatedAtAction(nameof(Get), new { id = projectDetail.projectDetailId }, projectDetail);
6061
}
61-
62+
6263
[HttpPatch("{id:length(24)}")]
64+
[Authorize]
6365
public async Task<IActionResult> Update(string id, ProjectDetailCreateDto updatedProjectDetail)
6466
{
6567
var projectDetail = await _projectDetailService.GetAsync(id);
@@ -77,6 +79,7 @@ public async Task<IActionResult> Update(string id, ProjectDetailCreateDto update
7779
}
7880

7981
[HttpDelete("{id:length(24)}")]
82+
[Authorize]
8083
public async Task<IActionResult> Delete(string id)
8184
{
8285
var projectDetail = await _projectDetailService.GetAsync(id);

Controllers/ProjectLikeController.cs

Lines changed: 69 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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]")]
1514
public 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
}

Controllers/ProjectUpdateController.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace MARKETPLACEAPI.Controllers;
1010
[ApiController]
1111
[Produces("application/json")]
1212
[Consumes("application/json")]
13-
[Authorize]
1413
[Route("api/project-updates/[controller]")]
1514
public class ProjectUpdateController : ControllerBase
1615
{
1716
private readonly IProjectUpdateService _projectUpdateService;
1817
private readonly IMapper _mapper;
1918

20-
public ProjectUpdateController(IProjectUpdateService projectUpdateService, IMapper mapper) {
19+
public ProjectUpdateController(IProjectUpdateService projectUpdateService, IMapper mapper)
20+
{
2121
_projectUpdateService = projectUpdateService;
2222
_mapper = mapper;
2323
}
@@ -42,15 +42,17 @@ public async Task<IActionResult> Get(string id)
4242
}
4343

4444
[HttpPost]
45+
[Authorize]
4546
public async Task<IActionResult> Post(ProjectUpdateCreateDto newProjectUpdate)
4647
{
4748
var projectUpdate = _mapper.Map<ProjectUpdate>(newProjectUpdate);
4849
await _projectUpdateService.CreateAsync(projectUpdate);
4950

5051
return CreatedAtAction(nameof(Get), new { id = projectUpdate.projectUpdateId }, projectUpdate);
5152
}
52-
53+
5354
[HttpPatch("{id:length(24)}")]
55+
[Authorize]
5456
public async Task<IActionResult> Update(string id, ProjectUpdateCreateDto updatedProjectUpdate)
5557
{
5658

@@ -70,6 +72,7 @@ public async Task<IActionResult> Update(string id, ProjectUpdateCreateDto update
7072
}
7173

7274
[HttpDelete("{id:length(24)}")]
75+
[Authorize]
7376
public async Task<IActionResult> Delete(string id)
7477
{
7578
var projectUpdate = await _projectUpdateService.GetAsync(id);

0 commit comments

Comments
 (0)