Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/14 user entity #30

Open
wants to merge 8 commits into
base: dev
Choose a base branch
from
3 changes: 1 addition & 2 deletions Api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ dependencies {
test {
useJUnitPlatform()
}

bootJar{
archivesBaseName = 'app'
archiveFileName = 'app.jar'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package playlist.server.config.security;


import java.util.List;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import playlist.server.exception.SecurityContextNotFoundException;

public class SecurityUtils {

private static SimpleGrantedAuthority swagger = new SimpleGrantedAuthority("ROLE_SWAGGER");

private static List<SimpleGrantedAuthority> notUserAuthority = List.of(swagger);

public static Long getCurrentUserId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw SecurityContextNotFoundException.EXCEPTION;
}

if (authentication.isAuthenticated()
&& !CollectionUtils.containsAny(
authentication.getAuthorities(), notUserAuthority)) {
return authentication.getName().equals("anonymousUser")
? 0L
: Long.valueOf(authentication.getName());
}
// μŠ€μ›¨κ±° μœ μ €μΌμ‹œ μœ μ € 아이디 0 λ°˜ν™˜
return 0L;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

κ΅Ώκ΅Ώ μ’‹μŠ΅λ‹ˆλ‹€

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package playlist.server.user.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import playlist.server.user.model.dto.UserCreateDTO;
import playlist.server.user.service.devLoginService;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
@Slf4j
public class UserController {

private final devLoginService devLoginService;

@GetMapping("/create")
public void createDevUser(@RequestParam String email, @RequestParam String password) {
log.info("email, password : " + email + " " + password);

UserCreateDTO userCreateDTO = new UserCreateDTO(email, password);
devLoginService.execute(userCreateDTO);

// model.addAttribute("email", email);
// model.addAttribute("password",password);

}
}
Comment on lines +21 to +32
Copy link
Contributor

Choose a reason for hiding this comment

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

RequestParam으둜 값이 λ‘κ°œλ‚˜ λ“€μ–΄κ°€λŠ” 것은 쒋지 μ•ŠμŠ΅λ‹ˆλ‹€
passwordλŠ” λ”λ”μš± uri에 ν‘œμ‹œλ˜λ©΄ 쒋지 μ•Šκ² μ£ 
email, passwordλ₯Ό ν•˜λ‚˜μ˜ DTO둜 λ°›λŠ”λ‹€λ©΄ μ’‹κ² μ£ ?
dto νŒ¨ν‚€μ§€μ— UserCreateRequest와 같은 μ΄λ¦„μ˜ DTOλ₯Ό λ§Œλ“€μ–΄ λ°›μ•„λ³΄μ„Έμš”!
tify ν”„λ‘œμ νŠΈμ˜ PostFavorAnswerRequest 클래슀λ₯Ό μ°Έκ³ ν•΄λ³΄μ„Έμš”

Copy link
Contributor

Choose a reason for hiding this comment

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

λ„€ μ°Έκ³ ν•˜κ² μŠ΅λ‹ˆλ‹€

Copy link
Author

Choose a reason for hiding this comment

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

ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€ μΆ”ν›„ API ν”Œλ¦¬ν€˜μŠ€νŠΈμ‹œ λ°˜μ˜ν•΄μ„œ μ˜¬λ¦¬κ² μŠ΅λ‹ˆλ‹€

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package playlist.server.user.model.dto;


import lombok.Getter;

@Getter
public class UserCreateDTO {
private final String email;
private final String password;

// @QueryProjection
public UserCreateDTO(String email, String password) {
this.email = email;
this.password = password;
}
}
Comment on lines +7 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

μ’‹μŠ΅λ‹ˆλ‹€
μœ„μ—μ„œ λ§ν•œ UserCreateRequestμ™€μ˜ 톡합을 κ³ λ €ν•΄λ³΄μ„Έμš”

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package playlist.server.user.service;


import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import playlist.server.domain.domains.auth.adaptor.UserAdaptor;
import playlist.server.domain.domains.auth.domain.AccountRole;
import playlist.server.domain.domains.auth.domain.User;
import playlist.server.user.model.dto.UserCreateDTO;

@Service
@RequiredArgsConstructor
@Transactional
public class devLoginService {
private final UserAdaptor userAdaptor;

public void execute(UserCreateDTO userCreateDTO) {
User user =
User.builder()
.email(userCreateDTO.getEmail())
.password(userCreateDTO.getPassword())
.role(AccountRole.ADMIN)
.build();
userAdaptor.save(user);
}
Comment on lines +18 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

@builder νŒ¨ν„΄μœΌλ‘œ κ΅¬ν˜„ν•œ 것 μ’‹μŠ΅λ‹ˆλ‹€

}
21 changes: 21 additions & 0 deletions Api/src/main/java/playlist/server/utils/UserUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package playlist.server.utils;


import lombok.RequiredArgsConstructor;
import playlist.server.config.security.SecurityUtils;
import playlist.server.domain.domains.auth.adaptor.UserAdaptor;
import playlist.server.domain.domains.auth.domain.User;

@RequiredArgsConstructor
public class UserUtils {

private final UserAdaptor userAdaptor;

public Long getUserId() {
return SecurityUtils.getCurrentUserId();
}

public User getUser() {
return userAdaptor.query(SecurityUtils.getCurrentUserId());
}
}
Comment on lines +10 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

Utils class둜 자주 μ¨μ•Όν•˜λŠ” λ©”μ†Œλ“œλ₯Ό λ”°λ‘œ κ΅¬ν˜„ν•΄λ†“μ€ 것 μ’‹λ„€μš”

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public enum GlobalException implements BaseErrorCode {
EXPIRED_REFRESH_TOKEN_ERROR(UNAUTHORIZED.value(), "401-1", "λ¦¬ν”„λ ˆμ‹œ 토큰이 λ§Œλ£Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€."),
INVALID_TOKEN_ERROR(UNAUTHORIZED.value(), "401-2", "μ˜¬λ°”λ₯΄μ§€ μ•Šμ€ ν† ν°μž…λ‹ˆλ‹€."),
DATE_FORMAT_ERROR(BAD_REQUEST.value(), "400-2", "λ‚ μ§œ ν˜•μ‹μ„ ν™•μΈν•΄μ£Όμ„Έμš”."),
;
SECURITY_CONTEXT_NOT_FOUND_ERROR(
INTERNAL_SERVER_ERROR.value(), "500-2", "Security Context μ—λŸ¬μž…λ‹ˆλ‹€.");

private final Integer statusCode;
private final String errorCode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package playlist.server.exception;

public class SecurityContextNotFoundException extends BaseException {

public static final BaseException EXCEPTION = new SecurityContextNotFoundException();

private SecurityContextNotFoundException() {
super(GlobalException.SECURITY_CONTEXT_NOT_FOUND_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package playlist.server.domain.domains;


import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import java.sql.Timestamp;
import lombok.Getter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

@Getter
@MappedSuperclass
@EntityListeners(value = {AuditingEntityListener.class})
public abstract class AbstractTimeStamp {

@Column(
name = "created_at",
nullable = false,
columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@CreationTimestamp
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private Timestamp createdAt;

@Column(
name = "updated_at",
nullable = false,
columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
@UpdateTimestamp
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private Timestamp updatedAt;
}
Comment on lines +14 to +34
Copy link
Contributor

Choose a reason for hiding this comment

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

AbstractTimeStamp μΆ”μƒν΄λž˜μŠ€λ₯Ό μ΄μš©ν•΄μ„œ
μ–΄λ–€ κΈ°λŠ₯을 κ΅¬ν˜„ν•œ κ±ΈκΉŒμš”?

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package playlist.server.domain.domains.auth.Exception;

import static org.springframework.http.HttpStatus.*;

import lombok.RequiredArgsConstructor;
import playlist.server.dto.ErrorDetail;
import playlist.server.exception.BaseErrorCode;

@RequiredArgsConstructor
public enum UserException implements BaseErrorCode {
USER_NOT_FOUND_ERROR(NOT_FOUND.value(), "User_404_1", "μœ μ €λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.");

private final Integer statusCode;
private final String errorCode;
private final String reason;

@Override
public ErrorDetail getErrorDetail() {
return ErrorDetail.of(statusCode, errorCode, reason);
}
}
Comment on lines +10 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

μƒˆλ‘œμš΄ Exception을 λ§Œλ“  것 μ•„μ£Ό μ’‹μŠ΅λ‹ˆλ‹€
μ•žμœΌλ‘œ User κ΄€λ ¨ Exception은 λͺ¨λ‘ 여기에 μΆ”κ°€ν•˜λ©΄ λ˜κ² λ„€μš”

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package playlist.server.domain.domains.auth.Exception;


import playlist.server.exception.BaseException;

public class UserNotFoundException extends BaseException {
public static final BaseException EXCEPTION = new UserNotFoundException();

private UserNotFoundException() {
super(UserException.USER_NOT_FOUND_ERROR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package playlist.server.domain.domains.auth.adaptor;


import lombok.RequiredArgsConstructor;
import playlist.server.annotation.Adaptor;
import playlist.server.domain.domains.auth.Exception.UserNotFoundException;
import playlist.server.domain.domains.auth.domain.User;
import playlist.server.domain.domains.auth.repository.UserRepository;

@Adaptor
@RequiredArgsConstructor
public class UserAdaptor {

private final UserRepository userRepository;

public User query(Long userId) {
return userRepository.findById(userId).orElseThrow(() -> UserNotFoundException.EXCEPTION);
}

Comment on lines +17 to +19
Copy link
Contributor

Choose a reason for hiding this comment

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

μ΄λΆ€λΆ„μ˜ orElseThrow()λŠ” μ–΄λ–€ κ²½μš°μ— μ“Έ 수 μžˆμ„κΉŒμš”
@donsonioc2010 이뢄이 μ•„μ£Ό μ’‹μ•„ν• λ§Œν•œ μ§ˆλ¬Έμ΄λ„€μš”

public User save(User user) {
return userRepository.save(user);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package playlist.server.domain.domains.auth.domain;

public enum AccountRole {
USER,
ADMIN;
}
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

USER의 경우 "μΌλ°˜μ‚¬μš©μž",
ADMIN의 경우 "κ΄€λ¦¬μž" λΌλŠ” μ„€λͺ…이 λ“€μ–΄κ°€μ•Όκ² μ§€λ§Œ
DBμ—μ„œ ν•„μš”μ—†κΈ°λ•Œλ¬Έμ— ν•˜μ§€ μ•Šμ€κ±ΈκΉŒμš”?

Copy link
Contributor

Choose a reason for hiding this comment

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

주석을 남기면 μ’‹κ² λ‹€λŠ” κ±΄κ°€μš”?

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package playlist.server.domain.domains.auth.domain;


import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import playlist.server.domain.domains.AbstractTimeStamp;

@Getter
@Entity
// @Table(name = "follow")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Follow extends AbstractTimeStamp {

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

// 이 λ°‘μœΌλ‘œλŠ” λ­”κ°€ κΈ°λ‘ν• λ§Œν•œ μš”μ†Œ λ“€

@Column(nullable = false)
private Long userId;

@Column(nullable = false)
private Long followUserId;
}
Comment on lines +20 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

κ΅Ώ

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package playlist.server.domain.domains.auth.domain;


import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import playlist.server.domain.domains.AbstractTimeStamp;

@Getter
@Entity
// @Table(name = "following")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Following extends AbstractTimeStamp {

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

// 이 λ°‘μœΌλ‘œλŠ” λ­”κ°€ κΈ°λ‘ν• λ§Œν•œ μš”μ†Œ λ“€

@Column(nullable = false)
private Long userId;

@Column(nullable = false)
private Long followingUserId;
}
Comment on lines +14 to +31
Copy link
Contributor

Choose a reason for hiding this comment

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

κ΅Ώ
그런데 @table의 이름은 μ™œ μ£Όμ„μΈκ°€μš”?

Copy link
Contributor

Choose a reason for hiding this comment

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

μƒκ°ν•΄λ³Όκ²Œμš”..

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package playlist.server.domain.domains.auth.domain;


import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import java.sql.Timestamp;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import playlist.server.domain.domains.AbstractTimeStamp;

@Getter
@Entity
@Table(name = "user_login_history")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class History extends AbstractTimeStamp {

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

@Column(name = "login_at", nullable = false)
@CreationTimestamp
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh:mm:ss")
private Timestamp loginAt;

// 이 λ°‘μœΌλ‘œλŠ” λ­”κ°€ κΈ°λ‘ν• λ§Œν•œ μš”μ†Œ λ“€

@Column(nullable = false, length = 200)
private String param1;

@Column(nullable = false, length = 200)
private String param2;

@Column(nullable = false, length = 200)
private String param3;

@Column(nullable = false, length = 200)
private String param4;

@Column(nullable = false, length = 200)
private String param5;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package playlist.server.domain.domains.auth.domain;

public enum LoginType {
DEFAULT,
KAKAO,
NAVER,
GOOGLE,
APPLE,
GITHUB
}
Loading