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(api): add search term recommend #327

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,23 @@
package com.depromeet.domains.recommend.search.controller;

import com.depromeet.domains.recommend.search.service.SearchRecommendService;
import com.depromeet.domains.user.dto.ResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/search-term/recommend")
@RequiredArgsConstructor
public class SearchRecommendController {

private final SearchRecommendService searchRecommendService;
@PostMapping
public ResponseEntity<Void> addRecommendSearchTerm() {
searchRecommendService.addRecommendSearchTerm();
return ResponseDto.noContent();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.domains.recommend.search.repository;

import com.depromeet.recommend.search.SearchRecommendTerm;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SearchRecommendTermRepository extends JpaRepository<SearchRecommendTerm, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.depromeet.domains.recommend.search.service;

import com.depromeet.domains.recommend.search.repository.SearchRecommendTermRepository;
import com.depromeet.recommend.search.DescriptionTextVo;
import com.depromeet.recommend.search.SearchRecommendTerm;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SearchRecommendService {
private final SearchRecommendTermRepository searchRecommendTermRepository;

public void addRecommendSearchTerm() {
var title = "검색 추천어";
var description = List.of(new DescriptionTextVo("검색 추천어 설명", "#000000"));
var terms = Arrays.asList("검색 추천어1", "검색 추천어2", "검색 추천어3");
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
SearchRecommendTerm searchRecommendTerm = new SearchRecommendTerm(title, description, terms);
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
searchRecommendTermRepository.save(searchRecommendTerm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.depromeet.domains.recommend.controller;

import com.depromeet.common.dto.ResponseDto;
import com.depromeet.domains.recommend.dto.response.SearchTermRecommendResponseDto;
import com.depromeet.domains.recommend.service.SearchRecommendService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/search-term/recommend")
@RequiredArgsConstructor
@Tag(name = "SearchRecommend", description = "Search Recommend API")
public class SearchRecommendController {
private final SearchRecommendService searchRecommendService;

@Operation(summary = "Recommend Search Term")
@GetMapping
public ResponseEntity<SearchTermRecommendResponseDto> recommendSearchTerm() {
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
var response = searchRecommendService.recommendSearchTerm();
return ResponseDto.ok(response);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.depromeet.domains.recommend.dto.response;


import com.depromeet.recommend.search.DescriptionTextVo;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@AllArgsConstructor
@Getter
public class SearchTermRecommendResponseDto {
private final List<DescriptionTextVo> description;
private final List<String> terms;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.depromeet.domains.recommend.repository;

import com.depromeet.recommend.search.SearchRecommendTerm;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.Optional;

public interface SearchRecommendTermRepository extends JpaRepository<SearchRecommendTerm, Long> {
@Query(value = "SELECT s FROM SearchRecommendTerm s ORDER BY RAND() LIMIT 1")
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
Optional<SearchRecommendTerm> getRandomSearchRecommendTerm();
}
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.depromeet.domains.recommend.service;

import com.depromeet.domains.recommend.dto.response.SearchTermRecommendResponseDto;
import com.depromeet.domains.recommend.repository.SearchRecommendTermRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SearchRecommendService {

private final SearchRecommendTermRepository searchRecommendTermRepository;

public SearchTermRecommendResponseDto recommendSearchTerm() {
var recommendTerm = searchRecommendTermRepository.getRandomSearchRecommendTerm().orElseThrow(
() -> new RuntimeException("검색 추천어가 존재하지 않습니다.")
);

var description = recommendTerm.getDescription();
var termList = recommendTerm.getTerms();
seonghun-dev marked this conversation as resolved.
Show resolved Hide resolved

return new SearchTermRecommendResponseDto(description, termList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE search_recommend_term (
search_recommend_term_id BIGINT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
description JSON NOT NULL,
terms JSON NOT NULL,
PRIMARY KEY (search_recommend_term_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
2 changes: 2 additions & 0 deletions backend/streetdrop-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.hibernate:hibernate-spatial:6.2.2.Final'
implementation 'org.locationtech.jts:jts-core'
implementation 'io.hypersistence:hypersistence-utils-hibernate-60:3.5.1'
implementation 'com.fasterxml.jackson.module:jackson-module-jakarta-xmlbind-annotations:2.15.2'

implementation "com.querydsl:querydsl-jpa:5.0.0:jakarta"
annotationProcessor "com.querydsl:querydsl-apt:5.0.0:jakarta"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.depromeet.recommend.search;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@AllArgsConstructor
public class DescriptionTextVo {
private String text;

private String color;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.depromeet.recommend.search;

import io.hypersistence.utils.hibernate.type.json.JsonType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;

import java.util.List;

import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Getter
@NoArgsConstructor(access = PROTECTED)
@Entity
public class SearchRecommendTerm {

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "search_recommend_term_id")
private Long id;

@Column(nullable = false)
private String title;

@Column(nullable = false, columnDefinition = "json")
@Type(value = JsonType.class)
private List<DescriptionTextVo> description;

@Column(nullable = false, columnDefinition = "json")
@Type(value = JsonType.class)
siyeonSon marked this conversation as resolved.
Show resolved Hide resolved
private List<String> terms;

@Column(nullable = false)
private boolean active;

public SearchRecommendTerm(String title, List<DescriptionTextVo> description, List<String> terms) {
this.title = title;
this.description = description;
this.terms = terms;
this.active = true;
}
}