Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3주차 과제] 이재민 #47

Open
wants to merge 23 commits into
base: chocojaem
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added DB/carrot_market.mv.db
Binary file not shown.
Binary file added ERD/carrot_market.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
115 changes: 115 additions & 0 deletions ERD/carrot_market.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
-- 테이블 순서는 관계를 고려하여 한 번에 실행해도 에러가 발생하지 않게 정렬되었습니다.

-- category Table Create SQL
-- 테이블 생성 SQL - category
CREATE TABLE category
(
`categoryId` INT NOT NULL AUTO_INCREMENT,
`category` VARCHAR(45) NULL,
`created` TIMESTAMP NULL,
`updated` TIMESTAMP NULL,
`status` VARCHAR(20) NULL,
PRIMARY KEY (categoryId)
);


-- users Table Create SQL
-- 테이블 생성 SQL - users
CREATE TABLE users
(
`userId` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`email` VARCHAR(45) NULL,
`password` VARCHAR(100) NULL,
`phone` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
`imageURL` TEXT NULL,
`created` TIMESTAMP NULL,
`updated` TIMESTAMP NULL,
`status` VARCHAR(20) NULL,
PRIMARY KEY (userId)
);


-- post Table Create SQL
-- 테이블 생성 SQL - post
CREATE TABLE post
(
`postId` INT NOT NULL AUTO_INCREMENT,
`userId` INT NULL,
`likeCount` TINYINT NULL,
`title` VARCHAR(45) NULL,
`content` TEXT NULL,
`created` TIMESTAMP NULL,
`updated` TIMESTAMP NULL,
`price` INT NULL,
`status` VARCHAR(20) NULL,
`categoryId` INT NULL,
PRIMARY KEY (postId)
);

-- Foreign Key 설정 SQL - post(userId) -> users(userId)
ALTER TABLE post
ADD CONSTRAINT FK_post_userId_users_userId FOREIGN KEY (userId)
REFERENCES users (userId) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - post(userId)
-- ALTER TABLE post
-- DROP FOREIGN KEY FK_post_userId_users_userId;

-- Foreign Key 설정 SQL - post(categoryId) -> category(categoryId)
ALTER TABLE post
ADD CONSTRAINT FK_post_categoryId_category_categoryId FOREIGN KEY (categoryId)
REFERENCES category (categoryId) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - post(categoryId)
-- ALTER TABLE post
-- DROP FOREIGN KEY FK_post_categoryId_category_categoryId;


-- wishList Table Create SQL
-- 테이블 생성 SQL - wishList
CREATE TABLE wishList
(
`wishListId` INT NOT NULL AUTO_INCREMENT,
`userId` INT NULL,
`postId` INT NULL,
`created` TIMESTAMP NULL,
`updated` TIMESTAMP NULL,
`status` VARCHAR(20) NULL,
PRIMARY KEY (wishListId)
);

-- Foreign Key 설정 SQL - wishList(userId) -> users(userId)
ALTER TABLE wishList
ADD CONSTRAINT FK_wishList_userId_users_userId FOREIGN KEY (userId)
REFERENCES users (userId) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - wishList(userId)
-- ALTER TABLE wishList
-- DROP FOREIGN KEY FK_wishList_userId_users_userId;


-- postImage Table Create SQL
-- 테이블 생성 SQL - postImage
CREATE TABLE postImage
(
`ImageId` INT NOT NULL AUTO_INCREMENT,
`postId` INT NULL,
`imageURL` TEXT NULL,
`created` TIMESTAMP NULL,
`updated` TIMESTAMP NULL,
`status` VARCHAR(20) NULL,
PRIMARY KEY (ImageId)
);

-- Foreign Key 설정 SQL - postImage(postId) -> post(postId)
ALTER TABLE postImage
ADD CONSTRAINT FK_postImage_postId_post_postId FOREIGN KEY (postId)
REFERENCES post (postId) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - postImage(postId)
-- ALTER TABLE postImage
-- DROP FOREIGN KEY FK_postImage_postId_post_postId;


8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Spring-JPA-study

### Week 1
- Inflearn - 스프링 입문(김영한)

### Week 2
- 당근마켓 ERD 만들기
- 간단한 API 추가
16 changes: 13 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.projectlombok:lombok:1.18.22'
implementation 'org.jetbrains:annotations:20.1.0'
annotationProcessor 'org.projectlombok:lombok'
runtimeOnly('com.h2database:h2')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'

implementation 'io.jsonwebtoken:jjwt:0.9.1'
// com.sun.xml.bind
implementation 'com.sun.xml.bind:jaxb-impl:4.0.1'
implementation 'com.sun.xml.bind:jaxb-core:4.0.1'
// javax.xml.bind
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
}

tasks.named('test') {
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/dku/springstudy/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.dku.springstudy;

import com.dku.springstudy.repository.MemberRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityTransaction;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class SpringConfig {

private final EntityManager em;

@Bean
public MemberRepository userRepository(){
return new MemberRepository(em);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.dku.springstudy.controller;

import com.dku.springstudy.domain.Member;
import com.dku.springstudy.dto.member.request.LoginRequestDto;
import com.dku.springstudy.dto.member.request.LogoutRequestDto;
import com.dku.springstudy.dto.member.request.MembershipRequestDto;
import com.dku.springstudy.dto.member.response.LoginResponseDto;
import com.dku.springstudy.dto.member.response.LogoutResponseDto;
import com.dku.springstudy.dto.member.response.MembershipResponseDto;
import com.dku.springstudy.dto.member.response.WithdrawResponseDto;
import com.dku.springstudy.security.CustomUserDetails;
import com.dku.springstudy.security.jwt.JwtProvider;
import com.dku.springstudy.service.MemberService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
public class MemberController {
private final MemberService memberService;
private final JwtProvider jwtProvider;

// 회원가입
@PostMapping("/member/membership")
public MembershipResponseDto membership(@Valid @RequestBody MembershipRequestDto membershipRequestDto) {

return memberService.membership(membershipRequestDto);
}

// 로그인
@PostMapping("/member/login")
public LoginResponseDto login(@Valid @RequestBody LoginRequestDto loginRequest) {
return memberService.login(loginRequest);
}

@PostMapping("/member/logout")
public LogoutResponseDto logout(@AuthenticationPrincipal CustomUserDetails customUserDetails, HttpServletRequest request) {
Member member = customUserDetails.getMember();
String accessToken = jwtProvider.resolveToken(request);
return memberService.logout(member, accessToken);
}


@DeleteMapping("/member/withdraw")
public WithdrawResponseDto withdraw(@AuthenticationPrincipal CustomUserDetails customUserDetails) {
Member member = customUserDetails.getMember();
return memberService.withdraw(member);

}

}
39 changes: 39 additions & 0 deletions src/main/java/com/dku/springstudy/domain/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dku.springstudy.domain;

import jakarta.persistence.*;
import lombok.*;

import java.sql.Timestamp;

@Entity
@Getter //Lombok
@Setter //Lombok
@ToString //Lombok
@Table(name = "USERS")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USERID")
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "EMAIL")
private String email;
@Column(name = "PASSWORD")
private String password;
@Column(name = "PHONE")
private String phone;
@Column(name = "ADDRESS")
private String address;
@Column(name = "IMAGEURL")
private String imageURL;
@Column(name = "CREATED")
private Timestamp created;
@Column(name = "UPDATED")
private Timestamp updated;
@Column(name = "STATUS")
private String status;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dku.springstudy.dto.common;

import lombok.Getter;

@Getter
public class ErrorResponseDto<T> extends ResponseDto{
private final T data;

public ErrorResponseDto(T data) {
super(false);
this.data = data;
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/dku/springstudy/dto/common/ExceptionDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dku.springstudy.dto.common;

import com.dku.springstudy.exception.CustomException;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;

@Getter
public class ExceptionDto {
private final HttpStatus error;
private final Integer status;
private final String message;

public ExceptionDto(CustomException e) {
this.status = e.getErrorCode().getStatus();
this.error = e.getErrorCode().getError();
this.message = e.getErrorCode().getMessage();
}

public ExceptionDto(MethodArgumentNotValidException e){ // @Vaild exception
this.error = (HttpStatus) e.getStatusCode();
this.status = this.error.value();
this.message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/dku/springstudy/dto/common/ResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.dku.springstudy.dto.common;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class ResponseDto {
private boolean isSuccessful;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dku.springstudy.dto.common;

import lombok.Getter;

@Getter
public class SuccessResponseDto<T> extends ResponseDto{
private final T data;

public SuccessResponseDto(T data) {
super(true);
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.dku.springstudy.dto.member.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class LoginRequestDto {

@Email(message = "Email format does not match")
@NotBlank(message = "Please input email")
private String email;

@NotBlank(message = "Please input password")
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dku.springstudy.dto.member.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class LogoutRequestDto {
private String token;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dku.springstudy.dto.member.request;

import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class MembershipRequestDto {

@Email(message = "Email format does not match")
@NotBlank(message = "Please input email")
private String email;

@NotBlank(message = "Please input password")
private String password;

@NotBlank(message = "Please input name")
private String name;

}
Loading