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

[Spring Data JPA] 방예혁 미션 제출합니다. #85

Open
wants to merge 15 commits into
base: yehyeokbang
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

implementation 'dev.akkinoc.spring.boot:logback-access-spring-boot-starter:4.0.0'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
Expand All @@ -14,7 +15,8 @@

@Component
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver {
private static final String SECRET_KEY = "Yn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=";
@Value("${roomescape.auth.jwt.secret}")
private String secretKey;

@Override
public boolean supportsParameter(MethodParameter parameter) {
Expand All @@ -35,7 +37,7 @@ public Object resolveArgument(@NotNull MethodParameter parameter,
}

return Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(SECRET_KEY.getBytes()))
.setSigningKey(Keys.hmacShaKeyFor(secretKey.getBytes()))
.build()
.parseClaimsJws(token)
.getBody()
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/roomescape/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package roomescape.member.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Member {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String email;
Expand All @@ -21,6 +29,9 @@ public Member(String name, String email, String password, String role) {
this.role = role;
}

public Member() {
}

public Long getId() {
return id;
}
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/roomescape/member/domain/MemberDao.java

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/roomescape/member/domain/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package roomescape.member.domain;

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

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
Optional<Member> findByEmailAndPassword(String email, String password);
Optional<Member> findByName(String name);
}
25 changes: 12 additions & 13 deletions src/main/java/roomescape/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import jakarta.servlet.http.Cookie;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import roomescape.member.controller.dto.MemberRequest;
import roomescape.member.controller.dto.MemberResponse;
import roomescape.member.domain.Member;
import roomescape.member.domain.MemberDao;
import roomescape.member.domain.MemberRepository;

@Service
public class MemberService {
private static final String SECRET_KEY = "Yn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=";
@Value("${roomescape.auth.jwt.secret}")
private String secretKey;

private final MemberDao memberDao;
private final MemberRepository memberRepository;

public MemberService(MemberDao memberDao) {
this.memberDao = memberDao;
public MemberService(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}

public MemberResponse createMember(MemberRequest memberRequest) {
Member member = memberDao.save(requestToMember(memberRequest));
Member member = memberRepository.save(requestToMember(memberRequest));
return memberToResponse(member);
}

Expand All @@ -33,16 +34,14 @@ private MemberResponse memberToResponse(Member member) {
}

public String login(MemberRequest memberRequest) {
Member member = memberDao.findByEmailAndPassword(memberRequest.email(), memberRequest.password());
if (member == null) {
throw new RuntimeException("로그인 실패");
}
Member member = memberRepository.findByEmailAndPassword(memberRequest.email(), memberRequest.password())
.orElseThrow(() -> new RuntimeException("로그인 실패"));

return Jwts.builder()
.setSubject(member.getId().toString())
.claim("name", member.getName())
.claim("role", member.getRole())
.signWith(Keys.hmacShaKeyFor(SECRET_KEY.getBytes()))
.signWith(Keys.hmacShaKeyFor(secretKey.getBytes()))
.compact();
}

Expand All @@ -51,7 +50,7 @@ public MemberResponse getMemberByName(String memberName) {
throw new RuntimeException("로그인이 필요합니다.");
}

return memberDao.findByName(memberName)
return memberRepository.findByName(memberName)
.map(this::memberToResponse)
.orElseThrow(() -> new RuntimeException("사용자를 찾을 수 없습니다."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import roomescape.global.login.LoginMemberName;
import roomescape.reservation.controller.dto.MyReservationResponse;
import roomescape.reservation.service.ReservationService;
import roomescape.reservation.controller.dto.ReservationRequest;
import roomescape.reservation.controller.dto.ReservationResponse;
Expand All @@ -30,6 +31,11 @@ public List<ReservationResponse> list() {
return reservationService.findAll();
}

@GetMapping("/reservations-mine")
public List<MyReservationResponse> mine(@LoginMemberName String memberName) {
return reservationService.findMyReservations(memberName);
}

@PostMapping("/reservations")
public ResponseEntity<ReservationResponse> create(@LoginMemberName String memberName,
@RequestBody ReservationRequest reservationRequest) {
Expand All @@ -47,6 +53,13 @@ public ResponseEntity<ReservationResponse> create(@LoginMemberName String member
.body(reservation);
}

@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
reservationService.deleteById(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT)
.build();
}

private boolean isNamePresentInRequest(ReservationRequest reservationRequest) {
return reservationRequest.name() != null;
}
Expand All @@ -57,11 +70,4 @@ private boolean isNotNullRequestParameters(String memberName, ReservationRequest
|| reservationRequest.theme() == null
|| reservationRequest.time() == null;
}

@DeleteMapping("/reservations/{id}")
public ResponseEntity<Void> delete(@PathVariable Long id) {
reservationService.deleteById(id);
return ResponseEntity.status(HttpStatus.NO_CONTENT)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package roomescape.reservation.controller.dto;

public record MyReservationResponse(Long id, String theme, String date, String time, String status) {
}
33 changes: 31 additions & 2 deletions src/main/java/roomescape/reservation/domain/Reservation.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package roomescape.reservation.domain;

import roomescape.theme.Theme;
import roomescape.time.Time;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import roomescape.member.domain.Member;
import roomescape.theme.domain.Theme;
import roomescape.time.domain.Time;

@Entity
public class Reservation {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String date;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "time_id")
private Time time;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "theme_id")
private Theme theme;

public Reservation(Long id, String name, String date, Time time, Theme theme) {
Expand All @@ -18,6 +39,14 @@ public Reservation(Long id, String name, String date, Time time, Theme theme) {
this.theme = theme;
}

public Reservation(String name, String date, Member member, Time time, Theme theme) {
this.name = name;
this.date = date;
this.member = member;
this.time = time;
this.theme = theme;
}

public Reservation(String name, String date, Time time, Theme theme) {
this.name = name;
this.date = date;
Expand Down
Loading