Skip to content

Commit

Permalink
[feat] 화면 구현 (#139)
Browse files Browse the repository at this point in the history
* [feat] login.html 구현

* [feat] stores.html 구현

* [feat] ViewController 구현

* [feat] store.html 구현
- 상점 상세 페이지

* [feat] ViewController 수정
- 가게 상세 페이지 핸들러 구현

* [style] store.html 수정
- 인덴팅 수정

* [feat] stores.html 수정
- 가게 정보 스타일 수정

* [feat] store.html 수정
- 장바구니 커서 구현

* [feat] 프론트를 위한 API 추가

Co-authored-by: june-777 <wlwhswnsrl96@gmail.com>

---------

Co-authored-by: june-777 <wlwhswnsrl96@gmail.com>
  • Loading branch information
kimhyun5u and june-777 authored Aug 26, 2024
1 parent dc71d81 commit 952f424
Show file tree
Hide file tree
Showing 7 changed files with 631 additions and 2 deletions.
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

0 comments on commit 952f424

Please sign in to comment.