-
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.
Merge pull request #2 from GDSC-snowflowerthon/feature/1
- Loading branch information
Showing
7 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
src/main/java/tenten/blooming/domain/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,62 @@ | ||
package tenten.blooming.domain.user.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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 tenten.blooming.domain.user.entity.User; | ||
import tenten.blooming.domain.user.dto.UserForm; | ||
import tenten.blooming.domain.user.service.UserService; | ||
import tenten.blooming.global.common.BasicResponse; | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
@Autowired | ||
private UserService userService; | ||
|
||
@PostMapping("/signup") | ||
public ResponseEntity<BasicResponse> signup(@RequestBody UserForm dto) { | ||
User user = userService.join(dto); | ||
BasicResponse basicResponse = new BasicResponse(); | ||
|
||
|
||
if (user != null) { | ||
basicResponse = BasicResponse.builder() | ||
.code(HttpStatus.OK.value()) | ||
.message("회원가입에 성공하였습니다.") | ||
.result(user) | ||
.build(); | ||
} | ||
|
||
return new ResponseEntity<>(basicResponse, HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("/login") | ||
public ResponseEntity<BasicResponse> login(@RequestBody UserForm dto) { | ||
try { | ||
User user = userService.login(dto.getLoginId(), dto.getPassword()); | ||
|
||
BasicResponse basicResponse = BasicResponse.builder() | ||
.code(HttpStatus.OK.value()) | ||
.message("로그인에 성공하였습니다.") | ||
.result(user) | ||
.build(); | ||
|
||
return new ResponseEntity<>(basicResponse, HttpStatus.OK); | ||
} catch (DataIntegrityViolationException e) { | ||
BasicResponse basicResponse = BasicResponse.builder() | ||
.code(HttpStatus.BAD_REQUEST.value()) | ||
.message("로그인에 실패했습니다.") | ||
.result(null) | ||
.build(); | ||
|
||
return new ResponseEntity<>(basicResponse, HttpStatus.BAD_REQUEST); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/tenten/blooming/domain/user/dto/UserForm.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 tenten.blooming.domain.user.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import tenten.blooming.domain.user.entity.User; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@Builder | ||
public class UserForm { | ||
private Long userId; | ||
|
||
private String loginId; | ||
private String nickname; | ||
private String password; | ||
|
||
public User toEntity() { | ||
User user = new User(); | ||
|
||
user.setLoginId(loginId); | ||
user.setNickname(nickname); | ||
user.setPassword(password); | ||
|
||
return user; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/tenten/blooming/domain/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,36 @@ | ||
package tenten.blooming.domain.user.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.*; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import java.sql.Timestamp; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
@Getter @Setter | ||
@Table(name = "users") | ||
public class User { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "user_id", nullable = false) | ||
private Long userId; // 기본키 | ||
|
||
@Column(name = "login_id", nullable = false) | ||
private String loginId; | ||
|
||
@Column(name = "nickname", nullable = false) | ||
private String nickname; | ||
|
||
@Column(name = "password", nullable = false) | ||
private String password; | ||
|
||
@Column(name = "has_goal", nullable = false) | ||
private Boolean hasGoal = false; | ||
|
||
@CreationTimestamp | ||
@Column(name = "created_at", nullable = false, updatable = false) | ||
private Timestamp createdAt; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/tenten/blooming/domain/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 tenten.blooming.domain.user.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import tenten.blooming.domain.user.entity.User; | ||
|
||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
boolean existsByLoginId(String loginId); | ||
User findByLoginId(String loginId); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/tenten/blooming/domain/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,39 @@ | ||
package tenten.blooming.domain.user.service; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.stereotype.Service; | ||
import tenten.blooming.domain.user.repository.UserRepository; | ||
import tenten.blooming.domain.user.dto.UserForm; | ||
import tenten.blooming.domain.user.entity.User; | ||
|
||
@Service | ||
public class UserService { | ||
@Autowired | ||
private UserRepository userRepository; | ||
|
||
// 로그인 id 중복 체크 | ||
public boolean existsMember(String loginId) { | ||
return userRepository.existsByLoginId(loginId); | ||
} | ||
|
||
public User join(UserForm dto) { | ||
|
||
// 이미 존재하는 아이디로 회원가입한 경우 | ||
if (existsMember(dto.getLoginId())) { | ||
throw new DataIntegrityViolationException("이미 존재하는 아이디입니다."); | ||
} | ||
|
||
return userRepository.save(dto.toEntity()); | ||
} | ||
|
||
public User login(String loginId, String password) { | ||
User findUser = userRepository.findByLoginId(loginId); | ||
|
||
if (findUser != null && findUser.getPassword().equals(password)) { | ||
return findUser; | ||
} else { | ||
throw new DataIntegrityViolationException("로그인에 실패하였습니다."); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/tenten/blooming/global/common/BasicResponse.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,17 @@ | ||
package tenten.blooming.global.common; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class BasicResponse { | ||
private Integer code; | ||
private String message; | ||
|
||
private Object result; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/tenten/blooming/global/error/GlobalExceptionHandler.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,23 @@ | ||
package tenten.blooming.global.error; | ||
|
||
import org.springframework.dao.DataIntegrityViolationException; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import tenten.blooming.global.common.BasicResponse; | ||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(DataIntegrityViolationException.class) | ||
public ResponseEntity<BasicResponse> handleDataIntegrityViolationException(DataIntegrityViolationException ex) { | ||
BasicResponse response = BasicResponse.builder() | ||
.code(HttpStatus.BAD_REQUEST.value()) | ||
.message(ex.getMessage()) | ||
.result(null) | ||
.build(); | ||
|
||
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); | ||
} | ||
} |