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 #172

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
31 changes: 31 additions & 0 deletions staff/leslie-gutierrez/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 }
84 changes: 84 additions & 0 deletions staff/leslie-gutierrez/backend/api/data/demo.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
const mongoose = require('mongoose')

const {Schema, model, ObjectId } = mongoose

const user = new Schema({
name:{
type: String,
require: true
},
email:{
type: String,
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'
},
imagen: {
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(()=> console.error(error))

// const post = new Post ({author: '659e7ba75fbd1937559ff99a', imagen: 'https://pepito.com/image', text: 'Hola,Pepito!'})

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

// Post.findById('659e81b716d73b3ec91dfe48')
// .then(post => {
// post.likes.push('659e81b716d73b3ec91dfe48')

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

User.findById('659e907c9172c3faf1f5481a')
.then(user => {
user.favs.push('659e907c9172c3faf1f5481b')

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

})
.catch(error => console.error(error))
43 changes: 43 additions & 0 deletions staff/leslie-gutierrez/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')

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(()=> console.error(error))

// const post = new Post ({author: '659e7ba75fbd1937559ff99a', imagen: 'https://pepito.com/image', text: 'Hola,Pepito!'})

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

// Post.findById('659e81b716d73b3ec91dfe48')
// .then(post => {
// post.likes.push('659e81b716d73b3ec91dfe48')

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

User.findById('659e907c9172c3faf1f5481a')
.then(user => {
user.favs.push('659e907c9172c3faf1f5481b')

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

})
.catch(error => console.error(error))
52 changes: 52 additions & 0 deletions staff/leslie-gutierrez/backend/api/data/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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
}
Loading