Skip to content

Commit

Permalink
Merge pull request #48 from TogetherWithOcean-TWO/SCRUM-122-recommend
Browse files Browse the repository at this point in the history
일주일마다 장소 추천 로직 및 추천 장소 조회
  • Loading branch information
KSJ0128 authored Jul 28, 2024
2 parents 31ff0d9 + bfc5a5b commit dbb0e64
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,26 @@
package com.togetherwithocean.TWO.Recommend.Controller;

import com.togetherwithocean.TWO.Recommend.DTO.RecommendPlaceDTO;
import com.togetherwithocean.TWO.Recommend.Service.RecommendService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequiredArgsConstructor
@RequestMapping("/recommend")
public class RecommendController {
private final RecommendService recommendService;
@GetMapping("")
public ResponseEntity<RecommendPlaceDTO> getRecommendPlace(Authentication principal) {
if (principal == null)
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
RecommendPlaceDTO recommendPlace = recommendService.getRecommendPlace();
return ResponseEntity.status(HttpStatus.OK).body(recommendPlace);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.togetherwithocean.TWO.Recommend.DTO;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class RecommendPlaceDTO {
String name;
Double latitude;
Double longtitude;
String direction;

@Builder
public RecommendPlaceDTO(String name, Double latitude, Double longtitude, String direction) {
this.name = name;
this.latitude = latitude;
this.longtitude = longtitude;
this.direction = direction;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

import org.springframework.data.jpa.repository.JpaRepository;
import com.togetherwithocean.TWO.Recommend.Domain.Recommend;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface RecommendRepository extends JpaRepository<Recommend, Long> {
Recommend findRecommendByName(String name);
@Query("SELECT COUNT(*) FROM Recommend WHERE recs=true")
Long getRecommendedCount();
@Query("SELECT r FROM Recommend r WHERE r.direction = :direction AND r.recs = false ORDER BY r.recNumber LIMIT 1")
Recommend getRecommendByDir(@Param("direction") String direction);


@Query("UPDATE Recommend r SET r.recs = false WHERE r.direction = :direction")
void updateRecsFalseByDirection(@Param("direction") String direction);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.togetherwithocean.TWO.Recommend.Service;

import com.togetherwithocean.TWO.Member.Domain.Member;
import com.togetherwithocean.TWO.Recommend.DTO.RecommendPlaceDTO;
import com.togetherwithocean.TWO.Recommend.Domain.Recommend;
import com.togetherwithocean.TWO.Recommend.Repository.RecommendRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
@RequiredArgsConstructor
public class RecommendService {
private final RecommendRepository recommendRepository;

String findNowDirection() {
Long recCount = recommendRepository.getRecommendedCount(); // 지금까지 추천한 장소의 개수

// 추천한 장소 개수를 기반으로 이번에 추천할 장소의 위치 결정
String direction = "";
if (recCount % 4 == 0)
direction = "동해";
else if (recCount % 4 == 1)
direction = "서해";
else if (recCount % 4 == 2)
direction = "남해";
else if (recCount % 4 == 3)
direction = "제주도";

return direction;
}

public RecommendPlaceDTO getRecommendPlace() {
Recommend recommendLoc = recommendRepository.getRecommendByDir(findNowDirection());

return RecommendPlaceDTO.builder()
.longtitude(recommendLoc.getLongitude())
.latitude(recommendLoc.getLatitude())
.direction(recommendLoc.getDirection())
.name(recommendLoc.getName())
.build();
}

@Scheduled(cron = "0 0 0 * * SUN")
public void initDailyAchieve() {
// 한 주간 추천한 장소 플래그 true로 변경
String nowDirection = findNowDirection();
Recommend nowRecommend = recommendRepository.getRecommendByDir(nowDirection);
nowRecommend.setRecs(true);
recommendRepository.save(nowRecommend);

// 해당 direction의 장소들이 이미 모두 추천된 경우 (새로 추천할 장소가 없는 경우)
// -> 해당 direction 장소들의 추천 여부를 다시 전부 false로 변경
if (recommendRepository.getRecommendByDir(findNowDirection()) == null) {
recommendRepository.updateRecsFalseByDirection(nowDirection);
}
}
}

0 comments on commit dbb0e64

Please sign in to comment.