-
Notifications
You must be signed in to change notification settings - Fork 2
/
GroupService.java
71 lines (62 loc) · 2.98 KB
/
GroupService.java
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
package com.amorgakco.backend.group.service;
import com.amorgakco.backend.global.IdResponse;
import com.amorgakco.backend.global.exception.GroupAuthorityException;
import com.amorgakco.backend.global.exception.ResourceNotFoundException;
import com.amorgakco.backend.group.domain.Group;
import com.amorgakco.backend.group.dto.GroupBasicResponse;
import com.amorgakco.backend.group.dto.GroupDetailResponse;
import com.amorgakco.backend.group.dto.GroupRegisterRequest;
import com.amorgakco.backend.group.repository.GroupRepository;
import com.amorgakco.backend.group.service.mapper.GroupMapper;
import com.amorgakco.backend.member.domain.Member;
import com.amorgakco.backend.groupapplication.repository.GroupApplicationRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class GroupService {
private final GroupRepository groupRepository;
private final GroupMapper groupMapper;
private final GroupApplicationRepository groupApplicationRepository;
@Transactional
public IdResponse register(final GroupRegisterRequest request, final Member host) {
final Group group = groupMapper.toGroup(host, request);
final Long groupId = groupRepository.save(group).getId();
return new IdResponse(groupId);
}
@Transactional
public void delete(final Member member, final Long groupId) {
final Group group = getGroup(groupId);
group.validateGroupHost(member);
groupRepository.delete(group);
}
public Group getGroup(final Long groupId) {
return groupRepository
.findById(groupId)
.orElseThrow(ResourceNotFoundException::groupNotFound);
}
public Group getGroupWithHost(final Long groupId) {
return groupRepository
.findByIdWithHost(groupId)
.orElseThrow(ResourceNotFoundException::groupNotFound);
}
public GroupDetailResponse getDetailGroup(final Long groupId, final Long memberId) {
final Group group = getGroup(groupId);
final boolean isGroupHost = group.isGroupHost(memberId);
return groupMapper.toGroupDetailResponse(group, isGroupHost);
}
public GroupBasicResponse getBasicGroup(final Long groupId, final Member member) {
final Group group =
groupRepository
.findByIdWithHost(groupId)
.orElseThrow(ResourceNotFoundException::groupNotFound);
boolean isParticipated = group.isMemberParticipated(member.getId());
boolean isParticipationRequested = isParticipationRequested(group, member);
return groupMapper.toGroupBasicInfoResponse(group, isParticipated, isParticipationRequested);
}
private boolean isParticipationRequested(final Group group, final Member member){
return groupApplicationRepository.existsByGroupAndParticipant(group,member);
}
}