Skip to content

Commit 4967964

Browse files
authored
Merge pull request #2 from hyohyo-zz/sprint1-advanced
[조현아] sprint1_심화 과제 구현 사항
2 parents 0062c10 + 35be3fd commit 4967964

File tree

9 files changed

+479
-184
lines changed

9 files changed

+479
-184
lines changed

src/main/java/com/sprint/mission/discodeit/JavaApplication.java

Lines changed: 299 additions & 125 deletions
Large diffs are not rendered by default.

src/main/java/com/sprint/mission/discodeit/entity/Channel.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
package com.sprint.mission.discodeit.entity;
22

33
import java.util.*;
4+
import java.util.stream.Collectors;
45

56
public class Channel {
67
private UUID id;
78
private String channelName;
9+
private User keyUser;
810
private List<String> categories;
911
private Set<User> members;
1012
private long createdAt;
1113
private long updatedAt;
1214

13-
public Channel(String channelName, List<String> categories, Set<User> members) {
15+
public Channel(String channelName, User keyUser, List<String> categories, Set<User> members) {
1416
this.id = UUID.randomUUID();
1517
this.channelName = channelName;
1618
this.categories = categories;
17-
this.members = members;
19+
20+
this.keyUser = keyUser;
21+
this.members = new HashSet<>(members);
22+
this.members.add(keyUser);
23+
1824
this.createdAt = System.currentTimeMillis();
19-
this.updatedAt = this.createdAt;
25+
this.updatedAt = this.createdAt; //updatedAt의 처음 시간은 createAt과 동일해야 함
2026
}
2127

2228
public UUID getId() {
@@ -58,18 +64,28 @@ public void setUpdatedAt(long updatedAt) {
5864
this.updatedAt = updatedAt;
5965
}
6066

67+
public User getKeyUser() {
68+
return keyUser;
69+
}
70+
71+
public void addMember(User user) {
72+
members.add(user);
73+
}
74+
6175
public void update(Channel updateChannelData) {
6276
this.channelName = updateChannelData.channelName;
63-
this.categories = new ArrayList<>(categories);
77+
this.keyUser = updateChannelData.keyUser;
78+
this.categories = new ArrayList<>(updateChannelData.categories);
6479
this.members = new HashSet<>(updateChannelData.members);
6580
this.updatedAt = System.currentTimeMillis();
6681
}
6782

6883
public String toString() {
6984
return "Channel{" +
70-
"ChannelName='" + channelName + '\'' +
71-
", Category=" + categories + '\'' +
72-
", Members=" + members +
73-
'}';
85+
"ChannelName= '" + channelName + '\'' +
86+
", KeyUser= '" + keyUser.getName() + '\'' +
87+
", Category= '" + categories + '\'' +
88+
", Members= '" + members.stream().map(User::getName).collect(Collectors.toList()) +
89+
"'}";
7490
}
7591
}

src/main/java/com/sprint/mission/discodeit/entity/Message.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Message(User sender,Channel channel, String category, String content) {
2020
this.category = category;
2121
this.content = content;
2222
this.createdAt = System.currentTimeMillis();
23-
this.updatedAt = this.createdAt;
23+
this.updatedAt = this.createdAt; //updatedAt의 처음 시간은 createAt과 동일해야 함
2424
}
2525

2626
public UUID getId() {
@@ -66,18 +66,19 @@ public long getUpdatedAt() {
6666
public void setUpdatedAt(long updatedAt) {
6767
this.updatedAt = updatedAt;
6868
}
69+
6970
public void update(Message updateMessageData) {
7071
this.content = updateMessageData.content;
7172
this.updatedAt = System.currentTimeMillis();
7273
}
74+
7375
public String toString() {
7476
return "Message{" +
75-
"id= " + id + '\'' +
76-
", sender= " + sender.getName() + '\'' +
77-
", channel= " + channel.getChannelName() + '\'' +
77+
"id= '" + id + '\'' +
78+
", sender= '" + sender.getName() + '\'' +
79+
", channel= '" + channel.getChannelName() + '\'' +
7880
", category= '" + category + '\'' +
79-
", content= " + content + '\'' +
80-
81+
", content= '" + content + '\'' +
8182
'}';
8283
}
8384

src/main/java/com/sprint/mission/discodeit/entity/User.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public User(String name, String gender, String email, String phone, String passw
2222
this.phone = phone;
2323
this.password = password;
2424
this.createdAt = System.currentTimeMillis();
25-
this.updatedAt = this.createdAt;
25+
this.updatedAt = this.createdAt; //updatedAt의 처음 시간은 createAt과 동일해야 함
2626
}
2727

2828
public UUID getId() {
@@ -85,11 +85,11 @@ public void update(User updateUserData) {
8585

8686
public String toString() {
8787
return "User{" +
88-
"UserName='" + name + '\'' +
89-
", Gender='" + gender + '\'' +
90-
", email=" + email + '\'' +
91-
", phone=" + phone + '\'' +
92-
", password=" + password + '\'' +
88+
"UserName= '" + name + '\'' +
89+
", Gender= '" + gender + '\'' +
90+
", email= '" + email + '\'' +
91+
", phone= '" + phone + '\'' +
92+
", password= '" + password + '\'' +
9393
'}';
9494
}
9595
}

src/main/java/com/sprint/mission/discodeit/service/ChannelService.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ public interface ChannelService {
1515

1616
public Channel update(UUID id, Channel update);
1717

18-
public boolean delete(UUID id);
18+
public boolean delete(UUID id, User user, String password);
1919

2020
public Set<User> members(UUID id);
2121

22-
public List<String> cat(Channel category);
2322
}

src/main/java/com/sprint/mission/discodeit/service/UserService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ public interface UserService {
1616

1717
public User update(UUID id, User update);
1818

19-
public boolean delete(UUID id);
19+
public boolean delete(UUID id, String password);
2020

2121
}

src/main/java/com/sprint/mission/discodeit/service/jcf/JCFChannelService.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,70 @@
1010
public class JCFChannelService implements ChannelService {
1111
private final Map<UUID, Channel> data = new HashMap<>();
1212

13+
//채널 생성
1314
@Override
1415
public void create(Channel channel) {
15-
this.data.put(channel.getId(),channel);
16+
17+
//중복 채널명 검증
18+
for (Channel existingChannel : data.values()) {
19+
if (existingChannel.getChannelName().equals(channel.getChannelName())
20+
&& existingChannel.getKeyUser().equals(channel.getKeyUser())) {
21+
22+
throw new IllegalArgumentException(" --- "+ channel.getKeyUser().getName()+"님! 이미 등록된 채널입니다.");
23+
}
24+
}
25+
26+
//중복 카테고리 검증
27+
Set<String> categorySet = new HashSet<>(channel.getCategory());
28+
if (channel.getCategory().size() != categorySet.size()) { //list사이즈=2, set사이즈1 -> 중복있다
29+
throw new IllegalArgumentException(" --- 중복된 카테고리가 포함되어 있습니다.");
30+
}
31+
channel.getMembers().add(channel.getKeyUser()); // 키 유저를 초기 멤버로 추가
32+
33+
data.put(channel.getId(), channel);
34+
1635
}
1736

37+
//채널 조회
1838
@Override
1939
public Channel read(UUID id) {
2040
return this.data.get(id);
2141
}
2242

43+
//채널 전체 조회
2344
@Override
2445
public List<Channel> readAll() {
2546
return new ArrayList<>(data.values());
2647
}
2748

49+
//채널 수정
2850
@Override
2951
public Channel update(UUID id, Channel update) {
3052
Channel selected = this.data.get(id);
3153
selected.update(update);
3254
return selected;
3355
}
3456

57+
//채널 삭제
3558
@Override
36-
public boolean delete(UUID id) {
37-
return data.remove(id) != null;
59+
public boolean delete(UUID id, User user, String password) {
60+
Channel channel = this.data.get(id);
61+
if (!user.getPassword().equals(password)) {
62+
System.out.println("!!채널 삭제 실패!! --- 비밀번호 불일치");
63+
return false;
64+
}
65+
System.out.println("<<채널 [" + channel.getChannelName() + "] 삭제 성공>>");
66+
return this.data.remove(id) != null;
3867
}
3968

69+
70+
//채널 멤버셋
4071
@Override
4172
public Set<User> members(UUID id) {
4273
Channel channel = data.get(id);
4374
return channel != null ? channel.getMembers() : Set.of();
4475
}
4576

46-
@Override
47-
public List<String> cat(Channel channel) {
48-
return channel.getCategory();
49-
}
50-
5177
//채널별 카테고리
5278
public Map<String, List<List<String>>> groupByChannel() {
5379
return data.values().stream()
@@ -64,8 +90,4 @@ public List<Channel> findChannel(String channelName) {
6490
.collect(Collectors.toList());
6591
}
6692

67-
68-
69-
70-
7193
}

src/main/java/com/sprint/mission/discodeit/service/jcf/JCFMessageService.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,64 @@
99
import java.util.*;
1010

1111
public class JCFMessageService implements MessageService {
12+
private final Map<UUID, Message> data = new LinkedHashMap<>();
13+
private final JCFUserService userService;
14+
private final JCFChannelService channelService;
1215

13-
private final Map<UUID, Message> data = new HashMap<>();
16+
public JCFMessageService(JCFUserService userService, JCFChannelService channelService) {
17+
this.userService = userService;
18+
this.channelService = channelService;
19+
}
1420

21+
//메시지 생성
1522
@Override
1623
public void create(Message message) {
24+
//채널 멤버가 아닌 유저가 메시지 생성시
25+
if (!message.getChannel().getMembers().contains(message.getSender())) {
26+
throw new IllegalArgumentException(
27+
" ---" + message.getSender().getName()+"(은/는) [" + message.getChannel().getChannelName() + "]채널 멤버가 아닙니다.");
28+
}
29+
30+
//채널에 없는 카테고리에 메시지 생성시
31+
if (!message.getChannel().getCategory().contains(message.getCategory())) {
32+
throw new IllegalArgumentException(
33+
" ---"+message.getCategory()+ "(은/는) [" + message.getChannel().getChannelName() + "]채널에 존재 하지 않는 카테고리입니다.");
34+
}
35+
1736
data.put(message.getId(), message);
1837
}
1938

39+
//메시지 조회
2040
@Override
21-
public Message update(UUID id, Message update) {
22-
Message selected = this.data.get(id);
23-
selected.update(update);
24-
return selected;
41+
public Message read(UUID id) {
42+
Message message = this.data.get(id);
43+
44+
//메시지id 존재하지 않음
45+
if (message == null) {
46+
throw new IllegalArgumentException(" --해당 ID의 메시지를 찾을 수 없습니다.");
47+
}
48+
49+
return this.data.get(id);
2550
}
2651

52+
//메시지 전체조회
2753
@Override
28-
public boolean delete(UUID id) {
29-
return data.remove(id) != null;
54+
public List<Message> readAll() {
55+
return new ArrayList<>(this.data.values());
3056
}
3157

58+
//메시지 수정
3259
@Override
33-
public Message read(UUID id) {
34-
return this.data.get(id);
60+
public Message update(UUID id, Message update) {
61+
Message selected = this.data.get(id);
62+
selected.update(update);
63+
return selected;
3564
}
3665

66+
//메시지 삭제
3767
@Override
38-
public List<Message> readAll() {
39-
return new ArrayList<>(this.data.values());
68+
public boolean delete(UUID id) {
69+
return data.remove(id) != null;
4070
}
4171

4272

0 commit comments

Comments
 (0)