Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
Resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Stravinsken committed Aug 26, 2024
2 parents fe223d0 + fabe7ac commit f3e6ba9
Show file tree
Hide file tree
Showing 27 changed files with 627 additions and 13 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ brew update && brew install azure-cli
```

```
# 윈도우
# 윈도우 powershell
$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; Remove-Item .\AzureCLI.msi
```

Expand Down Expand Up @@ -287,7 +287,7 @@ https://portal.azure.com/#home 에 접속합니다.

![png](https://github.com/hackersground-kr/hg-crescendo-server/blob/main/images/az4.png)

1-1. Cosmos DB를 검색합니다.
1-1. Cosmos DB를 검색합니다. +만들기를 클릭합니다

1-2. Others 에 PostgreSQL 을 선택합니다. (만들기 클릭)

Expand Down
5 changes: 3 additions & 2 deletions crescendo-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ configurations {

repositories {
mavenCentral()
maven {url 'https://repo.spring.io/snapshot'} //스냅샷 저장소 추가
}

dependencies {
Expand Down Expand Up @@ -57,10 +58,10 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// spring AI
// implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter:1.0.0-SNAPSHOT'
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter:1.0.0-SNAPSHOT'

// 소켓 통신
// implementation 'org.springframework.boot:spring-boot-starter-websocket'
implementation 'org.springframework.boot:spring-boot-starter-websocket'

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.crescendoserver.domain.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/ai")
@RestController
@RequiredArgsConstructor
public class AiController {
private final OpenAiChatModel openAiChatModel;

@GetMapping("/chat")
public String chat(@RequestParam String message) {
return openAiChatModel.call(String.valueOf(message));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.example.crescendoserver.domain.auth.dto.LoginRequest;
import com.example.crescendoserver.domain.auth.dto.ReissueRequest;
import com.example.crescendoserver.domain.auth.dto.SignUpRequest;
import com.example.crescendoserver.domain.user.domain.User;
import com.example.crescendoserver.domain.user.dto.UserResponse;
import com.example.crescendoserver.global.security.jwt.dto.Jwt;

Expand All @@ -15,4 +16,6 @@ public interface AuthService {

UserResponse me();

User getUser();

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ public UserResponse me() {

return new UserResponse(user.getId(), user.getUsername(), user.getRole());
}

@Override
public User getUser() {
String username = SecurityContextHolder.getContext().getAuthentication().getName();

return userRepository.findByUsername(username)
.orElseThrow(() -> new CustomException(CustomErrorCode.USER_NOT_FOUND));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.crescendoserver.domain.chat.config;

import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@RequiredArgsConstructor
@Configuration
@EnableWebSocket
public class WebSockConfig implements WebSocketConfigurer {
private final WebSocketHandler webSocketHandler;

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(webSocketHandler, "/ws/chat").setAllowedOrigins("*");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.crescendoserver.domain.chat.controller;

import com.example.crescendoserver.domain.chat.room.ChatRoom;
import com.example.crescendoserver.domain.chat.service.ChatService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/chat")
public class ChatController {
private final ChatService chatService;

@PostMapping
public ChatRoom createRoom(@RequestParam String name) {
return chatService.createRoom(name);
}

@GetMapping
public List<ChatRoom> findAllRoom() {
return chatService.findAllRoom();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.crescendoserver.domain.chat.hadler;

import com.example.crescendoserver.domain.chat.message.ChatMessage;
import com.example.crescendoserver.domain.chat.room.ChatRoom;
import com.example.crescendoserver.domain.chat.service.ChatService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

@Slf4j
@RequiredArgsConstructor
@Component
public class WebSockChatHandler extends TextWebSocketHandler {
private final ObjectMapper objectMapper;
private final ChatService chatService;

@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
String payload = message.getPayload();
log.info("payload {}", payload);
ChatMessage chatMessage = objectMapper.readValue(payload, ChatMessage.class);
ChatRoom room = chatService.findRoomById(chatMessage.getRoomId());
room.handleActions(session, chatMessage, chatService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.crescendoserver.domain.chat.message;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Builder
@Getter
@Setter
@RequiredArgsConstructor
@AllArgsConstructor
public class ChatMessage {
// 메시지 타입 : 입장, 채팅
public enum MessageType {
ENTER, TALK
}
private MessageType type; // 메시지 타입
private String roomId; // 방번호
private String sender; // 메시지 보낸사람
private String message; // 메시지
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.crescendoserver.domain.chat.room;

import com.example.crescendoserver.domain.chat.message.ChatMessage;
import com.example.crescendoserver.domain.chat.service.ChatService;
import lombok.Builder;
import lombok.Getter;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashSet;
import java.util.Set;

@Getter
public class ChatRoom {
private String roomId;
private String name;
private Set<WebSocketSession> sessions = new HashSet<>();

@Builder
public ChatRoom(String roomId, String name) {
this.roomId = roomId;
this.name = name;
}

public void handleActions(WebSocketSession session, ChatMessage chatMessage, ChatService chatService) {
if (chatMessage.getType().equals(ChatMessage.MessageType.ENTER)) {
sessions.add(session);
chatMessage.setMessage(chatMessage.getSender() + "님이 입장했습니다.");
}
sendMessage(chatMessage, chatService);
}

public <T> void sendMessage(T message, ChatService chatService) {
sessions.parallelStream().forEach(session -> chatService.sendMessage(session, message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.example.crescendoserver.domain.chat.service;

import com.example.crescendoserver.domain.chat.room.ChatRoom;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
@Service
public class ChatService {

private final ObjectMapper objectMapper;
private Map<String, ChatRoom> chatRooms;

@PostConstruct
private void init() {
chatRooms = new LinkedHashMap<>();
}

public List<ChatRoom> findAllRoom() {
return new ArrayList<>(chatRooms.values());
}

public ChatRoom findRoomById(String roomId) {
return chatRooms.get(roomId);
}

public ChatRoom createRoom(String name) {
String randomId = UUID.randomUUID().toString();
ChatRoom chatRoom = ChatRoom.builder()
.roomId(randomId)
.name(name)
.build();
chatRooms.put(randomId, chatRoom);
return chatRoom;
}

public <T> void sendMessage(WebSocketSession session, T message) {
try {
session.sendMessage(new TextMessage(objectMapper.writeValueAsString(message)));
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.example.crescendoserver.domain.post.comment;

import com.example.crescendoserver.domain.post.domain.Post;
import com.example.crescendoserver.domain.user.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.time.LocalDateTime;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Table(name = "comments")
@Entity
public class Comment {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(columnDefinition = "TEXT", nullable = false)
private String comment;

@Column(name = "created_date")
@CreatedDate
private LocalDateTime createdDate;

@Column(name = "modified_date")
@LastModifiedDate
private LocalDateTime modifiedDate;

@ManyToOne
@JoinColumn(name = "post_id")
private Post post;

@ManyToOne
@JoinColumn(name = "user_id")
private User user; // 작성자

//댓글 수정
public void update(String comment) {
this.comment = comment;
}
}
Loading

0 comments on commit f3e6ba9

Please sign in to comment.