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

feature/backend #142

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
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
133 changes: 133 additions & 0 deletions staff/benjamin-mayiba/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage
*.lcov

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)
web_modules/

# TypeScript cache
*.tsbuildinfo

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional stylelint cache
.stylelintcache

# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache

# Next.js build output
.next
out

# Nuxt.js build / generate output
.nuxt
dist

# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public

# vuepress build output
.vuepress/dist

# vuepress v2.x temp and cache directory
.temp
.cache

# Docusaurus cache and generated files
.docusaurus

# Serverless directories
.serverless/

# FuseBox cache
.fusebox/

# DynamoDB Local files
.dynamodb/

# TernJS port file
.tern-port

# Stores VSCode versions used for testing VSCode extensions
.vscode-test

# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

.DS_Store
.vscode
31 changes: 31 additions & 0 deletions staff/benjamin-mayiba/backend/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# API

## Register user

- Request: POST /users "Content-Type: application/json" { name, email, password }
- Response: 201
- Response (error): 400|409|500 "Content-Type: application/json" { error, message }

## Authenticate user

- Request: POST /users/auth "Content-Type: application/json" { email, password }
- Response: 200 "Content-Type: application/json" userId
Response (error): 400 "Content-Type: application/json" { error, message }

## Retrieve user

- Request: GET /users "Authorization: Bearer userId"
- Response: 200 "Content-Type: application/json" { name }
Response (error): 400 "Content-Type: application/json" { error, message }

## Create post

- Request: POST /posts "Authorization: Bearer userId" "Content-type: application/json" { image, text }
- Response: 201
Response (error): 400 "Content-Type: application/json" { error, message }

## Toggle like post

- Request: PATCH /posts/postId/likes "Authorization: Bearer userId"
- Response: 204
Response (error): 400|404|406|500 "Content-Type: application/json" { error, message }
90 changes: 90 additions & 0 deletions staff/benjamin-mayiba/backend/api/data/demo.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const mongoose = require('mongoose')

const { Schema, model, ObjectId } = mongoose

const user = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
minlength: 8
},
favs: [{
type: ObjectId,
ref: 'Post'
}]

})

const post = new Schema({
author: {
type: ObjectId,
required: true,
ref: 'User'
},
image: {
type: String,
required: true
},
text: {
type: String,
required: true
},
likes:[{
type: ObjectId,
ref: 'User'
}]

})

const User = model('User', user)
const Post = model('Post', post)

mongoose.connect('mongodb://127.0.0.1:27017/test')
.then(() =>{
// const pepito = new User({name: 'Pepito Grillo', email: '[email protected]', password: '123123123'})

// pepito.save()
// .then(()=> console.log('user created'))
// .catch(error => console.error(error))

// const post = new Post({author: '6586f85b5d7e04abd02973a1', image: 'https://pepito.com/image', text: 'Hola, Pepito!'})
// post.save()
// .then(() => console.log('post created'))
// .catch(error => console.error(error))

// Post.findById('658716812b64763feed3c7ac')
// .then(post =>{
// post.likes.push('658716812b64763feed3c7ac')

// post.save()
// .then(() => console.log('post liked'))
// .catch(error => console.log(error))

// })
// .catch(error => console.error(error))


User.findById('6586f85b5d7e04abd02973a1')
.then(user =>{
user.favs.push('65845e4f5958c7e732ea2839')

user.save()
.then(() => console.log('post favorited'))
.catch(error => console.log(error))

})
.catch(error => console.error(error))



})
.catch(error => console.log(error))
43 changes: 43 additions & 0 deletions staff/benjamin-mayiba/backend/api/data/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const mongoose = require('mongoose')
const { User, Post } = require('./models')

mongoose.connect('mongodb://127.0.0.1:27017/test')
.then(() =>{
// const pepito = new User({name: 'Pepito Grillo', email: '[email protected]', password: '123123123'})

// pepito.save()
// .then(()=> console.log('user created'))
// .catch(error => console.error(error))

// const post = new Post({author: '6586f85b5d7e04abd02973a1', image: 'https://pepito.com/image', text: 'Hola, Pepito!'})
// post.save()
// .then(() => console.log('post created'))
// .catch(error => console.error(error))

// Post.findById('658716812b64763feed3c7ac')
// .then(post =>{
// post.likes.push('658716812b64763feed3c7ac')

// post.save()
// .then(() => console.log('post liked'))
// .catch(error => console.log(error))

// })
// .catch(error => console.error(error))


User.findById('6586f85b5d7e04abd02973a1')
.then(user =>{
user.favs.push('65845e4f5958c7e732ea2839')

user.save()
.then(() => console.log('post favorited'))
.catch(error => console.log(error))

})
.catch(error => console.error(error))



})
.catch(error => console.log(error))
5 changes: 5 additions & 0 deletions staff/benjamin-mayiba/backend/api/data/generateId.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function generateId() {
return Math.floor(Math.random() * 1000000000000000000).toString(36)
}

module.exports = generateId
54 changes: 54 additions & 0 deletions staff/benjamin-mayiba/backend/api/data/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const mongoose = require('mongoose')

const { Schema, model, ObjectId } = mongoose

const user = new Schema({
name: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true,
minlength: 8
},
favs: [{
type: ObjectId,
ref: 'Post'
}]

})

const post = new Schema({
author: {
type: ObjectId,
required: true,
ref: 'User'
},
image: {
type: String,
required: true
},
text: {
type: String,
required: true
},
likes:[{
type: ObjectId,
ref: 'User'
}]

})

const User = model('User', user)
const Post = model('Post', post)

module.exports = {
User,
Post
}
27 changes: 27 additions & 0 deletions staff/benjamin-mayiba/backend/api/data/posts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[
{
"id": "1h0mciutqx6o",
"author": "6ttz1tptn2c0",
"image": "https://media.istockphoto.com/id/181072765/es/foto/lechuga-aislado.jpg?s=612x612&w=0&k=20&c=7spdLdTK_iyTUdpdp6cjdHkDE9dCkahoTtnOvQYY8mE=",
"text": "what a fresh day",
"likes": [
"2g7e6f4id6m8"
]
},
{
"id": "5ltu9tg71500",
"author": "730thx7n4n4",
"image": "https://cdn2.vectorstock.com/i/1000x1000/81/46/hello-world-code-vector-22928146.jpg",
"text": "Hello, World!",
"likes": []
},
{
"id": "zkh28kldecw",
"author": "6ufezr85vfg0",
"image": "http://image.com/123",
"text": "Hola, Popeye!",
"likes": [
"730thx7n4n4"
]
}
]
Loading