Skip to content

Commit

Permalink
🔥 refactor: conflict 해결 및 코드 구조 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
woosung1223 committed Jul 7, 2024
2 parents a55e7c1 + b9e9719 commit c721fbb
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 116 deletions.
47 changes: 25 additions & 22 deletions src/main/java/slvtwn/khu/toyouserver/application/GroupService.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package slvtwn.khu.toyouserver.application;

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

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

private final GroupRepository groupRepository;
private final GroupRepository groupRepository;
private final UserRepository userRepository;

private final UserService userService;
public GroupService(GroupRepository groupRepository, UserRepository userRepository) {
this.groupRepository = groupRepository;
this.userRepository = userRepository;
}

public GroupService(GroupRepository groupRepository, UserService userService) {
this.groupRepository = groupRepository;
this.userService = userService;
}
@Transactional
public GroupResponse registerUser(long groupId, long userId) {
Group group = groupRepository.findById(groupId)
.orElseThrow(() -> new ToyouException(ErrorType.GROUP_NOT_FOUND));
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND));

public GroupResponse registerUser(final long groupId, final long userId) {
final var group = groupRepository.getById(groupId);
final var user = userService.findById(userId);
// TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다.
// TODO : 그룹에 유저를 가입시킨다. 유저는 멤버로 등록된다.
// group.addMember(user);
return new GroupResponse(group.getId(), group.getName());
}

public List<GroupMemberResponse> findAllMembers(final long groupId) {
final var group = groupRepository.getById(groupId);
return List.of(new GroupMemberResponse(1L, 1L, "user1", "profile1"));
}
}
return new GroupResponse(group.getId(), group.getName());
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package slvtwn.khu.toyouserver.application;

import org.springframework.stereotype.Service;
import slvtwn.khu.toyouserver.common.ErrorType;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.domain.UserRepository;

import slvtwn.khu.toyouserver.exception.ToyouException;
import slvtwn.khu.toyouserver.persistance.UserRepository;
import slvtwn.khu.toyouserver.dto.UserResponse;

@Service
public class UserService {
Expand All @@ -14,7 +16,10 @@ public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public User findById(final long userId) {
return userRepository.getById(userId);
public UserResponse findUser(Long userId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ErrorType.USER_NOT_FOUND));

return new UserResponse(user.getId(), user.getName(), user.getProfilePicture());
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package slvtwn.khu.toyouserver.persistance;
package slvtwn.khu.toyouserver.common;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package slvtwn.khu.toyouserver.common;

// TODO: 효율적인 예외처리 Wrapping 과정에 대해 고민
public record ToyouResponse<T>(String code, T data) {

public static <T> ToyouResponse<T> success(T data) {
return new ToyouResponse<>("SUCCESS", data);
}
}
2 changes: 1 addition & 1 deletion src/main/java/slvtwn/khu/toyouserver/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import jakarta.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.persistance.BaseTimeEntity;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/slvtwn/khu/toyouserver/domain/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.persistance.BaseTimeEntity;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@Table(name = "groups")
Expand Down
24 changes: 0 additions & 24 deletions src/main/java/slvtwn/khu/toyouserver/domain/GroupRepository.java

This file was deleted.

26 changes: 13 additions & 13 deletions src/main/java/slvtwn/khu/toyouserver/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.persistance.BaseTimeEntity;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

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

@Id
@GeneratedValue
private Long id;
@Id
@GeneratedValue
private Long id;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id")
private Group group;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "group_id")
private Group group;

public Member(Group group) {
this.group = group;
}
public Member(Group group) {
this.group = group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.persistance.BaseTimeEntity;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/slvtwn/khu/toyouserver/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import slvtwn.khu.toyouserver.persistance.BaseTimeEntity;
import slvtwn.khu.toyouserver.common.BaseTimeEntity;

@Entity
@Table(name = "users")
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/slvtwn/khu/toyouserver/domain/UserRepository.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/dto/GroupResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package slvtwn.khu.toyouserver.dto;

public record GroupResponse(Long id, String name) {
}
4 changes: 4 additions & 0 deletions src/main/java/slvtwn/khu/toyouserver/dto/UserResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package slvtwn.khu.toyouserver.dto;

public record UserResponse(Long id, String name, String imageUrl) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

@RequiredArgsConstructor
@Getter
public final class ToyouException extends RuntimeException {
public class ToyouException extends RuntimeException {

private final ErrorType errorType;

public String code() {
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.Group;

public interface GroupRepository extends JpaRepository<Group, Long> {
}
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.User;

public interface UserRepository extends JpaRepository<User, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package slvtwn.khu.toyouserver.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import slvtwn.khu.toyouserver.application.GroupService;
import slvtwn.khu.toyouserver.dto.GroupResponse;

@RequiredArgsConstructor
@RestController
public class GroupController {

private final GroupService groupService;
private final GroupService groupService;

public GroupController(GroupService groupService) {
this.groupService = groupService;
}

@PostMapping("/groups/{groupId}/members")
public GroupResponse registerMember(@PathVariable final long groupId) {
return groupService.registerMember(groupId);
}
@PostMapping("/groups/{groupId}/members")
public GroupResponse registerMember(@PathVariable long groupId) {
return groupService.registerUser(groupId, 1L); // TODO: user -> argumentResolver 등록 필요
}
}

This file was deleted.

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

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import slvtwn.khu.toyouserver.application.UserService;
import slvtwn.khu.toyouserver.common.ToyouResponse;
import slvtwn.khu.toyouserver.dto.UserResponse;

@RequiredArgsConstructor
@RestController
public class UserController {

private final UserService userService;

@GetMapping("/users/{userId}")
public ToyouResponse<UserResponse> findUser(@PathVariable Long userId) {
UserResponse userResponse = userService.findUser(userId);

return ToyouResponse.success(userResponse);
}
}
19 changes: 9 additions & 10 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
spring:
data-source:
url: jdbc:h2:mem:test
driver-class-name: org.h2.Driver
username: sa
password:

jpa:
database: mysql
# database: mysql
open-in-view: false
show-sql: true
database-platform: org.hibernate.dialect.H2Dialect
generate-ddl: true
show-sql: true
properties:
hibernate:
format_sql: true
ddl-auto: update

datasource:
url: jdbc:h2:mem:testdb
driver-class-name: org.h2.Driver
username: sa
password:

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package slvtwn.khu.toyouserver.user.service;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.application.UserService;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.persistance.UserRepository;
import slvtwn.khu.toyouserver.dto.UserResponse;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SpringBootTest
@Transactional
class UserServiceTest {

@Autowired
private UserRepository userRepository;
@Autowired
private UserService userService;

@Test
void 유저를_조회할_수_있다() {
// given
User user = new User("teo", "www.profile-picture.com");
userRepository.save(user);

// when
UserResponse found = userService.findUser(user.getId());

// then
SoftAssertions.assertSoftly(softly -> {
softly.assertThat(user.getId()).isEqualTo(found.id());
softly.assertThat(user.getName()).isEqualTo(found.name());
softly.assertThat(user.getProfilePicture()).isEqualTo(found.imageUrl());
});
}
}

0 comments on commit c721fbb

Please sign in to comment.