Skip to content

Commit

Permalink
[FEAT] GroupController + Service FIX + WebSocket Config FIX + (위치 공유 …
Browse files Browse the repository at this point in the history
…on/off 추가)
  • Loading branch information
jinjoo-lab committed Oct 16, 2023
1 parent 5cd7ef1 commit 78b0a51
Show file tree
Hide file tree
Showing 14 changed files with 101 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -18,5 +19,23 @@ public GroupController(GroupService groupService) {

// 자신이 속한 그룹 반환 API
@GetMapping("/get/{id}")
public void getMyGroups(@PathVariable String id) {}
public ResponseEntity<GroupInfoDto> getGroupByGroupId(@PathVariable String id) {
return ResponseEntity.ok(groupService.getGroupByGroupId(id));
}

@PostMapping("/make")
public ResponseEntity<GroupInfoDto> makeGroup(@RequestBody MakeGroupDto makeGroupDto){
return ResponseEntity.ok(groupService.makeGroup(makeGroupDto));
}

@PostMapping("/join")
public ResponseEntity<GroupInfoDto> joinGroup(@RequestBody JoinGroupDto joinGroupDto){
return ResponseEntity.ok(groupService.joinGroup(joinGroupDto));
}

@PutMapping("/share/{group}")
public ResponseEntity<Void> changeShare(@PathVariable String group){
groupService.changeShare(group);
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@NoArgsConstructor
@AllArgsConstructor
public class GroupInfoDto {
private UUID id;
private UUID groupId;
private UUID leaderId;
private String name;
private String groupImage;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupMember> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
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) {
return new GroupMember(group, 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupMember, UUID> {}
public interface GroupMemberRepository extends JpaRepository<GroupMember, UUID> {

@Query("SELECT gm from GroupMember gm WHERE gm.group.id = :groupID AND " +
"gm.member.id = :memberID")
Optional<GroupMember> findByGroupIdAndMemberId(UUID groupId,UUID memberId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ public void share(
rabbitTemplate.convertAndSend(
EXCHANGE_NAME, ROUTING_KEY + planId, locationService.addInfo(locationRequest));
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public class LocationService {
public LocationResponse addInfo(final LocationRequest locationRequest) {
return locationMapper.toResponse(locationRequest, LocalDateTime.now());
}


}
5 changes: 2 additions & 3 deletions backend/src/test/java/com/twtw/backend/group/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
}

0 comments on commit 78b0a51

Please sign in to comment.