Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 유저 태그관련 기능 #59

Merged
merged 5 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
public interface TagRepository {
List<Tag> findAll();

void save(Tag tag);
Tag save(Tag tag);

List<Tag> findByRecommendedStatus(RecommendedStatus recommendedStatus);

List<Tag> findByNames(List<String> tags);
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions src/main/java/com/catcher/core/database/UserTagRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.catcher.core.database;

import com.catcher.core.domain.entity.UserTag;

import java.util.List;

public interface UserTagRepository {
void deleteByUserId(Long userId);

void saveAll(List<UserTag> userTags);

List<UserTag> findByUserId(Long userId);
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions src/main/java/com/catcher/core/domain/entity/UserTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.catcher.core.domain.entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Table(name = "user_tag")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder
@Getter
public class UserTag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "tag_id", unique = false)
private Tag tag;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", unique = false)
private User user;

public static UserTag createUserTag(User user, Tag tag) {
return UserTag.builder()
.user(user)
.tag(tag)
.build();
}

}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
12 changes: 12 additions & 0 deletions src/main/java/com/catcher/core/dto/request/UserTagRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.catcher.core.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@NoArgsConstructor
@Getter
public class UserTagRequest {
private List<String> tags;
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.catcher.core.dto.response;

import com.catcher.core.domain.entity.Tag;
import com.catcher.core.domain.entity.UserTag;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class UserTagResponse {
private List<String> tags;

public UserTagResponse(List<UserTag> userTags) {
this.tags = userTags.stream()
.map(UserTag::getTag)
.map(Tag::getName)
.toList();
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
65 changes: 65 additions & 0 deletions src/main/java/com/catcher/core/service/UserTagService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.catcher.core.service;

import com.catcher.core.database.TagRepository;
import com.catcher.core.database.UserTagRepository;
import com.catcher.core.db.UserRepository;
import com.catcher.core.domain.entity.Tag;
import com.catcher.core.domain.entity.User;
import com.catcher.core.domain.entity.UserTag;
import com.catcher.core.domain.entity.enums.RecommendedStatus;
import com.catcher.core.dto.response.UserTagResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
@RequiredArgsConstructor
public class UserTagService {
private final TagRepository tagRepository;
private final UserTagRepository userTagRepository;
private final UserRepository userRepository;

@Transactional
public void updateTags(User user, List<String> requestTags) {
user = userRepository.findById(user.getId()).orElseThrow();
userTagRepository.deleteByUserId(user.getId());

List<Tag> foundTags = tagRepository.findByNames(requestTags);
Set<Tag> tagSet = new HashSet<>(foundTags);

requestTags.forEach(tagName -> {
if (!tagSet.contains(tagName)) {
Tag newTag = createTagAndSave(tagName);
tagSet.add(newTag);
}
});

List<UserTag> userTags = generateUserTags(user, tagSet);
userTagRepository.saveAll(userTags);
}

public UserTagResponse findTagsByUser(User user) {
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
List<UserTag> userTags = userTagRepository.findByUserId(user.getId());

return new UserTagResponse(userTags);
}

private List<UserTag> generateUserTags(User user, Set<Tag> tagSet) {
return tagSet.stream().map(
tag -> UserTag.createUserTag(user, tag)
).toList();
}

private Tag createTagAndSave(String tagName) {
Tag tag = Tag.builder()
.name(tagName)
.recommendedStatus(RecommendedStatus.DISABLED)
.build();

return tagRepository.save(tag);
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 7 additions & 2 deletions src/main/java/com/catcher/datasource/TagRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ public List<Tag> findAll() {
}

@Override
public void save(Tag tag) {
tagJpaRepository.save(tag);
public Tag save(Tag tag) {
return tagJpaRepository.save(tag);
}

@Override
public List<Tag> findByRecommendedStatus(RecommendedStatus recommendedStatus) {
return tagJpaRepository.findByRecommendedStatus(recommendedStatus);
}

@Override
public List<Tag> findByNames(List<String> tags) {
return tagJpaRepository.findByNameIn(tags);
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions src/main/java/com/catcher/datasource/UserTagRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.catcher.datasource;


import com.catcher.core.database.UserTagRepository;
import com.catcher.core.domain.entity.UserTag;
import com.catcher.datasource.repository.UserTagJpaRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class UserTagRepositoryImpl implements UserTagRepository {
private final UserTagJpaRepository userTagJpaRepository;

@Override
public void deleteByUserId(Long userId) {
userTagJpaRepository.deleteByUserId(userId);
}

@Override
public void saveAll(List<UserTag> userTags) {
userTagJpaRepository.saveAll(userTags);
}

@Override
public List<UserTag> findByUserId(Long userId) {
return userTagJpaRepository.findByUserIdFJTag(userId);
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@

public interface TagJpaRepository extends JpaRepository<Tag, Long> {
List<Tag> findByRecommendedStatus(RecommendedStatus recommendedStatus);

List<Tag> findByNameIn(List<String> tagNames);
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.catcher.datasource.repository;

import com.catcher.core.domain.entity.UserTag;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface UserTagJpaRepository extends JpaRepository<UserTag, Long> {
void deleteByUserId(Long userId);

@Query("select ut from UserTag ut " +
"join fetch ut.tag " +
"where ut.user.id = :userId")
List<UserTag> findByUserIdFJTag(@Param("userId") Long userId);
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
32 changes: 32 additions & 0 deletions src/main/java/com/catcher/resource/UserTagController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.catcher.resource;

import com.catcher.common.response.CommonResponse;
import com.catcher.core.domain.entity.User;
import com.catcher.core.dto.request.UserTagRequest;
import com.catcher.core.dto.response.UserTagResponse;
import com.catcher.core.service.UserTagService;
import com.catcher.security.annotation.CurrentUser;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/user-tag")
public class UserTagController {
private final UserTagService userTagService;

@PutMapping
public CommonResponse<Void> updateUserTags(
@RequestBody UserTagRequest userTagRequest,
@CurrentUser User user
) {
userTagService.updateTags(user, userTagRequest.getTags());

return CommonResponse.success();
}

@GetMapping
public CommonResponse<UserTagResponse> findUserTags(@CurrentUser User user) {
return CommonResponse.success(userTagService.findTagsByUser(user));
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Loading