/api/cart
This commit is contained in:
parent
813cf95677
commit
338db078d8
@ -23,7 +23,7 @@ from alkatorapi.views import (
|
|||||||
payment_result, payment_state,
|
payment_result, payment_state,
|
||||||
invoice, upload_files,
|
invoice, upload_files,
|
||||||
login_status, change_racer,
|
login_status, change_racer,
|
||||||
products, cart_add
|
products, cart_add, cart
|
||||||
)
|
)
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
@ -43,4 +43,5 @@ urlpatterns = [
|
|||||||
path('api/upload_files', upload_files),
|
path('api/upload_files', upload_files),
|
||||||
path('api/products', products),
|
path('api/products', products),
|
||||||
path('api/cart/add', cart_add),
|
path('api/cart/add', cart_add),
|
||||||
|
path('api/cart', cart),
|
||||||
]
|
]
|
||||||
|
@ -355,18 +355,40 @@ def cart_add(request):
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
cart = Cart(user=user)
|
cart = Cart(user=user)
|
||||||
cart.save()
|
cart.save()
|
||||||
|
product = Product.objects.get(id=_id)
|
||||||
try:
|
try:
|
||||||
cart_product = CartProduct(
|
cart_product = CartProduct(
|
||||||
product=Product.objects.get(id=_id),
|
product=product,
|
||||||
cart=cart,
|
cart=cart,
|
||||||
quantity=1,
|
quantity=1,
|
||||||
)
|
)
|
||||||
cart_product.save()
|
cart_product.save()
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
return HttpResponse('{"status":"failed", "reason":"Předmět už v košíku je!"}', status=400, content_type='application/json')
|
cart_product = CartProduct.objects.get(cart=cart, product=product)
|
||||||
|
cart_product.quantity += 1
|
||||||
|
cart_product.save()
|
||||||
return HttpResponse('{"status":"success", "reason":"Úspěšně přidáno do košíku."}', status=200)
|
return HttpResponse('{"status":"success", "reason":"Úspěšně přidáno do košíku."}', status=200)
|
||||||
|
|
||||||
|
|
||||||
|
def cart(request):
|
||||||
|
user = request.user
|
||||||
|
if not user.is_authenticated:
|
||||||
|
return HttpResponse("[]")
|
||||||
|
cart = []
|
||||||
|
try:
|
||||||
|
for product in CartProduct.objects.filter(user.cart):
|
||||||
|
cart.append({
|
||||||
|
"id": product.product.id,
|
||||||
|
"name": product.product.name,
|
||||||
|
"quantity": product.quantity,
|
||||||
|
"price": product.price,
|
||||||
|
"img": product.product.img.url,
|
||||||
|
})
|
||||||
|
except AttributeError:
|
||||||
|
return HttpResponse("[]")
|
||||||
|
return HttpResponse(json.dumps(cart))
|
||||||
|
|
||||||
|
|
||||||
def results(request):
|
def results(request):
|
||||||
results = []
|
results = []
|
||||||
n = 1
|
n = 1
|
||||||
|
@ -42,6 +42,9 @@ class Main extends Component {
|
|||||||
fetch(addr_prefix + "/api/products").then(resp => resp.json()).then(json => {
|
fetch(addr_prefix + "/api/products").then(resp => resp.json()).then(json => {
|
||||||
this.setState({products: json})
|
this.setState({products: json})
|
||||||
})
|
})
|
||||||
|
fetch(addr_prefix + "/api/cart").then(resp => resp.json()).then(json => {
|
||||||
|
this.setState({cart: json})
|
||||||
|
})
|
||||||
fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => {
|
fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => {
|
||||||
this.setState({login_status: json})
|
this.setState({login_status: json})
|
||||||
})
|
})
|
||||||
@ -79,19 +82,33 @@ class Main extends Component {
|
|||||||
showRacerModalWindow: true,
|
showRacerModalWindow: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
addToCart = (id) =>{
|
addToCart = (product) =>{
|
||||||
fetch(addr_prefix + "/api/cart/add?id=" + id).then(resp => resp.json()).then(json=>{
|
fetch(addr_prefix + "/api/cart/add?id=" + product.id).then(resp => resp.json()).then(json=>{
|
||||||
if(json.status == "failed"){
|
if(json.status == "failed"){
|
||||||
this.setState({
|
this.setState({
|
||||||
status_text: json.reason,
|
status_text: json.reason,
|
||||||
status: "failed",
|
status: "failed",
|
||||||
})
|
})
|
||||||
}else{
|
}else{
|
||||||
|
if(this.state.cart.filter(iter_product => {
|
||||||
|
if(iter_product.id == product.id){
|
||||||
|
iter_product.quantity += 1
|
||||||
|
return true
|
||||||
|
}else{
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}).length == 0){
|
||||||
this.setState({
|
this.setState({
|
||||||
status_text: json.reason,
|
status_text: json.reason,
|
||||||
status: "success",
|
status: "success",
|
||||||
cart: [...this.state.cart, id],
|
cart: [...this.state.cart, {...product, quantity: 1}],
|
||||||
});
|
})
|
||||||
|
}else {
|
||||||
|
this.setState({
|
||||||
|
status_text: json.reason,
|
||||||
|
status: "success",
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -122,6 +139,9 @@ class Main extends Component {
|
|||||||
fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => {
|
fetch(addr_prefix + "/api/login_status").then(resp => resp.json()).then(json => {
|
||||||
this.setState({login_status: json})
|
this.setState({login_status: json})
|
||||||
})
|
})
|
||||||
|
fetch(addr_prefix + "/api/cart").then(resp => resp.json()).then(json => {
|
||||||
|
this.setState({cart: json})
|
||||||
|
})
|
||||||
this.setState({
|
this.setState({
|
||||||
showRacerModalWindow: false,
|
showRacerModalWindow: false,
|
||||||
status_text: data.success,
|
status_text: data.success,
|
||||||
@ -249,7 +269,7 @@ class Main extends Component {
|
|||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h5 class="card-title">{product.name}</h5>
|
<h5 class="card-title">{product.name}</h5>
|
||||||
<p class="card-text">{product.description}</p>
|
<p class="card-text">{product.description}</p>
|
||||||
{product.price} Kč <a href="#" onClick={(e) => {e.preventDefault();this.addToCart(product.id)}} class="btn btn-primary">Přidat do košíku</a>
|
{product.price} Kč <a href="#" onClick={(e) => {e.preventDefault();this.addToCart(product)}} class="btn btn-primary">Přidat do košíku</a>
|
||||||
</div>
|
</div>
|
||||||
</div>)}
|
</div>)}
|
||||||
</div>}
|
</div>}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user