Skip to content

Commit f4e34af

Browse files
committed
Add user type support to profile endpoints
1 parent 663042b commit f4e34af

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

OpenBioCardServer/Models/DTOs/Classic/ClassicProfile.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class ClassicProfile
77
[JsonProperty("username")]
88
public string Username { get; set; } = string.Empty;
99

10+
[JsonProperty("userType")]
11+
public string UserType { get; set; } = "personal";
12+
1013
[JsonProperty("name")]
1114
public string Name { get; set; } = string.Empty;
1215

@@ -57,4 +60,4 @@ public class ClassicProfile
5760

5861
[JsonProperty("gallery")]
5962
public List<ClassicGalleryItem> Gallery { get; set; } = new();
60-
}
63+
}

OpenBioCardServer/Models/DTOs/Classic/ClassicProfilePatch.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class ClassicProfilePatch
1111
[JsonProperty("username")]
1212
public string? Username { get; set; }
1313

14+
[JsonProperty("userType")]
15+
public string? UserType { get; set; }
16+
1417
[JsonProperty("name")]
1518
public string? Name { get; set; }
1619

@@ -44,10 +47,7 @@ public class ClassicProfilePatch
4447
[JsonProperty("currentSchoolLink")]
4548
public string? CurrentSchoolLink { get; set; }
4649

47-
// 集合类型:
48-
// Null = 不更新
49-
// Empty List [] = 清空列表
50-
50+
// Collections...
5151
[JsonProperty("contacts")]
5252
public List<ClassicContact>? Contacts { get; set; }
5353

OpenBioCardServer/Models/Enums/AccountType.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ namespace OpenBioCardServer.Models.Enums;
22

33
public enum AccountType
44
{
5+
Unknown,
56
Personal,
67
Company,
78
Organization,

OpenBioCardServer/Services/ClassicProfileService.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public ClassicProfileService(
3838
var profile = await _context.Profiles
3939
.AsNoTracking()
4040
.AsSplitQuery()
41+
.Include(p => p.Account)
4142
.Include(p => p.Contacts)
4243
.Include(p => p.SocialLinks)
4344
.Include(p => p.Projects)
@@ -102,6 +103,7 @@ public async Task<bool> UpdateProfileAsync(string username, ClassicProfile reque
102103
{
103104
var profile = await _context.Profiles
104105
.AsTracking()
106+
.Include(p => p.Account)
105107
.FirstOrDefaultAsync(p => p.AccountName == username && p.Language == null);
106108

107109
if (profile == null)
@@ -187,6 +189,7 @@ public async Task<bool> PatchProfileAsync(string username, ClassicProfilePatch p
187189
{
188190
var profile = await _context.Profiles
189191
.AsTracking()
192+
.Include(p => p.Account)
190193
.FirstOrDefaultAsync(p => p.AccountName == username && p.Language == null);
191194

192195
if (profile == null)

OpenBioCardServer/Utilities/Mappers/ClassicMapper.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public static ClassicProfile ToClassicProfile(ProfileEntity profile)
1414
return new ClassicProfile
1515
{
1616
Username = profile.AccountName,
17+
UserType = profile.Account != null ? ToUserTypeString(profile.Account.Type) : ToUserTypeString(AccountType.Personal),
18+
1719
Name = profile.Nickname ?? string.Empty,
1820
Pronouns = profile.Pronouns ?? string.Empty,
1921
Avatar = AssetToString(profile.AvatarType, profile.AvatarText, profile.AvatarData),
@@ -178,6 +180,12 @@ private static ClassicSocialLink ToClassicSocialLink(SocialLinkItemEntity s)
178180
public static void UpdateProfileFromClassic(ProfileEntity profile, ClassicProfile classic)
179181
{
180182
profile.AccountName = classic.Username;
183+
184+
if (profile.Account != null)
185+
{
186+
profile.Account.Type = ParseUserType(classic.UserType);
187+
}
188+
181189
profile.Nickname = classic.Name;
182190
profile.Pronouns = classic.Pronouns;
183191

@@ -367,6 +375,12 @@ public static void UpdateProfileFromPatch(ProfileEntity profile, ClassicProfileP
367375
// 注意:如果前端传空字符串 "",这里会更新为空字符串,符合预期(清空字段)
368376

369377
if (patch.Username != null) profile.AccountName = patch.Username;
378+
379+
if (patch.UserType != null && profile.Account != null)
380+
{
381+
profile.Account.Type = ParseUserType(patch.UserType);
382+
}
383+
370384
if (patch.Name != null) profile.Nickname = patch.Name;
371385
if (patch.Pronouns != null) profile.Pronouns = patch.Pronouns;
372386

@@ -405,4 +419,21 @@ public static void UpdateProfileFromPatch(ProfileEntity profile, ClassicProfileP
405419
if (patch.CurrentSchool != null) profile.CurrentSchool = patch.CurrentSchool;
406420
if (patch.CurrentSchoolLink != null) profile.CurrentSchoolLink = patch.CurrentSchoolLink;
407421
}
422+
423+
// === Helpers for Account Type ===
424+
private static string ToUserTypeString(AccountType type) => type switch
425+
{
426+
AccountType.Company => "company",
427+
AccountType.Organization => "organization",
428+
AccountType.Personal => "personal",
429+
_ => "personal" // Default Unknown or others to personal
430+
};
431+
432+
private static AccountType ParseUserType(string? type) => type?.ToLowerInvariant() switch
433+
{
434+
"company" => AccountType.Company,
435+
"organization" => AccountType.Organization,
436+
"personal" => AccountType.Personal,
437+
_ => AccountType.Personal // Default to Personal as requested
438+
};
408439
}

0 commit comments

Comments
 (0)