From 78b0a51d751ce832e9a7e710989f10894f8a9fbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A7=84=EC=A3=BC=EC=9B=90?= <84346055+jinjoo-lab@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:21:06 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]=20GroupController=20+=20Service=20FIX=20?= =?UTF-8?q?+=20WebSocket=20Config=20FIX=20+=20(=EC=9C=84=EC=B9=98=20?= =?UTF-8?q?=EA=B3=B5=EC=9C=A0=20on/off=20=EC=B6=94=EA=B0=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/security/SecurityConfig.java | 2 +- .../backend/config/socket/StompConfig.java | 2 +- .../group/controller/GroupController.java | 29 ++++++++++++++---- .../group/dto/request/JoinGroupDto.java | 12 ++++++++ .../group/dto/request/MakeGroupDto.java | 3 ++ .../group/dto/response/GroupInfoDto.java | 3 +- .../backend/domain/group/entity/Group.java | 4 ++- .../domain/group/entity/GroupMember.java | 7 +++++ .../domain/group/mapper/GroupMapper.java | 9 +++++- .../repository/GroupMemberRepository.java | 9 +++++- .../domain/group/service/GroupService.java | 30 +++++++++++++++---- .../controller/LocationController.java | 3 ++ .../location/service/LocationService.java | 2 ++ .../com/twtw/backend/group/GroupTest.java | 5 ++-- 14 files changed, 101 insertions(+), 19 deletions(-) create mode 100644 backend/src/main/java/com/twtw/backend/domain/group/dto/request/JoinGroupDto.java diff --git a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java index 120b645f..79b72810 100644 --- a/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/security/SecurityConfig.java @@ -38,7 +38,7 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception { "auth/save", "auth/login", "member/duplicate/**", - "socket/**") + "plan/**") .permitAll()) .authorizeHttpRequests( x -> x.requestMatchers("/test/**").permitAll().anyRequest().authenticated()) diff --git a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java index 75e1a185..62d0d44f 100644 --- a/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java +++ b/backend/src/main/java/com/twtw/backend/config/socket/StompConfig.java @@ -19,7 +19,7 @@ public class StompConfig implements WebSocketMessageBrokerConfigurer { @Override public void registerStompEndpoints(final StompEndpointRegistry registry) { - registry.addEndpoint("/socket").setAllowedOrigins("*"); + registry.addEndpoint("/plan").setAllowedOrigins("*"); } @Override diff --git a/backend/src/main/java/com/twtw/backend/domain/group/controller/GroupController.java b/backend/src/main/java/com/twtw/backend/domain/group/controller/GroupController.java index 078c1ce4..7daf63b6 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/controller/GroupController.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/controller/GroupController.java @@ -1,11 +1,12 @@ package com.twtw.backend.domain.group.controller; +import com.twtw.backend.domain.group.dto.request.JoinGroupDto; +import com.twtw.backend.domain.group.dto.request.MakeGroupDto; +import com.twtw.backend.domain.group.dto.response.GroupInfoDto; import com.twtw.backend.domain.group.service.GroupService; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/group") @@ -18,5 +19,23 @@ public GroupController(GroupService groupService) { // 자신이 속한 그룹 반환 API @GetMapping("/get/{id}") - public void getMyGroups(@PathVariable String id) {} + public ResponseEntity getGroupByGroupId(@PathVariable String id) { + return ResponseEntity.ok(groupService.getGroupByGroupId(id)); + } + + @PostMapping("/make") + public ResponseEntity makeGroup(@RequestBody MakeGroupDto makeGroupDto){ + return ResponseEntity.ok(groupService.makeGroup(makeGroupDto)); + } + + @PostMapping("/join") + public ResponseEntity joinGroup(@RequestBody JoinGroupDto joinGroupDto){ + return ResponseEntity.ok(groupService.joinGroup(joinGroupDto)); + } + + @PutMapping("/share/{group}") + public ResponseEntity changeShare(@PathVariable String group){ + groupService.changeShare(group); + return ResponseEntity.ok().build(); + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/dto/request/JoinGroupDto.java b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/JoinGroupDto.java new file mode 100644 index 00000000..2ecc99fe --- /dev/null +++ b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/JoinGroupDto.java @@ -0,0 +1,12 @@ +package com.twtw.backend.domain.group.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class JoinGroupDto { + private String groupId; +} diff --git a/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java index 76520dcf..58fef86d 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/dto/request/MakeGroupDto.java @@ -4,10 +4,13 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.UUID; + @Getter @NoArgsConstructor @AllArgsConstructor public class MakeGroupDto { private String name; private String groupImage; + private String leaderId; } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java b/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java index ca3329ac..40806f37 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/dto/response/GroupInfoDto.java @@ -10,7 +10,8 @@ @NoArgsConstructor @AllArgsConstructor public class GroupInfoDto { - private UUID id; + private UUID groupId; + private UUID leaderId; private String name; private String groupImage; } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java index 90bddb47..6fa9334d 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java @@ -21,14 +21,16 @@ public class Group { private String name; private String groupImage; + private UUID leader; @OneToMany( mappedBy = "group", cascade = {CascadeType.PERSIST, CascadeType.MERGE}) private List groupMembers = new ArrayList<>(); - public Group(String name, String groupImage) { + public Group(String name, String groupImage, UUID leader) { this.name = name; this.groupImage = groupImage; + this.leader = leader; } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java b/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java index f23785b2..81c4a3a4 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/entity/GroupMember.java @@ -27,10 +27,17 @@ public class GroupMember { @JoinColumn(name = "member_id") private Member member; + private Boolean share; + public GroupMember(Group group, Member member) { this.group = group; this.member = member; + this.share = true; group.getGroupMembers().add(this); member.getGroupMembers().add(this); } + + public void changeShare(){ + this.share = !this.share; + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java b/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java index dff06776..f5a49719 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/mapper/GroupMapper.java @@ -1,12 +1,15 @@ package com.twtw.backend.domain.group.mapper; import com.twtw.backend.domain.group.dto.request.MakeGroupDto; +import com.twtw.backend.domain.group.dto.response.GroupInfoDto; import com.twtw.backend.domain.group.entity.Group; import com.twtw.backend.domain.group.entity.GroupMember; import com.twtw.backend.domain.member.entity.Member; import org.springframework.stereotype.Component; +import java.util.UUID; + @Component public class GroupMapper { public GroupMember connectGroupMember(Group group, Member member) { @@ -14,6 +17,10 @@ public GroupMember connectGroupMember(Group group, Member member) { } public Group toGroupEntity(MakeGroupDto groupDto) { - return new Group(groupDto.getName(), groupDto.getGroupImage()); + return new Group(groupDto.getName(), groupDto.getGroupImage(), UUID.fromString(groupDto.getLeaderId())); + } + + public GroupInfoDto toGroupInfo(Group group){ + return new GroupInfoDto(group.getId(),group.getLeader(),group.getName(),group.getGroupImage()); } } diff --git a/backend/src/main/java/com/twtw/backend/domain/group/repository/GroupMemberRepository.java b/backend/src/main/java/com/twtw/backend/domain/group/repository/GroupMemberRepository.java index 5b242778..bdbb11dc 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/repository/GroupMemberRepository.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/repository/GroupMemberRepository.java @@ -3,7 +3,14 @@ import com.twtw.backend.domain.group.entity.GroupMember; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import java.util.Optional; import java.util.UUID; -public interface GroupMemberRepository extends JpaRepository {} +public interface GroupMemberRepository extends JpaRepository { + + @Query("SELECT gm from GroupMember gm WHERE gm.group.id = :groupID AND " + + "gm.member.id = :memberID") + Optional findByGroupIdAndMemberId(UUID groupId,UUID memberId); +} diff --git a/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java b/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java index 982575c0..633fba9b 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/service/GroupService.java @@ -1,9 +1,12 @@ package com.twtw.backend.domain.group.service; +import com.twtw.backend.domain.group.dto.request.JoinGroupDto; import com.twtw.backend.domain.group.dto.request.MakeGroupDto; +import com.twtw.backend.domain.group.dto.response.GroupInfoDto; import com.twtw.backend.domain.group.entity.Group; import com.twtw.backend.domain.group.entity.GroupMember; import com.twtw.backend.domain.group.mapper.GroupMapper; +import com.twtw.backend.domain.group.repository.GroupMemberRepository; import com.twtw.backend.domain.group.repository.GroupRepository; import com.twtw.backend.domain.member.entity.Member; import com.twtw.backend.domain.member.service.AuthService; @@ -17,31 +20,48 @@ @Service public class GroupService { private final GroupRepository groupRepository; + private final GroupMemberRepository groupMemberRepository; private final AuthService authService; private final GroupMapper groupMapper; public GroupService( - GroupRepository groupRepository, AuthService authService, GroupMapper groupMapper) { + GroupRepository groupRepository, GroupMemberRepository groupMemberRepository, AuthService authService, GroupMapper groupMapper) { this.groupRepository = groupRepository; + this.groupMemberRepository = groupMemberRepository; this.authService = authService; this.groupMapper = groupMapper; } + public GroupInfoDto getGroupByGroupId(String groupId){ + return groupMapper.toGroupInfo(groupRepository.findById(UUID.fromString(groupId)).orElseThrow(EntityNotFoundException::new)); + } + @Transactional - public void makeGroup(MakeGroupDto groupDto) { + public GroupInfoDto makeGroup(MakeGroupDto groupDto) { Member member = authService.getMemberByJwt(); Group group = groupMapper.toGroupEntity(groupDto); GroupMember groupMember = groupMapper.connectGroupMember(group, member); - groupRepository.save(group); + return groupMapper.toGroupInfo(groupRepository.save(group)); } @Transactional - public void joinGroup(UUID groupId) { + public GroupInfoDto joinGroup(JoinGroupDto joinGroupDto) { Member member = this.authService.getMemberByJwt(); - Group group = groupRepository.findById(groupId).orElseThrow(EntityNotFoundException::new); + Group group = groupRepository.findById(UUID.fromString(joinGroupDto.getGroupId())).orElseThrow(EntityNotFoundException::new); groupMapper.connectGroupMember(group, member); + + return groupMapper.toGroupInfo(group); + } + + @Transactional + public void changeShare(String id){ + Member member = this.authService.getMemberByJwt(); + GroupInfoDto groupInfo = getGroupByGroupId(id); + + GroupMember groupMember = groupMemberRepository.findByGroupIdAndMemberId(groupInfo.getGroupId(),member.getId()).orElseThrow(EntityNotFoundException::new); + groupMember.changeShare(); } public void removeGroup(UUID groupId) { diff --git a/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java b/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java index 1a959ba8..de4a216d 100644 --- a/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java +++ b/backend/src/main/java/com/twtw/backend/domain/location/controller/LocationController.java @@ -29,4 +29,7 @@ public void share( rabbitTemplate.convertAndSend( EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest)); } + + + } diff --git a/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java index c76672f6..a17ace7f 100644 --- a/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java +++ b/backend/src/main/java/com/twtw/backend/domain/location/service/LocationService.java @@ -19,4 +19,6 @@ public class LocationService { public LocationResponse addInfo(final LocationRequest locationRequest) { return locationMapper.toResponse(locationRequest, LocalDateTime.now()); } + + } diff --git a/backend/src/test/java/com/twtw/backend/group/GroupTest.java b/backend/src/test/java/com/twtw/backend/group/GroupTest.java index a5db9698..60a3ae0d 100644 --- a/backend/src/test/java/com/twtw/backend/group/GroupTest.java +++ b/backend/src/test/java/com/twtw/backend/group/GroupTest.java @@ -15,12 +15,11 @@ @ActiveProfiles("test") @Import(QuerydslConfig.class) public class GroupTest { - @Autowired private GroupRepository groupRepository; + @Autowired private GroupRepository groupRepository; @Test @Transactional void saveGroup() { - Group group = new Group("HDJ", "1111"); - Group regroup = groupRepository.save(group); + } }