Skip to content

Commit

Permalink
Merge pull request #35 from Princess-in-silvertown/refactor/34
Browse files Browse the repository at this point in the history
Refactor: 전반적인 코드 리포맷, 리팩토링
  • Loading branch information
loveysuby authored Aug 22, 2024
2 parents 866ada6 + bfa98eb commit 23e493c
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 263 deletions.
25 changes: 11 additions & 14 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package slvtwn.khu.toyouserver.application;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.common.ErrorType;
import slvtwn.khu.toyouserver.domain.Group;
import slvtwn.khu.toyouserver.domain.Member;
import slvtwn.khu.toyouserver.domain.MemberRepository;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.GroupCreateRequest;
import slvtwn.khu.toyouserver.dto.GroupMemberResponse;
import slvtwn.khu.toyouserver.dto.GroupResponse;
import slvtwn.khu.toyouserver.exception.ToyouException;
import slvtwn.khu.toyouserver.persistance.GroupRepository;
import slvtwn.khu.toyouserver.persistance.MemberRepository;
import slvtwn.khu.toyouserver.persistance.UserRepository;

@Service
public class GroupService {

private final GroupRepository groupRepository;
private final MemberRepository memberRepository;
private final UserRepository userRepository;

public GroupService(GroupRepository groupRepository, MemberRepository memberRepository, UserRepository userRepository) {
public GroupService(GroupRepository groupRepository, MemberRepository memberRepository) {
this.groupRepository = groupRepository;
this.memberRepository = memberRepository;
this.userRepository = userRepository;
}

@Transactional
public void registerMember(long groupId, long userId) {
public void registerMember(long groupId, User user) {
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND));

memberRepository.save(new Member(user, group));
}
Expand All @@ -45,17 +41,18 @@ public List<GroupMemberResponse> findMembers(long groupId) {
}

@Transactional
public GroupResponse createGroup(String name) {
Group group = new Group(name);
public GroupResponse createGroup(GroupCreateRequest request) {
Group group = new Group(request.name());
Group savedGroup = groupRepository.save(group);
return new GroupResponse(savedGroup.getId(), savedGroup.getName());
}

@Transactional(readOnly = true)
public List<GroupResponse> findRegisteredGroups(long userId) {
List<Group> groups = memberRepository.findGroupsByUserId(userId);
return groups.stream()
.map(group -> new GroupResponse(group.getId(), group.getName()))
public List<GroupResponse> findRegisteredGroupsByUser(User user) {
// TODO: 쿼리 최적화
return memberRepository.findByUser(user).stream()
.map(Member::getGroup)
.map(each -> new GroupResponse(each.getId(), each.getName()))
.toList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package slvtwn.khu.toyouserver.application;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.common.ErrorType;
import slvtwn.khu.toyouserver.domain.Session;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.exception.ToyouException;
import slvtwn.khu.toyouserver.persistance.SessionRepository;

@Service
@Transactional(readOnly = true)
public class SessionService {

private final SessionRepository sessionRepository;

public SessionService(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}

public User findById(Long sessionId) {
Session session = sessionRepository.findById(sessionId)
.orElseThrow(() -> new ToyouException(ErrorType.UNAUTHORIZED_USER_ACCESS));
return session.getUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import slvtwn.khu.toyouserver.application.SessionService;

public class CookieAuthArgumentResolver implements HandlerMethodArgumentResolver {
private final HttpSession httpSession;
private final SessionUserService sessionUserService;
private final SessionService sessionService;

public CookieAuthArgumentResolver(HttpSession httpSession, SessionUserService sessionUserService) {
public CookieAuthArgumentResolver(HttpSession httpSession, SessionService sessionService) {
this.httpSession = httpSession;
this.sessionUserService = sessionUserService;
this.sessionService = sessionService;
}

@Override
Expand All @@ -24,7 +25,7 @@ public boolean supportsParameter(MethodParameter parameter) {
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
String sessionId = (String) httpSession.getAttribute("sessionId");
return sessionUserService.findUserBySessionId(sessionId);
Long sessionId = Long.parseLong((String) httpSession.getAttribute("sessionId"));
return sessionService.findById(sessionId);
}
}

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/slvtwn/khu/toyouserver/common/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.util.List;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import slvtwn.khu.toyouserver.application.SessionService;

public class WebConfig implements WebMvcConfigurer {

private HttpSession httpSession;

private SessionUserService sessionUserService;
private SessionService sessionService;

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(new CookieAuthArgumentResolver(httpSession, sessionUserService));
resolvers.add(new CookieAuthArgumentResolver(httpSession, sessionService));
}
}
35 changes: 35 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/domain/Session.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package slvtwn.khu.toyouserver.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class Session extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private User user;

// TODO ; 카드

public Session(Long id, User user) {
this.id = id;
this.user = user;
}
}
31 changes: 0 additions & 31 deletions src/main/java/slvtwn/khu/toyouserver/domain/SessionUser.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@
public class ToyouException extends RuntimeException {

private final ErrorType errorType;

public String code() {
return errorType.code();
}

public String message() {
return errorType.message();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package slvtwn.khu.toyouserver.domain;
package slvtwn.khu.toyouserver.persistance;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import slvtwn.khu.toyouserver.domain.Group;
import slvtwn.khu.toyouserver.domain.Member;
import slvtwn.khu.toyouserver.domain.User;

@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
List<Member> findByGroupId(Long groupId);
List<Member> findByUser(User user);

@Query("SELECT m.group FROM Member m WHERE m.user.id = :userId")
List<Group> findGroupsByUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package slvtwn.khu.toyouserver.persistance;

import org.springframework.data.jpa.repository.JpaRepository;
import slvtwn.khu.toyouserver.domain.Session;

public interface SessionRepository extends JpaRepository<Session, Long> {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import slvtwn.khu.toyouserver.application.GroupService;
import slvtwn.khu.toyouserver.common.UserAuthentication;
Expand All @@ -19,25 +18,25 @@
@RestController
public class GroupController {

private final GroupService groupService;
private final GroupService groupService;

@PostMapping("/groups")
public GroupResponse createGroup(@RequestBody GroupCreateRequest request) {
return groupService.createGroup(request.name());
}
@PostMapping("/groups")
public GroupResponse createGroup(@RequestBody GroupCreateRequest request) {
return groupService.createGroup(request);
}

@GetMapping("/groups")
public List<GroupResponse> findRegisteredGroups(@RequestParam Long memberId) {
return groupService.findRegisteredGroups(memberId);
}
@GetMapping("/groups")
public List<GroupResponse> findRegisteredGroups(@UserAuthentication User user) {
return groupService.findRegisteredGroupsByUser(user);
}

@PostMapping("/groups/{groupId}/members")
public void registerMember(@PathVariable Long groupId) {
groupService.registerMember(groupId, 1L); // TODO: user -> argumentResolver 등록 필요
}
@PostMapping("/groups/{groupId}/members")
public void registerMember(@UserAuthentication User user, @PathVariable Long groupId) {
groupService.registerMember(groupId, user);
}

@GetMapping("/groups/{groupId}/members")
public List<GroupMemberResponse> findMembers(@PathVariable Long groupId) {
return groupService.findMembers(groupId);
}
@GetMapping("/groups/{groupId}/members")
public List<GroupMemberResponse> findMembers(@PathVariable Long groupId) {
return groupService.findMembers(groupId);
}
}
Loading

0 comments on commit 23e493c

Please sign in to comment.