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

4 주차 PR #6

Open
wants to merge 17 commits into
base: master
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.ll.exam.final__2022_10_08.app.member.dto;

import com.ll.exam.final__2022_10_08.app.member.entity.Member;
import com.ll.exam.final__2022_10_08.util.DateTimeParser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Data
@AllArgsConstructor
@Builder
@RequiredArgsConstructor
public class MemberDto {
Long id;
List<Integer> createDate;
List<Integer> modifyDate;
String username;
String email;
boolean emailVerified = false;
String nickname = "";

public static MemberDto fromEntity(Member member){
List<Integer> createDateList = DateTimeParser.dateTimeToIntegerList(member.getCreateDate());
List<Integer> modifyDateList = DateTimeParser.dateTimeToIntegerList(member.getModifyDate());

return MemberDto.builder()
.id(member.getId())
.createDate(createDateList)
.modifyDate(modifyDateList)
.username(member.getUsername())
.email(member.getEmail())
.emailVerified(member.isEmailVerified())
.nickname(member.getNickname() == null ? "" : member.getNickname())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ll.exam.final__2022_10_08.app.base.entity.BaseEntity;
import com.ll.exam.final__2022_10_08.app.member.entity.emum.AuthLevel;
import com.ll.exam.final__2022_10_08.util.Ut;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.security.core.GrantedAuthority;
Expand All @@ -15,6 +16,7 @@
import javax.persistence.Entity;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Entity
@Setter
Expand Down Expand Up @@ -67,4 +69,15 @@ public List<GrantedAuthority> genAuthorities() {

return authorities;
}

public Map<String, Object> getAccessTokenClaims() {
return Ut.mapOf(
"id", getId(),
"createDate", getCreateDate(),
"modifyDate", getModifyDate(),
"username", getUsername(),
"email", getEmail(),
"authorities", genAuthorities() //수정해야 할 듯. gen 을 쓰는게 말이 되나
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.ll.exam.final__2022_10_08.app.member.restcontroller;

import com.ll.exam.final__2022_10_08.app.base.dto.RsData;
import com.ll.exam.final__2022_10_08.app.base.rq.Rq;
import com.ll.exam.final__2022_10_08.app.member.dto.MemberDto;
import com.ll.exam.final__2022_10_08.app.member.entity.Member;
import com.ll.exam.final__2022_10_08.app.member.service.MemberService;
import com.ll.exam.final__2022_10_08.app.security.dto.MemberContext;
import com.ll.exam.final__2022_10_08.util.Ut;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequiredArgsConstructor
public class MemberRestController {
private final MemberService memberService;
private final PasswordEncoder passwordEncoder;
private final Rq rq;

//리퀘스트에 토큰 담는 법?
//세션 방식에서 완전한 변경 필요함
//@PreAuthorize("isAuthenticated()")
@GetMapping("/api/v1/member/me")
public ResponseEntity<RsData> showProfile(){

Long id = rq.getMember() != null ? rq.getMember().getId() : 1;
Member member = memberService.findById(id).orElseThrow(()->{throw new RuntimeException("해당 ID의 유저가 없습니다.");});
MemberDto memberDto = MemberDto.fromEntity(member);

return Ut.spring.responseEntityOf(
RsData.of(
"S-1",
"회원 조회 성공",
Ut.mapOf(
"member", memberDto
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dto를 활용해서 요구사항에 맞추는게 더 좋겠네요!!

)
)
);
}


// https://www.youtube.com/watch?v=EMsXhobcUDc
@PreAuthorize("isAnonymous()")
@PostMapping("/api/v1/member/login")
public ResponseEntity<RsData> login(@RequestBody LoginDto loginDto) {
if (loginDto.isNotValid()) {
return Ut.spring.responseEntityOf(RsData.of("F-1", "로그인 정보가 올바르지 않습니다."));
}

Member member = memberService.findByUsername(loginDto.getUsername()).orElse(null);

if (member == null) {
return Ut.spring.responseEntityOf(RsData.of("F-2", "일치하는 회원이 존재하지 않습니다."));
}

if (passwordEncoder.matches(loginDto.getPassword(), member.getPassword()) == false) {
return Ut.spring.responseEntityOf(RsData.of("F-3", "비밀번호가 일치하지 않습니다."));
}

String accessToken = "JWT_Access_Token";

return Ut.spring.responseEntityOf(
RsData.of(
"S-1",
"로그인 성공, Access Token 을 발급합니다.",
Ut.mapOf(
"accessToken", accessToken
)
),
Ut.spring.httpHeadersOf("Authentication", accessToken)
);
}
@Data
public static class LoginDto {
private String username;
private String password;

public boolean isNotValid() {
return username == null || password == null || username.trim().length() == 0 || password.trim().length() == 0;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MemberService {
private final EmailVerificationService emailVerificationService;
private final EmailService emailService;
private final CashService cashService;
//private final JwtProvider jwtProvider;

@Transactional
public Member join(String username, String password, String email, String nickname) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.ll.exam.final__2022_10_08.app.myBook.dto;

import com.ll.exam.final__2022_10_08.app.member.entity.Member;
import com.ll.exam.final__2022_10_08.app.myBook.entity.MyBook;
import com.ll.exam.final__2022_10_08.app.post.dto.PostDto;
import com.ll.exam.final__2022_10_08.app.product.dto.ProductDto;
import com.ll.exam.final__2022_10_08.app.product.entity.Product;
import com.ll.exam.final__2022_10_08.util.DateTimeParser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.List;

@Data
@AllArgsConstructor
@Builder
@RequiredArgsConstructor
public class MyBookDetailDto{ //상속해도 될 듯..?
Long id;
List<Integer> createDate;
List<Integer> modifyDate;
Long ownerId;
ProductDto product;
Long authorId;
String authorName;
String subject;
List<PostDto> bookChapters; //밖에서 만들어서 넣어줘야 함.

public static MyBookDetailDto fromEntity(@NotNull MyBook myBook){

Product productEntity = myBook.getProduct();
Member author = productEntity.getAuthor();
Member owner = myBook.getOwner();
List<Integer> createDateList = DateTimeParser.dateTimeToIntegerList(myBook.getCreateDate());
List<Integer> modifyDateList = DateTimeParser.dateTimeToIntegerList(myBook.getModifyDate());

return MyBookDetailDto.builder()
.id(myBook.getId())
.createDate(createDateList)
.modifyDate(modifyDateList)
.ownerId(owner.getId())
.product(ProductDto.fromEntity(productEntity))
.authorId(author.getId())
.authorName(author.getNickname())
.subject(productEntity.getSubject())
.build();
}
public MyBookDetailDto(@NotNull MyBookDto myBookDto)
{
id = myBookDto.getId();
createDate = myBookDto.getCreateDate();
modifyDate = myBookDto.getModifyDate();
ownerId = myBookDto.getOwnerId();
product = myBookDto.getProduct();
authorId = myBookDto.getAuthorId();
authorName = myBookDto.getAuthorName();
subject = myBookDto.getSubject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ll.exam.final__2022_10_08.app.myBook.dto;

import com.ll.exam.final__2022_10_08.app.member.entity.Member;
import com.ll.exam.final__2022_10_08.app.myBook.entity.MyBook;
import com.ll.exam.final__2022_10_08.app.product.dto.ProductDto;
import com.ll.exam.final__2022_10_08.app.product.entity.Product;
import com.ll.exam.final__2022_10_08.util.DateTimeParser;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;

import javax.validation.constraints.NotNull;
import java.util.List;

@Data
@AllArgsConstructor
@Builder

@RequiredArgsConstructor
public class MyBookDto {
Long id;
List<Integer> createDate;
List<Integer> modifyDate;
Long ownerId;
ProductDto product;
Long authorId;
String authorName;
String subject;

public static MyBookDto fromEntity(@NotNull MyBook myBook){

Product productEntity = myBook.getProduct();
Member author = productEntity.getAuthor();
Member owner = myBook.getOwner();
List<Integer> createDateList = DateTimeParser.dateTimeToIntegerList(myBook.getCreateDate());
List<Integer> modifyDateList = DateTimeParser.dateTimeToIntegerList(myBook.getModifyDate());

return MyBookDto.builder()
.id(myBook.getId())
.createDate(createDateList)
.modifyDate(modifyDateList)
.ownerId(owner.getId())
.product(ProductDto.fromEntity(productEntity))
.authorId(author.getId())
.authorName(author.getNickname())
.subject(productEntity.getSubject())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.ll.exam.final__2022_10_08.app.myBook.restcontroller;

import com.ll.exam.final__2022_10_08.app.base.dto.RsData;
import com.ll.exam.final__2022_10_08.app.base.rq.Rq;
import com.ll.exam.final__2022_10_08.app.member.entity.Member;
import com.ll.exam.final__2022_10_08.app.member.service.MemberService;
import com.ll.exam.final__2022_10_08.app.myBook.dto.MyBookDetailDto;
import com.ll.exam.final__2022_10_08.app.myBook.dto.MyBookDto;
import com.ll.exam.final__2022_10_08.app.myBook.entity.MyBook;
import com.ll.exam.final__2022_10_08.app.myBook.service.MyBookService;
import com.ll.exam.final__2022_10_08.app.post.entity.Post;
import com.ll.exam.final__2022_10_08.app.postTag.entity.PostTag;
import com.ll.exam.final__2022_10_08.app.postTag.service.PostTagService;
import com.ll.exam.final__2022_10_08.app.postkeyword.entity.PostKeyword;
import com.ll.exam.final__2022_10_08.app.product.dto.ProductDto;
import com.ll.exam.final__2022_10_08.app.product.entity.Product;
import com.ll.exam.final__2022_10_08.app.product.service.ProductService;
import com.ll.exam.final__2022_10_08.app.productTag.entity.ProductTag;
import com.ll.exam.final__2022_10_08.util.Ut;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
//@RequestMapping("/api/v1/myBooks")
public class MyBookRestController {

private final MemberService memberService;
private final Rq rq;
private final MyBookService myBookService;
private final ProductService productService;
private final PostTagService postTagService;

//@PreAuthorize("isAuthenticated()")
@GetMapping("/api/v1/myBooks")
public ResponseEntity<RsData> booksList(){

Long id = rq.getMember() != null ? rq.getMember().getId() : 1; //임시, 로그인 정보 못 가져올시 1로
List<MyBookDto> myBooks = myBookService.findAllByOwnerId(id);

return Ut.spring.responseEntityOf(
RsData.of(
"S-1",
"내 책 리스트 조회 성공",
Ut.mapOf(
"myBooks", myBooks
)
)
);
}

//@PreAuthorize("isAuthenticated()")
@GetMapping("/api/v1/myBooks/{id}")
public ResponseEntity<RsData> bookDetail(long id){


MyBook myBook = myBookService.findById(id);
MyBookDetailDto myBookDetailDto = MyBookDetailDto.fromEntity(myBook);


Product product = myBook.getProduct();
//Long postKeywordId = product.getPostKeyword().getId();
//PostTag postTag = postTagService.


//PostTag postTag = postTagService.
//List<Post> posts = myBook.getProduct().getPostKeyword().getContent()
//List<PostDto> bookChapters =
//myBookDetailDto.setBookChapters(bookChapters);

return Ut.spring.responseEntityOf(
RsData.of(
"S-1",
"내 책 조회 성공",
Ut.mapOf(
"myBook", myBookDetailDto
)
)
);
}

}
Loading