Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Wellerson committed Jul 26, 2019
0 parents commit e8bbd4b
Show file tree
Hide file tree
Showing 33 changed files with 12,352 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
yarn.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Wellerson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<p align="center">
<img src="./backend.png" height="130"/>
</p>
<!-- <p align="center">
<img src="https://img.shields.io/github/package-json/v/wellers0n/4fun-fullstack.svg"/>
<img src="https://img.shields.io/github/last-commit/wellers0n/4fun-fullstack.svg"/>
<img src="https://img.shields.io/github/license/wellers0n/4fun-fullstack.svg"/>
<a href="https://twitter.com/wellers0n_" target="_blank">
<img src="https://img.shields.io/twitter/url/https/wellers0n_.svg?style=social"/>
</a>
</p> -->

<p>
<h1 align="center">Backend-diff</h1>
<p/>

<br/>

## Backend-diff JS and TS

Backend-diff uses a stack full `JS/TS` on the backend, I'm making this project to improve
my skills in the backend.

## Initing in the your PC

- For clone the project `git clone https://github.com/Wellers0n/Backend-diff.git`
- Enter in the folder `cd Backend-diff/`
- To install project dependency: `yarn install`
- After the installation of the dependencies `yarn start` in the default directory

## Stack used

[NodeJS](https://nodejs.org/en/)<br/>
[KoaJS](https://koajs.com/)<br/>
[MongoDB](https://www.mongodb.com/)<br/>
[GraphQL](https://graphql.org/)<br/>
[Yarn](https://yarnpkg.com/en/)<br/>
[WorkSpaces](https://yarnpkg.com/lang/en/docs/workspaces/)<br/>
Binary file added backend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "Main",
"version": "0.0.1",
"description": "Api graphql and rest",
"main": "index.js",
"author": "Wellerson",
"license": "MIT",
"private": true,
"scripts": {
"start": "yarn start:rest & yarn start:server",
"start:rest": "yarn workspace @rest/server run start",
"start:server": "yarn workspace @graphql/server run start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": [
"**/react-native",
"**/react-native-*"
]
}
}
12 changes: 12 additions & 0 deletions packages/server/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

{
"presets": [
"@babel/env",
"@babel/typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread",
"babel-plugin-idx"
]
}
Empty file added packages/server/.env.example
Empty file.
3 changes: 3 additions & 0 deletions packages/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.env
package-lock.json
12 changes: 12 additions & 0 deletions packages/server/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#5ece50",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#6a76d5",
"activityBarBadge.foreground": "#e7e7e7",
"statusBar.background": "#42b833",
"statusBarItem.hoverBackground": "#349028",
"statusBar.foreground": "#15202b"
}
}
68 changes: 68 additions & 0 deletions packages/server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"name": "@graphql/server",
"version": "1.0.0",
"description": "GraphQL test",
"main": "index.js",
"author": "Wellerson",
"license": "MIT",
"private": true,
"scripts": {
"start": "nodemon --exec ts-node ./src/index.ts",
"tslint": "tslint --project .",
"test": "jest",
"test:watch": "jest --watchAll --coverage"
},
"devDependencies": {
"@babel/cli": "^7.4.3",
"@babel/core": "^7.4.3",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/plugin-proposal-object-rest-spread": "^7.4.3",
"@babel/preset-env": "^7.4.3",
"@babel/preset-typescript": "^7.3.3",
"@types/jest": "^24.0.11",
"@types/kcors": "^2.2.3",
"@types/koa": "^2.0.48",
"@types/koa-bodyparser": "^4.2.2",
"@types/koa-json": "^2.0.18",
"@types/koa-logger": "^3.1.1",
"@types/koa-router": "^7.0.40",
"get-graphql-schema": "^2.1.2",
"jest": "^24.7.0",
"nodemon": "^1.18.10",
"ts-jest": "^24.0.1",
"ts-node": "^8.0.3",
"tslint": "^5.15.0",
"tslint-config-airbnb": "^5.11.1",
"typescript": "^3.4.1"
},
"dependencies": {
"@types/dotenv-safe": "^5.0.3",
"@types/form-data": "^2.2.1",
"@types/graphql": "^14.2.0",
"@types/graphql-relay": "^0.4.9",
"@types/jsonwebtoken": "^8.3.2",
"@types/koa-multer": "^1.0.0",
"@types/mongoose": "^5.3.25",
"@types/node-fetch": "^2.3.7",
"babel-plugin-idx": "^2.4.0",
"dataloader": "^1.4.0",
"dotenv-safe": "^6.1.0",
"form-data": "^2.4.0",
"graphql": "^14.2.1",
"graphql-relay": "^0.6.0",
"idx": "^2.5.6",
"jsonwebtoken": "^8.5.1",
"kcors": "^2.2.2",
"koa": "^2.7.0",
"koa-bodyparser": "^4.2.1",
"koa-graphql": "^0.8.0",
"koa-json": "^2.0.2",
"koa-logger": "^3.2.0",
"koa-multer": "^1.0.2",
"koa-router": "^7.4.0",
"mongoose": "^5.5.6",
"multer": "^1.4.1",
"multer-gridfs-storage": "^3.2.3",
"node-fetch": "^2.6.0"
}
}
10 changes: 10 additions & 0 deletions packages/server/src/__test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {greeter} from '../server'

describe('func test', () => {
it('should return an string with name: Hello, wellerson', () => {
expect(greeter('wellerson')).toBe('Hello, wellerson');
});
it('should return an string with name: Hello, moreira', () => {
expect(greeter('moreira')).toBe('Hello, moreira');
});
})
28 changes: 28 additions & 0 deletions packages/server/src/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import jwt from 'jsonwebtoken';
import User from './model/users';
import mongoose from 'mongoose'

export async function getUser(token: string) {
if (!token) return { user: null };

try {
const decodedToken:any = jwt.verify(token, 'batman');

const user = await User.findOne({ _id: decodedToken.id });

return {
user,
};
} catch (err) {
return { user: null };
}
}

type UserType = {
_id: string,
};

export function generateToken(user: UserType) {
return jwt.sign({id: user._id}, 'batman');
}
19 changes: 19 additions & 0 deletions packages/server/src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from "mongoose";

export default function connectDatabase() {
return new Promise((resolve, reject) => {
mongoose.Promise = global.Promise;
mongoose.connection
.on("error", error => reject(error))
.on("close", () => console.log("Database connection closed."))
.once("open", () => resolve(mongoose.connections[0]));

mongoose.connect(
"mongodb+srv://Wellerson:[email protected]/blog?retryWrites=true&w=majority",
{
useNewUrlParser: true,
useCreateIndex: true
}
);
});
}
22 changes: 22 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "@babel/polyfill";
import { createServer } from "http";
import app from "./server";
import connectDatabase from "./database";

(async () => {
try {
await connectDatabase();
} catch (error) {
console.log("Could not connect to database", { error });
throw error;
}

const server = createServer(app.callback());

server.listen(5000, () => {
return console.log(
`SERVER ON: http://localhost:${process.env.PORT || 5000}/graphql`
);
});
})();

34 changes: 34 additions & 0 deletions packages/server/src/model/article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import mongoose from "mongoose";
const Schema = mongoose.Schema;

const article = new Schema({
idUser: {
type: String,
required: "idUser is requerid"
},
title: {
type: String,
required: "title is requerid"
},
subtitle: {
type: String,
required: "subtitle is requerid"
},
description: {
type: String,
required: "author is requerid"
},
author: {
type: [String],
required: "author is requerid"
},
date: {
type: Date,
default: Date.now,
},
date_update: {
type: Date
}
});

export default mongoose.model("articles", article);
19 changes: 19 additions & 0 deletions packages/server/src/model/comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;

const comment = new Schema({
name: {
type: String,
required: 'name is required',
},
description: {
type: String,
required: 'description is required',
},
idArticle: {
type: String,
required: 'idArticle required'
}
});

export default mongoose.model('comments', comment)
20 changes: 20 additions & 0 deletions packages/server/src/model/users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import mongoose from 'mongoose';
const Schema = mongoose.Schema;

const users = new Schema({
name: {
type: String,
required: 'name is requerid',
},
email:{
type: String,
required: 'email is requerid',
},
password:{
type: String,
required: 'password is requerid',
}

});

export default mongoose.model('users', users)
39 changes: 39 additions & 0 deletions packages/server/src/modules/main/ArticleType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
GraphQLObjectType,
GraphQLString,
GraphQLID,
GraphQLList
} from 'graphql'
import {globalIdField} from 'graphql-relay'

export default new GraphQLObjectType({
name: 'Articles',
fields: () => ({
id: globalIdField('Articles'),
_id:{
type: GraphQLID
},
author:{
type: new GraphQLList(GraphQLString),
},
date:{
type: GraphQLString
},
date_update:{
type: GraphQLString
},
title:{
type: GraphQLString
},
subtitle:{
type: GraphQLString
},
description:{
type: GraphQLString
},
idUser: {
type: GraphQLString
}
})
})

Loading

0 comments on commit e8bbd4b

Please sign in to comment.