diff --git a/src/backend/.idea/.gitignore b/src/backend/.idea/.gitignore new file mode 100644 index 00000000..13566b81 --- /dev/null +++ b/src/backend/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/src/backend/.idea/backend.iml b/src/backend/.idea/backend.iml new file mode 100644 index 00000000..d6ebd480 --- /dev/null +++ b/src/backend/.idea/backend.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/backend/.idea/compiler.xml b/src/backend/.idea/compiler.xml new file mode 100644 index 00000000..bcba8915 --- /dev/null +++ b/src/backend/.idea/compiler.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/backend/.idea/dataSources.xml b/src/backend/.idea/dataSources.xml new file mode 100644 index 00000000..304c7830 --- /dev/null +++ b/src/backend/.idea/dataSources.xml @@ -0,0 +1,17 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://localhost:5432/jootalkpia + + + + + + $ProjectFileDir$ + + + \ No newline at end of file diff --git a/src/backend/.idea/gradle.xml b/src/backend/.idea/gradle.xml new file mode 100644 index 00000000..43ba7ce9 --- /dev/null +++ b/src/backend/.idea/gradle.xml @@ -0,0 +1,32 @@ + + + + + + + \ No newline at end of file diff --git a/src/backend/.idea/misc.xml b/src/backend/.idea/misc.xml new file mode 100644 index 00000000..d696a87d --- /dev/null +++ b/src/backend/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/src/backend/.idea/modules.xml b/src/backend/.idea/modules.xml new file mode 100644 index 00000000..e066844e --- /dev/null +++ b/src/backend/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/backend/.idea/vcs.xml b/src/backend/.idea/vcs.xml new file mode 100644 index 00000000..b2bdec2d --- /dev/null +++ b/src/backend/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/controller/WorkSpaceController.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/controller/WorkSpaceController.java index 91ada8e5..63c5320a 100644 --- a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/controller/WorkSpaceController.java +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/controller/WorkSpaceController.java @@ -4,7 +4,6 @@ //import com.jootalkpia.aop.JootalkpiaAuthenticationContext; import com.jootalkpia.workspace_server.dto.ChannelListDTO; import com.jootalkpia.workspace_server.dto.SimpleChannel; -import com.jootalkpia.workspace_server.entity.Channels; import com.jootalkpia.workspace_server.service.WorkSpaceService; import com.jootalkpia.workspace_server.util.ValidationUtils; import lombok.RequiredArgsConstructor; @@ -43,6 +42,20 @@ public ResponseEntity createChannel(@PathVariable Long workspaceI log.info("Creating channels for workspace with id: {}", workspaceId); SimpleChannel channel = workSpaceService.createChannel(workspaceId, channelName); + + // 유저를 생성된 채널에 가입시킴 + workSpaceService.addMember(workspaceId, userId, channel.getChannelId()); + return ResponseEntity.ok().body(channel); } + + @PostMapping("/{workspaceId}/channels/{channelId}/members") + public ResponseEntity addMember(@PathVariable Long workspaceId, @PathVariable Long channelId) { + + ValidationUtils.validateWorkSpaceId(workspaceId); + ValidationUtils.validateChannelId(channelId); + log.info("Adding member for workspace with id: {} {}", workspaceId, channelId); + + return ResponseEntity.ok(workSpaceService.addMember(workspaceId, userId, channelId)); + } } diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/entity/UserChannel.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/entity/UserChannel.java index 021eab81..7405b9b9 100644 --- a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/entity/UserChannel.java +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/entity/UserChannel.java @@ -13,13 +13,16 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; +import lombok.Builder; import lombok.Getter; +import lombok.RequiredArgsConstructor; import org.hibernate.annotations.CreationTimestamp; import org.hibernate.annotations.UpdateTimestamp; @Entity @Table(name = "user_channel") @Getter +@RequiredArgsConstructor public class UserChannel extends BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -40,4 +43,10 @@ public class UserChannel extends BaseEntity { @Column(name = "mute", nullable = false) private Boolean mute; + @Builder + public UserChannel(Users users, Channels channels, Boolean mute) { + this.users = users; + this.channels = channels; + this.mute = mute; + } } diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/exception/common/ErrorCode.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/exception/common/ErrorCode.java index b9e4b809..130f0287 100644 --- a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/exception/common/ErrorCode.java +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/exception/common/ErrorCode.java @@ -14,9 +14,12 @@ public enum ErrorCode { MISSING_PARAMETER("W40003", "필수 파라미터가 누락되었습니다."), INVALID_PARAMETER("W40004", "잘못된 파라미터가 포함되었습니다."), DUPLICATE_CHANNEL_NAME("W40005", "동일한 채널명이 이미 해당 워크스페이스에 존재합니다."), + DUPLICATE_USER_IN_CHANNEL("W40006", "유저가 이미 해당 채널에 참여하고 있습니다"), // 404 Not Found WORKSPACE_NOT_FOUND("W40401", "등록되지 않은 워크스페이스입니다."), + CHANNEL_NOT_FOUND("W40402", "등록되지 않은 채널입니다."), + USER_NOT_FOUND("W40403", "등록되지 않은 유저입니다."), // 500 Internal Server Error diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/repository/UserRepository.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/repository/UserRepository.java new file mode 100644 index 00000000..a6cf3395 --- /dev/null +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/repository/UserRepository.java @@ -0,0 +1,10 @@ +package com.jootalkpia.workspace_server.repository; + +import com.jootalkpia.workspace_server.entity.UserChannel; +import com.jootalkpia.workspace_server.entity.Users; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface UserRepository extends JpaRepository { +} diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/service/WorkSpaceService.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/service/WorkSpaceService.java index 0c8c54ab..85bdeab4 100644 --- a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/service/WorkSpaceService.java +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/service/WorkSpaceService.java @@ -4,11 +4,14 @@ import com.jootalkpia.workspace_server.dto.ChannelListDTO; import com.jootalkpia.workspace_server.dto.SimpleChannel; import com.jootalkpia.workspace_server.entity.Channels; +import com.jootalkpia.workspace_server.entity.UserChannel; +import com.jootalkpia.workspace_server.entity.Users; import com.jootalkpia.workspace_server.entity.WorkSpace; import com.jootalkpia.workspace_server.exception.common.CustomException; import com.jootalkpia.workspace_server.exception.common.ErrorCode; import com.jootalkpia.workspace_server.repository.ChannelRepository; import com.jootalkpia.workspace_server.repository.UserChannelRepository; +import com.jootalkpia.workspace_server.repository.UserRepository; import com.jootalkpia.workspace_server.repository.WorkSpaceRepository; import java.util.ArrayList; import java.util.List; @@ -24,6 +27,7 @@ public class WorkSpaceService { private final ChannelRepository channelRepository; private final UserChannelRepository userChannelRepository; private final WorkSpaceRepository workSpaceRepository; + private final UserRepository userRepository; public ChannelListDTO getChannels(Long userId, Long workspaceId) { // workspaceId로 모든 채널 조회 @@ -47,7 +51,7 @@ private List fetchAllChannels(Long workspaceId) { private List classifyChannels(Long userId, List channelList, boolean isJoined) { List classifiedChannels = new ArrayList<>(); for (Channels channel : channelList) { - boolean joined = isJoinedChannel(userId, channel); + boolean joined = isUserInChannel(userId, channel.getChannelId()); if (joined == isJoined) { classifiedChannels.add(new SimpleChannel(channel.getChannelId(), channel.getName(), channel.getCreatedAt())); } @@ -55,8 +59,8 @@ private List classifyChannels(Long userId, List channel return classifiedChannels; } - private boolean isJoinedChannel(Long userId, Channels channels) { - return userChannelRepository.findByUsersUserIdAndChannelsChannelId(userId, channels.getChannelId()).isPresent(); + private boolean isUserInChannel(Long userId, Long channelId) { + return userChannelRepository.findByUsersUserIdAndChannelsChannelId(userId, channelId).isPresent(); } private ChannelListDTO createChannelListDTO(List joinedChannels, List unjoinedChannels) { @@ -98,4 +102,46 @@ private WorkSpace fetchWorkSpace(Long workspaceId) { return workSpaceRepository.findById(workspaceId) .orElseThrow(() -> new CustomException(ErrorCode.WORKSPACE_NOT_FOUND.getCode(), ErrorCode.WORKSPACE_NOT_FOUND.getMsg())); } + + public String addMember(Long workspaceId, Long userId, Long channelId) { + WorkSpace workSpace = fetchWorkSpace(workspaceId); + + // 워크스페이스에 채널 있는지 + IsChannelInWorkSpace(workspaceId, channelId); + + // 채널에 유저 있는지 + if (isUserInChannel(userId, channelId)) { + throw new CustomException(ErrorCode.DUPLICATE_USER_IN_CHANNEL.getCode(), ErrorCode.DUPLICATE_USER_IN_CHANNEL.getMsg()); + } + + Channels channel = fetchChannel(channelId); + Users user = fetchUser(userId); + + UserChannel userChannel = UserChannel.builder() + .users(user) + .channels(channel) + .mute(false) + .build(); + userChannelRepository.save(userChannel); + + return "success"; + } + + private void IsChannelInWorkSpace(Long workspaceId, Long channelId) { + List channelList = fetchAllChannels(workspaceId); + for (Channels channel : channelList) { + if (channel.getChannelId().equals(channelId)) { return ; } + } + throw new CustomException(ErrorCode.CHANNEL_NOT_FOUND.getCode(), ErrorCode.CHANNEL_NOT_FOUND.getMsg()); + } + + private Users fetchUser(Long userId) { + return userRepository.findById(userId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND.getCode(), ErrorCode.USER_NOT_FOUND.getMsg())); + } + + private Channels fetchChannel(Long channelId) { + return channelRepository.findById(channelId) + .orElseThrow(() -> new CustomException(ErrorCode.CHANNEL_NOT_FOUND.getCode(), ErrorCode.CHANNEL_NOT_FOUND.getMsg())); + } } diff --git a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/util/ValidationUtils.java b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/util/ValidationUtils.java index cae8970b..e0ad9449 100644 --- a/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/util/ValidationUtils.java +++ b/src/backend/workspace_server/src/main/java/com/jootalkpia/workspace_server/util/ValidationUtils.java @@ -14,4 +14,10 @@ public static void validateWorkSpaceId(Long workSpaceId) { throw new CustomException(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMsg()); } } + + public static void validateChannelId(Long channelId) { + if (channelId == null || channelId <= 0) { + throw new CustomException(ErrorCode.INVALID_PARAMETER.getCode(), ErrorCode.INVALID_PARAMETER.getMsg()); + } + } } \ No newline at end of file