Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,18 @@ public enum ErrorCode implements ResponseCode {
API_METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, 40500, "허용되지 않는 HTTP 메소드입니다."),
API_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 50000, "서버 내부 오류입니다."),

API_BAD_REQUEST(HttpStatus.BAD_REQUEST, 40002, "잘못된 요청입니다."),
API_BAD_REQUEST(HttpStatus.BAD_REQUEST, 40000, "잘못된 요청입니다."),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이거 잘못되어 있었네요,, 수정 감사합니다 ~

API_MISSING_PARAM(HttpStatus.BAD_REQUEST, 40001, "필수 파라미터가 없습니다."),
API_INVALID_PARAM(HttpStatus.BAD_REQUEST, 40002, "파라미터 값 중 유효하지 않은 값이 있습니다."),
API_INVALID_TYPE(HttpStatus.BAD_REQUEST, 40003, "파라미터 타입이 잘못되었습니다."),

/* 60000부터 비즈니스 예외 */
/**
* 60000 : alias error
*/
ALIAS_NOT_FOUND(HttpStatus.NOT_FOUND, 60001, "존재하지 않는 ALIAS 입니다.");


;

private final HttpStatus httpStatus;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
package konkuk.thip.user.adapter.in.web;

import konkuk.thip.common.dto.BaseResponse;
import konkuk.thip.user.adapter.in.web.request.UserSignupRequest;
import konkuk.thip.user.adapter.in.web.response.UserSignupResponse;
import konkuk.thip.user.application.port.in.UserSignupUseCase;
import konkuk.thip.user.application.port.in.dto.UserSignupCommand;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class UserCommandController {

private final UserSignupUseCase userSignupUseCase;

@PostMapping("/users/signup")
public BaseResponse<UserSignupResponse> signup(@Validated @RequestBody UserSignupRequest request) {
return BaseResponse.ok(UserSignupResponse.of(
userSignupUseCase.signup(request.toCommand()))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
package konkuk.thip.user.adapter.in.web.request;

import lombok.Getter;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import konkuk.thip.user.application.port.in.dto.UserSignupCommand;
import org.hibernate.validator.constraints.Length;

@Getter
public class UserSignupRequest {
public record UserSignupRequest(
@NotNull(message = "aliasId는 필수입니다.")
Long aliasId,

@NotBlank(message = "닉네임은 공백일 수 없습니다.")
@Length(max = 10, message = "닉네임은 최대 10자 입니다.")
String nickname,

@NotBlank(message = "이메일은 공백일 수 없습니다.")
@Email(message = "이메일 형식이 올바르지 않습니다.")
String email
) {
public UserSignupCommand toCommand() {
return UserSignupCommand.builder()
.aliasId(aliasId)
.nickname(nickname)
.email(email)
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package konkuk.thip.user.adapter.in.web.response;

public record UserSignupResponse(Long userId) {
public static UserSignupResponse of(Long userId) {
return new UserSignupResponse(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ public class AliasJpaEntity extends BaseJpaEntity {
@Column(name = "alias_id")
private Long aliasId;

@Column(name = "alias_value",length = 50, nullable = false)
@Column(name = "alias_value", length = 50, nullable = false)
private String value;

@Column(name = "image_url", columnDefinition = "TEXT", nullable = false)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻

private String imageUrl;

@Column(name = "alias_color", length = 10, nullable = false)
private String color;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class UserJpaEntity extends BaseJpaEntity {
@Column(name = "user_id")
private Long userId;

@Column(name = "email", length = 100, nullable = false)
private String email;

@Column(length = 60, nullable = false)
private String nickname;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ public class AliasMapper {
public AliasJpaEntity toJpaEntity(Alias alias) {
return AliasJpaEntity.builder()
.value(alias.getValue())
.imageUrl(alias.getImageUrl())
.color(alias.getColor())
.build();
}

public Alias toDomainEntity(AliasJpaEntity aliasJpaEntity) {
return Alias.builder()
.id(aliasJpaEntity.getAliasId())
.value(aliasJpaEntity.getValue())
.imageUrl(aliasJpaEntity.getImageUrl())
.color(aliasJpaEntity.getColor())
.createdAt(aliasJpaEntity.getCreatedAt())
.modifiedAt(aliasJpaEntity.getModifiedAt())
.status(aliasJpaEntity.getStatus())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UserMapper {

public UserJpaEntity toJpaEntity(User user, AliasJpaEntity aliasJpaEntity) {
return UserJpaEntity.builder()
.email(user.getEmail())
.nickname(user.getNickname())
.imageUrl(user.getImageUrl())
.role(UserRole.from(user.getUserRole()))
Expand All @@ -21,6 +22,7 @@ public UserJpaEntity toJpaEntity(User user, AliasJpaEntity aliasJpaEntity) {
public User toDomainEntity(UserJpaEntity userJpaEntity) {
return User.builder()
.id(userJpaEntity.getUserId())
.email(userJpaEntity.getEmail())
.nickname(userJpaEntity.getNickname())
.imageUrl(userJpaEntity.getImageUrl())
.userRole(userJpaEntity.getRole().getType())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package konkuk.thip.user.adapter.out.persistence;

import konkuk.thip.common.exception.EntityNotFoundException;
import konkuk.thip.user.adapter.out.jpa.AliasJpaEntity;
import konkuk.thip.user.adapter.out.mapper.AliasMapper;
import konkuk.thip.user.application.port.out.AliasCommandPort;
import konkuk.thip.user.domain.Alias;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import static konkuk.thip.common.exception.code.ErrorCode.ALIAS_NOT_FOUND;

@Repository
@RequiredArgsConstructor
public class AliasCommandPersistenceAdapter implements AliasCommandPort {

private final AliasMapper aliasMapper;
private final AliasJpaRepository aliasJpaRepository;

@Override
public Alias findById(Long aliasId) {
AliasJpaEntity aliasJpaEntity = aliasJpaRepository.findById(aliasId).orElseThrow(
() -> new EntityNotFoundException(ALIAS_NOT_FOUND));

return aliasMapper.toDomainEntity(aliasJpaEntity);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package konkuk.thip.user.adapter.out.persistence;

import konkuk.thip.common.exception.EntityNotFoundException;
import konkuk.thip.user.adapter.out.jpa.AliasJpaEntity;
import konkuk.thip.user.adapter.out.jpa.UserJpaEntity;
import konkuk.thip.user.adapter.out.mapper.UserMapper;
import konkuk.thip.user.application.port.out.UserCommandPort;
import konkuk.thip.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import static konkuk.thip.common.exception.code.ErrorCode.ALIAS_NOT_FOUND;

@Repository
@RequiredArgsConstructor
public class UserCommandPersistenceAdapter implements UserCommandPort {

private final UserJpaRepository jpaRepository;
private final UserJpaRepository userJpaRepository;
private final AliasJpaRepository aliasJpaRepository;

private final UserMapper userMapper;

@Override
public Long save(User user) {
AliasJpaEntity aliasJpaEntity = aliasJpaRepository.findById(user.getAliasId()).orElseThrow(
() -> new EntityNotFoundException(ALIAS_NOT_FOUND));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏻👍🏻


UserJpaEntity userJpaEntity = userMapper.toJpaEntity(user, aliasJpaEntity);
return userJpaRepository.save(userJpaEntity).getUserId();
}
Comment on lines +23 to +30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 : 음 저는 여기서 id를 반환하지 않고 User 엔티티 자체를 반환한 후 컨트롤러 핸들러 안의 response dto의 매개변수로 User 엔티티 자체를 넣어서 그 안에서 파싱되도록 하는 것이 더 캡슐화가 잘 지켜지는 것 아닐까 싶은데 다른 분들은 어떻게 생각하시나욥

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음 저는 User 도메인은 application 패키지를 벗어나지 않는게 좋다고 생각합니다!
아니면 persistenceAdapter 의 save 메서드가 User를 반환하고, service에서 controller 로 데이터를 반환할때는 User.getId() 로 Long type의 id값을 반환하는 방법도 있을거 같습니다

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package konkuk.thip.user.application.port.in;

import konkuk.thip.user.application.port.in.dto.UserSignupCommand;

public interface UserSignupUseCase {

Long signup(UserSignupCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package konkuk.thip.user.application.port.in.dto;

import lombok.Builder;

@Builder
public record UserSignupCommand(
Long aliasId,
String nickname,
String email
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package konkuk.thip.user.application.port.out;

import konkuk.thip.user.domain.Alias;

public interface AliasCommandPort {

Alias findById(Long aliasId);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package konkuk.thip.user.application.port.out;

import konkuk.thip.user.domain.User;

public interface UserCommandPort {

Long save(User user);

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package konkuk.thip.user.application.service;

import konkuk.thip.user.application.port.in.UserSignupUseCase;
import konkuk.thip.user.application.port.in.dto.UserSignupCommand;
import konkuk.thip.user.application.port.out.AliasCommandPort;
import konkuk.thip.user.application.port.out.UserCommandPort;
import konkuk.thip.user.domain.Alias;
import konkuk.thip.user.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static konkuk.thip.user.adapter.out.jpa.UserRole.USER;


@Service
@RequiredArgsConstructor
public class UserSignupService implements UserSignupUseCase {

private final UserCommandPort userCommandPort;
private final AliasCommandPort aliasCommandPort;

@Override
@Transactional
public Long signup(UserSignupCommand command) {
Alias alias = aliasCommandPort.findById(command.aliasId());
User user = User.withoutId(
command.email(), command.nickname(), alias.getImageUrl(), USER.getType(), alias.getId()
);

return userCommandPort.save(user);
}
}
4 changes: 4 additions & 0 deletions src/main/java/konkuk/thip/user/domain/Alias.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ public class Alias extends BaseDomainEntity {
private Long id;

private String value;

private String imageUrl;

private String color;
}
13 changes: 13 additions & 0 deletions src/main/java/konkuk/thip/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class User extends BaseDomainEntity {

private Long id;

private String email;

private String nickname;

private String imageUrl;
Expand All @@ -18,4 +20,15 @@ public class User extends BaseDomainEntity {

private Long aliasId;

public static User withoutId(String email, String nickname, String imageUrl, String userRole, Long aliasId) {
return User.builder()
.id(null)
.email(email)
.nickname(nickname)
.imageUrl(imageUrl)
.userRole(userRole)
.aliasId(aliasId)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package konkuk.thip.domain.feed.adapter.out.jpa;
package konkuk.thip.feed.adapter.out.jpa;

import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import konkuk.thip.book.adapter.out.jpa.BookJpaEntity;
import konkuk.thip.book.adapter.out.persistence.BookJpaRepository;
import konkuk.thip.feed.adapter.out.jpa.FeedJpaEntity;
import konkuk.thip.feed.adapter.out.persistence.FeedJpaRepository;
import konkuk.thip.user.adapter.out.jpa.AliasJpaEntity;
import konkuk.thip.user.adapter.out.jpa.UserJpaEntity;
Expand Down Expand Up @@ -39,10 +38,16 @@ class FeedJpaEntityTest {
private FeedJpaRepository feedRepository;

private UserJpaEntity createUser() {
AliasJpaEntity alias = AliasJpaEntity.builder().value("익명1").build();
AliasJpaEntity alias = AliasJpaEntity.builder()
.value("칭호")
.imageUrl("test-image-url")
.color("red")
.build();

aliasRepository.save(alias);

UserJpaEntity user = UserJpaEntity.builder()
.email("test@test.com")
.nickname("테스터")
.imageUrl("https://test.img")
.aliasForUserJpaEntity(alias)
Expand Down
Loading