1
0

add i18n support for CoopShareTx (#168)

Co-authored-by: Michael Hoennig <michael@hoennig.de>
Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/168
Reviewed-by: Timotheus Pokorra <timotheus.pokorra@hostsharing.net>
This commit is contained in:
Michael Hoennig
2025-04-01 12:18:36 +02:00
parent 413ca0917e
commit 4f00d1b920
6 changed files with 64 additions and 22 deletions
@@ -1,4 +1,4 @@
package net.hostsharing.hsadminng.hs.hosting.asset;
package net.hostsharing.hsadminng.hs.office.coopassets;
import net.hostsharing.hsadminng.config.MessageTranslator;
import net.hostsharing.hsadminng.config.RetroactiveTranslator;
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Service;
// HOWTO translate messages which got created without i18n support, in this case in a PostgreSQL constraint trigger
@Service
public class HsHostingAssetTranslations implements RetroactiveTranslator {
public class HsCoopAssetTranslations implements RetroactiveTranslator {
public static final String ERROR_400_PREFIX = "ERROR: [400] ";
@@ -0,0 +1,26 @@
package net.hostsharing.hsadminng.hs.office.coopshares;
import net.hostsharing.hsadminng.config.MessageTranslator;
import net.hostsharing.hsadminng.config.RetroactiveTranslator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
// HOWTO translate messages which got created without i18n support, in this case in a PostgreSQL constraint trigger
@Service
public class HsCoopShareTranslations implements RetroactiveTranslator {
public static final String ERROR_400_PREFIX = "ERROR: [400] ";
@Autowired
private MessageTranslator messageTranslator;
@Override
public boolean canTranslate(final String message) {
return message.equals("ERROR: [400] coop shares transaction would result in a negative number of shares");
}
@Override
public String translate(final String message) {
return ERROR_400_PREFIX + messageTranslator.translate(message.substring(ERROR_400_PREFIX.length()));
}
}
@@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.coopshares;
import io.micrometer.core.annotation.Timed;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import net.hostsharing.hsadminng.config.MessageTranslator;
import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.errors.MultiValidationException;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeCoopSharesApi;
@@ -37,6 +38,9 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
@Autowired
private StrictMapper mapper;
@Autowired
private MessageTranslator messageTranslator;
@Autowired
private HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
@@ -118,32 +122,31 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
MultiValidationException.throwIfNotEmpty(violations);
}
private static void validateSubscriptionTransaction(
private void validateSubscriptionTransaction(
final HsOfficeCoopSharesTransactionInsertResource requestBody,
final ArrayList<String> violations) {
if (requestBody.getTransactionType() == SUBSCRIPTION
&& requestBody.getShareCount() < 0) {
violations.add("for %s, shareCount must be positive but is \"%d\"".formatted(
violations.add(messageTranslator.translate("for transactionType={0}, shareCount must be positive but is {1}",
requestBody.getTransactionType(), requestBody.getShareCount()));
}
}
private static void validateCancellationTransaction(
private void validateCancellationTransaction(
final HsOfficeCoopSharesTransactionInsertResource requestBody,
final ArrayList<String> violations) {
if (requestBody.getTransactionType() == CANCELLATION
&& requestBody.getShareCount() > 0) {
violations.add("for %s, shareCount must be negative but is \"%d\"".formatted(
violations.add(messageTranslator.translate("for transactionType={0}, shareCount must be negative but is {1}",
requestBody.getTransactionType(), requestBody.getShareCount()));
}
}
private static void validateshareCount(
private void validateshareCount(
final HsOfficeCoopSharesTransactionInsertResource requestBody,
final ArrayList<String> violations) {
if (requestBody.getShareCount() == 0) {
violations.add("shareCount must not be 0 but is \"%d\"".formatted(
requestBody.getShareCount()));
violations.add(messageTranslator.translate("shareCount must not be 0"));
}
}