Skip to content

Commit 6477524

Browse files
committed
base
0 parents  commit 6477524

32 files changed

+8760
-0
lines changed

.gitignore

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# TypeScript v1 declaration files
45+
typings/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
78+
# Next.js build output
79+
.next
80+
81+
# Nuxt.js build / generate output
82+
.nuxt
83+
dist
84+
85+
# Gatsby files
86+
.cache/
87+
# Comment in the public line in if your project uses Gatsby and *not* Next.js
88+
# https://nextjs.org/blog/next-9-1#public-directory-support
89+
# public
90+
91+
# vuepress build output
92+
.vuepress/dist
93+
94+
# Serverless directories
95+
.serverless/
96+
97+
# FuseBox cache
98+
.fusebox/
99+
100+
# DynamoDB Local files
101+
.dynamodb/
102+
103+
# TernJS port file
104+
.tern-port

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 The-Potluck-Planner
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Potluck Planner API
2+
3+
Hosted Backend URL:
4+
5+
Endpoints with an (auth) require an authorization token like so:
6+
7+
Headers:
8+
| Key | Value |
9+
| :-- | :-- |
10+
| Authorization | <AUTH_TOKEN> |
11+
12+
## Authentication
13+
14+
| Method | Endpoint | Requirements | |
15+
| ------ | -------------- | ------------------------ | -------------------------------- |
16+
| POST | /auth/register | name, username, password | Creates a new user object |
17+
| POST | /auth/login | username, password | Logs in users who already exists |
18+
19+
## Users
20+
21+
| Method | Endpoint | Requirements | |
22+
| ------ | -------------- | --------------------------------------- | ------------------------- |
23+
| GET | /api/users | | Returns all users |
24+
| GET | /api/users/:id | | Returns user object by id |
25+
| PUT | /api/users/:id | either name, username, password, or all | Updates the user by id |
26+
| DELETE | /api/users/:id | | Deletes the user by id |
27+
28+
## Events
29+
30+
| Method | Endpoint | Requirements | |
31+
| ------ | ---------------------- | ------------------------------------------------------- | --------------------------------------------------------------- |
32+
| GET | /api/events | | Returns all events |
33+
| GET | /api/events/users/:id | | Returns all events related to user id, returns data of event id |
34+
| GET | /api/events/:id | | Returns event by id |
35+
| GET | /api/events/:id/food | | Gets food for event id |
36+
| GET | /api/events/:id/guests | | Gets all guests by event id |
37+
| POST | /api/events | title, location, month, day, year, start_time, end_time | Creates new event |
38+
| POST | /api/events/:id/guests | userID (of the user being added) | Add guest to event by id |
39+
| PUT | /api/events/:id | title, location, month, day, year, start_time, end_time | Updates event by id |
40+
| DELETE | /api/events/:id | | Deletes event by id |
41+
42+
## Food
43+
44+
| Method | Endpoint | Requirements | |
45+
| ------ | -------------------- | --------------------------------------------------------------------- | -------------------------------- |
46+
| GET | /api/food | | Returns all food items |
47+
| GET | /api/events/:id/food | | Returns all the food by event id |
48+
| GET | /api/food/:id | | Returns food object by id |
49+
| POST | /api/food | eventId, userID(optional), category, quantity, name (must be unique) | Creates new food object |
50+
| PUT | /api/food/:id | eventId, userID(optional), category, quantity , name (must be unique) | Updates the food by id |
51+
| DELETE | /api/food/:id | | Deletes the food by id" |

Secrets/secret.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
jwtSecret: process.env.JWT_SECRET || "Gotta secret, can you keep it?"
3+
}

__tests__/auth.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const supertest = require('supertest')
2+
const server = require('../api/server')
3+
const db = require('../data/db-config')
4+
5+
beforeEach(() => {
6+
return db.migrate.rollback()
7+
.then(() => db.migrate.latest())
8+
.then(() => db.seed.run())
9+
})
10+
11+
describe('AUTH', () => {
12+
it('can run test', () => {
13+
expect(true).toBeTruthy()
14+
})
15+
16+
describe('POST Register', () => {
17+
it('Returns status 201', () => {
18+
return supertest(server)
19+
.post('/auth/register')
20+
.send({name: "Derek", username: "derknhood", password: "school"})
21+
.then(res => {
22+
expect(res.status).toEqual(201)
23+
})
24+
})
25+
26+
it('Should return that user', () => {
27+
return supertest(server)
28+
.post('/auth/register')
29+
.send({name: "Derek", username: "derknhood", password: "school"})
30+
.then(res => {
31+
expect(Array.isArray).toHaveLength(1)
32+
})
33+
})
34+
})
35+
36+
describe('POST Login', () => {
37+
it('Returns status 200', () => {
38+
return supertest(server)
39+
.post('/auth/login')
40+
.send({username: "lambda", password: "school"})
41+
.then(res => {
42+
expect(res.status).toEqual(200)
43+
})
44+
})
45+
it('can see users name', () => {
46+
return supertest(server)
47+
.post('/auth/login')
48+
.send({username: "lambda", password: "school"})
49+
.then(res => {
50+
expect(res.body.user).toBe("Rachele")
51+
})
52+
})
53+
it('sends 401 status if invalid username or password', () => {
54+
return supertest(server)
55+
.post('/auth/login')
56+
.send({username: "lamdba", password: "school"})
57+
.then(res => {
58+
expect(res.status).toEqual(401)
59+
})
60+
})
61+
})
62+
})

__tests__/friends.test.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const supertest = require('supertest')
2+
const server = require('../api/server')
3+
const db = require('../data/db-config')
4+
5+
beforeEach(() => {
6+
return db.migrate.rollback()
7+
.then(() => db.migrate.latest())
8+
.then(() => db.seed.run())
9+
})
10+
11+
describe("FRIENDS", () => {
12+
it('can run test', () => {
13+
expect(true).toBeTruthy()
14+
})
15+
//REMEMBER TO SEND TOKEN
16+
describe('GET friends', () => {
17+
it('returns status 200', () => {
18+
return supertest(server)
19+
.post('/auth/login')
20+
.send({username: "lambda", password: "school"})
21+
.then(res => {
22+
const token = res.body.token
23+
return supertest(server)
24+
.get('/api/events/1/invited')
25+
.set('authorization', token)
26+
.then(res => {
27+
expect(res.status).toEqual(200)
28+
})
29+
})
30+
})
31+
it('Access denied if doesnt have token', () => {
32+
return supertest(server)
33+
.post('/auth/login')
34+
.send({username: "lambda", password: "school"})
35+
.then(res => {
36+
return supertest(server)
37+
.get('/api/events/1/invited')
38+
.then(res => {
39+
expect(res.status).toEqual(401)
40+
})
41+
})
42+
})
43+
})
44+
45+
describe('POST friends', () => {
46+
it('returns status 201', () => {
47+
return supertest(server)
48+
.post('/auth/login')
49+
.send({username: "lambda", password: "school"})
50+
.then(res => {
51+
const token = res.body.token
52+
return supertest(server)
53+
.post('/api/events/1/invited')
54+
.set('authorization', token)
55+
.send({userID: 2})
56+
.then(res => {
57+
expect(res.status).toEqual(201)
58+
})
59+
})
60+
})
61+
})
62+
})

__tests__/server.test.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const supertest = require('supertest')
2+
const server = require('../api/server')
3+
4+
describe('SERVER', () => {
5+
it('can run test', () => {
6+
expect(true).toBeTruthy()
7+
})
8+
9+
describe('TEST GET', () => {
10+
it('Returns status 200', () => {
11+
return supertest(server)
12+
.get('/')
13+
.then(res => {
14+
expect(res.status).toBe(200)
15+
})
16+
})
17+
18+
it('returns `api: Running Successfully!`', () => {
19+
return supertest(server)
20+
.get('/')
21+
.then(res => {
22+
expect(res.body.api).toBe('Running Successfully!')
23+
})
24+
})
25+
})
26+
})
27+

api/server.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const cors = require('cors')
2+
const helmet = require('helmet')
3+
4+
const express = require('express')
5+
const authRouter = require('../auth/auth-router')
6+
const eventRouter = require('../events/events-router')
7+
const userRouter = require('../users/users-router')
8+
const foodRouter = require('../food/food-router')
9+
//other routes here
10+
11+
const server = express()
12+
13+
server.use(helmet())
14+
server.use(cors())
15+
server.use(express.json())
16+
17+
server.get('/', (req, res) => {
18+
res.status(200).json({
19+
api: "Running Successfully!"
20+
})
21+
})
22+
23+
server.use('/auth', authRouter)
24+
server.use('/api/events', eventRouter)
25+
server.use('/api/users', userRouter)
26+
server.use('/api/food', foodRouter)
27+
//other routes here
28+
29+
//error middleware
30+
server.use((err, req, res, next) => {
31+
console.log({err})
32+
res.status(500).json({
33+
message: `There was an error retrieving this data: ${err.message}`
34+
})
35+
})
36+
37+
module.exports = server;

auth/auth-model.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const db = require("../data/db-config");
2+
3+
module.exports = {
4+
add,
5+
findByID,
6+
findBy,
7+
};
8+
9+
function findByID(id) {
10+
return db("users").where({ id }).first();
11+
}
12+
13+
function findBy(filter) {
14+
return db("users").where(filter);
15+
}
16+
17+
async function add(user) {
18+
const [newUser] = await db("users").insert(user);
19+
return newUser;
20+
}

0 commit comments

Comments
 (0)