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/socialcode #125

Open
wants to merge 15 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
Binary file added staff/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions staff/angel-patino/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
thumbs.db
4 changes: 4 additions & 0 deletions staff/angel-patino/socialcode/api/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PORT = 9010
JWT_SECRET = peter and wendy have a rollete
MONGODB_URL = mongodb://localhost:27017/socialcode
MONGODB_URL_TEST = mongodb://localhost:27017/socialcode-test
2 changes: 2 additions & 0 deletions staff/angel-patino/socialcode/api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
!.env
13 changes: 13 additions & 0 deletions staff/angel-patino/socialcode/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# API

## Installation

```sh
$ npm i
```

## Execution

```sh
$ npm start
```
36 changes: 36 additions & 0 deletions staff/angel-patino/socialcode/api/data/Post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Schema, model, Types } from 'mongoose'

const { ObjectId } = Types

const post = new Schema({
author: {
type: ObjectId,
required: true,
ref: 'User'
},
title: {
type: String,
required: true
},
image: {
type: String,
required: true
},
description: {
type: String,
required: true
},
date: {
type: Date,
required: true,
default: Date.now
},
likes: [{
type: ObjectId,
ref: 'User'
}]
})

const Post = model('Post', post)

export default Post
30 changes: 30 additions & 0 deletions staff/angel-patino/socialcode/api/data/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Schema, model } from 'mongoose'

const user = new Schema({
name: {
type: String,
required: true
},
surname: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
username: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
}
})

const User = model('User', user)

export default User
27 changes: 27 additions & 0 deletions staff/angel-patino/socialcode/api/data/demo_mongo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { MongoClient, ObjectId } from 'mongodb'

const client = new MongoClient('mongodb://localhost:27017')

client.connect()
.then(connection => {
const db = connection.db('test')

const users = db.collection('users')

users.insertOne({ name: 'Bat', surname: 'Man', email: '[email protected]', username: 'BatMan', password: '123123123' })
.then(result => console.log(result))
.catch(error => console.error(error))

// users.deleteOne({ _id: new ObjectId('667c5c1d8e6ac610bb9f7f6a') })
// .then(result => console.log(result))
// .catch(error => console.error(error))

//users.find({}).toArray()
// .then(results => console.log(results))
//.catch(error => console.error(error))

// users.updateOne({ _id: new ObjectId('667c60becdc4257cb3416eca') }, { $set: { password: '234234234' } })
// .then(result => console.log(result))
// .catch(error => console.error(result))
})
.catch(error => console.error(error))
21 changes: 21 additions & 0 deletions staff/angel-patino/socialcode/api/data/demo_mongoose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// import mongoose from 'mongoose'
// mongoose.connect('mongodb://127.0.0.1:27017/test')

// const User = mongoose.model("User", { name: String, surname: String, email: String, username: String, password: String })

// const pepito = new User({ name: "Depor", surname: "Tivo", email: "[email protected]", username: "deportivo", password: { password: 123 } })
// pepito.save().then(() => console.log("user insert")).catch(console.error)

import mongoose from "mongoose"

mongoose.connect('mongodb://localhost:27017/test')
.then(() => {
User.create({ name: 'Super', surname: 'Woman', email: '[email protected]', username: 'superwoman', password: '123123123' })
.then(() => console.log('created'))
.catch(error => console.error(error))

Post.create({ author: 'pepon', title: 'comedoritos', image: 'https.//loquesea.com', description: '...' })
.then(() => console.log('created'))
.catch(error => console.error(error))
})
.catch(error => console.error(error))
7 changes: 7 additions & 0 deletions staff/angel-patino/socialcode/api/data/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import User from './User.js'
import Post from './Post.js'

export {
User,
Post
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import jwt from "../util/jsonwebtoken-promised.js"
import logic from "../logic/index.js"
import { SystemError } from "com/errors.js"

const { JWT_SECRET } = process.env

export default (req, res, next) => {
const { username, password } = req.body

try {
logic.authenticateUser(username, password)
.then(userId =>
jwt.sign({ sub: userId }, JWT_SECRET, { expiresIn: '1h' })
.then(token => res.json(token))
.catch(error => next(new SystemError(error.message)))
)
.catch(error => next(error))
} catch (error) {
next(error)
}
}
29 changes: 29 additions & 0 deletions staff/angel-patino/socialcode/api/handlers/createPostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logic from "../logic/index.js"
import { CredentialsError } from "com/errors.js"
import jwt from "../util/jsonwebtoken-promised.js"

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { title, image, description } = req.body

try {
logic.createPost(userId, title, image, description)
.then(() => res.status(201).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}
29 changes: 29 additions & 0 deletions staff/angel-patino/socialcode/api/handlers/deletePostHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import logic from "../logic/index.js"
import jwt from "../util/jsonwebtoken-promised.js"
import { CredentialsError } from "com/errors.js"

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { postId } = req.params

try {
logic.deletePost(userId, postId)
.then(() => res.status(204).send())
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}
18 changes: 18 additions & 0 deletions staff/angel-patino/socialcode/api/handlers/errorHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ContentError, CredentialsError, DuplicityError, MatchError, NotFoundError } from 'com/errors.js'

export default (error, req, res, next) => {
let status = 500

if (error instanceof DuplicityError)
status = 409
else if (error instanceof ContentError)
status = 400
else if (error instanceof MatchError)
status = 412
else if (error instanceof CredentialsError)
status = 401
else if (error instanceof NotFoundError)
status = 404

res.status(status).json({ error: error.constructor.name, message: error.message })
}
27 changes: 27 additions & 0 deletions staff/angel-patino/socialcode/api/handlers/getAllPostsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import logic from "../logic/index.js"
import { CredentialsError } from "com/errors.js"
import jwt from "../util/jsonwebtoken-promised.js"

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

try {
logic.getAllPosts(userId)
.then(posts => res.json(posts))
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}
64 changes: 64 additions & 0 deletions staff/angel-patino/socialcode/api/handlers/getUserNameHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import jwt from '../util/jsonwebtoken-promised.js'
import { CredentialsError } from 'com/errors.js'
import logic from '../logic/index.js'

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { targetUserId } = req.params

try {
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}

}

/*
import jwt from '../util/jsonwebtoken-promised.js'

import logic from '../logic/index.js'

import { CredentialsError } from 'com/errors.js'

const { JWT_SECRET } = process.env

export default (req, res, next) => {
try {
const token = req.headers.authorization.slice(7)

jwt.verify(token, JWT_SECRET)
.then(payload => {
const { sub: userId } = payload

const { targetUserId } = req.params

try {
logic.getUserName(userId, targetUserId)
.then(name => res.json(name))
.catch(error => next(error))
} catch (error) {
next(error)
}
})
.catch(error => next(new CredentialsError(error.message)))
} catch (error) {
next(error)
}
}
*/
Loading