Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/backend/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/backend/.idea/backend.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/backend/.idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions src/backend/.idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/backend/.idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/backend/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/backend/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/backend/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +42,20 @@ public ResponseEntity<SimpleChannel> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Users, Long> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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로 모든 채널 조회
Expand All @@ -47,16 +51,16 @@ private List<Channels> fetchAllChannels(Long workspaceId) {
private List<SimpleChannel> classifyChannels(Long userId, List<Channels> channelList, boolean isJoined) {
List<SimpleChannel> 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()));
}
}
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<SimpleChannel> joinedChannels, List<SimpleChannel> unjoinedChannels) {
Expand Down Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

큰 것도 잘못된 것도 아니지만, 저는 Entity나 DTO를 만들때 해당 class의 변수들의 노출을 막고 싶어 정적 팩토리 메서드를 즐겨 사용합니다! 이건 고쳤으면 좋겠다가 아니라 저는 이렇게 즐겨 한다~ 입니다!!

https://tecoble.techcourse.co.kr/post/2020-05-26-static-factory-method/

.users(user)
.channels(channel)
.mute(false)
.build();
userChannelRepository.save(userChannel);

return "success";
}

private void IsChannelInWorkSpace(Long workspaceId, Long channelId) {
List<Channels> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Loading