Skip to content

Commit

Permalink
Feature/sequelize (#1)
Browse files Browse the repository at this point in the history
* feat: adding models

* feat: saving user

* refact: users repositories

* feat: create tool

* feat: find all tools

* feat: adding edit and delete endpoints
  • Loading branch information
EvertonTomalok committed Jan 24, 2021
1 parent 3efadcc commit bf6c21f
Show file tree
Hide file tree
Showing 12 changed files with 364 additions and 218 deletions.
137 changes: 134 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"start:reload": "nodemon index.js",
"postgres:start": "docker-compose up -d",
"postgres:stop": "docker-compose down",
"migrate": "node src/utils/schema.js",
"migrate": "node src/utils/migrate.js",
"drop": "node src/utils/dropAllTables.js",
"lint": "eslint ./src",
"lint:fix": "eslint --fix --ext .js ./src"
},
Expand All @@ -30,7 +31,10 @@
"koa-bodyparser": "^4.3.0",
"koa-router": "^10.0.0",
"koa2-swagger-ui": "^5.0.5",
"pg": "^8.5.0",
"pg": "^8.5.1",
"pg-hstore": "^2.3.3",
"prompt-sync": "^4.2.0",
"sequelize": "^6.4.0",
"yamljs": "^0.3.0"
},
"devDependencies": {
Expand Down
43 changes: 24 additions & 19 deletions src/controllers/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,72 @@
const response = require("../utils/response");
const ToolsDB = require("../repositories/tools");

async function createTool(ctx) {
const createTool = async (ctx) => {
const {
title = null,
link = null,
description = null,
tags = null,
} = ctx.request.body;

const { userId } = ctx.state;

if (!title || !link || !description || !tags) {
return response(ctx, 400, { message: "BAD REQUEST." });
}

const tool = await ToolsDB.createTool(title, link, description, tags);
const tool = await ToolsDB.createTool(userId, title, link, description, tags);

if (tool) {
return response(ctx, 201, { tool });
}
return response(ctx, 400, { message: "Something went wrong." });
}
};

async function findTools(ctx) {
const findTools = async (ctx) => {
const {
tag = "",
skip = 0,
limit = 0,
limit = 500,
} = ctx.request.query;

const tools = await ToolsDB.getTools(tag, skip, limit);
const { userId } = ctx.state;

const tools = await ToolsDB.getTools(userId, tag, skip, limit);
return response(ctx, 200, tools);
}
};

async function editTool(ctx) {
const editTool = async (ctx) => {
const {
title = "",
description = "",
link = "",
tags = null,
} = ctx.request.body;

const { userId } = ctx.state;
const { id } = ctx.request.params;

if (!id || (!title && !link && !description && !tags)) {
return response(ctx, 400, "BAD REQUEST!");
}

const tool = await ToolsDB.editTool(id, title, description, link, tags);
if (tool) {
return response(ctx, 200, tool);
const tool = await ToolsDB.editTool(userId, id, title, description, link, tags);
if (tool.updated) {
return response(ctx, 200, tool.data);
} if (!tool.updated) {
return ctx.response.status = 204;
}
return ctx.response.status = 204;
}
return response(ctx, 500, "SOMETHING WENT WRONG!");
};

async function deleteTool(ctx) {
const deleteTool = async (ctx) => {
const { userId } = ctx.state;
const { id } = ctx.request.params;
if (!id) {
return response(ctx, 400, "BAD REQUEST!");
}

await ToolsDB.deleteTool(id);
await ToolsDB.deleteTool(userId, id);
return ctx.response.status = 204;
}
};

module.exports = {
createTool, deleteTool, editTool, findTools,
Expand Down
26 changes: 10 additions & 16 deletions src/helpers/database.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
/* eslint-disable no-console */

const { Client } = require("pg");
const { Sequelize } = require("sequelize");

require("dotenv").config();

const client = new Client({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
password: process.env.DB_PW,
user: process.env.DB_USER,
database: process.env.DB_NAME,
ssl: false,
});
const HOST = process.env.DB_HOST || "localhost";
const PORT = process.env.DB_PORT || 5432;
const PASS = process.env.DB_PW;
const USER = process.env.DB_USER;
const DATABASE = process.env.DB_NAME;

client
.connect()
.then(() => console.log("connected"))
.catch((err) => console.error("conection error", err.stack));
const sequelize = new Sequelize(
`postgres://${USER}:${PASS}@${HOST}:${PORT}/${DATABASE}`,
);

module.exports = client;
module.exports = sequelize;
Loading

0 comments on commit bf6c21f

Please sign in to comment.