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

[feat] 화면 구현 #139

Merged
merged 9 commits into from
Aug 26, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/main/java/camp/woowak/lab/cart/service/CartService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public long getTotalPrice(CartTotalPriceCommand command) {
return totalPrice;
}

private Cart getCart(String customerId) {
public Cart getCart(String customerId) {
return cartRepository.findByCustomerId(customerId)
.orElse(cartRepository.save(new Cart(customerId)));
.orElseGet(() -> cartRepository.save(new Cart(customerId)));
}
}
12 changes: 12 additions & 0 deletions src/main/java/camp/woowak/lab/web/api/cart/CartApiController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import camp.woowak.lab.cart.domain.Cart;
import camp.woowak.lab.cart.domain.vo.CartItem;
import camp.woowak.lab.cart.service.CartService;
import camp.woowak.lab.cart.service.command.AddCartCommand;
import camp.woowak.lab.cart.service.command.CartTotalPriceCommand;
import camp.woowak.lab.web.authentication.LoginCustomer;
import camp.woowak.lab.web.authentication.annotation.AuthenticationPrincipal;
import camp.woowak.lab.web.dto.request.cart.AddCartRequest;
import camp.woowak.lab.web.dto.response.cart.AddCartResponse;
import camp.woowak.lab.web.dto.response.cart.CartResponse;
import camp.woowak.lab.web.dto.response.cart.CartTotalPriceResponse;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -26,6 +29,14 @@ public CartApiController(CartService cartService) {
this.cartService = cartService;
}

@GetMapping
public CartResponse getCart(@AuthenticationPrincipal LoginCustomer customer) {
Cart cart = cartService.getCart(customer.getId().toString());
long totalPrice = cartService.getTotalPrice(new CartTotalPriceCommand(customer.getId().toString()));
return new CartResponse(cart.getCustomerId(), cart.getCartItems().stream().mapToLong(CartItem::getAmount).sum(),
totalPrice);
}

@PostMapping
public AddCartResponse addCart(
@AuthenticationPrincipal LoginCustomer loginCustomer,
Expand All @@ -47,4 +58,5 @@ public CartTotalPriceResponse getCartTotalPrice(
log.info("Customer({})'s total price in cart is {}", loginCustomer.getId(), totalPrice);
return new CartTotalPriceResponse(totalPrice);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package camp.woowak.lab.web.dto.response.cart;

public record CartResponse(String customerId, Long totalAmount, Long totalPrice) {
}
37 changes: 37 additions & 0 deletions src/main/java/camp/woowak/lab/web/view/ViewController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package camp.woowak.lab.web.view;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import camp.woowak.lab.store.service.StoreDisplayService;
import camp.woowak.lab.store.service.response.StoreDisplayResponse;

@Controller
@RequestMapping("/view")
public class ViewController {
private StoreDisplayService storeDisplayService;

public ViewController(StoreDisplayService storeDisplayService) {
this.storeDisplayService = storeDisplayService;
}

@GetMapping("/login")
public String login() {
return "login";
}

@GetMapping("/stores")
public String stores() {
return "stores";
}

@GetMapping("/stores/{id}")
public String store(@PathVariable Long id, Model model) {
StoreDisplayResponse storeDisplayResponse = storeDisplayService.displayStore(id);
model.addAttribute("store", storeDisplayResponse);
return "store";
}
}
143 changes: 143 additions & 0 deletions src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>배달의민족 로그인</title>
<style>
body {
font-family: 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

.container {
background-color: white;
width: 100%;
max-width: 400px;
padding: 20px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 8px;
}

h1 {
text-align: center;
margin-bottom: 30px;
}

form {
display: flex;
flex-direction: column;
}

input {
margin-bottom: 15px;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}

button {
padding: 10px;
background-color: #2AC1BC;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}

button:hover {
background-color: #219a95;
}

.login-options {
margin-top: 20px;
}

.login-option {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 10px;
text-align: center;
border-radius: 4px;
text-decoration: none;
color: black;
}

.kakao {
background-color: #FEE500;
}

.naver {
background-color: #03C75A;
color: white;
}

.apple {
background-color: #000;
color: white;
}

.phone {
background-color: #f5f5f5;
}

.email-login {
text-align: center;
margin-top: 15px;
font-size: 14px;
color: #666;
}
</style>
</head>
<body>
<div class="container">
<h1>배달의민족</h1>
<form id="loginForm">
<input name="email" placeholder="이메일" required type="email">
<input name="password" placeholder="비밀번호" required type="password">
<button type="submit">로그인</button>
</form>
</div>

<script>
document.getElementById('loginForm').addEventListener('submit', function (e) {
e.preventDefault();

const email = this.email.value;
const password = this.password.value;

fetch('/customers/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({email, password})
})
.then(response => {
if (response.status === 204) {
// 로그인 성공
alert('로그인 성공!');
// 여기에 로그인 성공 후 수행할 작업을 추가하세요.
// 예: 메인 페이지로 리다이렉트
window.location.href = '/view/stores';
} else if (!response.ok) {
// 204가 아닌 다른 상태 코드인 경우 에러로 처리
throw new Error('로그인 실패');
}
})
.catch(error => {
console.error('Error:', error);
alert('로그인 실패: ' + error.message);
});
});
</script>
</body>
</html>
Loading