Yuyuqt commited on
Commit
202c378
·
1 Parent(s): a83fc52

fix: top up function

Browse files
Backend/Features/Wallet/WalletController.cs CHANGED
@@ -1,3 +1,4 @@
 
1
  using Microsoft.AspNetCore.Authorization;
2
  using Microsoft.AspNetCore.Mvc;
3
  using System.Security.Claims;
@@ -5,7 +6,7 @@ using System.Security.Claims;
5
  namespace Backend.Features.Wallet
6
  {
7
  [ApiController]
8
- [Route("api/[controller]")]
9
  [Authorize]
10
  public class WalletController : ControllerBase
11
  {
@@ -37,28 +38,30 @@ namespace Backend.Features.Wallet
37
  }
38
 
39
  [HttpPost("topup")]
40
- [Authorize(Roles = "Librarian")]
41
  public async Task<IActionResult> TopUp([FromBody] TopUpRequest request)
 
42
  {
43
- var librarianIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
44
- if (string.IsNullOrEmpty(librarianIdStr)) return Unauthorized();
 
 
45
 
46
- var success = await _walletService.TopUpAsync(
47
- request.UserId,
48
- request.Amount,
49
- Guid.Parse(librarianIdStr),
50
- request.Description);
51
 
52
- if (!success) return BadRequest(new { message = "Failed to top up wallet." });
53
 
54
- return Ok(new { message = "Wallet topped up successfully." });
 
 
 
 
 
 
55
  }
56
  }
57
-
58
- public class TopUpRequest
59
- {
60
- public Guid UserId { get; set; }
61
- public decimal Amount { get; set; }
62
- public string? Description { get; set; }
63
- }
64
  }
 
1
+ using LibraryManagement.Shared.Models;
2
  using Microsoft.AspNetCore.Authorization;
3
  using Microsoft.AspNetCore.Mvc;
4
  using System.Security.Claims;
 
6
  namespace Backend.Features.Wallet
7
  {
8
  [ApiController]
9
+ [Route("api/wallet")]
10
  [Authorize]
11
  public class WalletController : ControllerBase
12
  {
 
38
  }
39
 
40
  [HttpPost("topup")]
41
+ [Authorize(Roles = "Librarian,Admin")]
42
  public async Task<IActionResult> TopUp([FromBody] TopUpRequest request)
43
+
44
  {
45
+ try
46
+ {
47
+ var librarianIdStr = User.FindFirstValue(ClaimTypes.NameIdentifier);
48
+ if (string.IsNullOrEmpty(librarianIdStr)) return Unauthorized();
49
 
50
+ var success = await _walletService.TopUpAsync(
51
+ request.UserId,
52
+ request.Amount,
53
+ Guid.Parse(librarianIdStr),
54
+ request.Description);
55
 
56
+ if (!success) return BadRequest(new { message = "User not found or database error." });
57
 
58
+ return Ok(new { message = "Wallet topped up successfully." });
59
+ }
60
+ catch (Exception ex)
61
+ {
62
+ // Return the actual error message to help debug (e.g., missing table error)
63
+ return StatusCode(500, new { message = ex.InnerException?.Message ?? ex.Message });
64
+ }
65
  }
66
  }
 
 
 
 
 
 
 
67
  }
BlazorWebAssembly/Pages/Members.razor CHANGED
@@ -3,7 +3,7 @@
3
  @using LibraryManagement.Shared.Models
4
  @using BlazorWebAssembly.Models
5
  @using Microsoft.AspNetCore.Authorization
6
- @attribute [Authorize(Roles = "Librarian")]
7
  @inject LibraryApiClient ApiClient
8
  @inject IJSRuntime JS
9
  @inject NavigationManager Navigation
@@ -646,14 +646,14 @@
646
 
647
  try
648
  {
649
- var success = await ApiClient.TopUpWalletAsync(new TopUpRequest
650
  {
651
  UserId = _selectedUser.User.Id,
652
  Amount = _topUpAmount,
653
  Description = "Librarian Cash Top-up"
654
  });
655
 
656
- if (success)
657
  {
658
  await JS.InvokeVoidAsync("alert", "Wallet topped up successfully!");
659
  _topUpAmount = 0;
@@ -661,7 +661,7 @@
661
  }
662
  else
663
  {
664
- await JS.InvokeVoidAsync("alert", "Failed to top up wallet.");
665
  }
666
  }
667
  catch (Exception ex)
@@ -671,3 +671,4 @@
671
  }
672
  }
673
 
 
 
3
  @using LibraryManagement.Shared.Models
4
  @using BlazorWebAssembly.Models
5
  @using Microsoft.AspNetCore.Authorization
6
+ @attribute [Authorize(Roles = "Librarian,Admin")]
7
  @inject LibraryApiClient ApiClient
8
  @inject IJSRuntime JS
9
  @inject NavigationManager Navigation
 
646
 
647
  try
648
  {
649
+ var result = await ApiClient.TopUpWalletAsync(new TopUpRequest
650
  {
651
  UserId = _selectedUser.User.Id,
652
  Amount = _topUpAmount,
653
  Description = "Librarian Cash Top-up"
654
  });
655
 
656
+ if (result.Success)
657
  {
658
  await JS.InvokeVoidAsync("alert", "Wallet topped up successfully!");
659
  _topUpAmount = 0;
 
661
  }
662
  else
663
  {
664
+ await JS.InvokeVoidAsync("alert", "Failed: " + result.Message);
665
  }
666
  }
667
  catch (Exception ex)
 
671
  }
672
  }
673
 
674
+
BlazorWebAssembly/Services/LibraryApiClient.cs CHANGED
@@ -203,14 +203,19 @@ namespace BlazorWebAssembly.Services
203
  catch { return Enumerable.Empty<WalletTransactionDto>(); }
204
  }
205
 
206
- public async Task<bool> TopUpWalletAsync(TopUpRequest request)
207
  {
208
  var response = await _httpClient.PostAsJsonAsync("api/wallet/topup", request);
209
- return response.IsSuccessStatusCode;
 
 
 
 
210
  }
211
  #endregion
212
 
213
 
 
214
  public async Task<LoyaltyAccountDto?> GetMyLoyaltyAccountAsync()
215
  {
216
  try
 
203
  catch { return Enumerable.Empty<WalletTransactionDto>(); }
204
  }
205
 
206
+ public async Task<(bool Success, string Message)> TopUpWalletAsync(TopUpRequest request)
207
  {
208
  var response = await _httpClient.PostAsJsonAsync("api/wallet/topup", request);
209
+ if (response.IsSuccessStatusCode) return (true, "Success");
210
+
211
+ var error = await response.Content.ReadFromJsonAsync<JsonDocument>();
212
+ var msg = error?.RootElement.TryGetProperty("message", out var m) == true ? m.GetString() : "Unknown error";
213
+ return (false, msg ?? "Failed to top up");
214
  }
215
  #endregion
216
 
217
 
218
+
219
  public async Task<LoyaltyAccountDto?> GetMyLoyaltyAccountAsync()
220
  {
221
  try
DbConnect/Data/AppDbContext.cs CHANGED
@@ -136,8 +136,10 @@ public partial class AppDbContext : DbContext
136
 
137
  modelBuilder.Entity<User>(entity =>
138
  {
 
139
  entity.HasIndex(e => e.Email, "UQ_Users_Email").IsUnique();
140
 
 
141
  entity.Property(e => e.Id).HasDefaultValueSql("gen_random_uuid()");
142
  entity.Property(e => e.Address).HasMaxLength(255);
143
  entity.Property(e => e.CreatedAt)
@@ -201,7 +203,9 @@ public partial class AppDbContext : DbContext
201
 
202
  modelBuilder.Entity<WalletTransaction>(entity =>
203
  {
 
204
  entity.HasKey(e => e.Id);
 
205
  entity.Property(e => e.Id).HasDefaultValueSql("gen_random_uuid()");
206
  entity.Property(e => e.Amount).HasColumnType("decimal(18, 2)");
207
  entity.Property(e => e.Type).HasMaxLength(20);
 
136
 
137
  modelBuilder.Entity<User>(entity =>
138
  {
139
+ entity.ToTable("Users");
140
  entity.HasIndex(e => e.Email, "UQ_Users_Email").IsUnique();
141
 
142
+
143
  entity.Property(e => e.Id).HasDefaultValueSql("gen_random_uuid()");
144
  entity.Property(e => e.Address).HasMaxLength(255);
145
  entity.Property(e => e.CreatedAt)
 
203
 
204
  modelBuilder.Entity<WalletTransaction>(entity =>
205
  {
206
+ entity.ToTable("WalletTransactions");
207
  entity.HasKey(e => e.Id);
208
+
209
  entity.Property(e => e.Id).HasDefaultValueSql("gen_random_uuid()");
210
  entity.Property(e => e.Amount).HasColumnType("decimal(18, 2)");
211
  entity.Property(e => e.Type).HasMaxLength(20);