Skip to content

Commit

Permalink
SpendList 추가(Domain 만)
Browse files Browse the repository at this point in the history
MemberId String 으로 수정

MemberController 추가
  • Loading branch information
RungDaRongDa committed Jan 19, 2024
1 parent 3d6963c commit d037fda
Show file tree
Hide file tree
Showing 26 changed files with 274 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
package sigma.http.member;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import sigma.application.auth.interactor.NewTokenUseCase;
import sigma.application.member.interactor.MemberFindUseCase;
import sigma.application.member.interactor.response.MemberFindByNameResponse;
import sigma.application.member.interactor.response.MemberFindOneResponse;
import sigma.http.member.request.SetFirebaseTokenRequest;

import java.util.List;

@RestController
@RequestMapping(value = "/member")
@RequiredArgsConstructor
public class MemberController {
private final MemberFindUseCase memberFindUseCase;
private final NewTokenUseCase newTokenUseCase;


//권한 추가(Student)
@GetMapping("/findOne")
public MemberFindOneResponse findOne(@RequestHeader(value = "Authorization") String token){
return memberFindUseCase.findOne(token.substring("Bearer".length()).trim());
}

@GetMapping("/search/{memberName}")
public List<MemberFindByNameResponse> findByName(@PathVariable(value = "memberName") String name){
return memberFindUseCase.findByName(name);
}

@GetMapping("/newAccessToken")
public String newAccessToKen(@RequestHeader(value = "RefreshToken") String token){
return newTokenUseCase.newAccessToKen(token);
}

@PostMapping("/setFirebase/{memberId}")
public void setFirebaseToken(@PathVariable(value = "memberId") String memberId, @RequestBody SetFirebaseTokenRequest setFirebaseTokenRequest){
return;newTokenUseCase.setNewFirebaseToken(memberId,setFirebaseTokenRequest.firebaseToken());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package sigma.http.member.request;

public record SetFirebaseTokenRequest(
String firebaseToken
) { }
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package sigma.jpa.account;

import sigma.domain.account.model.Account;
import sigma.domain.account.model.value.AccountId;
import sigma.domain.account.model.value.*;
import sigma.domain.account.model.value.AccountNumber;
import sigma.domain.account.model.value.Balance;
import sigma.domain.account.model.value.HolderId;

abstract class AccountConverter {

Expand All @@ -15,30 +13,35 @@ static Account toDomain(final AccountJPAEntity entity) {

return Account.builder()
.id(new AccountId(entity.getId()))
.number(new AccountNumber(entity.getNumber()))
.accountInfo(new AccountInfo(entity.getNumber(), entity.getName()))
.holderId(new HolderId(entity.getHolderId()))
.balance(new Balance(entity.getBalance()))
.type(entity.getType())
.profile(new Profile(entity.getProfileImage()))
.build();
}

static AccountJPAEntity toEntity(final Account domain) {
try {
return AccountJPAEntity.builder()
.id(domain.id().getId())
.number(domain.number().number())
.number(domain.accountInfo().number())
.name(domain.accountInfo().name())
.holderId(domain.holderId().getId())
.balance(domain.balance().getValue())
.type(domain.type())
.profileImage(domain.profile().profileUrl())
.createdAt(domain.createdAt())
.modifiedAt(domain.modifiedAt())
.build();
} catch (NullPointerException e) {
return AccountJPAEntity.builder()
.number(domain.number().number())
.number(domain.accountInfo().number())
.name(domain.accountInfo().name())
.holderId(domain.holderId().getId())
.balance(domain.balance().getValue())
.type(domain.type())
.profileImage(domain.profile().profileUrl())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ public class AccountJPAEntity extends AbstractIdEntity {
@NotNull
@Column(unique = true)
private Long number;

@NotNull
private String name;

@NotNull
private Long holderId;

@NotNull
private Long balance;

@NotNull
@Enumerated(value = EnumType.STRING)
private AccountType type;

@NotNull
private String profileImage;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sigma.jpa.member;

import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import sigma.application.member.outport.MemberDBPort;
import sigma.domain.member.model.Member;

import java.util.List;
import java.util.Optional;

import static sigma.jpa.member.MemberConverter.*;
Expand All @@ -14,15 +17,23 @@
class MemberJPAAdapter implements MemberDBPort {

private final MemberJPARepository repository;
private final EntityManager entityManager;

@Override
public Member save(final Member member) {
return toDomain(repository.save(toEntity(member)));
}

@Override
public Optional<Member> findById(final Long id) {
public Optional<Member> findById(final String id) {
return Optional.ofNullable(toDomain(repository.findById(id).orElse(null)));
}

@Override
public List<Member> findByName(final String name){
return entityManager.createQuery("SELECT m from tbl_member as m where m.studentName = :name", Member.class)
.setParameter("name",name)
.getResultList();
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package sigma.jpa.member;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Table;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -17,7 +14,10 @@
@Getter
@SuperBuilder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberJPAEntity extends AbstractIdEntity {
public class MemberJPAEntity /* extends AbstractIdEntity*/ {

@jakarta.persistence.Id
private String Id;

@NotNull
@Enumerated(value = EnumType.STRING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

import org.springframework.data.jpa.repository.JpaRepository;

interface MemberJPARepository extends JpaRepository<MemberJPAEntity, Long> {
interface MemberJPARepository extends JpaRepository<MemberJPAEntity, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,18 @@ class JwtAdapter implements TokenPort {
private final JwtProperties jwtProperties;

@Override
public Long parseAccessToken(final String accessToken) {
public String parseToken(final String accessToken, final String jwtType) {
final String key = jwtProperties.getAccessKey();

final Jws<Claims> jwsClaims = Jwts.parser().setSigningKey(key).parseClaimsJws(extract(accessToken));

if(!(jwsClaims.getHeader().get(Header.JWT_TYPE).equals(JwtType.ACCESS.toString()))) {
if(!(jwsClaims.getHeader().get(Header.JWT_TYPE).equals(jwtType))) {
throw WrongTokenType.EXCEPTION;
}

return Long.parseLong(jwsClaims.getBody().getSubject());
return jwsClaims.getBody().getSubject();

//이게 validateToken부분인것 같은데 예외처리 안된것 같음
}

@Override
Expand All @@ -38,6 +40,11 @@ public TokenResponse issueToken(final MemberId id, final Role role) {
);
}

@Override
public String newAccessToken(String id, Role role) {
return generateToken(id, role, JwtType.ACCESS);
}

static String extract(final String token) {
if (StringUtils.hasText(token) && token.startsWith("Bearer ")) {
return token.substring(7);
Expand All @@ -46,7 +53,7 @@ static String extract(final String token) {
return token;
}

private String generateToken(final Long id, final Role role, final JwtType jwtType) {
private String generateToken(final String id, final Role role, final JwtType jwtType) {
final String key;
final String expire;

Expand All @@ -64,12 +71,11 @@ private String generateToken(final Long id, final Role role, final JwtType jwtTy

return Jwts.builder()
.setHeaderParam(Header.JWT_TYPE, jwtType)
.setSubject(id.toString())
.setSubject(id)
.claim("authority", role)
.setIssuedAt(new Date())
.setExpiration(new Date(System.currentTimeMillis() + expire))
.signWith(SignatureAlgorithm.HS512, key)
.compact();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class JwtHelper {

public void setAuthentication(final String accessToken) {
if(accessToken != null) {
final Long memberId = tokenPort.parseAccessToken(accessToken);
final String memberId = tokenPort.parseToken(accessToken, "ACCESS");

final Member member = memberDBPort.getById(memberId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package sigma.application.auth.interactor;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import sigma.application.auth.outport.TokenPort;
import sigma.application.member.outport.MemberDBPort;
import sigma.domain.member.model.Member;
import sigma.domain.member.model.value.Role;

import java.util.Optional;

@Component
@RequiredArgsConstructor
public class NewTokenUseCase {
private final TokenPort tokenPort;
private final MemberDBPort memberDBPort;

public String newAccessToKen(String token) {
final String id = tokenPort.parseToken(token, "REFRESH");
final Role role = memberDBPort.findById(id).orElseThrow().role(); // 이거 고칠 수 있나?
return tokenPort.newAccessToken(id,role);
}

public void setNewFirebaseToken(String memberId, String firebaseToken) {
Member member = memberDBPort.getById(memberId);

// login 부분 알아야 될것 같음
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

public interface TokenPort {

Long parseAccessToken(String accessToken);
String parseToken(String accessToken, String jwtType);

TokenResponse issueToken(MemberId id, Role role);

String newAccessToken(String id, Role role);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package sigma.application.member.interactor;

import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import sigma.application.member.interactor.response.MemberFindByNameResponse;
import sigma.application.member.interactor.response.MemberFindOneResponse;
import sigma.application.member.outport.MemberDBPort;
import sigma.domain.member.model.Member;
import sigma.domain.member.model.event.MemberRegisteredEvent;
import sigma.domain.member.model.value.StudentInfo;

import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class MemberFindUseCase {
private final MemberDBPort memberDBPort;
private final ApplicationEventPublisher applicationEventPublisher;


public MemberFindOneResponse findOne(final String token){
// 들어올 때 인증한 멤버 가져오기
//지금은 그 부분이 어딘지 모르겠음
return null;
}

public List<MemberFindByNameResponse> findByName(String name) {
List<Member> members = memberDBPort.findByName(name);
List<MemberFindByNameResponse> result = new ArrayList<>();
for (Member member : members) {
result.add(new MemberFindByNameResponse(member.id(),member.studentInfo(),member.memberInfo()));
// applicationEventPublisher.publishEvent(new MemberRegisteredEvent(member.id()));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sigma.application.member.interactor.response;

import sigma.domain.member.model.value.MemberId;
import sigma.domain.member.model.value.MemberInfo;
import sigma.domain.member.model.value.StudentInfo;

public record MemberFindByNameResponse(
MemberId memberId,
StudentInfo studentInfo,
MemberInfo memberInfo
) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package sigma.application.member.interactor.response;

import sigma.domain.account.model.Account;
import sigma.domain.member.model.value.MemberId;
import sigma.domain.member.model.value.MemberInfo;
import sigma.domain.member.model.value.Role;
import sigma.domain.member.model.value.StudentInfo;

import java.util.List;

public record MemberFindOneResponse(
MemberId memberId,
StudentInfo studentInfo,
MemberInfo memberInfo,
Role role,
List<Account> accounts
) { }
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import sigma.domain.member.model.Member;
import sigma.domain.member.model.exception.MemberNotFound;

import java.util.List;
import java.util.Optional;

public interface MemberDBPort {

Member save(Member member);

Optional<Member> findById(Long id);
Optional<Member> findById(String id);

default Member getById(final Long id) {
default Member getById(final String id) {
return findById(id)
.orElseThrow(() -> MemberNotFound.EXCEPTION);
}

List<Member> findByName(String name);
}
Loading

0 comments on commit d037fda

Please sign in to comment.