Skip to content

Commit

Permalink
[BE] 스터디 API 전면 수정 (#84)
Browse files Browse the repository at this point in the history
* fix : 스터디 개설 API에 인증 추가, 스터디 등록일 추가

* fix : 스터디 수정 API에 인증 추가 + 리더 아이디 체크 서비스 생성

* fix : 스터디 삭제 API 수정 (access token -> id -> leaderCheck 서비스 -> 삭제)
  • Loading branch information
gandi0330 authored Jun 30, 2022
1 parent 6a20a21 commit 43cbcae
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 64 deletions.
121 changes: 82 additions & 39 deletions backend/src/main/java/tayo/sseuktudy/controller/StudyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,66 +33,108 @@ public class StudyController {
}

@PostMapping("/study/regist")
public ResponseEntity<Map<String, Object>> registStudy(@RequestBody StudyRegistDto studyRegistDto){
public ResponseEntity<Map<String, Object>> registStudy(@RequestBody StudyRegistDto studyRegistDto, HttpServletRequest request){
Map<String, Object> resultMap = new HashMap<>();

HttpStatus status = null;
HttpStatus status;
logger.info("스터디 등록 요청");
String decodeUserId = jwtService.decodeToken(request.getHeader("access-token"));
if(!decodeUserId.equals(ACCESS_TOKEN_TIMEOUT)){
logger.info("사용 가능한 토큰!!!");
try{
studyRegistDto.setStudyLeaderId(decodeUserId);

int result = studyService.registStudy(studyRegistDto); // 스터디 테이블에 집어넣기
// 사전질문 테이블에도 집어넣는 서비스 필요
studyService.registStudy(studyRegistDto);
resultMap.put("message","SUCCESS");
status = HttpStatus.ACCEPTED;

if(result != 0){
resultMap.put("message", "SUCCESS");
status = HttpStatus.ACCEPTED;
}catch(Exception e){
logger.error("예외 발생", e);
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
}else{
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
logger.error("사용 불가능 토큰!!!");
resultMap.put("message","FAIL");
status = HttpStatus.UNAUTHORIZED;
}

return new ResponseEntity<Map<String, Object>>(resultMap, status);
return new ResponseEntity<>(resultMap, status);
}

@PutMapping("/study")
public ResponseEntity<Map<String, Object>> modifyStudy(@RequestBody StudyModifyDto studyModifyDto){
public ResponseEntity<Map<String, Object>> modifyStudy(@RequestBody StudyModifyDto studyModifyDto, HttpServletRequest request ){
logger.info("스터디 수정 API 실행");
Map<String, Object> resultMap = new HashMap<>();
HttpStatus status;

HttpStatus status = null;
logger.info("스터디 수정 요청");
String decodeUserId = jwtService.decodeToken(request.getHeader("access-token"));
if(!decodeUserId.equals(ACCESS_TOKEN_TIMEOUT)){
logger.info("사용 가능한 토큰!!!");
try{

int result = studyService.modifyStudy(studyModifyDto); // 스터디 테이블에 집어넣기
StudyUserIdDto studyUserIdDto = new StudyUserIdDto();
studyUserIdDto.setStudyId(studyModifyDto.getStudyId());
studyUserIdDto.setUserId(decodeUserId);

if(result != 0){
resultMap.put("message", "SUCCESS");
status = HttpStatus.ACCEPTED;
}else{
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
if(studyService.leaderCheck(studyUserIdDto) != 1) {
resultMap.put("message", "FAIL");
status = HttpStatus.UNAUTHORIZED;

}
}else{
studyService.modifyStudy(studyModifyDto);
resultMap.put("message","SUCCESS");
status = HttpStatus.ACCEPTED;
}

return new ResponseEntity<Map<String, Object>>(resultMap, status);
}catch(Exception e){
logger.error("예외 발생", e);
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
}else{
logger.error("사용 불가능 토큰!!!");
resultMap.put("message","FAIL");
status = HttpStatus.UNAUTHORIZED;
}
return new ResponseEntity<>(resultMap, status);
}

@DeleteMapping("/study")
public ResponseEntity<Map<String, Object>> deleteStudy(@RequestBody StudyDeleteDto studyDeleteDto){
public ResponseEntity<Map<String, Object>> deleteStudy(@RequestParam int studyId , HttpServletRequest request){
logger.info("스터디 삭제 API 실행");
Map<String, Object> resultMap = new HashMap<>();
HttpStatus status;

HttpStatus status = null;
logger.info("스터디 삭제 요청");

int result = studyService.deleteStudy(studyDeleteDto);
String decodeUserId = jwtService.decodeToken(request.getHeader("access-token"));
if(!decodeUserId.equals(ACCESS_TOKEN_TIMEOUT)){
logger.info("사용 가능한 토큰!!!");
try{

if(result != 0){
resultMap.put("message", "SUCCESS");
status = HttpStatus.ACCEPTED;
StudyUserIdDto studyUserIdDto = new StudyUserIdDto();
studyUserIdDto.setStudyId(studyId);
studyUserIdDto.setUserId(decodeUserId);

if(studyService.leaderCheck(studyUserIdDto) != 1) {
resultMap.put("message", "FAIL");
status = HttpStatus.UNAUTHORIZED;

}else{
studyService.deleteStudy(studyUserIdDto);
resultMap.put("message","SUCCESS");
status = HttpStatus.ACCEPTED;
}

}catch(Exception e){
logger.error("예외 발생", e);
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
}
}else{
resultMap.put("message", "FAIL");
status = HttpStatus.INTERNAL_SERVER_ERROR;
logger.error("사용 불가능 토큰!!!");
resultMap.put("message","FAIL");
status = HttpStatus.UNAUTHORIZED;
}

return new ResponseEntity<Map<String, Object>>(resultMap, status);
return new ResponseEntity<>(resultMap, status);
}

@GetMapping("/study/list")
Expand Down Expand Up @@ -125,11 +167,11 @@ public ResponseEntity<Map<String, Object>> likeStudy(@RequestParam int studyId,
if(!decodeUserId.equals(ACCESS_TOKEN_TIMEOUT)){
logger.info("사용 가능한 토큰!!!");
try{
StudyLikeDto studyLikeDto = new StudyLikeDto();
studyLikeDto.setUserId(decodeUserId);
studyLikeDto.setStudyId(studyId);
StudyUserIdDto studyUserIdDto = new StudyUserIdDto();
studyUserIdDto.setUserId(decodeUserId);
studyUserIdDto.setStudyId(studyId);

studyService.likeStudy(studyLikeDto);
studyService.likeStudy(studyUserIdDto);
resultMap.put("message","SUCCESS");
status = HttpStatus.ACCEPTED;

Expand All @@ -146,4 +188,5 @@ public ResponseEntity<Map<String, Object>> likeStudy(@RequestParam int studyId,
return new ResponseEntity<>(resultMap, status);
}


}
11 changes: 0 additions & 11 deletions backend/src/main/java/tayo/sseuktudy/dto/study/StudyLikeDto.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public class StudyRegistDto {
String studyContent;
List<String> studyPrequestion;
int studyView;
String studyRegistdate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Setter
@Getter
public class StudyDeleteDto {
public class StudyUserIdDto {
String userId;
int studyId;
}
5 changes: 3 additions & 2 deletions backend/src/main/java/tayo/sseuktudy/mapper/StudyMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public interface StudyMapper {

public int joinStudy(MemberJoinDto memberJoinDto);
public int modifyStudy(StudyModifyDto studyModifyDto);
public int deleteStudy(StudyDeleteDto studyDeleteDto);
public int deleteStudy(StudyUserIdDto studyUserIdDto);
public List<StudyInfoDto> getStudyByFilter(StudyFilterDto studyFilterDto);
public int likeStudy(StudyLikeDto studyLikeDto);
public int likeStudy(StudyUserIdDto studyUserIdDto);
public int leaderCheck(StudyUserIdDto studyUserIdDto);
// public StudyInfoDto getStudyInfo()

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
public interface StudyService {
public int registStudy(StudyRegistDto studyRegistDto);
public int modifyStudy(StudyModifyDto studyModifyDto);
public int deleteStudy(StudyDeleteDto studyDeleteDto);
public int deleteStudy(StudyUserIdDto studyUserIdDto);
public List<StudyInfoDto> getStudyByFilter(StudyFilterDto studyFilterDto);
public int likeStudy(StudyLikeDto studyLikeDto);
public int likeStudy(StudyUserIdDto studyUserIdDto);
public int leaderCheck(StudyUserIdDto studyUserIdDto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public int modifyStudy(StudyModifyDto studyModifyDto){
}

@Override
public int deleteStudy(StudyDeleteDto studyDeleteDto) {
return studyMapper.deleteStudy(studyDeleteDto);
public int deleteStudy(StudyUserIdDto studyUserIdDto) {
return studyMapper.deleteStudy(studyUserIdDto);
}

@Override
Expand All @@ -84,8 +84,13 @@ public List<StudyInfoDto> getStudyByFilter(StudyFilterDto studyFilterDto) {
}

@Override
public int likeStudy(StudyLikeDto studyLikeDto) {
return studyMapper.likeStudy(studyLikeDto);
public int likeStudy(StudyUserIdDto studyUserIdDto) {
return studyMapper.likeStudy(studyUserIdDto);
}

@Override
public int leaderCheck(StudyUserIdDto studyUserIdDto) {
return studyMapper.leaderCheck(studyUserIdDto);
}


Expand Down
13 changes: 8 additions & 5 deletions backend/src/main/resources/mapper/study.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

<mapper namespace="tayo.sseuktudy.mapper.StudyMapper">
<insert id="registStudy" parameterType="StudyRegistDto" useGeneratedKeys="true" keyProperty="studyId">
insert into study (study_title, study_leader_id, study_status, study_introduction, study_startdate, study_enddate, study_goals, study_type, study_user_min, study_user_max, study_content, study_place, study_category_id, study_view)
value (#{studyTitle}, #{studyLeaderId}, #{studyStatus}, #{studyIntroduction}, #{studyStartdate}, #{studyEnddate}, #{studyGoals}, #{studyType}, #{studyUserMin}, #{studyUserMax},#{studyContent}, #{studyPlace},#{studyCategoryId}, #{studyView});
insert into study (study_title, study_leader_id, study_status, study_introduction, study_startdate, study_enddate, study_goals, study_type, study_user_min, study_user_max, study_content, study_place, study_category_id, study_view, study_registdate)
value (#{studyTitle}, #{studyLeaderId}, #{studyStatus}, #{studyIntroduction}, #{studyStartdate}, #{studyEnddate}, #{studyGoals}, #{studyType}, #{studyUserMin}, #{studyUserMax},#{studyContent}, #{studyPlace},#{studyCategoryId}, #{studyView}, #{studyRegistdate});
</insert>

<insert id="joinStudy" parameterType="MemberJoinDto">
Expand All @@ -20,7 +20,7 @@
where study_id = #{studyId}
</update>

<delete id="deleteStudy" parameterType="StudyDeleteDto">
<delete id="deleteStudy" parameterType="StudyUserIdDto">
delete from study
where study_id=#{studyId}
</delete>
Expand Down Expand Up @@ -54,12 +54,15 @@

</select>

<insert id="likeStudy" parameterType="StudyLikeDto">
<insert id="likeStudy" parameterType="StudyUserIdDto">
insert into user_like_study(user_id, study_id)
values(#{userId}, #{studyId})
</insert>


<select id="leaderCheck" parameterType="StudyUserIdDto" resultType="int">
select count(*) from study
where study_id = #{studyId} and study_leader_id = #{userId}
</select>


</mapper>

0 comments on commit 43cbcae

Please sign in to comment.