-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
8 changed files
with
116 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/com/hobak/happinessql/domain/activity/application/ActivitySearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.hobak.happinessql.domain.activity.application; | ||
|
||
import com.hobak.happinessql.domain.activity.converter.ActivityConverter; | ||
import com.hobak.happinessql.domain.activity.domain.Activity; | ||
import com.hobak.happinessql.domain.activity.domain.Category; | ||
import com.hobak.happinessql.domain.activity.dto.ActivityDto; | ||
import com.hobak.happinessql.domain.activity.dto.ActivitySearchResponseDto; | ||
import com.hobak.happinessql.domain.activity.dto.CategoryDto; | ||
import com.hobak.happinessql.domain.activity.repository.ActivityRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
@RequiredArgsConstructor | ||
public class ActivitySearchService { | ||
private final ActivityRepository activityRepository; | ||
|
||
public ActivitySearchResponseDto searchActivities(String keyword, Long userId) { | ||
List<Activity> activitiesByCategory = activityRepository.findByCategoryNameContaining(keyword); | ||
List<Activity> activitiesByName = activityRepository.findByNameContaining(keyword); | ||
|
||
Map<Long, CategoryDto> categoryDtoMap = new HashMap<>(); | ||
|
||
for (Activity activity : activitiesByCategory) { | ||
Category category = activity.getCategory(); | ||
if(category.getUserId() == null || category.getUserId().equals(userId)){ | ||
CategoryDto categoryDto = categoryDtoMap.getOrDefault(category.getCategoryId(), ActivityConverter.toCategoryDto(category)); | ||
categoryDto.getActivities().add(ActivityConverter.toActivityDto(activity)); | ||
categoryDtoMap.put(category.getCategoryId(), categoryDto); | ||
} | ||
} | ||
|
||
for (Activity activity : activitiesByName) { | ||
Category category = activity.getCategory(); | ||
if(categoryDtoMap.containsKey(category.getCategoryId())){ | ||
continue; | ||
} | ||
if(category.getUserId() == null || category.getUserId().equals(userId)){ | ||
CategoryDto categoryDto = categoryDtoMap.getOrDefault(category.getCategoryId(), ActivityConverter.toCategoryDto(category)); | ||
ActivityDto activityDto = ActivityConverter.toActivityDto(activity); | ||
if (!categoryDto.getActivities().contains(activityDto)) { | ||
categoryDto.getActivities().add(activityDto); | ||
} | ||
categoryDtoMap.put(category.getCategoryId(), categoryDto); | ||
} | ||
} | ||
|
||
return ActivityConverter | ||
.toActivitySearchResponseDto(new ArrayList<>(categoryDtoMap.values())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/main/java/com/hobak/happinessql/domain/activity/dto/ActivitySearchRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.hobak.happinessql.domain.activity.dto; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ActivitySearchRequestDto { | ||
private String search; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/hobak/happinessql/domain/activity/dto/ActivitySearchResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.hobak.happinessql.domain.activity.dto; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ActivitySearchResponseDto { | ||
private List<CategoryDto> categories; | ||
|
||
@Builder | ||
public ActivitySearchResponseDto(List<CategoryDto> categories) { | ||
this.categories = categories; | ||
} | ||
|
||
} |
5 changes: 0 additions & 5 deletions
5
src/main/java/com/hobak/happinessql/domain/activity/dto/ActivityUpdateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
package com.hobak.happinessql.domain.activity.dto; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class ActivityUpdateRequestDto { | ||
String name; | ||
@Builder | ||
ActivityUpdateRequestDto(String name){ | ||
this.name = name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters