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

Open
wants to merge 23 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
006ef2a
add backend initial code; add register user logic
juditta99 Dec 10, 2023
147c207
add frontend files and gitcommands help file
juditta99 Dec 10, 2023
9305a10
add login user logic #154
juditta99 Dec 11, 2023
6011500
add change user email logic
juditta99 Dec 11, 2023
c429cd7
add change user password functionality to api #155
juditta99 Dec 11, 2023
6ebe306
update users database
juditta99 Dec 11, 2023
6286089
implement delete user functionality in API
juditta99 Dec 11, 2023
4160fee
cert test
juditta99 Dec 12, 2023
ffb802b
udpate to latest
juditta99 Jan 7, 2024
adcfcf3
sync with latest version post holiday
juditta99 Jan 7, 2024
4c17fd9
migrate db from csv to JSON; add retrieveUser and createPost logics #252
juditta99 Jan 12, 2024
7577cfe
implement basic http server with express and register userendpoint #252
juditta99 Jan 13, 2024
6131299
change name of loginUser logic to authenticateUser
juditta99 Jan 13, 2024
42e7ab2
integrate authenticate logic in server; refactor endpoints testing (a…
juditta99 Jan 13, 2024
9ee3bdc
rename files
juditta99 Jan 13, 2024
b71e85d
add create post and toggle like post logic and endpoints, doc, test; …
juditta99 Jan 14, 2024
37ae589
add api tests with fetch in html files; add API response headers to a…
juditta99 Jan 15, 2024
3e6c768
add stuff folder with mongo readme file
juditta99 Jan 16, 2024
3cc25ae
create README.md with commands to run in mongoshell
juditta99 Jan 19, 2024
2923393
clean up backend api folders #268
juditta99 Jan 21, 2024
2657a4d
take baby steps in mongo with node for api #269
juditta99 Jan 21, 2024
fb22f83
take baby steps in mongodb via mongoose; integrate mongoose models in…
juditta99 Jan 22, 2024
155f861
add retrieve and create posts logics and endpoints #269
juditta99 Jan 24, 2024
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/judy-grayland/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 }
69 changes: 69 additions & 0 deletions staff/judy-grayland/backend/api/data/demo_mongodriver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const mongodb = require('mongodb')

// Para conectarnos a la bbdd, nos traemos la clase MongoClient, y lo instanciamos. MongoClient es una clase que está dentro del objeto mongodb. Aquí estamos destructurando. MongoClient nos sirve para crear un cliente como Mongosh. Es decir nos vamos a conectar desde aquí con node, en vez de através de mongosh en la terminal.
// Para hacer un updateOne, necesitamos el ObjectId. Como es una función de Mongo, nos la traemos aquí:
const { MongoClient, ObjectId } = mongodb

// le pasamos la ruta de la conexión: 1) usamos el protocolo de mongo (en vez de http)). 2)la dirección de nuestra máquina. Localhost no funciona siempre desde VSC, así que mejor poner la dirección ip: 127.0.0.1, y 3)El puerto en que suele abrirse Mongo es: 27017
const client = new MongoClient('mongodb://127.0.0.1:27017')

// nos conectamos así con client.connect y un try catch. Si nos conectamos succesffuly, en el callback del then recibimos el connector, con el que podemos decir que usemos el test y luego la colección users
client
.connect()
.then((connector) => {
const db = connector.db('test')

const users = db.collection('users')
const posts = db.collection('posts')

// users
// .insertOne({
// name: 'Pata Tus',
// email: '[email protected]',
// password: 'aaa',
// favs: [],
// })
// .then(result => console.log('inserted', result))
// .catch((error) => console.error(error))

// users
// .updateOne(
// { _id: new ObjectId('65acf7e1f0bad8bad5bcdd9e') },
// { $set: { name: 'Pata Tin', email: '[email protected]' } }
// )
// .then(result => console.log('updated', result))
// .catch((error) => console.error(error))

// users
// .findOne({ _id: new ObjectId('65acf7e1f0bad8bad5bcdd9e') })
// .then((result) => console.log('found', result))
// .catch((error) => console.error(error))

// users
// .deleteOne({ _id: new ObjectId('659be6460093e6bdc0b0ad0e') })
// .then((result) => console.log('deleted', result))
// .catch((error) => console.error(error))

// users
// .find().toArray()
// .then((result) => console.log('found all', result))
// .catch((error) => console.error(error))

// posts
// .insertOne({
// author: new ObjectId('65acf7e1f0bad8bad5bcdd9e'),
// image:
// 'https://valenciafruits.com/wp-content/uploads/2023/10/Calabacin-01-1024x820.jpg',
// text: 'Time to grow!',
// likes: [],
// })
// .then((result) => console.log('inserted', result))
// .catch((error) => console.error(error))

// posts
// .find({ author: new ObjectId('65acf7e1f0bad8bad5bcdd9e') })
// .toArray()
// .then((result) => console.log('found posts', result))
// .catch((error) => console.error(error))
})
.catch((error) => console.error(error))
53 changes: 53 additions & 0 deletions staff/judy-grayland/backend/api/data/demo_mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const mongoose = require('mongoose')

const { User, Post } = require('./models')
// en la propia dirección podemos indicar la bbdd a la que queremos conectarnos - test
mongoose
.connect('mongodb://127.0.0.1:27017/test')
.then(() => {
// // CREAR USUARIO
// // creamos el usuario en memoria guardándolo como una variable. Para guardarlo en la BBDD hacemos .save() después:
// const ajo = new User({ name: 'A Jo', email: '[email protected]', password: 'aaa' })
// ajo
// .save()
// .then(() => console.log('user created'))
// .catch((error) => console.error(error))

// CREAR POST
// const post = new Post({
// author: '65ad62b7b958109e79915778',
// image:
// 'https://content.fortune.com/wp-content/uploads/2023/08/Barbie-Dancing-MCDBARB_WB048.jpg',
// text: "Let's dance the night away!",
// })

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

// // DARLE LIKE A UN POST:
// Post.findById('65ad69d88ce623d0fdd7cdc6')
// .then((post) => {
// post.likes.push('65acf7e1f0bad8bad5bcdd9e')

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

// GUARDAR POST COMO FAV
User.findById('65acf7e1f0bad8bad5bcdd9e')
.then((user) => {
user.favs.push('65ad69d88ce623d0fdd7cdc6')

user
.save()
.then(() => console.log('post favd'))
.catch((error) => console.error(error))
})
.catch((error) => console.error(error))
})
.catch((error) => console.error(error))
56 changes: 56 additions & 0 deletions staff/judy-grayland/backend/api/data/models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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',
},
],
})

// el ref lo ponemos para decirle a qué colección pertenece. El Id pertenece a los usuarios. Así relacionamos que el autor viene de la colección usuarios.
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