-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolutionService.java
More file actions
408 lines (336 loc) · 16.4 KB
/
SolutionService.java
File metadata and controls
408 lines (336 loc) · 16.4 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package com.gamzabat.algohub.feature.solution.service;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.gamzabat.algohub.constants.BOJResultConstants;
import com.gamzabat.algohub.exception.ProblemValidationException;
import com.gamzabat.algohub.exception.StudyGroupValidationException;
import com.gamzabat.algohub.exception.UserValidationException;
import com.gamzabat.algohub.feature.group.ranking.service.RankingService;
import com.gamzabat.algohub.feature.group.ranking.service.RankingUpdateService;
import com.gamzabat.algohub.feature.group.studygroup.domain.GroupMember;
import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.group.studygroup.exception.CannotFoundGroupException;
import com.gamzabat.algohub.feature.group.studygroup.exception.GroupMemberValidationException;
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
import com.gamzabat.algohub.feature.notification.service.NotificationService;
import com.gamzabat.algohub.feature.problem.domain.Problem;
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
import com.gamzabat.algohub.feature.solution.domain.Solution;
import com.gamzabat.algohub.feature.solution.domain.SolutionComment;
import com.gamzabat.algohub.feature.solution.dto.CreateSolutionRequest;
import com.gamzabat.algohub.feature.solution.dto.GetCurrentSolvingStatusResponse;
import com.gamzabat.algohub.feature.solution.dto.GetSolutionResponse;
import com.gamzabat.algohub.feature.solution.dto.GetSolutionWithGroupIdResponse;
import com.gamzabat.algohub.feature.solution.dto.GetSolvingStatusPerProblemResponse;
import com.gamzabat.algohub.feature.solution.enums.ProgressCategory;
import com.gamzabat.algohub.feature.solution.exception.CannotFoundSolutionException;
import com.gamzabat.algohub.feature.solution.repository.SolutionCommentRepository;
import com.gamzabat.algohub.feature.solution.repository.SolutionRepository;
import com.gamzabat.algohub.feature.user.domain.User;
import com.gamzabat.algohub.feature.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Service
@RequiredArgsConstructor
public class SolutionService {
private final SolutionRepository solutionRepository;
private final ProblemRepository problemRepository;
private final StudyGroupRepository studyGroupRepository;
private final GroupMemberRepository groupMemberRepository;
private final UserRepository userRepository;
private final NotificationService notificationService;
private final SolutionCommentRepository commentRepository;
private final RankingService rankingService;
private final RankingUpdateService rankingUpdateService;
private final SolutionCommentRepository solutionCommentRepository;
@Transactional(readOnly = true)
public Page<GetSolutionResponse> getSolutionList(User user, Long problemId, String nickname,
String language, String result, Pageable pageable) {
Problem problem = problemRepository.findById(problemId)
.orElseThrow(() -> new ProblemValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 문제 입니다."));
StudyGroup group = studyGroupRepository.findById(problem.getStudyGroup().getId())
.orElseThrow(() -> new StudyGroupValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 그룹 입니다."));
if (!groupMemberRepository.existsByUserAndStudyGroup(user, group)) {
throw new GroupMemberValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 그룹 입니다.");
}
Page<Solution> solutions = solutionRepository.findAllFilteredSolutions(problem, nickname, language, result,
pageable);
return solutions.map(solution -> this.getGetSolutionResponse(user, solution));
}
@Transactional(readOnly = true)
public GetSolutionResponse getSolution(User user, Long solutionId) {
Solution solution = solutionRepository.findById(solutionId)
.orElseThrow(() -> new CannotFoundSolutionException("존재하지 않는 풀이 입니다."));
StudyGroup group = solution.getProblem().getStudyGroup();
if (groupMemberRepository.existsByUserAndStudyGroup(user, group)) {
return getGetSolutionResponse(user, solution);
} else {
throw new UserValidationException("해당 풀이를 확인 할 권한이 없습니다.");
}
}
@Transactional(readOnly = true)
public Page<GetSolutionResponse> getMySolutionsInGroupInProgress(User user, Long groupId, Integer problemNumber,
String language,
String result, Pageable pageable) {
StudyGroup group = validateGroupAndMember(user, groupId);
Page<GetSolutionResponse> inProgressSolutions = solutionRepository.findAllFilteredMySolutionsInGroup(user,
group, problemNumber, language, result, ProgressCategory.IN_PROGRESS, pageable)
.map(solution -> this.getGetSolutionResponse(user, solution));
log.info("success to get my in-progress solutions in group {}", groupId);
return inProgressSolutions;
}
@Transactional(readOnly = true)
public Page<GetSolutionResponse> getMySolutionsInGroupExpired(User user, Long groupId, Integer problemNumber,
String language,
String result, Pageable pageable) {
StudyGroup group = validateGroupAndMember(user, groupId);
Page<GetSolutionResponse> expiredSolutions = solutionRepository.findAllFilteredMySolutionsInGroup(user, group,
problemNumber, language, result, ProgressCategory.EXPIRED, pageable)
.map(solution -> this.getGetSolutionResponse(user, solution));
log.info("success to get my expired solutions in group {}", groupId);
return expiredSolutions;
}
@Transactional(readOnly = true)
public Page<GetSolutionWithGroupIdResponse> getMySolutionsInProgress(User user, Integer problemNumber,
String language,
String result,
Pageable pageable) {
Page<GetSolutionWithGroupIdResponse> inProgressSolutions = solutionRepository.findAllFilteredMySolutions(user,
problemNumber,
language,
result, ProgressCategory.IN_PROGRESS, pageable)
.map(solution -> this.getGetSolutionWithGroupIdResponse(user, solution));
log.info("success to get my in-progress solutions.");
return inProgressSolutions;
}
@Transactional(readOnly = true)
public Page<GetSolutionWithGroupIdResponse> getMySolutionsExpired(User user, Integer problemNumber, String language,
String result,
Pageable pageable) {
Page<GetSolutionWithGroupIdResponse> expiredSolutions = solutionRepository.findAllFilteredMySolutions(user,
problemNumber,
language,
result, ProgressCategory.EXPIRED, pageable)
.map(solution -> this.getGetSolutionWithGroupIdResponse(user, solution));
log.info("success to get my expired solutions.");
return expiredSolutions;
}
@Transactional(readOnly = true)
public List<GetCurrentSolvingStatusResponse> getCurrentSolvingStatuses(User user, Long groupId) {
StudyGroup group = validateGroupAndMember(user, groupId);
List<Problem> inProgressProblems = problemRepository.findAllInProgressProblem(group);
List<GroupMember> members = groupMemberRepository.findAllByStudyGroup(group);
Map<GroupMember, SolvedStatusResult> solvedStatuses = calculateMemberStatusRanks(members, inProgressProblems);
return createSolvingStatusResponses(solvedStatuses);
}
private Map<GroupMember, SolvedStatusResult> calculateMemberStatusRanks(List<GroupMember> members,
List<Problem> inProgressProblems) {
Map<GroupMember, SolvedStatusResult> ranks = new LinkedHashMap<>();
for (GroupMember member : members) {
int totalSubmissionCount = 0;
Duration totalPassedTime = Duration.ZERO;
List<GetSolvingStatusPerProblemResponse> statusResponses = new ArrayList<>();
for (Problem problem : inProgressProblems) {
List<Solution> solutions = solutionRepository.findAllByUserAndProblem(member.getUser(), problem);
int submissionCount = solutions.size();
Long firstCorrectSolutionId = null;
String firstCorrectDuration = "--";
boolean solved = false;
Optional<Solution> firstCorrectSolution = solutions.stream()
.filter(solution -> isCorrect(solution.getResult()))
.min(Comparator.comparing(Solution::getSolvedDateTime));
if (firstCorrectSolution.isPresent()) {
Solution solution = firstCorrectSolution.get();
firstCorrectSolutionId = solution.getId();
Duration duration = calculateGap(problem, solution);
totalPassedTime = totalPassedTime.plus(duration);
firstCorrectDuration = convertToSolvedTimeFormat(duration);
solved = true;
}
totalSubmissionCount += submissionCount;
statusResponses.add(new GetSolvingStatusPerProblemResponse(
problem.getId(), firstCorrectSolutionId,
submissionCount, firstCorrectDuration, solved
));
}
float totalScore = calculateTotalScore(totalSubmissionCount, totalPassedTime);
String formattedPassedTime = convertToSolvedTimeFormat(totalPassedTime);
ranks.put(member,
new SolvedStatusResult(totalScore, totalSubmissionCount, formattedPassedTime, statusResponses));
}
return ranks;
}
private Duration calculateGap(Problem problem, Solution solution) {
LocalDateTime startDate = problem.getStartDate().atStartOfDay();
LocalDateTime solvedDateTime = solution.getSolvedDateTime();
return Duration.between(startDate, solvedDateTime);
}
private float calculateTotalScore(int totalSubmissionCount, Duration totalPassedTime) {
float totalScore = 0;
if (!(totalSubmissionCount == 0 || totalPassedTime.isZero())) {
long minutes = totalPassedTime.toMinutes();
totalScore = (float)1 / (totalSubmissionCount * minutes);
}
return totalScore;
}
private List<GetCurrentSolvingStatusResponse> createSolvingStatusResponses(
Map<GroupMember, SolvedStatusResult> ranks) {
List<GroupMember> memberOrders = ranks.keySet().stream()
.sorted((m1, m2) -> Float.compare(ranks.get(m2).totalScore, ranks.get(m1).totalScore))
.toList();
List<GetCurrentSolvingStatusResponse> responses = new ArrayList<>();
for (int i = 0; i < memberOrders.size(); i++) {
GroupMember member = memberOrders.get(i);
responses.add(new GetCurrentSolvingStatusResponse(
i + 1, member.getUser().getNickname(),
ranks.get(member).totalSubmissionCount,
ranks.get(member).totalPassedTime,
ranks.get(member).problems
));
}
return responses;
}
private void sendNewSolutionNotification(StudyGroup group, GroupMember solver, Problem problem) {
List<GroupMember> groupMembers = groupMemberRepository.findAllByStudyGroup(group).stream()
.filter(member -> !member.getId().equals(solver.getId()))
.toList();
notificationService.sendNotificationToMembers(
group,
groupMembers,
problem,
null,
NotificationCategory.NEW_SOLUTION_POSTED,
NotificationCategory.NEW_SOLUTION_POSTED.getMessage(solver.getUser().getNickname())
);
}
private String convertToSolvedTimeFormat(Duration duration) {
long totalMinutes = duration.toMinutes();
long hours = totalMinutes / 60;
long minutes = totalMinutes % 60;
return String.format("%d:%02d", hours, minutes);
}
private GetSolutionWithGroupIdResponse getGetSolutionWithGroupIdResponse(User user, Solution solution) {
Integer correctCount = getCorrectCount(solution);
Integer submitMemberCount = solutionRepository.countDistinctUsersByProblem(solution.getProblem());
Integer totalMemberCount = groupMemberRepository.countMembersByStudyGroup(getGroup(solution)) + 1;
Integer accuracy = calculateAccuracy(submitMemberCount, correctCount);
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
boolean isRead = true;
if (isMySolution(user, solution)) {
isRead = isAllCommentsRead(solution);
}
return GetSolutionWithGroupIdResponse.toDTO(solution, accuracy, submitMemberCount, totalMemberCount,
commentCount, isRead);
}
private GetSolutionResponse getGetSolutionResponse(User user, Solution solution) {
Integer correctCount = getCorrectCount(solution);
Integer submitMemberCount = solutionRepository.countDistinctUsersByProblem(solution.getProblem());
Integer totalMemberCount = groupMemberRepository.countMembersByStudyGroup(getGroup(solution)) + 1;
Integer accuracy = calculateAccuracy(submitMemberCount, correctCount);
long commentCount = commentRepository.countCommentsBySolutionId(solution.getId());
boolean isRead = true;
if (isMySolution(user, solution)) {
isRead = isAllCommentsRead(solution);
}
return GetSolutionResponse.toDTO(solution, accuracy, submitMemberCount, totalMemberCount, commentCount, isRead);
}
private Integer getCorrectCount(Solution solution) {
return solutionRepository.countDistinctUsersWithCorrectSolutionsByProblemId(
solution.getProblem().getId(),
BOJResultConstants.CORRECT);
}
@Transactional
public void createSolution(CreateSolutionRequest request) {
List<User> users = userRepository.findAllByBjNickname(request.userName());
if (users.isEmpty()) {
log.warn("user {} not found for create solution", request.userName());
throw new UserValidationException("해당 아이디로 등록된 백준 유저는 없습니다.");
}
final LocalDateTime now = LocalDateTime.now();
for (User user : users) {
List<Problem> problems = problemRepository.findValidProblemsByNumberAndUser(
request.problemNumber(), LocalDate.from(now), user);
if (problems.isEmpty()) {
log.warn("problem {} not found for create solution, user : {}", request.problemNumber(), user.getId());
}
for (Problem problem : problems) {
final boolean isFirstCorrect =
isCorrect(request.result()) && !solutionRepository.existsByUserAndProblemAndResult(user, problem,
BOJResultConstants.CORRECT);
Long solutionId = solutionRepository.save(Solution.builder()
.problem(problem)
.user(user)
.content(request.code())
.memoryUsage(request.memoryUsage())
.executionTime(request.executionTime())
.language(request.codeType())
.codeLength(request.codeLength())
.result(request.result())
.solvedDateTime(now)
.build()
).getId();
GroupMember groupMember = groupMemberRepository.findByUserAndStudyGroup(user, problem.getStudyGroup())
.orElseThrow(() -> new IllegalStateException("Logic Error"));
if (isFirstCorrect) {
rankingService.updateScore(groupMember, problem.getEndDate(), now);
rankingUpdateService.updateRanking(problem.getStudyGroup());
}
sendNewSolutionNotification(problem.getStudyGroup(), groupMember, problem);
log.info("success to create solution for user: {}, solutionId: {}", user.getId(), solutionId);
}
}
}
private StudyGroup validateGroupAndMember(User user, Long groupId) {
StudyGroup group = studyGroupRepository.findById(groupId)
.orElseThrow(() -> new CannotFoundGroupException("존재하지 않는 그룹입니다."));
if (!groupMemberRepository.existsByUserAndStudyGroup(user, group)) {
throw new GroupMemberValidationException(HttpStatus.FORBIDDEN.value(), "참여하지 않은 그룹입니다.");
}
return group;
}
private boolean isCorrect(String result) {
return result.equals(BOJResultConstants.CORRECT) || result.endsWith("점");
}
private Integer calculateAccuracy(Integer submitMemberCount, Integer correctCount) {
if (submitMemberCount == 0)
return 0;
Double tempCorrectCount = correctCount.doubleValue();
Double tempSubmitMemberCount = submitMemberCount.doubleValue();
Double tempAccuracy = ((tempCorrectCount / tempSubmitMemberCount) * 100);
return tempAccuracy.intValue();
}
private StudyGroup getGroup(Solution solution) {
return solution.getProblem().getStudyGroup();
}
private boolean isMySolution(User user, Solution solution) {
return solution.getUser().getId().equals(user.getId());
}
private boolean isAllCommentsRead(Solution solution) {
List<SolutionComment> comments = solutionCommentRepository.findAllBySolution(solution);
for (SolutionComment solutionComment : comments) {
if (!solutionComment.isRead())
return false;
}
return true;
}
private record SolvedStatusResult(float totalScore,
int totalSubmissionCount,
String totalPassedTime,
List<GetSolvingStatusPerProblemResponse> problems) {
}
}