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] 안정현 미션 제출합니다. #59

Open
wants to merge 7 commits into
base: anhye0n
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
3 changes: 0 additions & 3 deletions src/main/java/roomescape/member/LoginMember.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public LoginMember(Long id, String name, String email, String role) {
this.role = role;
}

// Getters and setters


public Long getId() {
return id;
}
Expand Down
31 changes: 8 additions & 23 deletions src/main/java/roomescape/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package roomescape.member;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import roomescape.JwtUtil;

@Service
public class MemberService {
private final MemberDao memberDao;
private final String secretKey = "Yn2kjibddFAWtnPJ2AFlL8WXmohJMCvigQggaEypa5E=";
private final JwtUtil jwtUtil;

public MemberService(MemberDao memberDao) {
public MemberService(MemberDao memberDao, JwtUtil jwtUtil) {
this.memberDao = memberDao;
this.jwtUtil = jwtUtil;
}

public MemberResponse createMember(MemberRequest memberRequest) {
Expand All @@ -25,12 +23,7 @@ public MemberResponse createMember(MemberRequest memberRequest) {

public void login(MemberRequest memberRequest, HttpServletResponse response) {
Member member = memberDao.findByEmailAndPassword(memberRequest.getEmail(), memberRequest.getPassword());
String accessToken = Jwts.builder()
.setSubject(member.getId().toString())
.claim("name", member.getName())
.claim("role", member.getRole())
.signWith(Keys.hmacShaKeyFor(secretKey.getBytes()))
.compact();
String accessToken = jwtUtil.generateToken(member.getId().toString(), member.getName(), member.getRole());

Cookie cookie = new Cookie("token", accessToken);
cookie.setHttpOnly(true);
Expand All @@ -45,29 +38,21 @@ public Member findById(Long id) {
public String getNameFromToken(Cookie[] cookies) {
String token = extractTokenFromCookies(cookies);
if (token != null) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(secretKey.getBytes()))
.build()
.parseClaimsJws(token)
.getBody();
Claims claims = jwtUtil.parseToken(token);
return claims.get("name", String.class);
}
return null;
}

public String getRoleFromToken(Cookie[] cookies) {
String token = extractTokenFromCookies(cookies);
if (token != null) {
Claims claims = Jwts.parserBuilder()
.setSigningKey(Keys.hmacShaKeyFor(secretKey.getBytes(StandardCharsets.UTF_8)))
.build()
.parseClaimsJws(token)
.getBody();
Claims claims = jwtUtil.parseToken(token);
return claims.get("role", String.class);
}
return null;
}


private String extractTokenFromCookies(Cookie[] cookies) {
if (cookies == null) {
return null;
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/roomescape/reservation/MyReservationResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package roomescape.reservation;

public class MyReservationResponse {

private Long id;
private String theme;
private String date;
private String time;
private String status;

public MyReservationResponse(Long id, String theme, String date, String time, String status) {
this.id = id;
this.theme = theme;
this.date = date;
this.time = time;
this.status = status;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTheme() {
return theme;
}

public String getDate() {
return date;
}

public String getTime() {
return time;
}

public String getStatus() {
return status;
}
}
31 changes: 24 additions & 7 deletions src/main/java/roomescape/reservation/Reservation.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
package roomescape.reservation;

import jakarta.persistence.*;
import roomescape.theme.Theme;
import roomescape.time.Time;

@Entity
public class Reservation {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String date;

@ManyToOne
@JoinColumn(name = "time_id")
private Time time;

@ManyToOne
@JoinColumn(name = "theme_id")
private Theme theme;

public Reservation(Long id, String name, String date, Time time, Theme theme) {
this.id = id;
private Long memberId;

public Reservation() {
}

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

public Reservation(String name, String date, Time time, Theme theme) {
public Reservation(Long id, String name, String date, Time time, Theme theme, Long memberId) {
this.id = id;
this.name = name;
this.date = date;
this.time = time;
this.theme = theme;
}

public Reservation() {

this.memberId = memberId;
}

public Long getId() {
Expand All @@ -48,4 +61,8 @@ public Time getTime() {
public Theme getTheme() {
return theme;
}

public Long getMemberId() {
return memberId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public ResponseEntity delete(@PathVariable Long id) {
reservationService.deleteById(id);
return ResponseEntity.noContent().build();
}

@GetMapping("/reservations-mine")
public List<MyReservationResponse> getMyReservations(@CookieValue("token") String token) {
return reservationService.getMyReservations(token);
}
}
35 changes: 23 additions & 12 deletions src/main/java/roomescape/reservation/ReservationDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public List<Reservation> findAll() {
return jdbcTemplate.query(
"SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " +
"t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " +
"ti.id AS time_id, ti.time_value AS time_value " +
"ti.id AS time_id, ti.time_value AS time_value, " +
"r.member_id AS member_id " +
"FROM reservation r " +
"JOIN theme t ON r.theme_id = t.id " +
"JOIN time ti ON r.time_id = ti.id",
Expand All @@ -40,17 +41,20 @@ public List<Reservation> findAll() {
rs.getLong("theme_id"),
rs.getString("theme_name"),
rs.getString("theme_description")
)));
),
rs.getLong("member_id")
));
}

public Reservation save(ReservationRequest reservationRequest) {
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(connection -> {
PreparedStatement ps = connection.prepareStatement("INSERT INTO reservation(date, name, theme_id, time_id) VALUES (?, ?, ?, ?)", new String[]{"id"});
PreparedStatement ps = connection.prepareStatement("INSERT INTO reservation(date, name, theme_id, time_id, member_id) VALUES (?, ?, ?, ?, ?)", new String[]{"id"});
ps.setString(1, reservationRequest.getDate());
ps.setString(2, reservationRequest.getName());
ps.setLong(3, reservationRequest.getTheme());
ps.setLong(4, reservationRequest.getTime());
ps.setLong(5, reservationRequest.getMemberId());
return ps;
}, keyHolder);

Expand All @@ -67,24 +71,26 @@ public Reservation save(ReservationRequest reservationRequest) {
reservationRequest.getName(),
reservationRequest.getDate(),
time,
theme
theme,
reservationRequest.getMemberId()
);
}

public void deleteById(Long id) {
jdbcTemplate.update("DELETE FROM reservation WHERE id = ?", id);
}

public List<Reservation> findReservationsByDateAndTheme(String date, Long themeId) {
public List<Reservation> findByMemberId(Long memberId) {
return jdbcTemplate.query(
"SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " +
"t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " +
"ti.id AS time_id, ti.time_value AS time_value " +
"ti.id AS time_id, ti.time_value AS time_value, " +
"r.member_id AS member_id " +
"FROM reservation r " +
"JOIN theme t ON r.theme_id = t.id " +
"JOIN time ti ON r.time_id = ti.id" +
"WHERE r.date = ? AND r.theme_id = ?",
new Object[]{date, themeId},
"JOIN time ti ON r.time_id = ti.id " +
"WHERE r.member_id = ?",
new Object[]{memberId},
(rs, rowNum) -> new Reservation(
rs.getLong("reservation_id"),
rs.getString("reservation_name"),
Expand All @@ -97,14 +103,17 @@ public List<Reservation> findReservationsByDateAndTheme(String date, Long themeI
rs.getLong("theme_id"),
rs.getString("theme_name"),
rs.getString("theme_description")
)));
),
rs.getLong("member_id")
));
}

public List<Reservation> findByDateAndThemeId(String date, Long themeId) {
return jdbcTemplate.query(
"SELECT r.id AS reservation_id, r.name as reservation_name, r.date as reservation_date, " +
"t.id AS theme_id, t.name AS theme_name, t.description AS theme_description, " +
"ti.id AS time_id, ti.time_value AS time_value " +
"ti.id AS time_id, ti.time_value AS time_value, " +
"r.member_id AS member_id " +
"FROM reservation r " +
"JOIN theme t ON r.theme_id = t.id " +
"JOIN time ti ON r.time_id = ti.id " +
Expand All @@ -122,6 +131,8 @@ public List<Reservation> findByDateAndThemeId(String date, Long themeId) {
rs.getLong("theme_id"),
rs.getString("theme_name"),
rs.getString("theme_description")
)));
),
rs.getLong("member_id")
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package roomescape.reservation;

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

public interface ReservationRepository extends JpaRepository<Reservation, Long> {
}
8 changes: 6 additions & 2 deletions src/main/java/roomescape/reservation/ReservationRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ public class ReservationRequest {
private Long theme;
private Long time;
private String name;

// Getters and setters
private Long memberId;

public String getDate() {
return date;
Expand Down Expand Up @@ -39,4 +38,9 @@ public String getName() {
public void setName(String name) {
this.name = name;
}


public Long getMemberId() {
return memberId;
}
}
37 changes: 35 additions & 2 deletions src/main/java/roomescape/reservation/ReservationService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package roomescape.reservation;

import org.springframework.stereotype.Service;
import roomescape.JwtUtil;
import roomescape.waiting.WaitingRepository;
import roomescape.waiting.WaitingWithRank;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReservationService {
private final ReservationDao reservationDao;
private final WaitingRepository waitingRepository;

public ReservationService(ReservationDao reservationDao) {
public ReservationService(ReservationDao reservationDao, WaitingRepository waitingRepository) {
this.reservationDao = reservationDao;
this.waitingRepository = waitingRepository;
}

public ReservationResponse save(ReservationRequest reservationRequest) {
Expand All @@ -24,6 +30,33 @@ public void deleteById(Long id) {
public List<ReservationResponse> findAll() {
return reservationDao.findAll().stream()
.map(it -> new ReservationResponse(it.getId(), it.getName(), it.getTheme().getName(), it.getDate(), it.getTime().getValue()))
.toList();
.collect(Collectors.toList());
}

public List<MyReservationResponse> getMyReservations(String token) {
Long userId = JwtUtil.getUserIdFromToken(token);
List<MyReservationResponse> reservations = reservationDao.findByMemberId(userId).stream()
.map(reservation -> new MyReservationResponse(
reservation.getId(),
reservation.getTheme().getName(),
reservation.getDate(),
reservation.getTime().getValue(),
"예약"
))
.collect(Collectors.toList());

List<WaitingWithRank> waitings = waitingRepository.findWaitingsWithRankByMemberId(userId);
List<MyReservationResponse> waitingResponses = waitings.stream()
.map(waitingWithRank -> new MyReservationResponse(
waitingWithRank.getWaiting().getId(),
waitingWithRank.getWaiting().getThemeId().toString(),
waitingWithRank.getWaiting().getDate(),
waitingWithRank.getWaiting().getTimeId().toString(),
waitingWithRank.getRank() + "번째 예약대기"
))
.collect(Collectors.toList());

reservations.addAll(waitingResponses);
return reservations;
}
}
Loading