Skip to content

Commit 9202154

Browse files
committed
update initial
1 parent 2a64f2e commit 9202154

File tree

15 files changed

+541
-50
lines changed

15 files changed

+541
-50
lines changed

.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

README.md

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,79 @@
11
# capstone
2-
2+
3+
# Server
4+
5+
## Getting started
6+
7+
First you'll need to get the source of the project. Do this by cloning the whole JSPrep repository:
8+
9+
`git clone [email protected]:olga-f/capstone.git`
10+
11+
`cd server`
12+
13+
#### Create a virtual environment in which we can install the dependencies
14+
15+
`python -m venv env`
16+
17+
`source env/Scripts/activate`
18+
19+
#### Now we can install our dependencies:
20+
21+
`pip install -r requirements.txt`
22+
23+
#### Setup a mongodb connection and create a database.
24+
25+
> 🔹 [MongoDB Atlas] (https://www.mongodb.com/) offers Free Shared Clusters.
26+
27+
1. Add `.env` file with your mongodb credentials to the `server` folder.
28+
29+
```
30+
SECRET_KEY=gozaXXXXXXXXXXXXXXXXXXXXXXXX
31+
DEBUG=True
32+
_MONGODB_URI=mongodb+srv://XXXXXXX:[email protected]/test?retryWrites=true&w=majority
33+
34+
```
35+
36+
2. Run the following command:
37+
38+
`python manage.py migrate`
39+
40+
3. Start the server:
41+
42+
`python manage.py runserver`
43+
44+
Now head on over to http://127.0.0.1:8000/graphql and run some queries!
45+
46+
For example:
47+
48+
```
49+
mutation($input: BikeInput!) {
50+
createBike(bikeData: $input) {
51+
bike {
52+
name
53+
brand
54+
year
55+
size
56+
wheelSize
57+
type
58+
}
59+
}
60+
}
61+
62+
```
63+
query variables:
64+
```
65+
{
66+
"input": {
67+
"name": "Greg",
68+
"brand": "Variables",
69+
"year": "2021",
70+
"size": "XX",
71+
"wheelSize": 2.0,
72+
"type": "smalltype"
73+
}
74+
}
75+
```
76+
77+
For tests run:
78+
79+
`pytest -v`

server/db.sqlite3

132 KB
Binary file not shown.

server/jsprep/apps.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BikeConfig(AppConfig):
5+
name = "bike"

server/jsprep/fixtures.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import pytest
2+
from .models import Bike, Shop
3+
4+
5+
def fixture_bike_data():
6+
Bike.drop_collection()
7+
bike_one = Bike(
8+
name="Level R",
9+
brand="Mondraker",
10+
year="2020",
11+
size=["S", "M", "L", "XL"],
12+
wheel_size=27.5,
13+
type="MTB",
14+
)
15+
bike_one.save()
16+
17+
bike_two = Bike(
18+
name="CAADX ULTEGRA",
19+
brand="Cannondale",
20+
year="2019",
21+
size=["46", "51", "54", "58"],
22+
wheel_size=28,
23+
type="Gravel",
24+
)
25+
bike_two.save()
26+
27+
bike_three = Bike(
28+
id="507f1f77bcf86cd799439011",
29+
name="Moterra Neo",
30+
brand="Cannondale",
31+
year="2019",
32+
size=["M", "L", "XL"],
33+
wheel_size=29,
34+
type="EBike",
35+
)
36+
bike_three.save()
37+
38+
39+
def fixture_shop_data():
40+
Shop.drop_collection()
41+
shop_one = Shop(
42+
name="Big Wheel Bicycles",
43+
address="2438 Hart Ridge Road",
44+
website="https://www.bigwheelbike.test",
45+
)
46+
shop_one.save()
47+
shop_two = Shop(
48+
name="Bike Tech",
49+
address="2175 Pearl Street",
50+
website="https://www.biketech.test",
51+
)
52+
shop_two.save()
53+
54+
55+
@pytest.fixture(scope="module")
56+
def fixtures_data():
57+
fixture_bike_data()
58+
fixture_shop_data()
59+
60+
return True
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by Django 3.1.7 on 2021-03-17 09:34
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('jsprep', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.DeleteModel(
14+
name='UserModel',
15+
),
16+
]

server/jsprep/models.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
1-
from django.db import models
1+
from mongoengine import Document
2+
from mongoengine.fields import (
3+
FloatField,
4+
StringField,
5+
ListField,
6+
URLField,
7+
ObjectIdField,
8+
)
29

310

4-
class UserModel(models.Model):
5-
first_name = models.CharField(max_length=100)
6-
last_name = models.CharField(max_length=100)
11+
class Shop(Document):
12+
meta = {"collection": "shop"}
13+
ID = ObjectIdField()
14+
name = StringField()
15+
address = StringField()
16+
website = URLField()
717

818

19+
class Bike(Document):
20+
meta = {"collection": "bike"}
21+
ID = ObjectIdField()
22+
name = StringField()
23+
brand = StringField()
24+
year = StringField()
25+
size = ListField(StringField())
26+
wheel_size = FloatField()
27+
type = StringField()

server/jsprep/mutations.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import graphene
2+
from django.core.exceptions import ObjectDoesNotExist
3+
from .models import Bike
4+
from .types import BikeType
5+
6+
7+
class BikeInput(graphene.InputObjectType):
8+
id = graphene.ID()
9+
name = graphene.String()
10+
brand = graphene.String()
11+
year = graphene.String()
12+
size = graphene.List(graphene.String)
13+
wheel_size = graphene.Float()
14+
type = graphene.String()
15+
16+
17+
class CreateBikeMutation(graphene.Mutation):
18+
bike = graphene.Field(BikeType)
19+
20+
class Arguments:
21+
bike_data = BikeInput(required=True)
22+
23+
def mutate(self, info, bike_data=None):
24+
bike = Bike(
25+
name=bike_data.name,
26+
brand=bike_data.brand,
27+
year=bike_data.year,
28+
size=bike_data.size,
29+
wheel_size=bike_data.wheel_size,
30+
type=bike_data.type,
31+
)
32+
bike.save()
33+
34+
return CreateBikeMutation(bike=bike)
35+
36+
37+
class UpdateBikeMutation(graphene.Mutation):
38+
bike = graphene.Field(BikeType)
39+
40+
class Arguments:
41+
bike_data = BikeInput(required=True)
42+
43+
@staticmethod
44+
def get_object(id):
45+
return Bike.objects.get(pk=id)
46+
47+
def mutate(self, info, bike_data=None):
48+
bike = UpdateBikeMutation.get_object(bike_data.id)
49+
if bike_data.name:
50+
bike.name = bike_data.name
51+
if bike_data.brand:
52+
bike.brand = bike_data.brand
53+
if bike_data.year:
54+
bike.year = bike_data.year
55+
if bike_data.size:
56+
bike.size = bike_data.size
57+
if bike_data.wheel_size:
58+
bike.wheel_size = bike_data.wheel_size
59+
if bike_data.type:
60+
bike.type = bike_data.type
61+
62+
bike.save()
63+
64+
return UpdateBikeMutation(bike=bike)
65+
66+
67+
class DeleteBikeMutation(graphene.Mutation):
68+
class Arguments:
69+
id = graphene.ID(required=True)
70+
71+
success = graphene.Boolean()
72+
73+
def mutate(self, info, id):
74+
try:
75+
Bike.objects.get(pk=id).delete()
76+
success = True
77+
except ObjectDoesNotExist:
78+
success = False
79+
80+
return DeleteBikeMutation(success=success)

server/jsprep/schema.py

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
11
import graphene
2-
from graphene_django import DjangoObjectType
2+
from graphene.relay import Node
3+
from graphene_mongo.fields import MongoengineConnectionField
4+
from .models import Shop
5+
from .types import BikeType, ShopType
6+
from .mutations import CreateBikeMutation, UpdateBikeMutation, DeleteBikeMutation
37

4-
from .models import UserModel
58

6-
class UserType(DjangoObjectType):
7-
class Meta:
8-
model = UserModel
9+
class Mutations(graphene.ObjectType):
10+
create_bike = CreateBikeMutation.Field()
11+
update_bike = UpdateBikeMutation.Field()
12+
delete_bike = DeleteBikeMutation.Field()
913

10-
class Query(graphene.ObjectType):
11-
users = graphene.List(UserType)
12-
13-
def resolve_users(self, info):
14-
return UserModel.objects.all()
15-
16-
17-
class CreateUser(graphene.Mutation):
18-
# id = graphene.Int()
19-
first_name = graphene.String()
20-
last_name = graphene.String()
2114

22-
class Arguments:
23-
first_name = graphene.String()
24-
last_name = graphene.String()
25-
26-
def mutate(self, info, first_name, last_name):
27-
user = UserModel(first_name=first_name, last_name=last_name)
28-
user.save()
29-
30-
return CreateUser(
31-
# id=user._id,
32-
first_name=user.first_name,
33-
last_name=user.last_name,
34-
)
15+
class Query(graphene.ObjectType):
16+
node = Node.Field()
17+
bikes = MongoengineConnectionField(BikeType)
18+
shop_list = graphene.List(ShopType)
3519

20+
def resolve_shop_list(self, info):
21+
return Shop.objects.all()
3622

37-
class Mutation(graphene.ObjectType):
38-
create_user = CreateUser.Field()
3923

40-
schema = graphene.Schema(query=Query, mutation=Mutation)
24+
schema = graphene.Schema(query=Query, mutation=Mutations, types=[BikeType, ShopType])

server/jsprep/settings.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from decouple import config
33
from pathlib import Path
4+
from mongoengine import connect
45

56
# Build paths inside the project like this: BASE_DIR / 'subdir'.
67
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -55,18 +56,13 @@
5556
WSGI_APPLICATION = 'jsprep.wsgi.application'
5657

5758

58-
# Mongo Database
59+
# # Database
60+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
5961

6062
DATABASES = {
61-
'default': {
62-
'ENGINE': 'djongo',
63-
"CLIENT": {
64-
"name": config('_MONGODB_NAME'),
65-
"host":config('_MONGODB_HOST'), # url to connect to your MongoDB starts with 'mongodb+srv://...'
66-
"username": config('_MONGODB_USER'),
67-
"password": config('_MONGODB_PASSWD'),
68-
"authMechanism": "SCRAM-SHA-1",
69-
},
63+
"default": {
64+
"ENGINE": "django.db.backends.sqlite3",
65+
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
7066
}
7167
}
7268

@@ -108,6 +104,9 @@
108104
# https://docs.djangoproject.com/en/3.1/howto/static-files/
109105

110106
STATIC_URL = '/static/'
107+
108+
connect(host=config('_MONGODB_URI'))
109+
111110
GRAPHENE = {
112111
'SCHEMA': 'jsprep.schema.schema'
113112
}

0 commit comments

Comments
 (0)