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

Open
wants to merge 1 commit 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
31 changes: 31 additions & 0 deletions staff/tano-aleu/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 }
83 changes: 83 additions & 0 deletions staff/tano-aleu/backend/api/data/demo.1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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: '658492be56b972c1b83fad15', image: 'https://pepito.com/image', text: 'Hola, Pepito!' })

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

// Post.findById('65a565f4d407209b1b1ceeab')
// .then(post => {
// post.likes.push('65a565f4d407209b1b1ceeab')

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

User.findById('65a53f98acdac7175a73b080')
.then(user => {
user.favs.push('65a53f98acdac7175a73b080')

user.save()
.then(() => console.log('post favorited'))
.catch(error => console.error(error))
})
.catch(error => console.error(error))
})
.catch(error => console.error(error))
38 changes: 38 additions & 0 deletions staff/tano-aleu/backend/api/data/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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: '658492be56b972c1b83fad15', image: 'https://pepito.com/image', text: 'Hola, Pepito!' })

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

// Post.findById('65849329ed634b64d5f501fd')
// .then(post => {
// post.likes.push('65849329ed634b64d5f501fd')

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

User.findById('658494db72da5d1f6dd318c4')
.then(user => {
user.favs.push('658494db72da5d1f6dd318c5')

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/tano-aleu/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