Skip to content

Commit 2f52186

Browse files
committed
Deployed backend with mongoDB
1 parent 422638a commit 2f52186

File tree

125 files changed

+23635
-78
lines changed

Some content is hidden

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

125 files changed

+23635
-78
lines changed

Controllers/AuthController.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using MARKETPLACEAPI.dto;
2+
using MARKETPLACEAPI.Models;
3+
using MARKETPLACEAPI.Services;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace MARKETPLACEAPI.Controllers;
7+
8+
9+
[ApiController]
10+
[Produces("application/json")]
11+
[Consumes("application/json")]
12+
[Route("api/auth/[controller]")]
13+
public class AuthController : ControllerBase
14+
{
15+
private readonly UserService _userService;
16+
private readonly AuthService _authService;
17+
18+
public AuthController(UserService userService, AuthService authService)
19+
{
20+
_userService = userService;
21+
_authService = authService;
22+
}
23+
24+
25+
[HttpPost("register")]
26+
[ProducesResponseType(typeof(SignInResponseDto), 201)]
27+
public async Task<IActionResult> Register(SignInRegisterDto regDto)
28+
{
29+
var user = await _userService.GetUserByWalletAddress(regDto.walletAddress);
30+
if (user != null)
31+
{
32+
var res = new SignInResponseDto
33+
{
34+
walletAddress = user.walletAddress,
35+
accountCreated = false,
36+
accountSignedIn = true,
37+
accountExists = true,
38+
invalidAddress = false,
39+
errorMessage = null,
40+
token = _authService.GenerateToken(user),
41+
};
42+
return Ok(res);
43+
}
44+
45+
var newUser = new User
46+
{
47+
userName = regDto.userName,
48+
walletAddress = regDto.walletAddress,
49+
socials = regDto.socials,
50+
};
51+
await _userService.CreateAsync(newUser);
52+
53+
var response = new SignInResponseDto
54+
{
55+
walletAddress = newUser.walletAddress,
56+
accountCreated = true,
57+
accountSignedIn = true,
58+
accountExists = false,
59+
invalidAddress = false,
60+
errorMessage = null,
61+
token = _authService.GenerateToken(newUser),
62+
};
63+
64+
return Ok(response);
65+
}
66+
67+
68+
[HttpPost("login")]
69+
[ProducesResponseType(typeof(SignInResponseDto), StatusCodes.Status200OK)]
70+
public async Task<IActionResult> Login(LoginDto loginDto)
71+
{
72+
var user = await _userService.GetUserByWalletAddress(loginDto.walletAddress!);
73+
74+
if (user is null)
75+
{
76+
return Unauthorized(new SignInResponseDto
77+
{
78+
walletAddress = null,
79+
accountCreated = false,
80+
accountSignedIn = false,
81+
accountExists = false,
82+
invalidAddress = true,
83+
errorMessage = "Invalid wallet address",
84+
});
85+
}
86+
87+
var token = _authService.GenerateToken(user);
88+
89+
return Ok(new SignInResponseDto
90+
{
91+
walletAddress = user.walletAddress,
92+
accountCreated = false,
93+
accountSignedIn = true,
94+
accountExists = true,
95+
invalidAddress = false,
96+
errorMessage = null,
97+
token = token,
98+
});
99+
}
100+
}
101+

Controllers/CategoryController.cs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using MARKETPLACEAPI.dto;
2+
using MARKETPLACEAPI.Models;
3+
using MARKETPLACEAPI.Services;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace MARKETPLACEAPI.Controllers;
8+
9+
[ApiController]
10+
[Produces("application/json")]
11+
[Consumes("application/json")]
12+
[Authorize]
13+
[Route("api/categories/[controller]")]
14+
public class CategoryController : ControllerBase
15+
{
16+
private readonly CategoryService _categoryService;
17+
18+
public CategoryController(CategoryService categoryService) =>
19+
_categoryService = categoryService;
20+
21+
[HttpGet]
22+
public async Task<List<Category>> Get() =>
23+
await _categoryService.GetAsync();
24+
25+
[HttpGet("{id:length(24)}")]
26+
public async Task<ActionResult<Category>> Get(string id, [FromHeader] string userId)
27+
{
28+
var category = await _categoryService.GetAsync(id);
29+
30+
if (category is null)
31+
{
32+
return NotFound();
33+
}
34+
35+
return category;
36+
}
37+
38+
[HttpPost]
39+
40+
public async Task<IActionResult> Post(CategoryCreateDto newCategory)
41+
{
42+
var category = new Category
43+
{
44+
categoryName = newCategory.categoryName,
45+
categoryDescription = newCategory.categoryDescription,
46+
};
47+
await _categoryService.CreateAsync(category);
48+
49+
return CreatedAtAction(nameof(Get), new { id = category.categoryId }, category);
50+
}
51+
52+
[HttpPatch("{id:length(24)}")]
53+
public async Task<IActionResult> Update(string id, CategoryUpdateDto updatedCategory)
54+
{
55+
var category = await _categoryService.GetAsync(id);
56+
57+
if (category is null)
58+
{
59+
return NotFound();
60+
}
61+
62+
category.categoryName = updatedCategory.categoryName;
63+
category.categoryDescription = updatedCategory.categoryDescription;
64+
category.updatedAt = DateTime.UtcNow;
65+
66+
await _categoryService.UpdateAsync(id, category);
67+
68+
return NoContent();
69+
}
70+
71+
[HttpDelete("{id:length(24)}")]
72+
public async Task<IActionResult> Delete(string id)
73+
{
74+
var category = await _categoryService.GetAsync(id);
75+
76+
if (category is null)
77+
{
78+
return NotFound();
79+
}
80+
81+
await _categoryService.RemoveAsync(id);
82+
83+
return NoContent();
84+
}
85+
}

Controllers/NftController.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using MARKETPLACEAPI.dto;
2+
using MARKETPLACEAPI.Models;
3+
using MARKETPLACEAPI.Services;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace MARKETPLACEAPI.Controllers;
8+
9+
[ApiController]
10+
[Produces("application/json")]
11+
[Consumes("application/json")]
12+
[Authorize]
13+
[Route("api/user/[controller]")]
14+
public class NftController : ControllerBase
15+
{
16+
private readonly NftService _nftService;
17+
private readonly CategoryService _categoryService;
18+
19+
public NftController(NftService nftService, CategoryService categoryService) {
20+
_nftService = nftService;
21+
_categoryService = categoryService;
22+
}
23+
24+
[HttpGet]
25+
public async Task<List<Nft>> Get() =>
26+
await _nftService.GetAsync();
27+
28+
[HttpGet("{id:length(24)}")]
29+
public async Task<ActionResult<NftDto>> Get(string id)
30+
{
31+
var nft = await _nftService.GetAsync(id);
32+
if (nft is null)
33+
{
34+
return NotFound();
35+
}
36+
37+
var nftDto = new NftDto
38+
{
39+
nft = nft,
40+
category = await _categoryService.GetAsync(nft.categoryId),
41+
};
42+
43+
return nftDto;
44+
}
45+
46+
[HttpPost]
47+
public async Task<IActionResult> Post(NftCreateDto newNft)
48+
{
49+
var userId = HttpContext.Request.Headers["userId"].ToString();
50+
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);
62+
63+
return CreatedAtAction(nameof(Get), new { id = nft.nftId }, nft);
64+
}
65+
66+
[HttpPatch("{id:length(24)}")]
67+
public async Task<IActionResult> Update(string id, NftUpdateDto updatedNft)
68+
{
69+
var nft = await _nftService.GetAsync(id);
70+
71+
if (nft is null)
72+
{
73+
return NotFound();
74+
}
75+
76+
nft.nftName = updatedNft.nftName ?? nft.nftName;
77+
nft.nftDescription = updatedNft.nftDescription ?? nft.nftDescription;
78+
nft.price = updatedNft.price ?? nft.price;
79+
nft.updatedAt = DateTime.UtcNow;
80+
81+
82+
await _nftService.UpdateAsync(id, nft);
83+
84+
return NoContent();
85+
}
86+
87+
[HttpDelete("{id:length(24)}")]
88+
public async Task<IActionResult> Delete(string id)
89+
{
90+
var nft = await _nftService.GetAsync(id);
91+
92+
if (nft is null)
93+
{
94+
return NotFound();
95+
}
96+
97+
await _nftService.RemoveAsync(id);
98+
99+
return NoContent();
100+
}
101+
102+
[HttpGet("creator")]
103+
public async Task<List<Nft>> GetNftsByCreatorId([FromQuery] string creatorId) =>
104+
await _nftService.GetNftsByCreatorId(creatorId);
105+
106+
[HttpGet("owner")]
107+
public async Task<List<Nft>> GetNftsByOwnerId([FromQuery] string ownerId) =>
108+
await _nftService.GetNftsByOwnerId(ownerId);
109+
110+
[HttpGet("with-price-filter")]
111+
public async Task<List<Nft>> GetNftWithPriceFilter(
112+
[FromQuery] double? priceMax, [FromQuery] double?
113+
priceMin, [FromQuery] bool? ascending = true) =>
114+
await _nftService.GetNftWithPriceFilter(priceMax, priceMin, ascending);
115+
116+
[HttpGet("search")]
117+
public async Task<List<Nft>> SearchByNftName([FromQuery] string nftName, [FromQuery] bool ascending = true) =>
118+
await _nftService.SearchByNftName(nftName, ascending);
119+
}

0 commit comments

Comments
 (0)