Skip to content

Commit 4fb5df7

Browse files
committed
Add address and secondary contact fields to user model and enhance address parsing logic
1 parent fd0949d commit 4fb5df7

2 files changed

Lines changed: 142 additions & 0 deletions

File tree

Controllers/UsersController.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,35 @@ private object UserToResponse(FlowModels.User user)
9595
lastName = user.LastName,
9696
middleName = user.MiddleName,
9797
contactNumber = user.ContactNumber,
98+
secondaryContactNumber = user.SecondaryContactNumber,
9899
birthDate = user.BirthDate,
99100
email = user.Email,
101+
address = user.Address != null ? new
102+
{
103+
region = user.Address.Region,
104+
regionCode = user.Address.RegionCode,
105+
province = user.Address.Province,
106+
provinceCode = user.Address.ProvinceCode,
107+
cityMunicipality = user.Address.CityMunicipality,
108+
cityMunicipalityCode = user.Address.CityMunicipalityCode,
109+
barangay = user.Address.Barangay,
110+
barangayCode = user.Address.BarangayCode,
111+
streetAddress = user.Address.StreetAddress,
112+
zipCode = user.Address.ZipCode
113+
} : null,
114+
secondaryAddress = user.SecondaryAddress != null ? new
115+
{
116+
region = user.SecondaryAddress.Region,
117+
regionCode = user.SecondaryAddress.RegionCode,
118+
province = user.SecondaryAddress.Province,
119+
provinceCode = user.SecondaryAddress.ProvinceCode,
120+
cityMunicipality = user.SecondaryAddress.CityMunicipality,
121+
cityMunicipalityCode = user.SecondaryAddress.CityMunicipalityCode,
122+
barangay = user.SecondaryAddress.Barangay,
123+
barangayCode = user.SecondaryAddress.BarangayCode,
124+
streetAddress = user.SecondaryAddress.StreetAddress,
125+
zipCode = user.SecondaryAddress.ZipCode
126+
} : null,
100127
userIMG = BytesToDataUrl(user.UserIMG),
101128
createdAt = user.CreatedAt
102129
};
@@ -128,6 +155,58 @@ private static bool IsNullValue(object? value)
128155
return false;
129156
}
130157

158+
// Helper: Parse Address from JsonElement or dictionary
159+
private static FlowModels.Address? ParseAddressFromValue(object? value)
160+
{
161+
if (value == null) return null;
162+
163+
try
164+
{
165+
if (value is JsonElement jsonElement)
166+
{
167+
if (jsonElement.ValueKind != JsonValueKind.Object) return null;
168+
169+
return new FlowModels.Address
170+
{
171+
Region = jsonElement.TryGetProperty("region", out var region) ? region.GetString() ?? string.Empty : string.Empty,
172+
RegionCode = jsonElement.TryGetProperty("regionCode", out var regionCode) ? regionCode.GetString() ?? string.Empty : string.Empty,
173+
Province = jsonElement.TryGetProperty("province", out var province) ? province.GetString() ?? string.Empty : string.Empty,
174+
ProvinceCode = jsonElement.TryGetProperty("provinceCode", out var provinceCode) ? provinceCode.GetString() ?? string.Empty : string.Empty,
175+
CityMunicipality = jsonElement.TryGetProperty("cityMunicipality", out var cityMunicipality) ? cityMunicipality.GetString() ?? string.Empty : string.Empty,
176+
CityMunicipalityCode = jsonElement.TryGetProperty("cityMunicipalityCode", out var cityMunicipalityCode) ? cityMunicipalityCode.GetString() ?? string.Empty : string.Empty,
177+
Barangay = jsonElement.TryGetProperty("barangay", out var barangay) ? barangay.GetString() ?? string.Empty : string.Empty,
178+
BarangayCode = jsonElement.TryGetProperty("barangayCode", out var barangayCode) ? barangayCode.GetString() ?? string.Empty : string.Empty,
179+
StreetAddress = jsonElement.TryGetProperty("streetAddress", out var streetAddress) ? streetAddress.GetString() ?? string.Empty : string.Empty,
180+
ZipCode = jsonElement.TryGetProperty("zipCode", out var zipCode) ? zipCode.GetString() ?? string.Empty : string.Empty
181+
};
182+
}
183+
184+
// Handle Dictionary<string, object> case
185+
if (value is Dictionary<string, object> dict)
186+
{
187+
return new FlowModels.Address
188+
{
189+
Region = dict.TryGetValue("region", out var region) ? GetStringValue(region) ?? string.Empty : string.Empty,
190+
RegionCode = dict.TryGetValue("regionCode", out var regionCode) ? GetStringValue(regionCode) ?? string.Empty : string.Empty,
191+
Province = dict.TryGetValue("province", out var province) ? GetStringValue(province) ?? string.Empty : string.Empty,
192+
ProvinceCode = dict.TryGetValue("provinceCode", out var provinceCode) ? GetStringValue(provinceCode) ?? string.Empty : string.Empty,
193+
CityMunicipality = dict.TryGetValue("cityMunicipality", out var cityMunicipality) ? GetStringValue(cityMunicipality) ?? string.Empty : string.Empty,
194+
CityMunicipalityCode = dict.TryGetValue("cityMunicipalityCode", out var cityMunicipalityCode) ? GetStringValue(cityMunicipalityCode) ?? string.Empty : string.Empty,
195+
Barangay = dict.TryGetValue("barangay", out var barangay) ? GetStringValue(barangay) ?? string.Empty : string.Empty,
196+
BarangayCode = dict.TryGetValue("barangayCode", out var barangayCode) ? GetStringValue(barangayCode) ?? string.Empty : string.Empty,
197+
StreetAddress = dict.TryGetValue("streetAddress", out var streetAddress) ? GetStringValue(streetAddress) ?? string.Empty : string.Empty,
198+
ZipCode = dict.TryGetValue("zipCode", out var zipCode) ? GetStringValue(zipCode) ?? string.Empty : string.Empty
199+
};
200+
}
201+
202+
return null;
203+
}
204+
catch
205+
{
206+
return null;
207+
}
208+
}
209+
131210
// Returns all users (passwords stripped) for assignment dropdowns
132211
[HttpGet]
133212
public IActionResult GetAll()
@@ -251,6 +330,46 @@ public IActionResult Patch(string id, [FromBody] Dictionary<string, object> upda
251330
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.Password, hashed));
252331
}
253332
break;
333+
case "secondarycontactnumber":
334+
case "secondary_contact_number":
335+
if (IsNullValue(value))
336+
{
337+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.SecondaryContactNumber, (string?)null));
338+
}
339+
else if (!string.IsNullOrWhiteSpace(stringValue))
340+
{
341+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.SecondaryContactNumber, stringValue));
342+
}
343+
break;
344+
case "address":
345+
if (IsNullValue(value))
346+
{
347+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.Address, (FlowModels.Address?)null));
348+
}
349+
else
350+
{
351+
var address = ParseAddressFromValue(value);
352+
if (address != null)
353+
{
354+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.Address, address));
355+
}
356+
}
357+
break;
358+
case "secondaryaddress":
359+
case "secondary_address":
360+
if (IsNullValue(value))
361+
{
362+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.SecondaryAddress, (FlowModels.Address?)null));
363+
}
364+
else
365+
{
366+
var secondaryAddress = ParseAddressFromValue(value);
367+
if (secondaryAddress != null)
368+
{
369+
updateDefs.Add(Builders<FlowModels.User>.Update.Set(u => u.SecondaryAddress, secondaryAddress));
370+
}
371+
}
372+
break;
254373
// intentionally skip Id and CreatedAt updates
255374
case "id":
256375
case "createdat":

Models/FlowboardModel.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ namespace Flowboard_Project_Management_System_Backend.Models
1212
/// </summary>
1313
public static class FlowboardModel
1414
{
15+
/// <summary>
16+
/// Philippine Standard Geographic Code (PSGC) based address model
17+
/// </summary>
18+
public class Address
19+
{
20+
public string Region { get; set; } = string.Empty;
21+
public string RegionCode { get; set; } = string.Empty;
22+
public string Province { get; set; } = string.Empty;
23+
public string ProvinceCode { get; set; } = string.Empty;
24+
public string CityMunicipality { get; set; } = string.Empty;
25+
public string CityMunicipalityCode { get; set; } = string.Empty;
26+
public string Barangay { get; set; } = string.Empty;
27+
public string BarangayCode { get; set; } = string.Empty;
28+
public string StreetAddress { get; set; } = string.Empty;
29+
public string ZipCode { get; set; } = string.Empty;
30+
}
31+
1532
public class User
1633
{
1734
[BsonId]
@@ -48,6 +65,12 @@ public class User
4865
[Required]
4966
public string Email { get; set; } = string.Empty;
5067

68+
public Address? Address { get; set; }
69+
70+
public Address? SecondaryAddress { get; set; }
71+
72+
public string? SecondaryContactNumber { get; set; }
73+
5174
[BsonRequired]
5275
[Required]
5376
public string Password { get; set; } = string.Empty;

0 commit comments

Comments
 (0)