Skip to content

Commit

Permalink
Merge pull request #5 from KUIT-Space/signup-seongjunnoh
Browse files Browse the repository at this point in the history
Signup seongjunnoh
  • Loading branch information
seongjunnoh authored Jul 14, 2024
2 parents bb1d101 + 42051a5 commit ce6cb6a
Show file tree
Hide file tree
Showing 26 changed files with 741 additions and 25 deletions.
14 changes: 12 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
// runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' // JPA ์Šคํƒ€ํ„ฐ ์ถ”๊ฐ€
runtimeOnly 'com.h2database:h2' // test์šฉ db์ธ h2Database ์ถ”๊ฐ€
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0' // p6spy ์„ค์ • ์ถ”๊ฐ€
implementation 'org.springframework.boot:spring-boot-starter-validation' // validation ์ถ”๊ฐ€
implementation 'org.springframework.boot:spring-boot-starter-security'

//JWT
implementation 'io.jsonwebtoken:jjwt-api:0.11.5'
implementation 'io.jsonwebtoken:jjwt-impl:0.11.5'
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'
}

tasks.named('test') {
test {
useJUnitPlatform()
}
34 changes: 34 additions & 0 deletions src/main/java/space/space_spring/JwtProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package space.space_spring;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import space.space_spring.domain.User;

import java.util.Date;

@Component
public class JwtProvider {

@Value("${secret.jwt-secret-key}")
private String JWT_TOKEN_KEY;

@Value("${secret.jwt-expired-in}")
private Long JWT_EXPIRATION_TIME;

public String generateToken(User user) {
Claims claims = Jwts.claims().setSubject(user.getEmail());
Date now = new Date();
Date expiration = new Date(now.getTime() + JWT_EXPIRATION_TIME);

return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(expiration)
.claim("userId", user.getUserId())
.signWith(SignatureAlgorithm.HS256, JWT_TOKEN_KEY)
.compact();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@SpringBootApplication
public class SpaceSpringApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package space.space_spring.Controller;
package space.space_spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -10,6 +9,5 @@ public class TestController {
@GetMapping("/test")
public String test(){
return "CI/CD ํ™˜๊ฒฝ ๊ตฌ์ถ• ํ…Œ์ŠคํŠธ ์ค‘. ์ด ๋ฉ”์„ธ์ง€๊ฐ€ ๋ณด์ธ๋‹ค๋ฉด ์„ฑ๊ณต์ž…๋‹ˆ๋‹ค";
// ํŒŒ์ดํŒ…~~~
}
}
55 changes: 55 additions & 0 deletions src/main/java/space/space_spring/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package space.space_spring.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import space.space_spring.dto.PostUserLoginRequest;
import space.space_spring.dto.PostUserLoginResponse;
import space.space_spring.dto.PostUserSignupRequest;
import space.space_spring.dto.PostUserSignupResponse;
import space.space_spring.exception.UserException;
import space.space_spring.response.BaseResponse;
import space.space_spring.service.UserService;

import static space.space_spring.response.status.BaseExceptionResponseStatus.INVALID_USER_VALUE;
import static space.space_spring.util.BindingResultUtils.getErrorMessage;

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

private final UserService userService;

/**
* ํšŒ์›๊ฐ€์ž…
*/
@PostMapping("/signup")
public BaseResponse<PostUserSignupResponse> signup(@Validated @RequestBody PostUserSignupRequest postUserSignupRequest, BindingResult bindingResult) {
log.info("<UserController> Signup request: {}", postUserSignupRequest);
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
}
return new BaseResponse<>(userService.signup(postUserSignupRequest));
}

/**
* ๋กœ๊ทธ์ธ
*/
@PostMapping("/login")
public BaseResponse<PostUserLoginResponse> login(@Validated @RequestBody PostUserLoginRequest postUserLoginRequest, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
throw new UserException(INVALID_USER_VALUE, getErrorMessage(bindingResult));
}
return new BaseResponse<>(userService.login(postUserLoginRequest));
}



}
46 changes: 46 additions & 0 deletions src/main/java/space/space_spring/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package space.space_spring.dao;

import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import space.space_spring.domain.User;
import space.space_spring.dto.PostUserSignupRequest;

@Repository
public class UserDao {

@PersistenceContext
private EntityManager em;

public Long saveUser(PostUserSignupRequest postUserSignupRequest) {
User user = new User();
user.saveUser(postUserSignupRequest.getEmail(), postUserSignupRequest.getPassword(), postUserSignupRequest.getUserName());

em.persist(user);
return user.getUserId();
}

public User findUserByEmail(String email) {
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class);
query.setParameter("email", email);
return query.getSingleResult();
}

public boolean hasDuplicateEmail(String email) {
String jpql = "SELECT COUNT(u) FROM User u WHERE u.email = :email";
Long count = em.createQuery(jpql, Long.class).setParameter("email", email).getSingleResult();
return count > 0;
}

public User getUserByEmail(String email) {
try {
TypedQuery<User> query = em.createQuery("SELECT u FROM User u WHERE u.email = :email", User.class);
query.setParameter("email", email);
return query.getSingleResult();
} catch (NoResultException e) {
return null;
}
}
}
36 changes: 36 additions & 0 deletions src/main/java/space/space_spring/domain/BaseEntity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package space.space_spring.domain;

import jakarta.persistence.*;

import java.time.LocalDateTime;

@MappedSuperclass
public abstract class BaseEntity {

// ๋ชจ๋“  ์—”ํ‹ฐํ‹ฐ๊ฐ€ ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ ธ์•ผํ•  ์†์„ฑ

@Column(name = "created_date", updatable = false)
private LocalDateTime createdAt;

@Column(name = "updated_date")
private LocalDateTime lastModifiedAt;

@Column(name = "status")
private String status;

@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
lastModifiedAt = LocalDateTime.now();
status = "ACTIVE";
}

@PreUpdate
protected void onUpdate() {
lastModifiedAt = LocalDateTime.now();
}

protected void initializeBaseEntityFields() {
onCreate();
}
}
22 changes: 22 additions & 0 deletions src/main/java/space/space_spring/domain/Space.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package space.space_spring.domain;

import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.Getter;

@Entity
@Getter
@Table(name = "Spaces")
public class Space extends BaseEntity {

@Id @GeneratedValue
@Column(name = "space_id")
private Long spaceId;

@Column(name = "space_name")
private String spaceName;

@Column(name = "space_profile_img")
@Nullable
private String spaceProfileImg;
}
39 changes: 39 additions & 0 deletions src/main/java/space/space_spring/domain/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package space.space_spring.domain;

import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import org.hibernate.validator.constraints.Length;

@Entity
@Table(name = "Users")
@Getter
public class User extends BaseEntity {

@Id @GeneratedValue
@Column(name = "user_id")
private Long userId;

@Column(name = "email")
private String email;

@Column(name = "password")
private String password;

@Column(name = "user_name")
private String userName;

@Column(name = "jwt")
@Nullable
private String jwt;

public void saveUser(String email, String password, String userName) {
this.email = email;
this.password = password;
this.userName = userName;
initializeBaseEntityFields();
}

}
42 changes: 42 additions & 0 deletions src/main/java/space/space_spring/domain/UserSpace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package space.space_spring.domain;

import jakarta.annotation.Nullable;
import jakarta.persistence.*;
import lombok.Getter;

@Entity
@Getter
@Table(name = "User_Space")
public class UserSpace extends BaseEntity {

@Id @GeneratedValue
@Column(name = "user_space_id")
private Long userSpaceId;

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

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "space_id")
private Space space;

@Column(name = "user_name")
private String userName;

@Column(name = "user_profile_img")
@Nullable
private String userProfileImg;

@Column(name = "user_profile_msg")
@Nullable
private String userProfileMsg;

@Column(name = "user_auth")
private String userAuth;

@Column(name = "space_order")
private int spaceOrder;


}
21 changes: 21 additions & 0 deletions src/main/java/space/space_spring/dto/PostUserLoginRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package space.space_spring.dto;

import jakarta.validation.constraints.Pattern;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class PostUserLoginRequest {

@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$", message = "์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.") // ์ด๋ฉ”์ผ ์ •๊ทœํ‘œํ˜„์‹ ํ™•์ธ ํ•„์š”ํ•จ
private String email;

@Pattern(
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$",
message = "8~16๊ธ€์ž์˜ ์˜๋ฌธ ๋Œ€/์†Œ๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."
)
private String password;
}
11 changes: 11 additions & 0 deletions src/main/java/space/space_spring/dto/PostUserLoginResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package space.space_spring.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class PostUserLoginResponse {

private String jwt;
}
27 changes: 27 additions & 0 deletions src/main/java/space/space_spring/dto/PostUserSignupRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package space.space_spring.dto;

import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.validator.constraints.Length;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class PostUserSignupRequest {

@Pattern(regexp = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,6}$", message = "์ด๋ฉ”์ผ ํ˜•์‹์— ๋งž์ง€ ์•Š์Šต๋‹ˆ๋‹ค.") // ์ด๋ฉ”์ผ ์ •๊ทœํ‘œํ˜„์‹ ํ™•์ธ ํ•„์š”ํ•จ
private String email;

@Pattern(
regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,16}$",
message = "8~16๊ธ€์ž์˜ ์˜๋ฌธ ๋Œ€/์†Œ๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค."
)
private String password;

@Length(min = 1, max = 10, message = "์ด๋ฆ„์€ 10์ž์ด๋‚ด์˜ ๋ฌธ์ž์—ด์ด์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
private String userName;
}
Loading

0 comments on commit ce6cb6a

Please sign in to comment.