Skip to content

Commit

Permalink
резолвим сининимы тегов перед отправкой уведомлений
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Aug 16, 2024
1 parent 693710c commit 81cf165
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 37 deletions.
20 changes: 7 additions & 13 deletions src/main/java/ru/org/linux/user/UserTagDao.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2022 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -20,14 +20,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
Expand Down Expand Up @@ -111,12 +108,9 @@ public ImmutableList<String> getTags(int userId, boolean isFavorite) {
"user_tags.user_id=:user_id AND tags_values.id=user_tags.tag_id AND user_tags.is_favorite=:is_favorite " +
"ORDER BY value",
parameters,
new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
tags.add(rs.getString("value"));
}
}
rs -> {
tags.add(rs.getString("value"));
}
);

return tags.build();
Expand All @@ -129,7 +123,7 @@ public void processRow(ResultSet rs) throws SQLException {
* @param tags список фаворитных тегов
* @return список ID пользователей
*/
public List<Integer> getUserIdListByTags(int userId, List<String> tags) {
public List<Integer> getUserIdListByTags(int userId, List<Integer> tags) {
if (tags.isEmpty()) {
return ImmutableList.of();
}
Expand All @@ -139,12 +133,12 @@ public List<Integer> getUserIdListByTags(int userId, List<String> tags) {
parameters.addValue("user_id", userId);

return jdbcTemplate.queryForList(
"select distinct user_id from user_tags where tag_id in (select id from tags_values where value in ( :values )) "
"select distinct user_id from user_tags where tag_id in (:values) "
+ "AND is_favorite = true "
+ "AND user_id not in (" +
"select userid from ignore_list where ignored=:user_id union " +
"select :user_id union " +
"select user_id from user_tags where tag_id in (select id from tags_values where value in ( :values )) and is_favorite = false)",
"select user_id from user_tags where tag_id in (:values) and is_favorite = false)",
parameters,
Integer.class
);
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/ru/org/linux/tag/TagService.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2023 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -59,15 +59,15 @@ class TagService(tagDao: TagDao, elastic: ElasticClient, actorSystem: ActorSyste

def getTagIdOpt(tag: String): Option[Int] = tagDao.getTagId(tag)

def getTagIdOptWithSynonym(tag: String): Option[Int] = tagDao.getTagId(tag).orElse(tagDao.getTagSynonymId(tag))

/**
* Получение идентификационного номера тега по названию, либо создание нового тега.
*
* @param tagName название тега
* @return идентификационный номер тега
*/
def getOrCreateTag(tagName: String): Int = {
tagDao.getTagId(tagName).orElse(tagDao.getTagSynonymId(tagName)).getOrElse(tagDao.createTag(tagName))
}
def getOrCreateTag(tagName: String): Int = getTagIdOptWithSynonym(tagName).getOrElse(tagDao.createTag(tagName))

def getTagInfo(tag: String, skipZero: Boolean): Option[TagInfo] = {
if (TagName.isGoodTag(tag)) {
Expand Down
16 changes: 8 additions & 8 deletions src/main/scala/ru/org/linux/user/UserTagService.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2022 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -19,7 +19,7 @@ import org.springframework.stereotype.Service
import org.springframework.validation.{Errors, MapBindingResult}
import ru.org.linux.tag.{TagName, TagNotFoundException, TagService}

import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*

@Service
class UserTagService(userTagDao: UserTagDao, tagService: TagService) {
Expand Down Expand Up @@ -113,7 +113,9 @@ class UserTagService(userTagDao: UserTagDao, tagService: TagService) {
* @return список ID пользователей
*/
def getUserIdListByTags(userid: Int, tags: java.util.List[String]): java.util.List[Integer] = {
userTagDao.getUserIdListByTags(userid, tags)
val tagIds = tags.asScala.flatMap(tagService.getTagIdOptWithSynonym).distinct

userTagDao.getUserIdListByTags(userid, tagIds.map(Integer.valueOf).asJava)
}

/**
Expand All @@ -123,9 +125,7 @@ class UserTagService(userTagDao: UserTagDao, tagService: TagService) {
* @param tagName название тега
* @return true если у пользователя есть тег
*/
def hasFavoriteTag(user: User, tagName: String): Boolean = {
favoritesGet(user).contains(tagName)
}
def hasFavoriteTag(user: User, tagName: String): Boolean = favoritesGet(user).contains(tagName)

/**
* Проверяет, есть ли указанный игнорируемый тег у пользователя.
Expand All @@ -139,7 +139,7 @@ class UserTagService(userTagDao: UserTagDao, tagService: TagService) {
}

/**
* Добавление нескольких тегов из строки. разделённых запятой.
* Добавление нескольких тегов из строки, разделённых запятой.
*
* @param user объект пользователя
* @param tagsStr строка, содержащая разделённые запятой теги
Expand Down Expand Up @@ -173,7 +173,7 @@ class UserTagService(userTagDao: UserTagDao, tagService: TagService) {
def countIgnore(id: Int): Int = userTagDao.countIgnore(id)

/**
* преобразование ошибок в массив строк.
* Преобразование ошибок в массив строк.
*
* @param errors объект ошибок
* @return массив строк, содержащий описания ошибок
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/ru/org/linux/user/UserTagDaoIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2016 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -234,18 +234,18 @@ public void getTest() {
public void getUserIdListByTagsTest() {
prepareUserTags();
List<Integer> userIdList;
List<String> tags = new ArrayList<>();
tags.add("UserTagDaoIntegrationTest_tag1");
List<Integer> tags = new ArrayList<>();
tags.add(tag1Id);
userIdList = userTagDao.getUserIdListByTags(user1Id, tags);
assertEquals("Wrong count of user ID's.", 1, userIdList.size());

tags.add("UserTagDaoIntegrationTest_tag2");
tags.add(tag2Id);
userIdList = userTagDao.getUserIdListByTags(user1Id, tags);
assertEquals("Wrong count of user ID's.", 1, userIdList.size());

tags.clear();
userTagDao.deleteTag(user1Id, tag5Id, true);
tags.add("UserTagDaoIntegrationTest_tag5");
tags.add(tag5Id);
userIdList = userTagDao.getUserIdListByTags(user1Id, tags);
assertEquals("Wrong count of user ID's.", 1, userIdList.size());
}
Expand Down
15 changes: 8 additions & 7 deletions src/test/java/ru/org/linux/user/UserTagServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2023 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -58,6 +58,7 @@ public void resetMockObjects() throws Exception {
reset(userTagDao);
reset(tagService);
when(tagService.getTagId(eq("tag1"), anyBoolean())).thenReturn(2);
when(tagService.getTagIdOptWithSynonym(eq("tag1"))).thenReturn(Option.apply(2));
user = getUser(1);
}

Expand All @@ -73,7 +74,7 @@ private User getUser(int id) {

@Test
public void favoriteAddTest() throws TagNotFoundException {
when(tagDao.getTagId("tag1", false)).thenReturn(Option.apply((Object) 2));
when(tagDao.getTagId("tag1", false)).thenReturn(Option.apply(2));
userTagService.favoriteAdd(user, "tag1");
verify(userTagDao).addTag(eq(1), eq(2), eq(true));
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public void favoritesGetTest() {

List<String> actual = userTagService.favoritesGet(user);
Assert.assertEquals(etalon.size(), actual.size());
Assert.assertEquals(etalon.get(0), actual.get(0));
Assert.assertEquals(etalon.getFirst(), actual.getFirst());
}

@Test
Expand All @@ -117,20 +118,20 @@ public void ignoresGetTest() {

List<String> actual = userTagService.ignoresGet(user);
Assert.assertEquals(etalon.size(), actual.size());
Assert.assertEquals(etalon.get(0), actual.get(0));
Assert.assertEquals(etalon.getFirst(), actual.getFirst());
}

@Test
public void getUserIdListByTagsTest() {
List<Integer> etalon = new ArrayList<>();
etalon.add(123);
List<String> tags = new ArrayList<>();
etalon.add(123);
tags.add("tag1");
when(userTagDao.getUserIdListByTags(1, tags)).thenReturn(etalon);
when(userTagDao.getUserIdListByTags(1, List.of(2))).thenReturn(etalon);

List<Integer> actual = userTagService.getUserIdListByTags(user.getId(), tags);
Assert.assertEquals(etalon.size(), actual.size());
Assert.assertEquals(etalon.get(0), actual.get(0));
Assert.assertEquals(etalon.getFirst(), actual.getFirst());
}

@Test
Expand Down

0 comments on commit 81cf165

Please sign in to comment.