Skip to content

Commit c3fc51e

Browse files
committed
setup rest apt backend and config frontend with axios
1 parent 49c8fe1 commit c3fc51e

File tree

12 files changed

+148
-12
lines changed

12 files changed

+148
-12
lines changed

README.md

Whitespace-only changes.

backend/Backend/settings.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@
1717
'django.contrib.messages',
1818
'django.contrib.staticfiles',
1919

20+
'rest_framework',
21+
'corsheaders',
22+
2023
'core.apps.CoreConfig'
2124
]
2225

2326
MIDDLEWARE = [
27+
'corsheaders.middleware.CorsMiddleware',
28+
29+
2430
'django.middleware.security.SecurityMiddleware',
2531
'django.contrib.sessions.middleware.SessionMiddleware',
2632
'django.middleware.common.CommonMiddleware',
@@ -90,3 +96,5 @@
9096
USE_TZ = True
9197

9298
STATIC_URL = '/static/'
99+
100+
CORS_ALLOW_ALL_ORIGINS = True

backend/Backend/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from django.contrib import admin
2-
from django.urls import path
2+
from django.urls import path, include
33

44
urlpatterns = [
55
path('admin/', admin.site.urls),
6+
path('api/', include('core.urls'))
67
]

backend/Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ name = "pypi"
66
[packages]
77
django = "*"
88
djangorestframework = "*"
9+
django-cors-headers = "*"
910

1011
[dev-packages]
1112
django-linter = "*"

backend/Pipfile.lock

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/core/product.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
PRODUCT = [
2+
{
3+
"_id": "1",
4+
"name": "Airpods Wireless Bluetooth Headphones",
5+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/airpods.jpg",
6+
"description": "Bluetooth technology lets you connect it with compatible devices wirelessly High-quality AAC audio offers immersive listening experience Built-in microphone allows you to take calls while working",
7+
"brand": "Apple",
8+
"category": "Electronics",
9+
"price": 89.99,
10+
"countInStock": 10,
11+
"rating": 4.5,
12+
"numReviews": 12
13+
},
14+
{
15+
"_id": "2",
16+
"name": "iPhone 11 Pro 256GB Memory",
17+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/phone.jpg",
18+
"description": "Introducing the iPhone 11 Pro. A transformative triple-camera system that adds tons of capability without complexity. An unprecedented leap in battery life",
19+
"brand": "Apple",
20+
"category": "Electronics",
21+
"price": 599.99,
22+
"countInStock": 7,
23+
"rating": 4.0,
24+
"numReviews": 8
25+
},
26+
{
27+
"_id": "3",
28+
"name": "Cannon EOS 80D DSLR Camera",
29+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/camera.jpg",
30+
"description": "Characterized by versatile imaging specs, the Canon EOS 80D further clarifies itself using a pair of robust focusing systems and an intuitive design",
31+
"brand": "Cannon",
32+
"category": "Electronics",
33+
"price": 929.99,
34+
"countInStock": 5,
35+
"rating": 3,
36+
"numReviews": 12
37+
},
38+
{
39+
"_id": "4",
40+
"name": "Sony Playstation 4 Pro White Version",
41+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/playstation.jpg",
42+
"description": "The ultimate home entertainment center starts with PlayStation. Whether you are into gaming, HD movies, television, music",
43+
"brand": "Sony",
44+
"category": "Electronics",
45+
"price": 399.99,
46+
"countInStock": 11,
47+
"rating": 5,
48+
"numReviews": 12
49+
},
50+
{
51+
"_id": "5",
52+
"name": "Logitech G-Series Gaming Mouse",
53+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/mouse.jpg",
54+
"description": "Get a better handle on your games with this Logitech LIGHTSYNC gaming mouse. The six programmable buttons allow customization for a smooth playing experience",
55+
"brand": "Logitech",
56+
"category": "Electronics",
57+
"price": 49.99,
58+
"countInStock": 7,
59+
"rating": 3.5,
60+
"numReviews": 10
61+
},
62+
{
63+
"_id": "6",
64+
"name": "Amazon Echo Dot 3rd Generation",
65+
"image": "https://github.com/divanov11/proshop_django/raw/master/static/images/alexa.jpg",
66+
"description": "Meet Echo Dot - Our most popular smart speaker with a fabric design. It is our most compact smart speaker that fits perfectly into small space",
67+
"brand": "Amazon",
68+
"category": "Electronics",
69+
"price": 29.99,
70+
"countInStock": 0,
71+
"rating": 4,
72+
"numReviews": 12
73+
}
74+
]

backend/core/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from django.urls import path
2+
from core.views import getProducts, getProduct
23

34
APP_NAME = 'core'
45

56
urlpatterns = [
6-
path(),
7+
path('products/', getProducts, name="get-products"),
8+
path('product/<int:id>/', getProduct, name="get-product"),
79
]

backend/core/views.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
from django.shortcuts import render
1+
from rest_framework.decorators import api_view
2+
from rest_framework.response import Response
23

3-
# Create your views here.
4+
from .product import PRODUCT
5+
6+
7+
@api_view(['GET'])
8+
def getProducts(request):
9+
products = PRODUCT
10+
return Response(products)
11+
12+
13+
@api_view(['GET'])
14+
def getProduct(request, id):
15+
product = PRODUCT[id-1]
16+
return Response(product)

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@testing-library/jest-dom": "^5.11.4",
1111
"@testing-library/react": "^11.1.0",
1212
"@testing-library/user-event": "^12.1.10",
13+
"axios": "^0.21.1",
1314
"bootstrap": "^4.6.0",
1415
"react": "^17.0.2",
1516
"react-bootstrap": "^1.5.2",

frontend/src/Pages/HomePage.jsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import React from "react";
1+
import React, { useState, useEffect } from "react";
22
import { Row, Col } from "react-bootstrap";
3-
import products from "../fakedata/products.json";
43
import Product from "../components/Product";
4+
import axios from "axios";
55

66
function HomePage() {
7+
const [products, setProducts] = useState([]);
8+
useEffect(() => {
9+
async function fetchProducts() {
10+
const { data } = await axios.get("http://localhost:8000/api/products/");
11+
setProducts(data);
12+
}
13+
fetchProducts();
14+
}, []);
715
return (
816
<div>
917
<h1>Latest Product</h1>

frontend/src/Pages/ProductPage.jsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useState, useEffect } from "react";
22
import { LinkContainer } from "react-router-bootstrap";
33
import {
44
Container,
@@ -10,12 +10,20 @@ import {
1010
Card,
1111
Badge,
1212
} from "react-bootstrap";
13-
import products from "../fakedata/products.json";
1413
import Rating from "../components/Rating";
14+
import axios from "axios";
1515

1616
function ProductPage(props) {
17-
const product = products.find((p) => p._id === props.match.params.id);
18-
17+
const [product, setProduct] = useState({});
18+
useEffect(() => {
19+
async function fetchProduct() {
20+
const { data } = await axios.get(
21+
`http://localhost:8000/api/product/${props.match.params.id}`
22+
);
23+
setProduct(data);
24+
}
25+
fetchProduct();
26+
});
1927
return (
2028
<Container>
2129
<LinkContainer to="/">
@@ -55,7 +63,7 @@ function ProductPage(props) {
5563
<Row>
5664
<Col>Price: </Col>
5765
<Col>
58-
<strong>${product.price}</strong>
66+
<strong>Rs. {product.price}</strong>
5967
</Col>
6068
</Row>
6169
</ListGroup.Item>

frontend/yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2585,6 +2585,13 @@ axe-core@^4.0.2:
25852585
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.1.2.tgz#7cf783331320098bfbef620df3b3c770147bc224"
25862586
integrity sha512-V+Nq70NxKhYt89ArVcaNL9FDryB3vQOd+BFXZIfO3RP6rwtj+2yqqqdHEkacutglPaZLkJeuXKCjCJDMGPtPqg==
25872587

2588+
axios@^0.21.1:
2589+
version "0.21.1"
2590+
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
2591+
integrity sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==
2592+
dependencies:
2593+
follow-redirects "^1.10.0"
2594+
25882595
axobject-query@^2.2.0:
25892596
version "2.2.0"
25902597
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
@@ -5105,6 +5112,11 @@ follow-redirects@^1.0.0:
51055112
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.2.tgz#dd73c8effc12728ba5cf4259d760ea5fb83e3147"
51065113
integrity sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA==
51075114

5115+
follow-redirects@^1.10.0:
5116+
version "1.13.3"
5117+
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.3.tgz#e5598ad50174c1bc4e872301e82ac2cd97f90267"
5118+
integrity sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==
5119+
51085120
for-in@^1.0.2:
51095121
version "1.0.2"
51105122
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"

0 commit comments

Comments
 (0)