Paper.Yellow
회원가입 본문
user.java
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import shop.mtcoding.final5th.domain.AudingTime;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@Table(name = "users")
@Entity
public class User extends AudingTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;
@Column(unique = true, nullable = false, length = 50)
private String userName;
@Column(nullable = false, length = 1000)
private String userPassword;
@Column(length = 50)
private String userRealname;
@Column(length = 255)
private String userImgfile;
@Column(length = 255)
private String userProfileIntro;
@Column(unique = true, nullable = false, length = 255)
private String userEmail;
@Column(unique = true, length = 50)
private String userPhonenumber;
@Column(nullable = false)
private Timestamp userCreatedAt;
@Column(nullable = false)
private Timestamp userUpdatedAt;
@Column(length = 255)
private String userWebLink;
@Column(length = 255)
private String userImageUrl;
@Column(length = 255)
private String userImageType;
@Column(length = 255)
private String userImageName;
@Column(length = 255)
private String userImageUuid;
@Builder
public User(Long userId, String userName, String userEmail, String userPhonenumber, String userPassword,
String userRealname, String userImgfile, String userProfileIntro, Timestamp userCreatedAt,
Timestamp userUpdatedAt, String userWebLink, String userImageUrl,
String userImageType, String userImageName, String userImageUuid) {
this.userId = userId;
this.userName = userName;
this.userEmail = userEmail;
this.userPhonenumber = userPhonenumber;
this.userPassword = userPassword;
this.userRealname = userRealname;
this.userImgfile = userImgfile;
this.userProfileIntro = userProfileIntro;
this.userWebLink = userWebLink;
this.userImageUrl = userImageUrl;
this.userImageType = userImageType;
this.userImageName = userImageName;
this.userImageUuid = userImageUuid;
}
}
UserRepository.java
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface UserRepository extends JpaRepository<User, Long> {
@Query("select u from User u where user_name = :userName")
Optional<User> findByUsername(@Param("userName") String userName);
}
UserService.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import shop.mtcoding.final5th.config.exception.CustomApiException;
import shop.mtcoding.final5th.domain.user.User;
import shop.mtcoding.final5th.domain.user.UserRepository;
import shop.mtcoding.final5th.dto.UserReqDto.JoinReqDto;
import shop.mtcoding.final5th.dto.UserReqDto.UserUpdateReqDto;
import shop.mtcoding.final5th.dto.UserRespDto.JoinRespDto;
import shop.mtcoding.final5th.dto.UserRespDto.UserRealnameRespDto;
import shop.mtcoding.final5th.dto.UserRespDto.UserUpdateRespDto;
@Transactional(readOnly = true)
@RequiredArgsConstructor
@Service
public class UserService {
private final UserRepository userRepository;
private final Logger log = LoggerFactory.getLogger(getClass());
public UserRealnameRespDto findUserRealnameById(Long userId) {
log.debug("디버그 : findUserRealnameById 서비스 실행됨");
User userPS = userRepository.findById(userId)
.orElseThrow(() -> new CustomApiException("해당 유저가 없습니다", HttpStatus.BAD_REQUEST));
log.debug("디버그 : findUserRealnameById 서비스 리턴 전");
return new UserRealnameRespDto(userPS);
}
@Transactional
public JoinRespDto joinUser(JoinReqDto joinReqDto) {
log.debug("디버그 : 서비스 회원가입 실행됨");
System.out.println("2 : " + joinReqDto.toEntity());
return new JoinRespDto(userRepository.save(joinReqDto.toEntity()));
}
}
UserApiController.java
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import shop.mtcoding.final5th.dto.ResponseDto;
import shop.mtcoding.final5th.dto.UserReqDto.JoinReqDto;
import shop.mtcoding.final5th.dto.UserRespDto.JoinRespDto;
import shop.mtcoding.final5th.service.UserService;
@ToString
@RequiredArgsConstructor
@RestController
public class UserApiController {
private final UserService userService;
private final Logger log = LoggerFactory.getLogger(getClass());
private final HttpSession session;
@PostMapping("/join")
public ResponseEntity<?> join(@RequestBody JoinReqDto joinReqDto) {
log.debug("디버그 : UserApiController join 실행됨");
JoinRespDto joinRespDto = userService.joinUser(joinReqDto);
System.out.println("1 : " + joinReqDto.toEntity());
return new ResponseEntity<>(new ResponseDto<>(HttpStatus.OK, "회원 가입 성공", joinRespDto),
HttpStatus.OK);
}
}
오류 1)
테이블 이름 User인데 Users로 되어서 오류가 남. s 삭제
오류 2)
경로 오류로 @RequestMapping ("/s/api") 삭제
오류 3)
기본 값에 timestamp(now()) 가 적용이 안 됨
수정1) 어노테이션 추가
수정 2) 수동으로 시간 값 입력
수정 3) Not null을 삭제해봄
'프로젝트 > 파이널 프로젝트' 카테고리의 다른 글
더미데이터 생성 (0) | 2022.12.06 |
---|---|
Email 인증 기능 구현 (0) | 2022.11.27 |
시작 전 조율용 프로젝트 - minishop (0) | 2022.11.17 |