Skip to content

Commit b6f9650

Browse files
committed
Modified code to conform with testing
1 parent 451dfc8 commit b6f9650

File tree

379 files changed

+3094
-7920
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

379 files changed

+3094
-7920
lines changed

.env

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DB_PASSWORD=iBpY5H8w8M4MzBP
2-
JWT_KEY=Thisisaverylongkey2432fgnfnd5ytrjgrg544gns
1+
DB_PASSWORD = "iBpY5H8w8M4MzBP"
2+
JWT_KEY = "Thisisaverylongkey2432fgnfnd5ytrjgrg544gns"

BookStoreApi.csproj

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,14 @@
88

99
<ItemGroup>
1010
<PackageReference Include="AutoMapper" Version="12.0.1" />
11+
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
1112
<PackageReference Include="DotNetEnv" Version="2.5.0" />
13+
<PackageReference Include="MailKit" Version="4.1.0" />
1214
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
13-
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="6.0.0" />
1415
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.32.0" />
1516
<PackageReference Include="MongoDB.Driver" Version="2.20.0" />
16-
<PackageReference Include="Moq" Version="4.18.4" />
1717
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
1818
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.32.0" />
19-
<PackageReference Include="xunit" Version="2.5.0" />
20-
<PackageReference Include="xunit.assert" Version="2.5.0" />
21-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
22-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
23-
<PrivateAssets>all</PrivateAssets>
24-
</PackageReference>
2519
</ItemGroup>
2620

2721
</Project>

Controllers/AuthController.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using MARKETPLACEAPI.dto;
2+
using MARKETPLACEAPI.Interfaces;
23
using MARKETPLACEAPI.Models;
3-
using MARKETPLACEAPI.Services;
4+
using MARKETPLACEAPI.Helpers;
45
using Microsoft.AspNetCore.Mvc;
6+
using AutoMapper;
7+
58

69
namespace MARKETPLACEAPI.Controllers;
710

@@ -12,13 +15,19 @@ namespace MARKETPLACEAPI.Controllers;
1215
[Route("api/auth/[controller]")]
1316
public class AuthController : ControllerBase
1417
{
15-
private readonly UserService _userService;
16-
private readonly AuthService _authService;
18+
private readonly IUserService _userService;
19+
private readonly IAuthService _authService;
20+
21+
private readonly IConfiguration _config;
22+
private readonly IMapper _mapper;
1723

18-
public AuthController(UserService userService, AuthService authService)
24+
public AuthController(IUserService userService, IAuthService authService, IConfiguration config, IMapper mapper)
1925
{
2026
_userService = userService;
2127
_authService = authService;
28+
_config = config;
29+
_mapper = mapper;
30+
2231
}
2332

2433

@@ -27,6 +36,7 @@ public AuthController(UserService userService, AuthService authService)
2736
public async Task<IActionResult> Register(SignInRegisterDto regDto)
2837
{
2938
var user = await _userService.GetUserByWalletAddress(regDto.walletAddress);
39+
TokenHelper _tokenHelper = new TokenHelper(_config);
3040
if (user != null)
3141
{
3242
var res = new SignInResponseDto
@@ -37,17 +47,12 @@ public async Task<IActionResult> Register(SignInRegisterDto regDto)
3747
accountExists = true,
3848
invalidAddress = false,
3949
errorMessage = null,
40-
token = _authService.GenerateToken(user),
50+
token = _tokenHelper.GenerateToken(user),
4151
};
4252
return Ok(res);
4353
}
4454

45-
var newUser = new User
46-
{
47-
userName = regDto.userName,
48-
walletAddress = regDto.walletAddress,
49-
socials = regDto.socials,
50-
};
55+
var newUser = _mapper.Map<User>(regDto);
5156
await _userService.CreateAsync(newUser);
5257

5358
var response = new SignInResponseDto
@@ -58,7 +63,7 @@ public async Task<IActionResult> Register(SignInRegisterDto regDto)
5863
accountExists = false,
5964
invalidAddress = false,
6065
errorMessage = null,
61-
token = _authService.GenerateToken(newUser),
66+
token = _tokenHelper.GenerateToken(newUser),
6267
};
6368

6469
return Ok(response);
@@ -83,8 +88,8 @@ public async Task<IActionResult> Login(LoginDto loginDto)
8388
errorMessage = "Invalid wallet address",
8489
});
8590
}
86-
87-
var token = _authService.GenerateToken(user);
91+
TokenHelper _tokenHelper = new TokenHelper(_config);
92+
var token = _tokenHelper.GenerateToken(user);
8893

8994
return Ok(new SignInResponseDto
9095
{

Controllers/CategoryController.cs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using MARKETPLACEAPI.dto;
22
using MARKETPLACEAPI.Models;
3-
using MARKETPLACEAPI.Services;
3+
using MARKETPLACEAPI.Interfaces;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6+
using AutoMapper;
67

78
namespace MARKETPLACEAPI.Controllers;
89

@@ -12,39 +13,62 @@ namespace MARKETPLACEAPI.Controllers;
1213
[Route("api/categories/[controller]")]
1314
public class CategoryController : ControllerBase
1415
{
15-
private readonly CategoryService _categoryService;
16+
private readonly IDefaultService<Category> _categoryService;
17+
private readonly IMapper _mapper;
1618

17-
public CategoryController(CategoryService categoryService) =>
19+
public CategoryController(IDefaultService<Category> categoryService, IMapper mapper) {
1820
_categoryService = categoryService;
19-
21+
_mapper = mapper;
22+
}
23+
2024
[HttpGet]
21-
public async Task<List<Category>> Get() =>
22-
await _categoryService.GetAsync();
25+
[ProducesResponseType(200, Type = typeof(IList<Category>))]
26+
public async Task<IActionResult> Get() {
27+
try
28+
{
29+
var categories = await _categoryService.GetAsync();
30+
return Ok(categories);
31+
}
32+
catch (Exception ex)
33+
{
34+
return BadRequest(ex);
35+
}
36+
37+
}
38+
2339

2440
[HttpGet("{id:length(24)}")]
25-
public async Task<ActionResult<Category>> Get(string id, [FromHeader] string userId)
41+
[ProducesResponseType(typeof(Category), 200)]
42+
public async Task<IActionResult> Get(string id)
2643
{
2744
var category = await _categoryService.GetAsync(id);
2845

2946
if (category is null)
3047
{
31-
return NotFound();
48+
return NotFound(
49+
new { Message = $"Category with id: {id} does not exist." }
50+
);
3251
}
3352

34-
return category;
53+
return Ok(category);
3554
}
3655

3756
[HttpPost]
3857
[Authorize]
58+
[ProducesResponseType(typeof(Category), 201)]
3959

4060
public async Task<IActionResult> Post(CategoryCreateDto newCategory)
4161
{
42-
var category = new Category
62+
var category = _mapper.Map<Category>(newCategory);
63+
64+
try
4365
{
44-
categoryName = newCategory.categoryName,
45-
categoryDescription = newCategory.categoryDescription,
46-
};
47-
await _categoryService.CreateAsync(category);
66+
await _categoryService.CreateAsync(category);
67+
}
68+
catch (Exception ex)
69+
{
70+
return BadRequest(ex.Message);
71+
}
4872

4973
return CreatedAtAction(nameof(Get), new { id = category.categoryId }, category);
5074
}
@@ -57,14 +81,14 @@ public async Task<IActionResult> Update(string id, CategoryUpdateDto updatedCate
5781

5882
if (category is null)
5983
{
60-
return NotFound();
84+
return NotFound(
85+
new { Message = $"Category with id: {id} does not exist." }
86+
);
6187
}
6288

63-
category.categoryName = updatedCategory.categoryName;
64-
category.categoryDescription = updatedCategory.categoryDescription;
65-
category.updatedAt = DateTime.UtcNow;
89+
var updatedCat = _mapper.Map(updatedCategory, category);
6690

67-
await _categoryService.UpdateAsync(id, category);
91+
await _categoryService.UpdateAsync(id, updatedCat);
6892

6993
return NoContent();
7094
}

Controllers/NftController.cs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using MARKETPLACEAPI.dto;
22
using MARKETPLACEAPI.Models;
3-
using MARKETPLACEAPI.Services;
3+
using MARKETPLACEAPI.Interfaces;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6+
using AutoMapper;
7+
68

79
namespace MARKETPLACEAPI.Controllers;
810

@@ -12,20 +14,24 @@ namespace MARKETPLACEAPI.Controllers;
1214
[Route("api/user/[controller]")]
1315
public class NftController : ControllerBase
1416
{
15-
private readonly NftService _nftService;
16-
private readonly CategoryService _categoryService;
17+
private readonly INftService _nftService;
18+
private readonly IDefaultService<Category> _categoryService;
19+
private readonly IMapper _mapper;
1720

18-
public NftController(NftService nftService, CategoryService categoryService) {
21+
public NftController(INftService nftService, IDefaultService<Category> categoryService, IMapper mapper) {
1922
_nftService = nftService;
2023
_categoryService = categoryService;
24+
_mapper = mapper;
2125
}
2226

2327
[HttpGet]
24-
public async Task<List<Nft>> Get() =>
25-
await _nftService.GetAsync();
28+
[ProducesResponseType(typeof(IList<Nft>), 200)]
29+
public async Task<IActionResult> Get() =>
30+
Ok(await _nftService.GetAsync());
2631

2732
[HttpGet("{id:length(24)}")]
28-
public async Task<ActionResult<NftDto>> Get(string id)
33+
[ProducesResponseType(typeof(NftDto), 200)]
34+
public async Task<IActionResult> Get(string id)
2935
{
3036
var nft = await _nftService.GetAsync(id);
3137
if (nft is null)
@@ -35,11 +41,11 @@ public async Task<ActionResult<NftDto>> Get(string id)
3541

3642
var nftDto = new NftDto
3743
{
38-
nft = nft,
39-
category = await _categoryService.GetAsync(nft.categoryId),
44+
nft = nft,
45+
category = await _categoryService.GetAsync(nft.categoryId)
4046
};
4147

42-
return nftDto;
48+
return Ok(nftDto);
4349
}
4450

4551
[HttpPost]
@@ -48,17 +54,11 @@ public async Task<IActionResult> Post(NftCreateDto newNft)
4854
{
4955
var userId = HttpContext.Request.Headers["userId"].ToString();
5056

51-
var nft = new Nft
52-
{
53-
nftName = newNft.nftName,
54-
nftDescription = newNft.nftDescription,
55-
price = newNft.price,
56-
ownerId = userId,
57-
creatorId = userId,
58-
categoryId = newNft.categoryId
59-
};
60-
61-
await _nftService.CreateAsync(nft);
57+
var nft = _mapper.Map<Nft>(newNft);
58+
nft.creatorId = userId;
59+
nft.ownerId = userId;
60+
61+
await _nftService.CreateAsync(nft);
6262

6363
return CreatedAtAction(nameof(Get), new { id = nft.nftId }, nft);
6464
}
@@ -74,13 +74,10 @@ public async Task<IActionResult> Update(string id, NftUpdateDto updatedNft)
7474
return NotFound();
7575
}
7676

77-
nft.nftName = updatedNft.nftName ?? nft.nftName;
78-
nft.nftDescription = updatedNft.nftDescription ?? nft.nftDescription;
79-
nft.price = updatedNft.price ?? nft.price;
80-
nft.updatedAt = DateTime.UtcNow;
77+
var nftUpdate= _mapper.Map(updatedNft, nft);
8178

8279

83-
await _nftService.UpdateAsync(id, nft);
80+
await _nftService.UpdateAsync(id, nftUpdate);
8481

8582
return NoContent();
8683
}
@@ -102,20 +99,24 @@ public async Task<IActionResult> Delete(string id)
10299
}
103100

104101
[HttpGet("creator")]
105-
public async Task<List<Nft>> GetNftsByCreatorId([FromQuery] string creatorId) =>
106-
await _nftService.GetNftsByCreatorId(creatorId);
102+
[ProducesResponseType(typeof(IList<Nft>), 200)]
103+
public async Task<IActionResult> GetNftsByCreatorId([FromQuery] string creatorId) =>
104+
Ok(await _nftService.GetNftsByCreatorId(creatorId));
107105

108106
[HttpGet("owner")]
109-
public async Task<List<Nft>> GetNftsByOwnerId([FromQuery] string ownerId) =>
110-
await _nftService.GetNftsByOwnerId(ownerId);
107+
[ProducesResponseType(typeof(IList<Nft>), 200)]
108+
public async Task<IActionResult> GetNftsByOwnerId([FromQuery] string ownerId) =>
109+
Ok(await _nftService.GetNftsByOwnerId(ownerId));
111110

112111
[HttpGet("with-price-filter")]
113-
public async Task<List<Nft>> GetNftWithPriceFilter(
112+
[ProducesResponseType(typeof(IList<Nft>), 200)]
113+
public async Task<IActionResult> GetNftWithPriceFilter(
114114
[FromQuery] double? priceMax, [FromQuery] double?
115115
priceMin, [FromQuery] bool? ascending = true) =>
116-
await _nftService.GetNftWithPriceFilter(priceMax, priceMin, ascending);
116+
Ok(await _nftService.GetNftWithPriceFilter(priceMax, priceMin, ascending));
117117

118118
[HttpGet("search")]
119-
public async Task<List<Nft>> SearchByNftName([FromQuery] string nftName, [FromQuery] bool ascending = true) =>
120-
await _nftService.SearchByNftName(nftName, ascending);
119+
[ProducesResponseType(typeof(IList<Nft>), 200)]
120+
public async Task<IActionResult> SearchByNftName([FromQuery] string nftName, [FromQuery] bool ascending = true) =>
121+
Ok(await _nftService.SearchByNftName(nftName, ascending));
121122
}

0 commit comments

Comments
 (0)