Skip to content

Commit

Permalink
Merge pull request #17 from KAKAO-TOUR-API-CONTEST/devyj
Browse files Browse the repository at this point in the history
Devyj
  • Loading branch information
Parkwochang authored Aug 31, 2024
2 parents 35e72f0 + d93ff01 commit dd7e12b
Show file tree
Hide file tree
Showing 19 changed files with 419 additions and 130 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ dependencies {

// graphQL 클래스, 인터페이스를 제공하는 라이브러리
implementation 'com.graphql-java-kickstart:graphql-spring-boot-starter:11.0.0'
runtimeOnly 'com.graphql-java-kickstart:graphiql-spring-boot-starter:11.0.0'
implementation 'org.projectlombok:lombok:1.18.26'
runtimeOnly 'com.graphql-java-kickstart:graphiql-spring-boot-starter:11.0.0'
// graphQL 쿼리 요청에 사용되는 라이브러리
implementation 'com.graphql-java-kickstart:playground-spring-boot-starter:11.0.0'

Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/ai_jeju/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class WebConfig implements WebMvcConfigurer {

@Override
public void addInterceptors(InterceptorRegistry registry) {
// 모든 경로에 대해 인터셉터를 적용 (필요에 따라 경로를 지정할 수 있음)
registry.addInterceptor(headerCheckInterceptor).addPathPatterns("/**");
}
}
70 changes: 55 additions & 15 deletions src/main/java/com/example/ai_jeju/controller/ChatController.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@ public void message(ChatMessageDto messageDto, @Header("simpSessionAttributes")

try {
String nickname = (String) sessionAttributes.get("nickname");
String profileImg = (String) sessionAttributes.get("profileImg");
log.info("Received message: " + messageDto.toString());

// sender가 null일 경우 세션에서 추출한 nickname을 sender로 설정
if (messageDto.getSender() == null) {
messageDto.setSender(nickname);
}
if (messageDto.getProfileImg() == null) {
messageDto.setProfileImg(profileImg);
}

switch (messageDto.getType()) {
case ENTER:
Expand All @@ -64,14 +68,28 @@ public void message(ChatMessageDto messageDto, @Header("simpSessionAttributes")
break;
}

ChatMessage chatMessage = ChatMessage.builder()
.roomId(messageDto.getRoomId())
.sender(messageDto.getSender()) // nickname이 sender로 설정됨
.message(messageDto.getMessage())
.timestamp(LocalDateTime.now())
.type(messageDto.getType().name())
.build();
chatMessageRepository.save(chatMessage);
// roomId를 사용해 ChatRoom 객체를 가져오기
ChatRoom chatRoom = chatRoomRepository.findByRoomId(messageDto.getRoomId())
.orElseThrow(() -> new IllegalArgumentException("Invalid roomId: " + messageDto.getRoomId()));

// ENTER 타입이 아닌 경우에만 메시지를 저장하기
if (messageDto.getType() != ChatMessageDto.MessageType.ENTER) {
try {
ChatMessage chatMessage = ChatMessage.builder()
.chatRoom(chatRoom)
.sender(messageDto.getSender())
.message(messageDto.getMessage())
.profileImg(messageDto.getProfileImg())
.timestamp(LocalDateTime.now())
.type(messageDto.getType().name())
.build();
log.info("Saving ChatMessage: " + chatMessage.toString());
chatMessageRepository.save(chatMessage);
} catch (Exception e) {
log.error("Error saving ChatMessage", e);
}
}


log.info("Sending message to /sub/chat/room/" + messageDto.getRoomId() + ": " + messageDto.toString());
messagingTemplate.convertAndSend("/sub/chat/room/" + messageDto.getRoomId(), messageDto);
Expand All @@ -81,26 +99,48 @@ public void message(ChatMessageDto messageDto, @Header("simpSessionAttributes")
}


//@GetMapping("/chatroom")
//public String chat(@RequestParam("roomId") String roomId, Model model) {
// model.addAttribute("roomId", roomId);
// return "chat"; // chat.html 파일을 반환
//}
@GetMapping("/chatroom")
public String chat(@RequestParam("roomId") String roomId, Model model) {
model.addAttribute("roomId", roomId);
return "chat"; // chat.html 파일을 반환
}

@GetMapping("/chat/previous")
@ResponseBody
public List<ChatMessage> getPreviousMessages(
@RequestParam("roomId") String roomId,
@RequestParam(value = "lastMessageid", required = false) Optional<Long> lastMessageId) {
return chatService.previousMessages(roomId, lastMessageId.orElse(null));

// roomId를 사용해 ChatRoom 객체 가져오기
ChatRoom chatRoom = chatRoomRepository.findByRoomId(roomId)
.orElseThrow(() -> new IllegalArgumentException("Invalid roomId: " + roomId));

return chatService.previousMessages(chatRoom, lastMessageId.orElse(null));
}

@GetMapping("/chat/allprevious")
@ResponseBody
public List<ChatMessage> getAllMessages(@RequestParam("roomId") String roomId) {
return chatService.getAllMessages(roomId);

// roomId를 사용해 ChatRoom 객체를 가져오기
ChatRoom chatRoom = chatRoomRepository.findByRoomId(roomId)
.orElseThrow(() -> new IllegalArgumentException("Invalid roomId: " + roomId));

return chatService.getAllMessages(chatRoom);
}

@GetMapping("/chat/messagecount")
@ResponseBody
public int getMessageCount(@RequestParam("roomId") String roomId) {


ChatRoom chatRoom = chatRoomRepository.findByRoomId(roomId)
.orElseThrow(() -> new IllegalArgumentException("Invalid roomId: " + roomId));

return chatService.getMessageCount(chatRoom);
}





Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.example.ai_jeju.controller;

import com.example.ai_jeju.domain.ChatMessage;
import com.example.ai_jeju.domain.ChatRoom;
import com.example.ai_jeju.handler.StompHandler;
import com.example.ai_jeju.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
public class ChatRoomController {

private final ChatService chatService;
private final StompHandler stompHandler;

@GetMapping("/rooms")
public List<ChatRoom> room() {
Expand All @@ -34,4 +40,25 @@ public String chatRooms() {
return "chatrooms";
}
}
}

@GetMapping("/room/{roomId}/lastmessage")
public ChatMessage getLastMessage(@PathVariable("roomId") String roomId) {
ChatRoom chatRoom = chatService.findRoomById(roomId);
if (chatRoom == null) {
throw new IllegalArgumentException("Invalid roomId: " + roomId);
}
return chatService.findLastMessage(chatRoom);
}


@GetMapping("/users/{roomId}/count")
public int getUserCount(@PathVariable("roomId") String roomId) {
return stompHandler.getUserCount(roomId);
}






}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
Expand All @@ -29,10 +30,11 @@ public Long registerUser(@RequestBody SignUpRequest signUpRequest, HttpServletRe
}

@GetMapping("/checks")
public Long CheckIfUser(@RequestParam(name = "email") String email, HttpServletRequest request, HttpServletResponse response){
return userService.checkIfUser(email,request,response);
public Map<String, Object> CheckIfUser(@RequestParam(name = "email") String email, HttpServletRequest request, HttpServletResponse response){
return userService.checkIfUser(email, request, response);
}


//탈퇴하기
@GetMapping("/withdraw")
public ResponseEntity<String> signOut(@RequestBody WithdrawRequest withdrawRequest) {
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/example/ai_jeju/domain/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.example.ai_jeju.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

Expand All @@ -22,9 +20,15 @@ public class ChatMessage {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String roomId;
@ManyToOne
@JoinColumn(name = "room_id", referencedColumnName = "roomId")
private ChatRoom chatRoom;

private String sender;
private String message;
private String type;
@UpdateTimestamp
@Column(name = "timestamp")
private LocalDateTime timestamp;
private String profileImg;
}
21 changes: 10 additions & 11 deletions src/main/java/com/example/ai_jeju/domain/ChatRoom.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
package com.example.ai_jeju.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.*;
import lombok.Data;

@Data
@Entity
public class ChatRoom {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String roomId;

private String name;
private String description;
private String imgurl;

// 기본 생성자
public ChatRoom() {
}

// 이름과 roomId를 인자로 받는 생성자
public ChatRoom(String roomId, String name) {
public ChatRoom(String roomId, String name, String description, String imgurl) {
this.roomId = roomId;
this.name = name;
this.description = description;
this.imgurl = imgurl;
}

public static ChatRoom create(String roomId, String name) {
return new ChatRoom(roomId, name);
public static ChatRoom create(String roomId, String name, String description, String imgurl) {
return new ChatRoom(roomId, name, description, imgurl);
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/example/ai_jeju/domain/Emergency.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Emergency {
private String imgsrc;
@Column(length = 1024)
private String operationtime;
@Column(length = 1024)
private String breaktime;
private String category;
double mapx;
double mapy;
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/example/ai_jeju/domain/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Store {
String name;

//이미지 소스
@Column(name = "imgSrc",updatable = false,nullable = true)
@Column(name = "imgSrc",updatable = false,nullable = true, columnDefinition = "TEXT")
String imgSrc;

@Column(name = "address",updatable = false,nullable = true)
Expand All @@ -46,26 +46,26 @@ public class Store {

//유모차 대여여부
@Column(name = "stroller",updatable = false,nullable = true)
boolean stroller;
Boolean stroller;

//유모차 편의성
@Column(name = "strollerVal",updatable = false,nullable = true)
boolean strollerVal;
Boolean strollerVal;

//아이 스페어 체어
@Column(name = "babySpareChair",updatable = false,nullable = true)
boolean babySpareChair;
Boolean babySpareChair;

//아이 놀이방
@Column(name = "playground",updatable = false,nullable = true)
boolean playground;
Boolean playground;

//노키즈존 여부
@Column(name = "noKidsZone",updatable = false,nullable = true)
boolean noKidsZone;
Boolean noKidsZone;

@Column(name = "categoryId",updatable = false,nullable = true)
int categoryId;
Integer categoryId;

@Column(name = "operationTime",updatable = false,nullable = true)
String operationTime;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/example/ai_jeju/dto/ChatMessageDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
public class ChatMessageDto {

public enum MessageType {
ENTER, TALK, EXIT;
TALK, EXIT, ENTER;
}

private MessageType type;
private String roomId;
private String sender;
private String message;
private String profileImg;
}
Loading

0 comments on commit dd7e12b

Please sign in to comment.