From 65baf5066433d6a52e30fbd6e7b32c4741ec0d55 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 17 Oct 2024 06:17:31 +0300 Subject: `User::getLanguage` --- src/main/java/com/juick/model/User.java | 9 +++++ src/main/java/com/juick/service/UserService.java | 3 +- .../java/com/juick/service/UserServiceImpl.java | 39 ++++++++++++---------- .../db/migration/V1.51__cast_users_lang.sql | 2 ++ .../java/com/juick/server/tests/ServerTests.java | 8 +++-- 5 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 src/main/resources/db/migration/V1.51__cast_users_lang.sql (limited to 'src') diff --git a/src/main/java/com/juick/model/User.java b/src/main/java/com/juick/model/User.java index 7b0b7099..dd0feda1 100644 --- a/src/main/java/com/juick/model/User.java +++ b/src/main/java/com/juick/model/User.java @@ -64,6 +64,7 @@ public class User implements Serializable { private boolean premium; @XmlTransient private boolean admin; + private String language; public User() { tokens = new ArrayList<>(); @@ -298,4 +299,12 @@ public class User implements Serializable { public void setAdmin(boolean admin) { this.admin = admin; } + + public String getLanguage() { + return language; + } + + public void setLanguage(String language) { + this.language = language; + } } diff --git a/src/main/java/com/juick/service/UserService.java b/src/main/java/com/juick/service/UserService.java index 3c7a3235..845028f7 100644 --- a/src/main/java/com/juick/service/UserService.java +++ b/src/main/java/com/juick/service/UserService.java @@ -32,7 +32,6 @@ import org.springframework.cache.annotation.Cacheable; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Optional; /** @@ -83,6 +82,8 @@ public interface UserService { boolean updateUserInfo(User info); + boolean updateLanguage(User user); + boolean isInWL(int uid, int check); boolean isInBL(int uid, int check); diff --git a/src/main/java/com/juick/service/UserServiceImpl.java b/src/main/java/com/juick/service/UserServiceImpl.java index a5ab8432..bad1378a 100644 --- a/src/main/java/com/juick/service/UserServiceImpl.java +++ b/src/main/java/com/juick/service/UserServiceImpl.java @@ -27,20 +27,13 @@ import org.apache.commons.lang3.tuple.Pair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; -import org.springframework.dao.DataAccessException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.EmptyResultDataAccessException; -import org.springframework.jdbc.UncategorizedSQLException; -import org.springframework.jdbc.core.PreparedStatementCallback; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.simple.SimpleJdbcInsert; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; -import java.lang.StackWalker.Option; import java.net.URI; import java.sql.*; import java.time.Instant; @@ -72,6 +65,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { user.setVerified(rs.getLong(6) > 0); user.setPremium(rs.getInt(7) > 0); user.setAdmin(adminUsers.contains(user.getName())); + user.setLanguage(rs.getString("lang")); return user; } } @@ -126,7 +120,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public Optional getUserByUID(final int uid) { var list = getJdbcTemplate().query(""" SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, - COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium + COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang FROM users u LEFT JOIN facebook f ON f.user_id = u.id LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id LEFT JOIN emails e ON e.user_id = u.id WHERE u.id = ?""", userMapper, uid); @@ -140,7 +134,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { if (StringUtils.isNotBlank(username)) { var list = getJdbcTemplate().query(""" SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, - COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium + COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang FROM users u LEFT JOIN facebook f ON f.user_id = u.id LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id LEFT JOIN emails e ON e.user_id = u.id @@ -165,7 +159,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { try { List list = getJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, " + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + @@ -191,7 +185,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { if (StringUtils.isNotBlank(jid)) { List list = getJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen," + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + @@ -213,7 +207,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { return getNamedParameterJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen," + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + @@ -230,7 +224,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { return getNamedParameterJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen," + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + @@ -265,7 +259,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { if (StringUtils.isNotBlank(hash)) { List list = getJdbcTemplate().query( "SELECT DISTINCT logins.user_id, u.nick, u.passw, u.banned, u.last_seen," + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM logins INNER JOIN users u ON logins.user_id = u.id " + "LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + @@ -303,7 +297,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { if (StringUtils.isNotBlank(username)) { List list = getJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen," + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id " + @@ -369,6 +363,15 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { } } + @Transactional + @Override + public boolean updateLanguage(final User user) { + return getJdbcTemplate().update( + "UPDATE users SET lang=? WHERE id=?", + user.getLanguage(), + user.getUid()) > 0; + } + @Transactional(readOnly = true) @Override public boolean isInWL(final int uid, final int check) { @@ -759,7 +762,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { List list = getJdbcTemplate().query( """ SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, - COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium + COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang FROM users u LEFT JOIN facebook f ON f.user_id = u.id LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id LEFT JOIN emails e ON e.user_id = u.id @@ -792,7 +795,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public Optional getUserByFacebookId(long facebookId) { List list = getJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, " + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id WHERE f.fb_id = ?", new UserMapper(), facebookId); @@ -819,7 +822,7 @@ public class UserServiceImpl extends BaseJdbcService implements UserService { public Optional getUserByVKID(long vkID) { List list = getJdbcTemplate().query( "SELECT DISTINCT u.id, u.nick, u.passw, u.banned, u.last_seen, " + - "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium " + + "COALESCE(f.fb_id, vk.vk_id, t.tg_id, e.user_id, 0) AS verified, premium, lang " + "FROM users u LEFT JOIN facebook f ON f.user_id = u.id " + "LEFT JOIN vk ON u.id = vk.user_id LEFT JOIN telegram t ON u.id = t.user_id " + "LEFT JOIN emails e ON e.user_id = u.id WHERE vk.vk_id = ?", new UserMapper(), vkID); diff --git a/src/main/resources/db/migration/V1.51__cast_users_lang.sql b/src/main/resources/db/migration/V1.51__cast_users_lang.sql new file mode 100644 index 00000000..bb65a157 --- /dev/null +++ b/src/main/resources/db/migration/V1.51__cast_users_lang.sql @@ -0,0 +1,2 @@ +DROP CAST IF EXISTS (varchar AS public.users_lang); +CREATE CAST (varchar AS public.users_lang) WITH INOUT AS IMPLICIT; diff --git a/src/test/java/com/juick/server/tests/ServerTests.java b/src/test/java/com/juick/server/tests/ServerTests.java index d0f4032a..eb467ad6 100644 --- a/src/test/java/com/juick/server/tests/ServerTests.java +++ b/src/test/java/com/juick/server/tests/ServerTests.java @@ -288,6 +288,10 @@ public class ServerTests { .orElseThrow(IllegalStateException::new); ugnich = makeUserVerified(ugnich); freefd = makeUserVerified(freefd); + assertThat(ugnich.getLanguage(), is("__")); + ugnich.setLanguage("en"); + userService.updateLanguage(ugnich); + assertThat(ugnich.getLanguage(), is("en")); webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); isSetUp = true; @@ -1524,8 +1528,8 @@ public class ServerTests { jsonMapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); String jsonUser = jsonMapper.writeValueAsString(yyy); Map user = JsonPath.read(jsonUser, "$"); - // only uid, name and uri - assertThat(user.keySet().size(), is(3)); + // only uid, name, lang and uri + assertThat(user.keySet().size(), is(4)); JAXBContext context = JAXBContext.newInstance(User.class); Marshaller m = context.createMarshaller(); -- cgit v1.2.3