Skip to content

Commit 2f4bb51

Browse files
committed
Standardize JSON property naming
1 parent 72f3ff2 commit 2f4bb51

File tree

5 files changed

+36
-34
lines changed

5 files changed

+36
-34
lines changed

OpenBioCardServer/Controllers/AdminController.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public async Task<IActionResult> CheckPermission()
3838

3939
if (!isValid || account == null)
4040
{
41-
return Unauthorized(new { error = "Invalid token" });
41+
return Unauthorized(new { Error = "Invalid token" });
4242
}
4343

4444
if (!await _authService.HasAdminPermissionAsync(account))
@@ -48,8 +48,8 @@ public async Task<IActionResult> CheckPermission()
4848

4949
return Ok(new
5050
{
51-
success = true,
52-
type = account.Type.ToString().ToLower()
51+
Success = true,
52+
Type = account.Type.ToString().ToLower()
5353
});
5454
}
5555

@@ -63,7 +63,7 @@ public async Task<ActionResult<UserListResponse>> GetUsers()
6363
var result = await ValidateAdminTokenAsync(token);
6464
if (!result.isValid)
6565
{
66-
return Unauthorized(new { error = "Invalid token or insufficient permissions" });
66+
return Unauthorized(new { Error = "Invalid token or insufficient permissions" });
6767
}
6868

6969
var users = await _context.Accounts
@@ -93,17 +93,17 @@ public async Task<ActionResult<TokenResponse>> CreateUser([FromBody] CreateUserR
9393

9494
if (!isValid || account == null)
9595
{
96-
return Unauthorized(new { error = "Invalid token or insufficient permissions" });
96+
return Unauthorized(new { Error = "Invalid token or insufficient permissions" });
9797
}
9898

9999
if (!Enum.TryParse<UserType>(request.Type, true, out var userType) || userType == UserType.Root)
100100
{
101-
return BadRequest(new { error = "Invalid user type" });
101+
return BadRequest(new { Error = "Invalid user type" });
102102
}
103103

104104
if (await _authService.UsernameExistsAsync(request.NewUsername))
105105
{
106-
return Conflict(new { error = "Username already exists" });
106+
return Conflict(new { Error = "Username already exists" });
107107
}
108108

109109
var newAccount = await _authService.CreateAccountAsync(request.NewUsername, request.Password, userType);
@@ -122,7 +122,7 @@ public async Task<ActionResult<TokenResponse>> CreateUser([FromBody] CreateUserR
122122
{
123123
await transaction.RollbackAsync();
124124
_logger.LogError(ex, "Error creating new user");
125-
return StatusCode(500, new { error = "User creation failed" });
125+
return StatusCode(500, new { Error = "User creation failed" });
126126
}
127127
}
128128

@@ -137,18 +137,18 @@ public async Task<IActionResult> DeleteUser(string username)
137137

138138
if (!isValid || adminAccount == null)
139139
{
140-
return Unauthorized(new { error = "Invalid token or insufficient permissions" });
140+
return Unauthorized(new { Error = "Invalid token or insufficient permissions" });
141141
}
142142

143143
if (adminAccount.UserName == username)
144144
{
145-
return BadRequest(new { error = "Cannot delete your own account" });
145+
return BadRequest(new { Error = "Cannot delete your own account" });
146146
}
147147

148148
var targetAccount = await _authService.FindAccountByUsernameAsync(username);
149149
if (targetAccount == null)
150150
{
151-
return NotFound(new { error = "User not found" });
151+
return NotFound(new { Error = "User not found" });
152152
}
153153

154154
if (targetAccount.Type == UserType.Root)
@@ -162,7 +162,7 @@ public async Task<IActionResult> DeleteUser(string username)
162162
_logger.LogInformation("Admin {AdminUser} deleted user: {TargetUser}",
163163
adminAccount.UserName, username);
164164

165-
return Ok(new { message = "User deleted successfully" });
165+
return Ok(new { Message = "User deleted successfully" });
166166
}
167167

168168
private async Task<(bool isValid, Account? account)> ValidateAdminTokenAsync(string? token)

OpenBioCardServer/Controllers/AuthController.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public async Task<ActionResult<TokenResponse>> SignUp([FromBody] SignUpRequest r
4444
string.IsNullOrWhiteSpace(request.Password) ||
4545
string.IsNullOrWhiteSpace(request.UserType))
4646
{
47-
return BadRequest(new { error = "Missing required fields" });
47+
return BadRequest(new { Error = "Missing required fields" });
4848
}
4949

5050
if (!Enum.TryParse<UserType>(request.UserType, true, out var userType))
5151
{
52-
return BadRequest(new { error = "Invalid user type" });
52+
return BadRequest(new { Error = "Invalid user type" });
5353
}
5454

5555
if (userType == UserType.Root)
@@ -59,7 +59,7 @@ public async Task<ActionResult<TokenResponse>> SignUp([FromBody] SignUpRequest r
5959

6060
if (await _authService.UsernameExistsAsync(request.Username))
6161
{
62-
return Conflict(new { error = "Username already exists" });
62+
return Conflict(new { Error = "Username already exists" });
6363
}
6464

6565
var account = await _authService.CreateAccountAsync(request.Username, request.Password, userType);
@@ -78,7 +78,7 @@ public async Task<ActionResult<TokenResponse>> SignUp([FromBody] SignUpRequest r
7878
{
7979
await transaction.RollbackAsync();
8080
_logger.LogError(ex, "Error during user registration");
81-
return StatusCode(500, new { error = "Account creation failed" });
81+
return StatusCode(500, new { Error = "Account creation failed" });
8282
}
8383
}
8484

@@ -93,7 +93,7 @@ public async Task<ActionResult<TokenResponse>> SignIn([FromBody] SignInRequest r
9393
var account = await _authService.FindAccountByUsernameAsync(request.Username);
9494
if (account == null || !await _authService.ValidatePasswordAsync(account, request.Password))
9595
{
96-
return Unauthorized(new { error = "Invalid username or password" });
96+
return Unauthorized(new { Error = "Invalid username or password" });
9797
}
9898

9999
var userToken = await _authService.CreateTokenAsync(account);
@@ -114,13 +114,13 @@ public async Task<IActionResult> DeleteAccount([FromBody] DeleteRequest request)
114114
var token = GetTokenFromHeader();
115115
if (string.IsNullOrEmpty(token))
116116
{
117-
return Unauthorized(new { error = "Missing authentication token" });
117+
return Unauthorized(new { Error = "Missing authentication token" });
118118
}
119119

120120
var (isValid, account) = await _authService.ValidateTokenAsync(token);
121121
if (!isValid || account == null || account.UserName != request.Username)
122122
{
123-
return Unauthorized(new { error = "Invalid token or token does not match username" });
123+
return Unauthorized(new { Error = "Invalid token or token does not match username" });
124124
}
125125

126126
if (account.Type == UserType.Root)
@@ -132,7 +132,7 @@ public async Task<IActionResult> DeleteAccount([FromBody] DeleteRequest request)
132132
await _authService.DeleteAccountAsync(account);
133133

134134
_logger.LogInformation("User deleted their account: {Username}", request.Username);
135-
return Ok(new { message = "Account deleted successfully" });
135+
return Ok(new { Message = "Account deleted successfully" });
136136
}
137137

138138
/// <summary>
@@ -144,7 +144,7 @@ public async Task<IActionResult> Logout()
144144
var token = GetTokenFromHeader();
145145
if (string.IsNullOrEmpty(token))
146146
{
147-
return Unauthorized(new { error = "Missing authentication token" });
147+
return Unauthorized(new { Error = "Missing authentication token" });
148148
}
149149

150150
var tokenEntity = await _context.Tokens
@@ -156,7 +156,7 @@ public async Task<IActionResult> Logout()
156156
await _context.SaveChangesAsync();
157157
}
158158

159-
return Ok(new { message = "Logged out successfully" });
159+
return Ok(new { Message = "Logged out successfully" });
160160
}
161161

162162
private string? GetTokenFromHeader() =>

OpenBioCardServer/Controllers/ProfileController.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ public async Task<ActionResult<ProfileDto>> GetProfile(string username)
6464

6565
if (profileDto == null)
6666
{
67-
return NotFound(new { error = "User not found" });
67+
return NotFound(new { Error = "User not found" });
6868
}
6969

7070
return profileDto;
7171
}
7272
catch (Exception ex)
7373
{
7474
_logger.LogError(ex, "Error retrieving profile for user: {Username}", username);
75-
return StatusCode(500, new { error = "Internal server error" });
75+
return StatusCode(500, new { Error = "Internal server error" });
7676
}
7777
}
7878

@@ -91,15 +91,15 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Profi
9191

9292
if (!isValid || account == null)
9393
{
94-
return Unauthorized(new { error = "Invalid token or token does not match username" });
94+
return Unauthorized(new { Error = "Invalid token or token does not match username" });
9595
}
9696

9797
var profile = await _context.Profiles
9898
.FirstOrDefaultAsync(p => p.Username == username);
9999

100100
if (profile == null)
101101
{
102-
return NotFound(new { error = "Profile not found" });
102+
return NotFound(new { Error = "Profile not found" });
103103
}
104104

105105
DataMapper.UpdateProfileEntity(profile, request);
@@ -115,13 +115,13 @@ public async Task<IActionResult> UpdateProfile(string username, [FromBody] Profi
115115
await _cacheService.RemoveAsync(GetProfileCacheKey(username));
116116

117117
_logger.LogInformation("Profile updated for user: {Username}", username);
118-
return Ok(new { success = true });
118+
return Ok(new { Success = true });
119119
}
120120
catch (Exception ex)
121121
{
122122
await transaction.RollbackAsync();
123123
_logger.LogError(ex, "Error updating profile for user: {Username}", username);
124-
return StatusCode(500, new { error = "Profile update failed" });
124+
return StatusCode(500, new { Error = "Profile update failed" });
125125
}
126126
}
127127

@@ -134,13 +134,13 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
134134
var token = GetTokenFromHeader();
135135
if (string.IsNullOrEmpty(token))
136136
{
137-
return Unauthorized(new { error = "Missing authentication token" });
137+
return Unauthorized(new { Error = "Missing authentication token" });
138138
}
139139

140140
var (isValid, account) = await _authService.ValidateTokenAsync(token);
141141
if (!isValid || account == null)
142142
{
143-
return Unauthorized(new { error = "Invalid token" });
143+
return Unauthorized(new { Error = "Invalid token" });
144144
}
145145

146146
var profile = await _context.Profiles
@@ -156,7 +156,7 @@ public async Task<ActionResult<ProfileDto>> GetMyProfile()
156156

157157
if (profile == null)
158158
{
159-
return NotFound(new { error = "Profile not found" });
159+
return NotFound(new { Error = "Profile not found" });
160160
}
161161

162162
return DataMapper.ToProfileDto(profile);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System.Text.Json.Serialization;
1+
using Newtonsoft.Json;
22

33
namespace OpenBioCardServer.Models.DTOs.Classic;
44

55
public class ClassicSystemSettingsResponse
66
{
7-
[JsonPropertyName("title")]
7+
[JsonProperty("title")]
88
public string Title { get; set; } = string.Empty;
99

10-
[JsonPropertyName("logo")]
10+
[JsonProperty("logo")]
1111
public string Logo { get; set; } = string.Empty;
1212
}

OpenBioCardServer/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using OpenBioCardServer.Utilities;
1313
using Microsoft.AspNetCore.ResponseCompression;
1414
using System.IO.Compression;
15+
using Newtonsoft.Json.Serialization;
1516
using OpenBioCardServer.Constants;
1617
using OpenBioCardServer.Interfaces;
1718
using OpenBioCardServer.Structs.ENums;
@@ -99,6 +100,7 @@ private static void RegisterServices(WebApplicationBuilder builder)
99100
.AddNewtonsoftJson(options =>
100101
{
101102
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
103+
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
102104
});
103105

104106
builder.Services.AddEndpointsApiExplorer();

0 commit comments

Comments
 (0)