Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ We will focus on creating RESTful endpoints for CRUD operations.
- Take time to make sure you're on the same page

## Project Directions

### Part 1
1. [Wave 01: Setup and Read](./project-directions/wave_01.md)
1. [Wave 02: Read and 404s](./project-directions/wave_02.md)
Expand Down
28 changes: 27 additions & 1 deletion app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from dotenv import load_dotenv
import os

db = SQLAlchemy()
migrate = Migrate()

load_dotenv()

def create_app(test_config=None):
app = Flask(__name__)

return app
if not test_config:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get(
"SQLALCHEMY_DATABASE_URI")
else:
app.config["TESTING"] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_TEST_DATABASE_URI")

db.init_app(app)
migrate.init_app(app, db)

from app.models.planet import Planet

from .routes import planets_bp
app.register_blueprint(planets_bp)

return app
Empty file added app/models/__init__.py
Empty file.
18 changes: 18 additions & 0 deletions app/models/planet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from app import db

class Planet(db.Model):
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String)
description = db.Column(db.String)
num_moons = db.Column(db.Integer)


def to_dict(self):
planet_as_dict = {}
planet_as_dict["id"] = self.id
planet_as_dict["name"] = self.name
planet_as_dict["description"] = self.description
planet_as_dict["num_moons"] = self.num_moons

return planet_as_dict

85 changes: 84 additions & 1 deletion app/routes.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,85 @@
from flask import Blueprint
from app import db
from app.models.planet import Planet
from flask import Blueprint, jsonify, make_response, request, abort

planets_bp = Blueprint("planets", __name__, url_prefix="/planets")

@planets_bp.route("", methods=["GET"])
def read_planets():
planet_query = Planet.query

name_query = request.args.get("name")

if name_query:
planet_query = planet_query.filter_by(name=name_query)

moon_query = request.args.get("num_moons")

if moon_query:
planet_query = planet_query.filter_by(num_moons=moon_query)

planets_response = []
planets = planet_query.all()

for planet in planets:
planets_response.append(planet.to_dict())

return jsonify(planets_response)


@planets_bp.route("", methods=["POST"])
def create_planet():
request_body = request.get_json()
new_planet = Planet(name=request_body["name"],
description=request_body["description"],
num_moons=request_body["num_moons"])

db.session.add(new_planet)
db.session.commit()

return make_response(jsonify(f"Planet {new_planet.name} successfully created"), 201)


@planets_bp.route("/<planet_id>", methods=["GET"])
def get_one_planet(planet_id):
planet = validate_planet(planet_id)

return planet.to_dict()


def validate_planet(planet_id):
try:
planet_id = int(planet_id)
except:
abort(make_response({"message":f"planet {planet_id} invalid"}, 400))

planet = Planet.query.get(planet_id)

if not planet:
abort(make_response({"message":f"planet {planet_id} not found"}, 404))

return planet


@planets_bp.route("/<planet_id>", methods=["PUT"])
def update_planet(planet_id):
planet = validate_planet(planet_id)

request_body = request.get_json()

planet.name = request_body["name"]
planet.description = request_body["description"]
planet.num_moons = request_body["num_moons"]

db.session.commit()

return make_response(f"Planet #{planet.id} successfully updated!")

@planets_bp.route("/<planet_id>", methods=["DELETE"])
def delete_planet(planet_id):
planet = validate_planet(planet_id)

db.session.delete(planet)
db.session.commit()

return make_response(f"Planet #{planet.id} successfully deleted!")
219 changes: 219 additions & 0 deletions intro-to-sql-problemset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
--
-- PostgreSQL database dump
--

-- Dumped from database version 13.2
-- Dumped by pg_dump version 13.2

-- Started on 2021-03-13 20:00:58 PST

SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

SET default_tablespace = '';

SET default_table_access_method = heap;

-- Exists just to be deleted
CREATE TABLE public.books();
ALTER TABLE public.books OWNER TO postgres;

--
-- TOC entry 200 (class 1259 OID 16663)
-- Name: products; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.products (
id integer NOT NULL,
name character varying(32),
publisher_id integer,
description text
);


ALTER TABLE public.products OWNER TO postgres;

--
-- TOC entry 201 (class 1259 OID 16669)
-- Name: products_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

ALTER TABLE public.products ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.products_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);


--
-- TOC entry 202 (class 1259 OID 16671)
-- Name: publishers; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.publishers (
id integer NOT NULL,
name character varying(32),
address character varying(64)
);


ALTER TABLE public.publishers OWNER TO postgres;

--
-- TOC entry 203 (class 1259 OID 16674)
-- Name: publishers_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

ALTER TABLE public.publishers ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.publishers_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);


--
-- TOC entry 204 (class 1259 OID 16676)
-- Name: users; Type: TABLE; Schema: public; Owner: postgres
--

CREATE TABLE public.users (
id integer NOT NULL,
first_name character varying(32),
last_name character varying(32),
email character varying(32)
);


ALTER TABLE public.users OWNER TO postgres;

--
-- TOC entry 205 (class 1259 OID 16679)
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--

ALTER TABLE public.users ALTER COLUMN id ADD GENERATED ALWAYS AS IDENTITY (
SEQUENCE NAME public.users_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);


--
-- TOC entry 3262 (class 0 OID 16663)
-- Dependencies: 200
-- Data for Name: products; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.products (id, name, publisher_id, description) FROM stdin;
1 Nimona 1 Wonderful comic by Noelle Stevenson
2 Watchmen 2 Dark, very dark
3 Maus: A Survivors Tale 1 Two volume historical memoir.
4 Daytripper 3 Death retroactively imposes a shape on a person’s life.
5 This One Summer 103 Written by Mariko Tamaki and illustrated by Jillian Tamaki.
6 Sweet Tooth 103 Interesting tale.
7 Through The Woods 99 It came from the woods. Most strange things do.
8 Blankets 3 Semiautobiographical story of a young man raised in a strict evangelical tradition.
\.

--
-- TOC entry 3264 (class 0 OID 16671)
-- Dependencies: 202
-- Data for Name: publishers; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.publishers (id, name, address) FROM stdin;
1 Blue Gem Publishers 123 Elm Street Tuscon AZ 12837
2 Ruby Comics P.O. Box Portland, OR 96545
3 Developers Academy 315 5th Ave S Suite 200, Seattle, WA 98104
4 Angry Elf Ltd 111 East Down Street London England 11111
5 Shueishan Comics P.O. Box 11231 New York, NY 01754
\.


--
-- TOC entry 3266 (class 0 OID 16676)
-- Dependencies: 204
-- Data for Name: users; Type: TABLE DATA; Schema: public; Owner: postgres
--

COPY public.users (id, first_name, last_name, email) FROM stdin;
2 Sujay Rosemary [email protected]
3 Vittorio Oliver [email protected]
4 Sofija Maraĵa [email protected]
5 Bendik Genoveva [email protected]
6 Auster Alexandra [email protected]
7 Deepali Efrem [email protected]
8 Coade OMoore [email protected]
\.


--
-- TOC entry 3273 (class 0 OID 0)
-- Dependencies: 201
-- Name: products_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.products_id_seq', 10, true);


--
-- TOC entry 3274 (class 0 OID 0)
-- Dependencies: 203
-- Name: publishers_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.publishers_id_seq', 5, true);


--
-- TOC entry 3275 (class 0 OID 0)
-- Dependencies: 205
-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--

SELECT pg_catalog.setval('public.users_id_seq', 8, true);

ALTER TABLE ONLY public.products
ADD CONSTRAINT products_pkey PRIMARY KEY (id);


--
-- TOC entry 3129 (class 2606 OID 16682)
-- Name: publishers publishers_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.publishers
ADD CONSTRAINT publishers_pkey PRIMARY KEY (id);


--
-- TOC entry 3131 (class 2606 OID 16684)
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--

ALTER TABLE ONLY public.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);


-- Completed on 2021-03-13 20:00:58 PST

--
-- PostgreSQL database dump complete
--
1 change: 1 addition & 0 deletions migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
Loading