Skip to content

Commit

Permalink
[feat] SearchKeyword 관련 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
wnd01jun committed Nov 13, 2024
1 parent f00df26 commit d78df08
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.bbteam.budgetbuddies.domain.searchkeyword.controller;

import com.bbteam.budgetbuddies.apiPayload.ApiResponse;
import com.bbteam.budgetbuddies.domain.searchkeyword.domain.SearchKeyword;
import com.bbteam.budgetbuddies.domain.searchkeyword.service.SearchKeywordService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/search-keyword")
public class SearchKeywordController {

private final SearchKeywordService searchKeywordService;

@PostMapping("")
public ApiResponse<SearchKeyword> saveKeyword(String keyword) {

return ApiResponse.onSuccess(searchKeywordService.saveKeyword(keyword));
}

@GetMapping("")
public ApiResponse<SearchKeyword> findOne(Long searchKeywordId) {
return ApiResponse.onSuccess(searchKeywordService.findOne(searchKeywordId));
}

@GetMapping("/all")
public ApiResponse<Page<SearchKeyword>> findAll(Pageable pageable) {
return ApiResponse.onSuccess(searchKeywordService.findAll(pageable));
}

@PutMapping("")
public ApiResponse<SearchKeyword> modifyOne(Long searchKeywordId, String newKeyword) {
return ApiResponse.onSuccess((searchKeywordService.modifyOne(searchKeywordId, newKeyword)));
}

@DeleteMapping("")
public ApiResponse<String> deleteOne(Long searchKeywordId) {
searchKeywordService.deleteOne(searchKeywordId);
return ApiResponse.onSuccess("OK");
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.bbteam.budgetbuddies.domain.searchkeyword.domain;

import com.bbteam.budgetbuddies.common.BaseEntity;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SearchKeyword extends BaseEntity {

private String keyword;

public void changeKeyword(String newKeyword) {
this.keyword = newKeyword;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.bbteam.budgetbuddies.domain.searchkeyword.repository;

import com.bbteam.budgetbuddies.domain.searchkeyword.domain.SearchKeyword;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SearchKeywordRepository extends JpaRepository<SearchKeyword, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.bbteam.budgetbuddies.domain.searchkeyword.service;

import com.bbteam.budgetbuddies.apiPayload.code.status.ErrorStatus;
import com.bbteam.budgetbuddies.apiPayload.exception.GeneralException;
import com.bbteam.budgetbuddies.domain.searchkeyword.domain.SearchKeyword;
import com.bbteam.budgetbuddies.domain.searchkeyword.repository.SearchKeywordRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class SearchKeywordService {

private final SearchKeywordRepository searchKeywordRepository;

public SearchKeyword saveKeyword(String keyword) {
SearchKeyword searchKeyword = SearchKeyword.builder().keyword(keyword).build();
searchKeywordRepository.save(searchKeyword);
return searchKeyword;
}

public SearchKeyword findOne(Long searchKeywordId) {
return searchKeywordRepository.findById(searchKeywordId).orElseThrow(() -> new GeneralException(ErrorStatus._SEARCH_KEYWORD_NOT_FOUND));
}

public Page<SearchKeyword> findAll(Pageable pageable) {
return searchKeywordRepository.findAll(pageable);
}

public SearchKeyword modifyOne(Long searchKeywordId, String keyword) {
SearchKeyword searchKeyword = searchKeywordRepository.findById(searchKeywordId).orElseThrow(() -> new GeneralException(ErrorStatus._SEARCH_KEYWORD_NOT_FOUND));
searchKeyword.changeKeyword(keyword);
return searchKeyword;
}

public void deleteOne(Long searchKeywordId) {
SearchKeyword searchKeyword = searchKeywordRepository.findById(searchKeywordId).orElseThrow(() -> new GeneralException(ErrorStatus._SEARCH_KEYWORD_NOT_FOUND));
searchKeywordRepository.delete(searchKeyword);
}
}

0 comments on commit d78df08

Please sign in to comment.