Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#161340662 add travis CI #10

Open
wants to merge 36 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
eb9905f
[Chore #161335865] Adding the Directory structure
MaggieKimani1 Oct 19, 2018
dab6a8f
[Chore #161335865] Adding the Directory structure
MaggieKimani1 Oct 19, 2018
5950c6e
[Chore #161335865] Updating the .gitignore file
MaggieKimani1 Oct 19, 2018
d63b8bd
[Chore #161331176] Adding the tests
MaggieKimani1 Oct 19, 2018
242048f
[Feature #161330191] An admin can add a product
MaggieKimani1 Oct 19, 2018
4223c3c
[Feature #161330897] Store attendant can add a sale record
MaggieKimani1 Oct 19, 2018
8421aa6
[Feature #161331125] admin can get all sale records
MaggieKimani1 Oct 19, 2018
75bb292
[Feature #161340517] attendant can create new sale
MaggieKimani1 Oct 19, 2018
b9e4960
[Chore #161340662] adding travis integration
MaggieKimani1 Oct 19, 2018
f3d8f1c
[Chore #161340662] update travis integration
MaggieKimani1 Oct 19, 2018
dc99909
[Chore #161340662] update travis integration
MaggieKimani1 Oct 19, 2018
dc3cd69
[Chore #161340662] update travis integration
MaggieKimani1 Oct 19, 2018
89afecc
[Chore #161340662] update travis integration
MaggieKimani1 Oct 19, 2018
83fad46
Update README.md
MaggieKimani1 Oct 19, 2018
3870cf6
Update README.md
MaggieKimani1 Oct 19, 2018
1b91f81
Update README.md
MaggieKimani1 Oct 22, 2018
ae4c12e
Update README.md
MaggieKimani1 Oct 22, 2018
dce47b5
Update README.md
MaggieKimani1 Oct 23, 2018
e58e135
Update README.md
MaggieKimani1 Oct 23, 2018
ef6a009
[Feature #161340662] refactored code
dev-jey Oct 23, 2018
862d46a
Merge branch 'ch-add-travis-161340662' of https://github.com/MaggieKi…
dev-jey Oct 23, 2018
4a6906b
Updated procfile and readme.cmd
MaggieKimani1 Oct 24, 2018
18d97f9
Updated requirements.txt
MaggieKimani1 Oct 24, 2018
1c9a53d
Renamed gitignore and modified contents
MaggieKimani1 Oct 24, 2018
c69c5b4
Updated travis.yml
MaggieKimani1 Oct 24, 2018
97e3704
Updated travis.yml
MaggieKimani1 Oct 24, 2018
8c89423
Updated travis.yml and README.md
MaggieKimani1 Oct 24, 2018
c7f137e
Updated README.md
MaggieKimani1 Oct 24, 2018
fd6341b
Updated README.md
MaggieKimani1 Oct 24, 2018
3d7470c
Updated README.md
MaggieKimani1 Oct 24, 2018
2c50b8a
Updated README.md
MaggieKimani1 Oct 24, 2018
c126592
Updated travis.yml
MaggieKimani1 Oct 24, 2018
d2fe83e
Updated travis.yml
MaggieKimani1 Oct 24, 2018
a7f5149
Updated travis.yml
MaggieKimani1 Oct 24, 2018
f66b4f5
Updated travis.yml
MaggieKimani1 Oct 24, 2018
9ecc9e9
Refactored code
MaggieKimani1 Oct 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
1 change: 1 addition & 0 deletions .coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines":{"C:\\Users\\Maggi.DESKTOP-90M23NT\\OneDrive\\Desktop\\ADC2\\STORE-MANAGER-API\\app\\api\\v1\\views.py":[1,2,3,6,7,9,10,11,35,39,40,45,47,60,63,65,12,13,14,15,16,17,19,20,22,24,26,28,30,33,36,41,42,43,48,49,50,51,52,54,56,57,61,66,67]}}
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source venv/scripts/activate
export FLASK_APP="run.py"
export FLASK_ENV="development"
export APP_SETTINGS="development"


11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
!.codeclimate.yml
!.coveragerc
!.gitignore
!.pylintrc
!.travis*
!.env
*.log
*.pyc

__pycache__/
venv/*
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: python
python:
- "3.6"
services:
- postgresql
install:
- pip install -r requirements.txt
- pip install coveralls
script:
- pytest
- coverage run --source=app.api.v1.views -m pytest && coverage report
after_success:
- coveralls
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "venv\\Scripts\\python.exe"
}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn run:app
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# STORE-MANAGER-API
Api endpoints for inventory management in store manager

[![Build Status](https://travis-ci.com/MaggieKimani1/STORE-MANAGER-API.svg?branch=ch-add-travis-161340662)](https://travis-ci.com/MaggieKimani1/STORE-MANAGER-API)
[![Coverage Status](https://coveralls.io/repos/github/MaggieKimani1/STORE-MANAGER-API/badge.svg?branch=ch-add-travis-161340662)](https://coveralls.io/github/MaggieKimani1/STORE-MANAGER-API?branch=ch-add-travis-161340662)
[![Maintainability](https://api.codeclimate.com/v1/badges/a99a88d28ad37a79dbf6/maintainability)](https://codeclimate.com/github/codeclimate/codeclimate/maintainability)

# STORE-MANAGER-API
- Admin can add a product
- Admin/store attendant can get all products
- Admin/store attendant can get a specific product
- Store attendant can add a sale order
- Admin can get all sale order records

### Endpoints

| HTTP Method | Endpoint | Function |
| ------------- |:--------------------------: | -----------------------:|
| GET | /api/v1/products | Get all products |
| GET | /api/v1/products/product_id | Get one product |
| GET | /api/v1/sales | Get all sales |
| GET | /api/v1/products/sale_id | Get one sale record |
| POST | /api/v1/products | Create a new sale |
| POST | /api/v1/sales | Create a new product |

### Pivotal Tracker Board
https://www.pivotaltracker.com/n/projects/2204195

### Heroku Link
https://maggie1.herokuapp.com/

### Prerequisites
- pip
- virtualenv
- python 3 or python 2.7

## Setting Up Locally
- Clone the repo
- git clone https://github.com/MaggieKimani1/STORE-MANAGER-API
- create a virtual environment and activate it
- virtualenv venv
- install dependencies
- pip install -r requirements.txt
Empty file added __init__.py
Empty file.
Binary file added __pycache__/__init__.cpython-37.pyc
Binary file not shown.
17 changes: 17 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask import Flask, Blueprint
from flask_restful import Api, Resource
from instance.config import app_config



def create_app(config_name):
app = Flask(__name__, instance_relative_config=True)
'''loads the right configurations from config.py given a config name'''

app.config.from_object(app_config[config_name])

from .api.v1 import api_v1 as v1
app.register_blueprint(v1)

return app

Binary file added app/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Empty file added app/api/__init__.py
Empty file.
Binary file added app/api/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions app/api/v1/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from flask import Blueprint
from flask_restful import Api, Resource
from .views import All_Products_Endpoint, One_Product_Endpoint, All_Sales_Endpoint,One_Sale_Endpoint


api_v1 = Blueprint('api_v1',__name__,url_prefix='/api/v1')
api = Api(api_v1)

api.add_resource(All_Products_Endpoint,'/products')
api.add_resource(One_Product_Endpoint,'/products/<int:product_id>')
api.add_resource(All_Sales_Endpoint,'/products/<int:product_id>/sales')
api.add_resource(One_Sale_Endpoint,'/sales/<int:sale_id>')


Binary file added app/api/v1/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file added app/api/v1/__pycache__/models.cpython-37.pyc
Binary file not shown.
Binary file added app/api/v1/__pycache__/views.cpython-37.pyc
Binary file not shown.
103 changes: 103 additions & 0 deletions app/api/v1/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
all_Products = []
all_Sales = []

All_Users= {}
user_details={}


class Products(object):
all_Products = []

def __init__(self, product_name , price, quantity, category):
'''constructor method that is called when an object is created'''
self.product_name = product_name
self.product_id = len(Products.all_Products) + 1
self.price = price
self.quantity= quantity
self.category = category

def create_new_product(self,product_name,product_price, quantity, category):
for product in all_Products:
if self.product_name == product["product_name"]:
return "message The product you entered already exists"

product_Description = {}
product_Description["product_id"] = self.product_id
product_Description["product_name"] = product_name
product_Description["price"] = product_price
product_Description["quantity"] = quantity
product_Description["category"] = category

Products.all_Products.append(product_Description)
return Products.all_Products
@classmethod
def get_all_Products(cls):
return cls.all_Products



class Sales(object):
all_Sales = []

def __init__(self,product_name, product_price):
self.product_name = product_name
self.product_price= product_price
self.sale_id = len(Sales.all_Sales) + 1

def create_new_sale_record(self, product_name, product_price, product_id):
new_Sale = {}
new_Sale["product_id"] = product_id
new_Sale["product_name"] = product_name
new_Sale["price"] = product_price
new_Sale["sale_id"] = self.sale_id

Sales.all_Sales.append(new_Sale)
return Sales.all_Sales

# for prod in all_Products:
# if self.product_id == prod["product_id"]:

# new_Sale={
# self.sale_id: prod
# }

# all_Sales.append(new_Sale)
# return "success"

def get_all_Sales(self):
return all_Sales

def get_one_sale(self, sale_id):
for sale in all_Sales:
if sale_id == sale["id"]:
return sale
return "message The sale doesn't exist"

# class Users(object):
# '''class for handling user data'''

# def __init__(self,username, email, password):
# self.username = username
# self.email = email
# self.password = password

# def register(self):
# if email in All_Users:
# return "message already exists"
# else:

# user_details["username"] = self.username
# user_details["email"] = self.email
# user_details["password"] = self.password

# All_Users.append(user_details)
# return "message successfully registered"

# def get_one_user(self):
# for user in All_Users:
# if email in All_Users:
# return All_Users[email]
# return "user not found"

# def get_all_users(self):
# return All_Users
106 changes: 106 additions & 0 deletions app/api/v1/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from flask import Flask, Blueprint, request, jsonify, make_response
from flask_restful import Api, Resource
from .models import all_Products, all_Sales, Products, Sales


app = Flask(__name__)
api = Api(app)

class All_Products_Endpoint(Resource):

def post(self):
'''Creating a new product'''
try:

data = request.get_json()
# product_id = len(all_Products) + 1
product_name = data["product_name"]
product_price = data["price"]
quantity = data["quantity"]
category = data["category"]



product = Products(product_name,product_price, quantity, category)
products=product.create_new_product(product_name,product_price, quantity, category)

if product_name == "" or not product_name:
return {"message":"please enter the product_name"},400
if quantity == "" or not quantity:
return {"message":"Please specify a quantity"},400
if product_price == "" or not product_price:
return {"message":"please enter the price"},400
if category == "" or not category:
return{"message":"please enter the category"},400
if quantity == 0:
return{"message":"invalid quantity"},400

# all_Products.append()
return make_response(jsonify({"message":"success","products":products}), 201)



except:
return make_response(jsonify({"message":"include all details"}), 400)






def get(self):
products=Products.get_all_Products()
'''Fetching a specific product'''
return make_response(jsonify({"message":"success","products":products}), 200)


class One_Product_Endpoint(Resource):

def get(self, product_id):
'''Fetching a product'''
products=Products.get_all_Products()
for product in products:
if product["product_id"] == product_id:
return product
return make_response(jsonify({"message":"no product found"}), 404)

class All_Sales_Endpoint(Resource):

def post(self, product_id):
'''Creating a new sale'''

data = request.get_json() #retrieving the data using the request in json format
product_name = data["product_name"]
product_price = data["price"]



products=Products.get_all_Products()
for prod in products:
if product_id == prod["product_id"]:
# new_Sale={
# "sale_id": prod
# }
# all_Sales.append(new_Sale)
sale = Sales(product_name,product_price)
sales=sale.create_new_sale_record(product_name,product_price,product_id)

return make_response(jsonify({"message":"successfully sold" ,"sales": sales}), 201)
return make_response(jsonify({"message":"No product to sell"}), 404)

def get(self):
'''Fetching all sale records'''

return make_response(jsonify({"message":"success","sales":all_Sales}), 200)

class One_Sale_Endpoint(Resource):

def get(self, sale_id):
'''Fetching a sale record'''

for sale in all_Sales:
if sale["sale_id"] == sale_id:
return sale



Empty file added app/tests/__init__.py
Empty file.
Binary file added app/tests/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
37 changes: 37 additions & 0 deletions app/tests/test_products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import unittest
import json
from app import create_app
import sys


class ProductsTestCase(unittest.TestCase):
#this class represents the products testcase for the client methods

def setUp(self):
#Define test variables and initialize app.
self.app = create_app(config_name="testing")
self.client= create_app('testing').test_client()


#test if user can get all products by using get method
def test_get_all_Products(self):
response=self.client.get('api/v1/products',content_type="application/json")
self.assertEqual(response.status_code,200)

def test_create_new_product(self):
data = {"product_name":"nivea","price":5000,"quantity":40, "category":"body lotion"}
response = self.client.post("/api/v1/products",data = json.dumps(data),content_type="application/json")
response_data = json.loads(response.data.decode())
self.assertEqual(response_data["message"], "success")
self.assertEqual(response.status_code,201)

def test_get_one_product(self):
response=self.client.get('api/v1/products/1',content_type="application/json")
response_data = json.loads(response.data.decode())
self.assertEqual(response_data["product_id"], 1)
self.assertEqual(response.status_code,200)



if __name__ == '__main__':
unittest.main()
Loading