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 all 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.
103 changes: 103 additions & 0 deletions ERD/carrot_market.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
-- 테이블 순서는 관계를 고려하여 한 번에 실행해도 에러가 발생하지 않게 정렬되었습니다.

-- POSTIMAGE Table Create SQL
-- 테이블 생성 SQL - POSTIMAGE
CREATE TABLE POSTIMAGE
(
`Image_id` INT NOT NULL AUTO_INCREMENT,
`post_id` INT NULL,
`image_url` TEXT NULL,
`created` TIMESTAMP NULL,
PRIMARY KEY (Image_id)
);


-- MEMBER Table Create SQL
-- 테이블 생성 SQL - MEMBER
CREATE TABLE MEMBER
(
`member_id` 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,
`created` TIMESTAMP NULL,
PRIMARY KEY (member_id)
);


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


-- POST Table Create SQL
-- 테이블 생성 SQL - POST
CREATE TABLE POST
(
`post_id` INT NOT NULL AUTO_INCREMENT,
`member_id` INT NULL,
`like_count` TINYINT NULL,
`title` VARCHAR(45) NULL,
`content` TEXT NULL,
`created` TIMESTAMP NULL,
`price` INT NULL,
`Image_id` INT NULL,
PRIMARY KEY (post_id)
);

-- Foreign Key 설정 SQL - POST(member_id) -> MEMBER(member_id)
ALTER TABLE POST
ADD CONSTRAINT FK_POST_member_id_MEMBER_member_id FOREIGN KEY (member_id)
REFERENCES MEMBER (member_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - POST(member_id)
-- ALTER TABLE POST
-- DROP FOREIGN KEY FK_POST_member_id_MEMBER_member_id;

-- Foreign Key 설정 SQL - POST(Image_id) -> POSTIMAGE(Image_id)
ALTER TABLE POST
ADD CONSTRAINT FK_POST_Image_id_POSTIMAGE_Image_id FOREIGN KEY (Image_id)
REFERENCES POSTIMAGE (Image_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - POST(Image_id)
-- ALTER TABLE POST
-- DROP FOREIGN KEY FK_POST_Image_id_POSTIMAGE_Image_id;


-- POSTCATEGORY Table Create SQL
-- 테이블 생성 SQL - POSTCATEGORY
CREATE TABLE POSTCATEGORY
(
`post_category_id` INT NOT NULL AUTO_INCREMENT,
`category_id` INT NULL,
`post_id` INT NULL,
PRIMARY KEY (post_category_id)
);

-- Foreign Key 설정 SQL - POSTCATEGORY(category_id) -> CATEGORY(category_id)
ALTER TABLE POSTCATEGORY
ADD CONSTRAINT FK_POSTCATEGORY_category_id_CATEGORY_category_id FOREIGN KEY (category_id)
REFERENCES CATEGORY (category_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - POSTCATEGORY(category_id)
-- ALTER TABLE POSTCATEGORY
-- DROP FOREIGN KEY FK_POSTCATEGORY_category_id_CATEGORY_category_id;

-- Foreign Key 설정 SQL - POSTCATEGORY(post_id) -> POST(post_id)
ALTER TABLE POSTCATEGORY
ADD CONSTRAINT FK_POSTCATEGORY_post_id_POST_post_id FOREIGN KEY (post_id)
REFERENCES POST (post_id) ON DELETE RESTRICT ON UPDATE RESTRICT;

-- Foreign Key 삭제 SQL - POSTCATEGORY(post_id)
-- ALTER TABLE POSTCATEGORY
-- DROP FOREIGN KEY FK_POSTCATEGORY_post_id_POST_post_id;


1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Spring-JPA-study
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,31 @@
package com.dku.springstudy.controller;

import com.dku.springstudy.dto.category.response.CategoryDeleteResponseDto;
import com.dku.springstudy.dto.category.response.CategoryGetResponseDto;
import com.dku.springstudy.service.CategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
public class CategoryController {

private final CategoryService categoryService;

@GetMapping("/category/get/list")
public List<CategoryGetResponseDto> getAllCategory(){
return categoryService.getAllCategory();
}

@GetMapping("/category/get/{category}")
public CategoryGetResponseDto getPost(@PathVariable("category") String category){
return categoryService.getCategory(category);
}

@DeleteMapping("/category/delete/{category}")
public CategoryDeleteResponseDto deletePost(@PathVariable("category") String category){
return categoryService.deleteCategory(category);
}
}
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);

}

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

import com.dku.springstudy.domain.Member;
import com.dku.springstudy.dto.post.request.PostCreateRequestDto;
import com.dku.springstudy.dto.post.response.PostCreateResponseDto;
import com.dku.springstudy.dto.post.response.PostDeleteResponseDto;
import com.dku.springstudy.dto.post.response.PostGetResponseDto;
import com.dku.springstudy.security.CustomUserDetails;
import com.dku.springstudy.service.PostService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class PostController {

private final PostService postService;

@PostMapping("/post/create")
public PostCreateResponseDto createPost(
@AuthenticationPrincipal CustomUserDetails customUserDetails,
@Valid @RequestBody PostCreateRequestDto postCreateRequestDto){

Member member = customUserDetails.getMember();

return postService.createPost(postCreateRequestDto, member.getMember_id());
}

@GetMapping("/post/get/list")
public List<PostGetResponseDto> getAllPost(){
return postService.getAllPost();
}

@GetMapping("/post/get/{postId}")
public PostGetResponseDto getPost(@PathVariable("postId") Long postId){
return postService.getPost(postId);
}

@DeleteMapping("/post/delete/{postId}")
public PostDeleteResponseDto deletePost(@PathVariable("postId") Long postId){
return postService.deletePost(postId);
}

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

import com.dku.springstudy.dto.post.response.PostGetResponseDto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.parameters.P;

import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

@Entity
@Getter
@Setter
@Table(name = "CATEGORY")
@NoArgsConstructor
@Builder
@AllArgsConstructor
public class Category{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long category_id;

private String category;

private Timestamp created;

@OneToMany(mappedBy = "category", cascade = CascadeType.ALL)
@Builder.Default
private List<PostCategory> posts = new ArrayList<>();

public void addPostCategory(PostCategory postCategory){
this.posts.add(postCategory);
}

}
25 changes: 25 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,25 @@
package com.dku.springstudy.domain;

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

import java.sql.Timestamp;

@Entity
@Getter //Lombok
@Setter //Lombok
@Table(name = "MEMBER")
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Member {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long member_id;
private String name;
private String email;
private String password;
private String phone;
private String address;
private Timestamp created;

}
Loading