Skip to content

Commit

Permalink
Merge pull request #2 from GDSC-snowflowerthon/feature/1
Browse files Browse the repository at this point in the history
  • Loading branch information
Haewonny authored Jan 10, 2024
2 parents 45bcb1a + 9b9d597 commit 2c7dbb7
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 0 deletions.
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 src/main/java/tenten/blooming/domain/user/dto/UserForm.java
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 src/main/java/tenten/blooming/domain/user/entity/User.java
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;

}
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 src/main/java/tenten/blooming/domain/user/service/UserService.java
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 src/main/java/tenten/blooming/global/common/BasicResponse.java
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;
}
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);
}
}

0 comments on commit 2c7dbb7

Please sign in to comment.