I am currently developing a web app and am using Spring as a Backend. I have some user data that is provided by one of our services and I need to display it in my frontend.
The subject of the code review is the part, where I process the data in my backend to create a simpler data type that I can directly use in the frontend. This avoids that I have to mess with the incoming data in my frontend app and can directly take the data I want to display from this data type.
This is my parser I wrote for this task. I tried some stuff that was relatively unknown to me before e.g. this return switch
thingy. Any feedback is greatly appreciated!
@Service
public class UserDataParser {
public ProfilePreviewData toProfilePreviewData(ExtendedUserData user) {
ProfilePreviewData profilePreviewData = new ProfilePreviewData();
profilePreviewData.setAccountId(user.getId());
profilePreviewData.setAccountName(user.getName());
profilePreviewData.setAccounts(getAccountPreviewData(user));
return profilePreviewData;
}
private List<AccountPreviewData> getAccountPreviewData(ExtendedUserData user) {
List<AccountPreviewData> accountsPreviewData = new ArrayList<>();
for (AccountData account : user.getAccounts()) {
if (!account.getPublish()) continue;
accountsPreviewData.add(getMinecraftAccountPreviewData(account));
}
for (ExternalAccountData externalAccount : user.getExternalAccounts()) {
if (!externalAccount.getPublish()) continue;
accountsPreviewData.add(getExternalAccountPreviewData(externalAccount));
}
return accountsPreviewData;
}
private AccountPreviewData getMinecraftAccountPreviewData(AccountData account) {
AccountPreviewData accountPreviewData = new AccountPreviewData();
accountPreviewData.setAccountType(AccountType.MINECRAFT);
accountPreviewData.setDisplayName(account.getName());
accountPreviewData.setUniqueIdentifier(account.getUuid().toString());
return accountPreviewData;
}
private AccountPreviewData getExternalAccountPreviewData(ExternalAccountData accountData) {
AccountPreviewData accountPreviewData = new AccountPreviewData();
AccountType type = getTypeOfExternalAccount(accountData.getTypeName());
accountPreviewData.setAccountType(type);
accountPreviewData.setDisplayName(accountData.getAccountId());
accountPreviewData.setUniqueIdentifier(accountData.getName());
return accountPreviewData;
}
private AccountType getTypeOfExternalAccount(String typeName) {
return switch (typeName) {
case "Discord" -> AccountType.DISCORD;
case "TeamSpeak" -> AccountType.TEAMSPEAK;
default -> AccountType.UNKNOWN;
};
}
}
```