Skip to content

Commit

Permalink
add: [인기차트] 지역구 내 인기차트 반환 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
hw130 committed Dec 19, 2022
1 parent 00d4582 commit 7f66cab
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
26 changes: 14 additions & 12 deletions src/main/java/com/sesac/gmd/src/Chart/ChartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,38 @@
import com.sesac.gmd.config.BaseResponseStatus;
import com.sesac.gmd.src.Chart.model.GetChartReq;
import com.sesac.gmd.src.Chart.model.GetChartRes;
import com.sesac.gmd.src.song.model.GetPinsRes;
import com.sesac.gmd.utils.JwtService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.sesac.gmd.config.BaseResponseStatus.SUCCESS;
import static com.sesac.gmd.utils.Validation.locationValidation;

@RestController
@RequestMapping("/chart")
public class ChartController {
@Autowired
private ChartProvider chartProvider;
@Autowired
private JwtService jwtService;

public ChartController(ChartProvider chartProvider, JwtService jwtService){
public ChartController(ChartProvider chartProvider){
this.chartProvider = chartProvider;
this.jwtService = jwtService;
}

//@ResponseBody
//@GetMapping("")
/* public BaseResponse<List<GetChartRes>> getChart(@RequestParam double latitude, @RequestParam double longitude, GetChartReq getChartReq){
/* 현 위치의 지역구 기준 인기차트 반환 */
@ApiOperation("지역 내 인기차트 반환")
@ResponseBody
@GetMapping("")
public BaseResponse<List<GetChartRes>> getChart(@RequestBody GetChartReq getChartReq){
try{
BaseResponseStatus status = locationValidation(latitude, longitude);
getChartReq = new GetChartReq(latitude, longitude);
List<GetChartRes> getChartRes = chartProvider.getChart(getChartReq);
return new BaseResponse<>(getChartRes);

} catch(BaseException exception){
return new BaseResponse<>(exception.getStatus());
}catch(BaseException exception){
return new BaseResponse<>((exception.getStatus()));
}
} */
}
}
39 changes: 38 additions & 1 deletion src/main/java/com/sesac/gmd/src/Chart/ChartDao.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
package com.sesac.gmd.src.Chart;

import com.sesac.gmd.src.Chart.model.GetChartReq;
import com.sesac.gmd.src.Chart.model.GetChartRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

@Repository
public class ChartDao {
String query = "select pinIdx, count(pinIdx) as 'count_like' from pin_like_tbl group by pinIdx order by count_like desc";
private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

/* 현 위치의 지역구 기준 인기차트 반환 */
public List<GetChartRes> getChart(GetChartReq getChartReq){
String query = "select sct.pinIdx, sct.likeCount, pt.city, pt.state, pt.albumImage, pt.songTitle, pt.artist, rank() over (order by likeCount desc) as songRank\n" +
"from (select pinIdx, count(pinIdx) as 'likeCount' from pin_like_tbl as plt group by pinIdx order by likeCount desc) as sct\n" +
"right join pin_tbl as pt on sct.pinIdx = pt.pinIdx\n" +
"where pt.city = ? and likeCount is not null\n" +
"order by likeCount desc;";

Object[] params = new Object[]{
getChartReq.getCity()
};

return this.jdbcTemplate.query(query,
(rs, rowNum) -> new GetChartRes(
rs.getInt("pinIdx"),
rs.getInt("likeCount"),
rs.getString("city"),
rs.getString("state"),
rs.getString("albumImage"),
rs.getString("songTitle"),
rs.getString("artist"),
rs.getInt("songRank")
), params);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/sesac/gmd/src/Chart/ChartProvider.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
package com.sesac.gmd.src.Chart;

import com.sesac.gmd.config.BaseException;
import com.sesac.gmd.config.BaseResponse;
import com.sesac.gmd.config.BaseResponseStatus;
import com.sesac.gmd.src.Chart.model.GetChartReq;
import com.sesac.gmd.src.Chart.model.GetChartRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.sesac.gmd.config.BaseResponseStatus.DATABASE_ERROR;

@Service
public class ChartProvider {
@Autowired
private ChartDao chartDao;

public ChartProvider(ChartDao chartDao){
this.chartDao = chartDao;
}

/* 현 위치의 지역구 기준 인기차트 반환 */
public List<GetChartRes> getChart(GetChartReq getChartReq) throws BaseException{
try{
return chartDao.getChart(getChartReq);
}catch(Exception exception){
throw new BaseException(DATABASE_ERROR);
}
}


}
3 changes: 1 addition & 2 deletions src/main/java/com/sesac/gmd/src/Chart/model/GetChartReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
@AllArgsConstructor
@NoArgsConstructor
public class GetChartReq {
private double latitude;
private double longitude;
private String city;
}
11 changes: 8 additions & 3 deletions src/main/java/com/sesac/gmd/src/Chart/model/GetChartRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
@AllArgsConstructor
@NoArgsConstructor
public class GetChartRes {
//핀 인덱스, 순위, 앨범 커버, 제목, 가수, 공감 수
private int pinIdx;
private String albumCover;
private String title;
private String singer;
private int songRank;
private String albumImage;
private String songTitle;
private String artist;
private String city;
private String state;
private int likeCount;
}

0 comments on commit 7f66cab

Please sign in to comment.