Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/main/java/com/golab/talk/config/WebSocketConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.golab.talk.repository.ChattingRepository;
import com.golab.talk.repository.RoomRepository;
import com.golab.talk.websocket.SocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

@Configuration
@EnableWebSocket
Expand All @@ -21,7 +22,9 @@ public class WebSocketConfig implements WebSocketConfigurer {

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(new SocketHandler(roomRepository, chattingRepository), "/socket").setAllowedOrigins("*");
registry.addHandler(new SocketHandler(roomRepository, chattingRepository), "/socket")
.addInterceptors(new HttpSessionHandshakeInterceptor())
.setAllowedOrigins("*");
}

}
24 changes: 22 additions & 2 deletions src/main/java/com/golab/talk/controller/ChattingController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import com.golab.talk.dto.ChattingListDto;
import com.golab.talk.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -13,6 +15,9 @@
import com.golab.talk.domain.Chatting;
import com.golab.talk.service.ChattingService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/chatting")
public class ChattingController {
Expand All @@ -21,13 +26,28 @@ public class ChattingController {
private ChattingService chattingService;

@GetMapping("/{receiveUserId}")
public ResponseEntity<List<Chatting>> getChattingList(@PathVariable int receiveUserId) {
List<Chatting> list = chattingService.getChattingList(receiveUserId);
public ResponseEntity<List<Chatting>> getChattingList(@PathVariable int receiveUserId, HttpServletRequest request) {
List<Chatting> list = chattingService.getChattingList(receiveUserId, request);

if (list != null) {
return new ResponseEntity<>(list, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}

//범석 추가
// 왼쪽 채팅목록들 가져오기..
@GetMapping("/")
public ResponseEntity<List<ChattingListDto>> getList(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if(session.getAttribute("loggedInUser")!=null) {
int id = ((UserDto) session.getAttribute("loggedInUser")).getId();
List<ChattingListDto> list = chattingService.getRoomList(id);
System.out.println(list.get(0).getRoom_id());
return new ResponseEntity<>(list, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
15 changes: 13 additions & 2 deletions src/main/java/com/golab/talk/controller/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.golab.talk.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -16,6 +17,9 @@
import com.golab.talk.dto.FriendDto;
import com.golab.talk.service.FriendService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/friend")
public class FriendController {
Expand Down Expand Up @@ -43,8 +47,15 @@ public ResponseEntity<String> deleteFriendList(@RequestBody FriendDto friendDto)
}
}

@GetMapping("/{myId}")
public ResponseEntity<List<String>> showFriendList(@PathVariable("myId") int myId) {
// @GetMapping("/{myId}")
// public ResponseEntity<List<String>> showFriendList(@PathVariable("myId") int myId,

@GetMapping("/")
public ResponseEntity<List<String>> showFriendList(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if(session == null) return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);;
UserDto a = (UserDto)session.getAttribute("loggedInUser");
int myId = a.getId();
List<String> list = friendService.showFriendList(myId);

if (list != null) {
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/golab/talk/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public ResponseEntity<String> login(@RequestBody LoginDto loginDto, HttpServletR
// 로그인에 성공한 경우 "loggedInUser" 모델 속성에 유저 정보를 저장하고 세션에 저장
HttpSession session = request.getSession();
session.setAttribute("loggedInUser", loggedInUser);
UserDto a = (UserDto)session.getAttribute("loggedInUser");
return new ResponseEntity<>("로그인에 성공했습니다.", HttpStatus.OK);
} else {
return new ResponseEntity<>("로그인에 실패했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
Expand All @@ -106,7 +105,7 @@ public ResponseEntity<String> logout(HttpServletRequest request) {
HttpSession session = request.getSession(false);
// session이 null이 아니라는건 기존에 세션이 존재했었다는 뜻이므로
// 세션이 null이 아니라면 session.invalidate()로 세션 삭제해주기.
if(session != null) {
if(session.getAttribute("loggedInUser")!=null) {
session.invalidate();
return new ResponseEntity<>("로그아웃 되었습니다.", HttpStatus.OK);
}
Expand Down Expand Up @@ -161,4 +160,14 @@ public boolean existSession(HttpServletRequest request) {
// return new UserDto();
// }

@GetMapping("/id")
public ResponseEntity<Integer> getId(HttpServletRequest request) {
Integer idValue = 0;
if(existSession(request)) {
idValue = Integer.valueOf(userService.getId(request));
return new ResponseEntity<>(idValue,HttpStatus.OK);
}
else return new ResponseEntity<>(idValue,HttpStatus.INTERNAL_SERVER_ERROR);
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/golab/talk/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class User {
@Column(name = "UPDATED_AT")
private String updatedAt;

public User(String userId, String name, String email, String password){
public User(int id, String userId, String name, String email, String password){
this.id = id;
this.userId = userId;
this.name = name;
this.email = email;
Expand All @@ -51,6 +52,7 @@ public User(String userId, String name, String email, String password){

public UserDto toDto() {
return UserDto.builder()
.id(id)
.userId(userId)
.name(name)
.email(email)
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/golab/talk/dto/ChattingListDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.golab.talk.dto;
// 범석 추가
// projection으로 원하는 필드만 DB에서 가져오고 싶을 시,
// Dto를 interface로 선언해야하며
// 가져올 필드명을 get + [필드명 그대로](단, 첫글자는 대문자로) 선언해야 한다.

//import org.springframework.lang.Nullable;


public interface ChattingListDto {
// @Nullable
String getProfile_img_url();
String getName();
int getRoom_id();
int getId();
}
2 changes: 1 addition & 1 deletion src/main/java/com/golab/talk/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@AllArgsConstructor
@NoArgsConstructor
public class UserDto {
private int id;
private String userId;
private String name;
private String email;
Expand All @@ -27,5 +28,4 @@ public User toEntity() {
.password(password)
.build();
}

}
13 changes: 9 additions & 4 deletions src/main/java/com/golab/talk/repository/ChattingRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public interface ChattingRepository extends JpaRepository<Chatting, Integer> {

List<Chatting> findByIdLessThanAndRoomIdOrderByIdDesc(int cursor, int roomId, Pageable pageable);

@Modifying
@Transactional
@Query(value = "update chatting set not_read=?3 where room_id=?1 and send_user_id=?2", nativeQuery = true)
int updateByRoomId(int roomId, int sendUserId, int notRead);

// Repository + Service + Controller 로직 정비

// 채팅 생성 -> {roomId, sendUserId, message, 1, datetime, datetime}
Expand All @@ -42,8 +47,8 @@ public interface ChattingRepository extends JpaRepository<Chatting, Integer> {
@Query(value = "select * from chatting where room_id=?1 order by id", nativeQuery = true)
List<Chatting> getChattingListByRoomId(int roodId);

@Modifying
@Transactional
@Query(value = "update chatting set not_read=?3 where room_id=?1 and send_user_id=?2", nativeQuery = true)
int updateByRoomId(int roomId, int sendUserId, int notRead);
// @Modifying
// @Transactional
// @Query(value = "update chatting set not_read=?3 where room_id=?1 and send_user_id=?2", nativeQuery = true)
// int updateByRoomId(int roomId, int sendUserId, int notRead);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.golab.talk.dto.ChattingListDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
Expand All @@ -23,4 +24,11 @@ public interface ParticipantRepository extends JpaRepository<Participant, Intege
@Query(value = "SELECT * FROM participant WHERE user_id = :userId AND room_id = :roomId", nativeQuery = true)
Participant findByUserIdAndRoomId(int userId, int roomId);


//범석 추가
@Query(value = "SELECT u.profile_img_url as profile_img_url, u.name as name, p.room_id as room_id, u.id as id " +
"FROM user u INNER JOIN participant p ON u.id = p.user_id " +
"WHERE p.room_id IN (SELECT room_id FROM participant WHERE user_id = :userId) AND p.user_id <> :userId", nativeQuery = true)
List<ChattingListDto> getRoomByUserId(int userId);

}
12 changes: 7 additions & 5 deletions src/main/java/com/golab/talk/service/ChattingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

import com.golab.talk.domain.Chatting;
import com.golab.talk.domain.Participant;
import com.golab.talk.dto.ChattingResponseDto;
import com.golab.talk.dto.ParticipantDto;
import com.golab.talk.dto.RoomDto;
import com.golab.talk.dto.RoomListResponseDto;
import com.golab.talk.dto.*;

import javax.servlet.http.HttpServletRequest;

public interface ChattingService {

Expand All @@ -23,6 +22,9 @@ public interface ChattingService {

Participant saveParticipant(Participant participant);

List<Chatting> getChattingList(int receiveUserId);
List<Chatting> getChattingList(int receiveUserId, HttpServletRequest request);

//범석추가
List<ChattingListDto> getRoomList(int userId);

}
2 changes: 2 additions & 0 deletions src/main/java/com/golab/talk/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface UserService {
int updatePassword(String password, HttpServletRequest request);

int updateName(String name, HttpServletRequest request);

int getId(HttpServletRequest request);
}
24 changes: 15 additions & 9 deletions src/main/java/com/golab/talk/service/impl/ChattingServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedList;
import java.util.List;

import com.golab.talk.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
Expand All @@ -12,15 +13,14 @@
import com.golab.talk.domain.Chatting;
import com.golab.talk.domain.Participant;
import com.golab.talk.domain.Room;
import com.golab.talk.dto.ChattingResponseDto;
import com.golab.talk.dto.ParticipantDto;
import com.golab.talk.dto.RoomDto;
import com.golab.talk.dto.RoomListResponseDto;
import com.golab.talk.repository.ChattingRepository;
import com.golab.talk.repository.ParticipantRepository;
import com.golab.talk.repository.RoomRepository;
import com.golab.talk.service.ChattingService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class ChattingServiceImpl implements ChattingService {

Expand Down Expand Up @@ -123,13 +123,14 @@ public Participant saveParticipant(Participant participant) {
}

@Override
public List<Chatting> getChattingList(int receiveUserId) {
public List<Chatting> getChattingList(int receiveUserId, HttpServletRequest request) {
int sendUserId = 1; // <Muk> 임시로 1로 설정

// session을 파싱하여 처리
// int sendUserId =
// session.getAttributes().get("userId") != null ?
// Integer.parseInt(session.getAttributes().get("userId").toString()) : -1;
HttpSession session = request.getSession(false);
sendUserId = ((UserDto)session.getAttribute("loggedInUser")).getId();


int sendUserId = 1; // <Muk> 임시로 1로 설정
String identifier =
sendUserId < receiveUserId ? sendUserId + "-" + receiveUserId : receiveUserId + "-" + sendUserId;

Expand All @@ -145,4 +146,9 @@ public List<Chatting> getChattingList(int receiveUserId) {
return chattingRepository.getChattingListByRoomId(roomId);
}

//범석 추가
@Override
public List<ChattingListDto> getRoomList(int userId) {
return participantRepository.getRoomByUserId(userId);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/golab/talk/service/impl/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@ public int updateName(String name, HttpServletRequest request) {
}
return check;
}

@Override
public int getId(HttpServletRequest request) {
int id = 0;
HttpSession session = request.getSession(false);
if(session!=null) {
id = ((UserDto) session.getAttribute("loggedInUser")).getId();
}
return id;
}
}
Loading