This repository was archived by the owner on Jan 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStudyCommandServiceImpl.java
More file actions
307 lines (240 loc) · 12.3 KB
/
StudyCommandServiceImpl.java
File metadata and controls
307 lines (240 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package com.example.spot.service.study;
import com.example.spot.api.code.status.ErrorStatus;
import com.example.spot.api.exception.handler.MemberHandler;
import com.example.spot.api.exception.handler.StudyHandler;
import com.example.spot.domain.Member;
import com.example.spot.domain.Region;
import com.example.spot.domain.Theme;
import com.example.spot.domain.enums.ApplicationStatus;
import com.example.spot.domain.enums.Status;
import com.example.spot.domain.enums.StudyLikeStatus;
import com.example.spot.domain.enums.StudyState;
import com.example.spot.domain.mapping.MemberStudy;
import com.example.spot.domain.mapping.PreferredStudy;
import com.example.spot.domain.mapping.RegionStudy;
import com.example.spot.domain.mapping.StudyTheme;
import com.example.spot.domain.study.Study;
import com.example.spot.repository.*;
import com.example.spot.security.utils.SecurityUtils;
import com.example.spot.web.dto.study.request.StudyJoinRequestDTO;
import com.example.spot.web.dto.study.request.StudyRegisterRequestDTO;
import com.example.spot.web.dto.study.request.StudyRegisterRequestDTO.RegisterDTO;
import com.example.spot.web.dto.study.response.StudyInfoResponseDTO.StudyInfoDTO;
import com.example.spot.web.dto.study.response.StudyJoinResponseDTO;
import com.example.spot.web.dto.study.response.StudyLikeResponseDTO;
import com.example.spot.web.dto.study.response.StudyRegisterResponseDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Slf4j
@Transactional
@RequiredArgsConstructor
public class StudyCommandServiceImpl implements StudyCommandService {
@Value("${study.keyword}")
private String KEYWORD;
private final MemberRepository memberRepository;
private final StudyRepository studyRepository;
private final RegionRepository regionRepository;
private final ThemeRepository themeRepository;
private final MemberStudyRepository memberStudyRepository;
private final RegionStudyRepository regionStudyRepository;
private final StudyThemeRepository studyThemeRepository;
private final PreferredStudyRepository preferredStudyRepository;
private final RedisTemplate<String, String> redisTemplate;
/* ----------------------------- 스터디 생성/참여 관련 API ------------------------------------- */
// [스터디 생성/참여] 참여 신청하기
@Transactional
public StudyJoinResponseDTO.JoinDTO applyToStudy(Long studyId, StudyJoinRequestDTO.StudyJoinDTO studyJoinRequestDTO) {
// Authorization
Long memberId = SecurityUtils.getCurrentUserId();
SecurityUtils.verifyUserId(memberId);
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(ErrorStatus._MEMBER_NOT_FOUND));
Study study = studyRepository.findById(studyId)
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_NOT_FOUND));
// 모집중이지 않은 스터디에 신청할 수 없음
if (study.getStudyState() != StudyState.RECRUITING) {
throw new StudyHandler(ErrorStatus._STUDY_NOT_RECRUITING);
}
if (study.getMaxPeople() <= memberStudyRepository.countByStatusAndStudyId(ApplicationStatus.APPROVED, studyId))
throw new StudyHandler(ErrorStatus._STUDY_IS_FULL);
// 이미 신청한 스터디에 다시 신청할 수 없음
List<MemberStudy> memberStudyList = memberStudyRepository.findByMemberIdAndStatusNot(memberId, ApplicationStatus.REJECTED).stream()
.filter(memberStudy -> study.equals(memberStudy.getStudy()))
.toList();
// memberStudy에 내가 소유한 스터디가 있으면 에러 발생
if (memberStudyList.stream().anyMatch(MemberStudy::getIsOwned)) {
throw new StudyHandler(ErrorStatus._STUDY_OWNER_CANNOT_APPLY);
}
if (!memberStudyList.isEmpty()) {
throw new StudyHandler(ErrorStatus._STUDY_ALREADY_APPLIED);
}
MemberStudy memberStudy = MemberStudy.builder()
.isOwned(false)
.introduction(studyJoinRequestDTO.getIntroduction())
.member(member)
.study(study)
.status(ApplicationStatus.APPLIED)
.activeStatus(Status.ON)
.build();
member.addMemberStudy(memberStudy);
study.addMemberStudy(memberStudy);
memberStudyRepository.save(memberStudy);
return StudyJoinResponseDTO.JoinDTO.toDTO(member, study);
}
// [스터디 생성/참여] 스터디 생성하기
@Transactional
public StudyRegisterResponseDTO.RegisterDTO registerStudy(StudyRegisterRequestDTO.RegisterDTO studyRegisterRequestDTO) {
// Authorization
Long memberId = SecurityUtils.getCurrentUserId();
SecurityUtils.verifyUserId(memberId);
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(ErrorStatus._MEMBER_NOT_FOUND));
Study study = Study.builder()
.gender(studyRegisterRequestDTO.getGender())
.minAge(studyRegisterRequestDTO.getMinAge())
.maxAge(studyRegisterRequestDTO.getMaxAge())
.hasFee(studyRegisterRequestDTO.isHasFee())
.fee(studyRegisterRequestDTO.getFee())
.profileImage(studyRegisterRequestDTO.getProfileImage())
.studyState(StudyState.RECRUITING)
.isOnline(studyRegisterRequestDTO.getIsOnline())
.heartCount(0)
.goal(studyRegisterRequestDTO.getGoal())
.introduction(studyRegisterRequestDTO.getIntroduction())
.title(studyRegisterRequestDTO.getTitle())
.status(Status.ON)
.hitNum(0L)
.maxPeople(studyRegisterRequestDTO.getMaxPeople())
.build();
study = studyRepository.save(study);
createMemberStudy(member, study);
createRegionStudy(study, studyRegisterRequestDTO);
createStudyTheme(study, studyRegisterRequestDTO);
studyRepository.save(study);
return StudyRegisterResponseDTO.RegisterDTO.toDTO(study);
}
/**
* 스터디 정보를 수정합니다.
* @param studyId 수정할 스터디 ID
* @param studyInfoDTO 수정할 스터디 정보
* @return 수정된 스터디 정보를 반환합니다.
*/
@Override
public StudyRegisterResponseDTO.RegisterDTO updateStudyInfo(Long studyId, RegisterDTO studyInfoDTO) {
Long currentUserId = SecurityUtils.getCurrentUserId();
MemberStudy memberStudy = memberStudyRepository.findByMemberIdAndStudyId(currentUserId, studyId)
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_MEMBER_NOT_FOUND));
if (!memberStudy.getIsOwned()) {
throw new StudyHandler(ErrorStatus._STUDY_NOT_OWNER);
}
Study study = studyRepository.findById(studyId)
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_NOT_FOUND));
study.updateStudyInfo(studyInfoDTO.getTitle(), studyInfoDTO.getIntroduction(), studyInfoDTO.getGoal(), studyInfoDTO.getIsOnline(),
studyInfoDTO.isHasFee(), studyInfoDTO.getFee(), studyInfoDTO.getMinAge(), studyInfoDTO.getMaxAge(),
studyInfoDTO.getGender(), studyInfoDTO.getMaxPeople(), studyInfoDTO.getProfileImage());
studyThemeRepository.deleteByStudyId(studyId);
study.getStudyThemes().clear();
regionStudyRepository.deleteByStudyId(studyId);
study.getRegionStudies().clear();
createRegionStudy(study, studyInfoDTO);
createStudyTheme(study, studyInfoDTO);
studyRepository.save(study);
return StudyRegisterResponseDTO.RegisterDTO.toDTO(study);
}
/**
* 특정 스터디에 좋아요를 누르거나 취소합니다. 이미 좋아요가 눌려있다면 취소하고, 아니라면 좋아요를 누릅니다.
* @param memberId 회원 ID
* @param studyId 스터디 ID
* @return 스터디 제목과 좋아요 상태를 반환합니다.
* @throws StudyHandler 스터디가 존재하지 않는 경우
* @throws MemberHandler 회원이 존재하지 않는 경우
* @see StudyLikeResponseDTO
*/
@Override
public StudyLikeResponseDTO likeStudy(Long memberId, Long studyId) {
// 회원과 스터디 조회
Study study = studyRepository.findById(studyId)
.orElseThrow(() -> new StudyHandler(ErrorStatus._STUDY_NOT_FOUND));
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(ErrorStatus._MEMBER_NOT_FOUND));
// 현재 좋아요 상태 확인 -> 만약 없다면, 객체 하나 생성
PreferredStudy preferredStudy = preferredStudyRepository
.findByMemberIdAndStudyId(memberId, studyId)
.orElse(PreferredStudy.builder()
.member(member)
.study(study)
.studyLikeStatus(StudyLikeStatus.DISLIKE)
.build());
// 상태에 따라 변경
if (preferredStudy.getStudyLikeStatus() == StudyLikeStatus.LIKE) {
preferredStudy.changeStatus(StudyLikeStatus.DISLIKE);
study.deletePreferredStudy(preferredStudy);
} else {
preferredStudy.changeStatus(StudyLikeStatus.LIKE);
study.addPreferredStudy(preferredStudy);
}
// 저장 및 응답 객체 생성
preferredStudyRepository.save(preferredStudy);
return new StudyLikeResponseDTO(preferredStudy);
}
private void createMemberStudy(Member member, Study study) {
MemberStudy memberStudy = MemberStudy.builder()
.isOwned(true)
.introduction(study.getIntroduction())
.member(member)
.study(study)
.status(ApplicationStatus.APPROVED)
.activeStatus(Status.ON)
.build();
member.addMemberStudy(memberStudy);
study.addMemberStudy(memberStudy);
memberStudyRepository.save(memberStudy);
study.addMemberStudy(memberStudy);
}
private void createRegionStudy(Study study, StudyRegisterRequestDTO.RegisterDTO studyRegisterRequestDTO) {
studyRegisterRequestDTO.getRegions()
.forEach(stringRegion -> {
Region region = regionRepository
.findByCode(stringRegion)
.orElseThrow(() -> new StudyHandler(ErrorStatus._REGION_NOT_FOUND));
RegionStudy regionStudy = RegionStudy.builder()
.region(region)
.study(study)
.build();
region.addRegionStudy(regionStudy);
study.addRegionStudy(regionStudy);
regionStudyRepository.save(regionStudy);
study.addRegionStudy(regionStudy);
});
}
private void createStudyTheme(Study study, StudyRegisterRequestDTO.RegisterDTO studyRegisterRequestDTO) {
studyRegisterRequestDTO.getThemes()
.forEach(stringTheme -> {
Theme theme = themeRepository.findByStudyTheme(stringTheme)
.orElseThrow(() -> new StudyHandler(ErrorStatus._THEME_NOT_FOUND));
StudyTheme studyTheme = StudyTheme.builder()
.theme(theme)
.study(study)
.build();
study.addStudyTheme(studyTheme);
theme.addStudyTheme(studyTheme);
studyThemeRepository.save(studyTheme);
study.addStudyTheme(studyTheme);
});
}
/* ---------------------------------- 인기 검색어 --------------------------------------------- */
/**
* 검색어를 인기 검색어(Redis)에 추가합니다. 이미 존재하는 검색어라면 score를 1 증가시킵니다.
* @param keyword 검색어
*/
@Override
public void addHotKeyword(String keyword) {
redisTemplate.opsForZSet().incrementScore(KEYWORD, keyword, 1);
}
}