forked from EveryUniv/next-student-council-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from kjungw1025/feat/chatting
feat: 각 채팅방 별, 채팅 메시지 관리를 위한 DynamoDB 설정 및 기능 추가
- Loading branch information
Showing
15 changed files
with
343 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value | ||
lombok.copyableannotations += com.dku.council.global.config.webclient.ChromeAgentWebClient | ||
lombok.anyConstructor.addConstructorProperties = true | ||
lombok.Setter.flagUsage = error | ||
lombok.data.flagUsage = error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/com/dku/council/domain/chatmessage/model/ChatRoomMessageId.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.dku.council.domain.chatmessage.model; | ||
|
||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverted; | ||
import com.dku.council.global.config.DynamoDBConfig; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.io.Serializable; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@Setter | ||
public class ChatRoomMessageId implements Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@DynamoDBHashKey | ||
private String roomId; | ||
|
||
@DynamoDBRangeKey | ||
@DynamoDBTypeConverted(converter = DynamoDBConfig.LocalDateTimeConverter.class) | ||
private LocalDateTime createdAt; | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/com/dku/council/domain/chatmessage/model/entity/ChatRoomMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.dku.council.domain.chatmessage.model.entity; | ||
|
||
import com.amazonaws.services.dynamodbv2.datamodeling.*; | ||
import com.dku.council.domain.chatmessage.model.ChatRoomMessageId; | ||
import com.dku.council.global.config.DynamoDBConfig; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.Id; | ||
|
||
import java.time.LocalDateTime; | ||
@DynamoDBTable(tableName = "ChatRoomMessage") | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor() | ||
public class ChatRoomMessage { | ||
|
||
@Id | ||
@Getter(AccessLevel.NONE) | ||
@Setter(AccessLevel.NONE) | ||
private ChatRoomMessageId chatRoomMessageId; | ||
|
||
@DynamoDBHashKey(attributeName = "roomId") | ||
public String getRoomId() { | ||
return chatRoomMessageId != null ? chatRoomMessageId.getRoomId() : null; | ||
} | ||
|
||
public void setRoomId(String roomId) { | ||
if (chatRoomMessageId == null) { | ||
chatRoomMessageId = new ChatRoomMessageId(); | ||
} | ||
chatRoomMessageId.setRoomId(roomId); | ||
} | ||
|
||
@DynamoDBRangeKey(attributeName = "createdAt") | ||
@DynamoDBTypeConverted(converter = DynamoDBConfig.LocalDateTimeConverter.class) | ||
public LocalDateTime getCreatedAt() { | ||
return chatRoomMessageId != null ? chatRoomMessageId.getCreatedAt() : null; | ||
} | ||
|
||
@DynamoDBTypeConverted(converter = DynamoDBConfig.LocalDateTimeConverter.class) | ||
public void setCreatedAt(LocalDateTime createdAt) { | ||
if (chatRoomMessageId == null) { | ||
chatRoomMessageId = new ChatRoomMessageId(); | ||
} | ||
chatRoomMessageId.setCreatedAt(createdAt); | ||
} | ||
|
||
@DynamoDBAttribute | ||
private String messageType; | ||
|
||
@DynamoDBAttribute | ||
private Long userId; | ||
|
||
@DynamoDBAttribute | ||
private String userNickname; | ||
|
||
@DynamoDBAttribute | ||
private String content; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/dku/council/domain/chatmessage/repository/ChatRoomMessageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.dku.council.domain.chatmessage.repository; | ||
|
||
import com.dku.council.domain.chatmessage.model.ChatRoomMessageId; | ||
import com.dku.council.domain.chatmessage.model.entity.ChatRoomMessage; | ||
import org.socialsignin.spring.data.dynamodb.repository.EnableScan; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.List; | ||
|
||
@EnableScan | ||
@Repository | ||
public interface ChatRoomMessageRepository extends CrudRepository<ChatRoomMessage, ChatRoomMessageId> { | ||
List<ChatRoomMessage> findAllByRoomIdOrderByCreatedAtAsc(String roomId); | ||
|
||
void deleteAllByRoomId(String roomId); | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/dku/council/domain/chatmessage/service/ChatRoomMessageService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.dku.council.domain.chatmessage.service; | ||
|
||
import com.dku.council.domain.chatmessage.model.entity.ChatRoomMessage; | ||
import com.dku.council.domain.chatmessage.repository.ChatRoomMessageRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class ChatRoomMessageService { | ||
|
||
private final ZoneId seoulZoneId = ZoneId.of("Asia/Seoul"); | ||
|
||
private final ChatRoomMessageRepository chatRoomMessageRepository; | ||
|
||
public void create(String roomId, | ||
String messageType, | ||
Long userId, | ||
String userNickname, | ||
String content) { | ||
|
||
ChatRoomMessage chatRoomMessage = new ChatRoomMessage(); | ||
chatRoomMessage.setRoomId(roomId); | ||
chatRoomMessage.setMessageType(messageType); | ||
chatRoomMessage.setUserId(userId); | ||
chatRoomMessage.setUserNickname(userNickname); | ||
chatRoomMessage.setContent(content); | ||
chatRoomMessage.setCreatedAt(LocalDateTime.now().atZone(seoulZoneId).toLocalDateTime()); | ||
|
||
chatRoomMessageRepository.save(chatRoomMessage); | ||
} | ||
|
||
public List<ChatRoomMessage> findAllChatRoomMessages(String roomId) { | ||
return chatRoomMessageRepository.findAllByRoomIdOrderByCreatedAtAsc(roomId); | ||
} | ||
|
||
public void deleteChatRoomMessages(String roomId) { | ||
chatRoomMessageRepository.deleteAllByRoomId(roomId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/main/java/com/dku/council/global/config/DynamoDBConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.dku.council.global.config; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import com.amazonaws.auth.AWSStaticCredentialsProvider; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB; | ||
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder; | ||
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTypeConverter; | ||
import org.socialsignin.spring.data.dynamodb.repository.config.EnableDynamoDBRepositories; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.FilterType; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
|
||
@Configuration | ||
@EnableDynamoDBRepositories(basePackages = {"com.dku.council.domain.chatmessage.repository"}, | ||
includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Repository.class)) | ||
public class DynamoDBConfig { | ||
|
||
@Value("${aws.dynamodb.accessKey}") | ||
private String awsAccessKey; | ||
|
||
@Value("${aws.dynamodb.secretKey}") | ||
private String awsSecretKey; | ||
|
||
@Value("${aws.dynamodb.region}") | ||
private String awsRegion; | ||
|
||
public AWSCredentials amazonAWSCredentials() { | ||
return new BasicAWSCredentials(awsAccessKey, awsSecretKey); | ||
} | ||
|
||
public AWSCredentialsProvider amazonAWSCredentialsProvider() { | ||
return new AWSStaticCredentialsProvider(amazonAWSCredentials()); | ||
} | ||
|
||
@Bean | ||
public AmazonDynamoDB amazonDynamoDB() { | ||
return AmazonDynamoDBClientBuilder.standard().withCredentials(amazonAWSCredentialsProvider()) | ||
.withRegion(awsRegion).build(); | ||
} | ||
|
||
/** | ||
* Java DynamoDB SDK가 Java의 기본 Date 타입만 허용하므로 | ||
* LocalDateTimeType과 Date를 상호 변환할 수 있는 컨버터 추가 | ||
*/ | ||
public static class LocalDateTimeConverter implements DynamoDBTypeConverter<Date, LocalDateTime> { | ||
@Override | ||
public Date convert(LocalDateTime source) { | ||
ZoneId seoulZoneId = ZoneId.of("Asia/Seoul"); | ||
return Date.from(source.atZone(seoulZoneId).toInstant()); | ||
} | ||
|
||
@Override | ||
public LocalDateTime unconvert(Date source) { | ||
return source.toInstant().atZone(ZoneId.of("Asia/Seoul")).toLocalDateTime(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.