Skip to content

Commit 20eb3a8

Browse files
committed
Solved Exceptions in Incrementing cart quantity
1 parent 06a6dac commit 20eb3a8

File tree

7 files changed

+32
-30
lines changed

7 files changed

+32
-30
lines changed

CozyCub/Controllers/CartController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public async Task<ActionResult> AddToCart(int productId)
7171

7272
// Increment quantity of a product in the cart
7373
[HttpPut("increment-quantity")]
74+
[Authorize] // Requires authentication
7475
[ProducesResponseType(200)] // Successful response
7576
[ProducesResponseType(500)] // Server error response
7677
public async Task<IActionResult> IncrementQuantity(int productId)
@@ -95,6 +96,7 @@ public async Task<IActionResult> IncrementQuantity(int productId)
9596

9697
// Decrement quantity of a product in the cart
9798
[HttpPut("decrement-quantity")]
99+
[Authorize] // Requires authentication
98100
[ProducesResponseType(200)] // Successful response
99101
[ProducesResponseType(500)] // Server error response
100102
public async Task<IActionResult> DecrementQuantity(int productId)
@@ -120,6 +122,7 @@ public async Task<IActionResult> DecrementQuantity(int productId)
120122

121123
// Remove a product from the cart
122124
[HttpDelete("remove-item-from-cart")]
125+
[Authorize] // Requires authentication
123126
[ProducesResponseType(typeof(bool), 200)] // Successful response
124127
[ProducesResponseType(500)] // Server error response
125128
public async Task<ActionResult> RemoveCartItem(int productId)

CozyCub/Controllers/OrderController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ public ActionResult Payment(RazorPayDTO razorpay)
8181
[ProducesResponseType(400)] // Bad request response
8282
[ProducesResponseType(401)] // Unauthorized response
8383
[ProducesResponseType(500)] // Server error response
84-
public async Task<ActionResult> PlaceOrder(int userId, OrderRequestDTO orderRequests)
84+
public async Task<ActionResult> PlaceOrder(OrderRequestDTO orderRequest)
8585
{
8686
try
8787
{
8888
// Check if orderRequests and userId are valid
89-
if (orderRequests == null || userId <= 0)
89+
if (orderRequest == null)
9090
{
9191
return BadRequest();
9292
}
@@ -95,8 +95,12 @@ public async Task<ActionResult> PlaceOrder(int userId, OrderRequestDTO orderRequ
9595
var splitToken = token.Split(' ');
9696
var jwtToken = splitToken[1];
9797
// Create order
98-
await _orderServices.CreateOrder(jwtToken, orderRequests);
99-
return Ok();
98+
if (orderRequest == null || jwtToken == null)
99+
{
100+
return BadRequest();
101+
}
102+
var status = await _orderServices.CreateOrder(jwtToken, orderRequest);
103+
return Ok(status);
100104
}
101105
catch (Exception e)
102106
{
@@ -121,11 +125,7 @@ public async Task<ActionResult> GetOrderDetailsAsAdmin(int userId)
121125
{
122126
return BadRequest();
123127
}
124-
// Get JWT token from request header
125-
var token = HttpContext.Request.Headers["Authorization"].FirstOrDefault();
126-
var splitToken = token.Split(' ');
127-
var jwtToken = splitToken[1];
128-
return Ok(await _orderServices.GetOrderDetails(jwtToken));
128+
return Ok(await _orderServices.GetOrderDetailsForAdmin());
129129
}
130130
catch (Exception e)
131131
{

CozyCub/CozyCub.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2020
</PackageReference>
2121
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="7.3.1" />
22-
<PackageReference Include="Razorpay" Version="3.1.0" />
22+
<PackageReference Include="Razorpay" Version="3.1.1" />
2323
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
2424
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.3.1" />
2525
</ItemGroup>

CozyCub/Payments/Orders/OrderService.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,34 +42,34 @@ public OrderService(IConfiguration cofn, ApplicationDbContext dbContext, IJwtSer
4242
/// </summary>
4343
/// <param name="price">The price of the order.</param>
4444
/// <returns>The ID of the created order.</returns>
45-
public Task<string> RazorPayPayment(long price)
45+
public async Task<string> RazorPayPayment(long price)
4646
{
4747
try
4848
{
4949
Dictionary<string, object> input = [];
50-
Guid transactionid = Guid.NewGuid();
50+
Random random = new();
51+
string TrasactionId = random.Next(0, 1000).ToString();
52+
input.Add("amount", Convert.ToDecimal(price) * 100);
53+
input.Add("currency", "INR");
54+
input.Add("receipt", TrasactionId);
5155

52-
string TransactionId = transactionid.ToString();
53-
input.Add("Amount", Convert.ToDecimal(price) * 100);
54-
input.Add("Currency", "INR");
55-
input.Add("Receipt", TransactionId);
56+
string key = _configuration["Razorpay:KeyId"];
57+
string secret = _configuration["Razorpay:KeySecret"];
5658

57-
string? Key = _configuration["RazorPay:KeyId"];
58-
string? secret = _configuration["RazorPay:KeySecret"];
59-
60-
RazorpayClient client = new(Key, secret);
59+
RazorpayClient client = new RazorpayClient(key, secret);
6160

6261
Razorpay.Api.Order order = client.Order.Create(input);
63-
var orderId = order["id"].ToString();
62+
var OrderId = order["id"].ToString();
63+
64+
return await Task.FromResult(OrderId);
6465

65-
return orderId;
6666

6767
}
6868
catch (Exception ex)
6969
{
70-
Console.WriteLine(ex.Message);
7170
return null;
72-
throw;
71+
throw new Exception("An Exception occured while performing the order in RazorPya :" + ex.Message);
72+
7373
}
7474
}
7575

@@ -107,7 +107,6 @@ public async Task<bool> CreateOrder(string token, OrderRequestDTO orderRequestDT
107107
CustomerPhone = orderRequestDTO.CustomerPhone,
108108
CustomerCity = orderRequestDTO.CustomerCity,
109109
Address = orderRequestDTO.HomeAddress,
110-
OrderStatus = orderRequestDTO.OrderStatus,
111110
OrderString = orderRequestDTO.OrderString,
112111
TransactionId = orderRequestDTO.TransactionId,
113112
OrderItems = cart.CartItems.Select(oc => new OrderedItem
@@ -127,9 +126,8 @@ public async Task<bool> CreateOrder(string token, OrderRequestDTO orderRequestDT
127126
}
128127
catch (Exception ex)
129128
{
130-
await Console.Out.WriteLineAsync(ex.Message);
131129
return false;
132-
throw;
130+
throw new Exception("An exception ocuured while placing the oredr :" + ex.Message);
133131

134132
}
135133
}

CozyCub/Services/Cart/CartServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public async Task<bool> IncreaseQuantity(string token, int ProductId)
196196
{
197197
try
198198
{
199-
int userId = _jwtservice.GetUserIdFromToken(token);
199+
int userId = _jwtservice.GetUserIdFromToken(token);
200200

201201
if (userId == 0)
202202
throw new Exception("A user with the current token is not found !");

CozyCub/appsettings.json

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

1515
//RazorPay api credentials
1616
"RazorPay": {
17-
"KeyId": "",
18-
"KeySecret": ""
17+
"KeyId": "rzp_test_6kz7oJuJLv4LHj",
18+
"KeySecret": "SKpzkTDZx5rw9gI3ByYlhqKu"
1919
},
2020

2121
//hosturl for getting the images

CozyCub_unit_Test/CozyCub_unit_Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<ItemGroup>
1313
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
1414
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
15+
<PackageReference Include="Razorpay" Version="3.1.1" />
1516
<PackageReference Include="xunit" Version="2.4.2" />
1617
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
1718
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

0 commit comments

Comments
 (0)