diff --git a/alkatorapi/models.py b/alkatorapi/models.py index 7bc8671..bf249f7 100644 --- a/alkatorapi/models.py +++ b/alkatorapi/models.py @@ -76,7 +76,7 @@ class Product(models.Model): class Cart(models.Model): - user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT) + user = models.ForeignKey(DjangoUser, on_delete=models.RESTRICT, related_name='cart') items = models.ManyToManyField(Product, through='CartProduct') diff --git a/alkatorapi/views.py b/alkatorapi/views.py index c051c73..fc2f611 100644 --- a/alkatorapi/views.py +++ b/alkatorapi/views.py @@ -19,7 +19,11 @@ import PIL.Image import random from collections import OrderedDict -from .models import User, ALKATOR_CHOICES_DICT, ALKATOR_CLASSES, Profile, Racer, Invoice, Product, InvoiceProduct +from .models import ( + User, ALKATOR_CHOICES_DICT, ALKATOR_CLASSES, + Profile, Racer, Invoice, Product, InvoiceProduct, + Cart, CartProduct +) from alkator.settings import COMGATE_MERCHANT, COMGATE_SECRET, COMGATE_TEST @@ -342,6 +346,20 @@ def products(request): ]), content_type='application/json') +def cart_add(request): + _id = request.GET['id'] + user = request.user + if not user.cart: + user.cart = Cart(user=user) + user.cart.save() + cart_product = CartProduct( + product=Product.objects.get(id=_id), + cart=user.cart, + quantity=1, + ) + cart_product.save() + + def results(request): results = [] n = 1 diff --git a/frontend/src/scripts/index.js b/frontend/src/scripts/index.js index 0aca30f..cf12895 100644 --- a/frontend/src/scripts/index.js +++ b/frontend/src/scripts/index.js @@ -80,9 +80,20 @@ class Main extends Component { }) } addToCart = (id) =>{ - this.setState({ - cart: [...this.state.cart, id] - }); + fetch(addr_prefix + "/api/cart/add?id=" + id).then(resp => resp.json()).then(json=>{ + if(json.reason){ + this.setState({ + status_text: data.reason, + status: "failed", + }) + }else{ + this.setState({ + status_text: data.success, + status: "success", + cart: [...this.state.cart, id], + }); + } + }) } onSubmit(event){ event.preventDefault();