-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat: 회원가입 api 구현
- Loading branch information
Showing
9 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/umc/hackaton/snapspot/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.umc.hackaton.snapspot.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Configuration | ||
public class SecurityConfig { | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc/hackaton/snapspot/config/entity/BaseEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.umc.hackaton.snapspot.config.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import java.time.LocalDateTime; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
@MappedSuperclass | ||
@Getter | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseEntity { | ||
|
||
@CreatedDate | ||
@Column(name = "created_at",updatable = false) | ||
private LocalDateTime createdAt; | ||
|
||
@LastModifiedDate | ||
@Column(name = "updated_at") | ||
private LocalDateTime updatedAt; | ||
|
||
@Column(name = "deleted", nullable = false, columnDefinition = "bit(1) default 0") | ||
private Boolean isDeleted = false; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/umc/hackaton/snapspot/user/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.umc.hackaton.snapspot.user.controller; | ||
|
||
import com.umc.hackaton.snapspot.user.dto.UserRequestDto; | ||
import com.umc.hackaton.snapspot.user.repository.UserRepository; | ||
import com.umc.hackaton.snapspot.user.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController("/api/v1/users") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<?> signUp(UserRequestDto dto) { | ||
try { | ||
userService.signUp(dto); | ||
return ResponseEntity.ok().body("회원가입 성공."); | ||
} catch (Exception e) { | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("회원가입에 실패하였습니다."); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/umc/hackaton/snapspot/user/dto/UserRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.umc.hackaton.snapspot.user.dto; | ||
|
||
import com.umc.hackaton.snapspot.user.entity.User; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
@Getter | ||
public class UserRequestDto { | ||
private String userId; | ||
private String password; | ||
private String nickname; | ||
|
||
@Builder | ||
public User toEntity(PasswordEncoder passwordEncoder) { | ||
return User.builder() | ||
.userId(userId) | ||
.password(passwordEncoder.encode(password)) | ||
.nickname(nickname) | ||
.build(); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/hackaton/snapspot/user/dto/UserResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.umc.hackaton.snapspot.user.dto; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
|
||
public class UserResponseDto { | ||
private String id; | ||
private String password; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/umc/hackaton/snapspot/user/entity/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.umc.hackaton.snapspot.user.entity; | ||
|
||
import com.umc.hackaton.snapspot.config.entity.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@Getter | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Table(name = "user") | ||
public class User extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@Column(name = "nickname", nullable = false) | ||
private String nickname; | ||
|
||
@Column(name = "user_id", nullable = false) | ||
private String userId; | ||
|
||
@Column(name = "password", nullable = false) | ||
private String password; | ||
|
||
@Column(name = "profile_img", nullable = false) | ||
private String profileImg; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/umc/hackaton/snapspot/user/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.umc.hackaton.snapspot.user.repository; | ||
|
||
import com.umc.hackaton.snapspot.user.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Optional<User> findByUserId(String userId); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/umc/hackaton/snapspot/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.umc.hackaton.snapspot.user.service; | ||
|
||
import com.umc.hackaton.snapspot.user.dto.UserRequestDto; | ||
import com.umc.hackaton.snapspot.user.entity.User; | ||
import com.umc.hackaton.snapspot.user.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class UserService { | ||
|
||
private final UserRepository userRepository; | ||
private final BCryptPasswordEncoder encoder; | ||
|
||
@Transactional | ||
public void signUp(UserRequestDto dto) { | ||
|
||
// Optional<User> existingUser = userRepository.findByUserId(dto.getUserId()); | ||
// if (existingUser.isPresent()) { | ||
// User user = existingUser.get(); | ||
// } else { | ||
User user = dto.toEntity(encoder); | ||
userRepository.save(user); | ||
} | ||
} | ||
|