├── .gitignore ├── prisma ├── migrations │ ├── migration_lock.toml │ ├── 20220509195613_init │ │ └── migration.sql │ ├── 20220509220202_init │ │ └── migration.sql │ └── 20220509190757_init │ │ └── migration.sql ├── schema.prisma └── seed.js ├── .env ├── package.json └── server.js /.gitignore: -------------------------------------------------------------------------------- 1 | **/node_modules -------------------------------------------------------------------------------- /prisma/migrations/migration_lock.toml: -------------------------------------------------------------------------------- 1 | # Please do not edit this file manually 2 | # It should be added in your version-control system (i.e. Git) 3 | provider = "postgresql" -------------------------------------------------------------------------------- /prisma/migrations/20220509195613_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE "Mission" ( 3 | "id" SERIAL NOT NULL, 4 | "name" TEXT NOT NULL, 5 | "lang" VARCHAR(255) NOT NULL, 6 | "missionCommander" VARCHAR(255) NOT NULL, 7 | "enrollments" INTEGER NOT NULL, 8 | "hasCertification" BOOLEAN NOT NULL DEFAULT false, 9 | 10 | CONSTRAINT "Mission_pkey" PRIMARY KEY ("id") 11 | ); 12 | 13 | -- CreateIndex 14 | CREATE UNIQUE INDEX "Mission_name_key" ON "Mission"("name"); 15 | -------------------------------------------------------------------------------- /.env: -------------------------------------------------------------------------------- 1 | # Environment variables declared in this file are automatically made available to Prisma. 2 | # See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema 3 | 4 | # Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB (Preview). 5 | # See the documentation for all the connection string options: https://pris.ly/d/connection-strings 6 | 7 | DATABASE_URL="postgresql://postgres:123@localhost:5432/explorers_api?schema=public" -------------------------------------------------------------------------------- /prisma/migrations/20220509220202_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE "missionCommander" ( 3 | "id" SERIAL NOT NULL, 4 | "name" TEXT NOT NULL, 5 | "username" VARCHAR(255) NOT NULL, 6 | "mainStack" VARCHAR(255) NOT NULL, 7 | "currentEnrollment" BOOLEAN NOT NULL DEFAULT false, 8 | "hazAzureCertification" BOOLEAN NOT NULL DEFAULT false, 9 | 10 | CONSTRAINT "missionCommander_pkey" PRIMARY KEY ("id") 11 | ); 12 | 13 | -- CreateIndex 14 | CREATE UNIQUE INDEX "missionCommander_name_key" ON "missionCommander"("name"); 15 | -------------------------------------------------------------------------------- /prisma/migrations/20220509190757_init/migration.sql: -------------------------------------------------------------------------------- 1 | -- CreateTable 2 | CREATE TABLE "Explorer" ( 3 | "id" SERIAL NOT NULL, 4 | "name" TEXT NOT NULL, 5 | "username" VARCHAR(255) NOT NULL, 6 | "mission" VARCHAR(255) NOT NULL, 7 | "azureCertification" BOOLEAN NOT NULL DEFAULT false, 8 | "dateCreated" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, 9 | "lastUpdated" TIMESTAMP(3) NOT NULL, 10 | 11 | CONSTRAINT "Explorer_pkey" PRIMARY KEY ("id") 12 | ); 13 | 14 | -- CreateIndex 15 | CREATE UNIQUE INDEX "Explorer_name_key" ON "Explorer"("name"); 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "express_db", 3 | "version": "1.0.0", 4 | "description": "App Express DB", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/RodolfoBaume/Express_DB.git" 12 | }, 13 | "author": "RodolfoBaume", 14 | "license": "ISC", 15 | "bugs": { 16 | "url": "https://github.com/RodolfoBaume/Express_DB/issues" 17 | }, 18 | "homepage": "https://github.com/RodolfoBaume/Express_DB#readme", 19 | "devDependencies": { 20 | "express": "^4.18.1", 21 | "prisma": "^3.13.0" 22 | }, 23 | "dependencies": { 24 | "@prisma/client": "^3.13.0", 25 | "cors": "^2.8.5" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /prisma/schema.prisma: -------------------------------------------------------------------------------- 1 | // This is your Prisma schema file, 2 | // learn more about it in the docs: https://pris.ly/d/prisma-schema 3 | 4 | generator client { 5 | provider = "prisma-client-js" 6 | } 7 | 8 | datasource db { 9 | provider = "postgresql" 10 | url = env("DATABASE_URL") 11 | } 12 | 13 | model Explorer { 14 | id Int @id @default(autoincrement()) 15 | name String @unique 16 | username String @db.VarChar(255) 17 | mission String @db.VarChar(255) 18 | azureCertification Boolean @default(false) 19 | dateCreated DateTime @default(now()) 20 | lastUpdated DateTime @updatedAt 21 | } 22 | 23 | model Mission { 24 | id Int @id @default(autoincrement()) 25 | name String @unique 26 | lang String @db.VarChar(255) 27 | missionCommander String @db.VarChar(255) 28 | enrollments Int 29 | hasCertification Boolean @default(false) 30 | } 31 | 32 | model missionCommander { 33 | id Int @id @default(autoincrement()) 34 | name String @unique 35 | username String @db.VarChar(255) 36 | mainStack String @db.VarChar(255) 37 | currentEnrollment Boolean @default(false) 38 | hazAzureCertification Boolean @default(false) 39 | } 40 | -------------------------------------------------------------------------------- /prisma/seed.js: -------------------------------------------------------------------------------- 1 | const { PrismaClient } = require('@prisma/client'); 2 | const prisma = new PrismaClient(); 3 | 4 | (async function main() { 5 | try { 6 | const woopa = await prisma.explorer.upsert({ 7 | where: { name: 'Woopa' }, 8 | update: {}, 9 | create: { 10 | name: 'Woopa', 11 | username: 'ajolonauta', 12 | mission: 'Node' 13 | }, 14 | }); 15 | 16 | const woopa1 = await prisma.explorer.upsert({ 17 | where: { name: 'Woopa1' }, 18 | update: {}, 19 | create: { 20 | name: 'Woopa1', 21 | username: 'ajolonauta1', 22 | mission: 'Node' 23 | }, 24 | }); 25 | 26 | const woopa2 = await prisma.explorer.upsert({ 27 | where: { name: 'Woopa 2' }, 28 | update: {}, 29 | create: { 30 | name: 'Woopa 2', 31 | username: 'ajolonauta2', 32 | mission: 'Java' 33 | }, 34 | }); 35 | 36 | const woopa3 = await prisma.explorer.upsert({ 37 | where: { name: 'Woopa 3' }, 38 | update: {}, 39 | create: { 40 | name: 'Woopa 3', 41 | username: 'ajolonauta3', 42 | mission: 'Node' 43 | }, 44 | }); 45 | 46 | const node = await prisma.mission.upsert({ 47 | where: { name: 'Mission Node' }, 48 | update: {}, 49 | create: { 50 | name: 'Mission Node', 51 | lang: 'Node', 52 | missionCommander: 'Carlo Gilmar', 53 | enrollments: 1 54 | }, 55 | }); 56 | 57 | const java = await prisma.mission.upsert({ 58 | where: { name: 'Mission Java'}, 59 | update: {}, 60 | create: { 61 | name: 'Mission Java', 62 | lang: 'Java', 63 | missionCommander: 'Fernanda', 64 | enrollments: 2 65 | }, 66 | }); 67 | 68 | const mc1 = await prisma.missionCommander.upsert({ 69 | where: { name: 'Carlo Gilmar'}, 70 | update: {}, 71 | create: { 72 | name: 'Carlo Gilmar', 73 | username: 'carlogilmar_', 74 | mainStack: 'Node' 75 | }, 76 | }); 77 | 78 | const mc2 = await prisma.missionCommander.upsert({ 79 | where: { name: 'Fernanda'}, 80 | update: {}, 81 | create: { 82 | name: 'Fernanda', 83 | username: 'fer', 84 | mainStack: 'Java' 85 | }, 86 | }); 87 | 88 | const mc3 = await prisma.missionCommander.upsert({ 89 | where: { name: 'Rodrigo'}, 90 | update: {}, 91 | create: { 92 | name: 'Rodrigo', 93 | username: 'romarpla', 94 | mainStack: 'Javascript' 95 | }, 96 | }); 97 | 98 | console.log('Create 3 explorers'); 99 | } catch(e) { 100 | console.error(e); 101 | process.exit(1); 102 | } finally { 103 | await prisma.$disconnect(); 104 | } 105 | })(); -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require('express'); 2 | const app = express(); 3 | app.use(express.json()); 4 | const port = process.env.PORT || 3000; 5 | 6 | // Require para usar Prisma 7 | const { PrismaClient } = require('@prisma/client'); 8 | const prisma = new PrismaClient(); 9 | 10 | // CORS 11 | const cors = require("cors"); 12 | 13 | const corsOptions = { 14 | origin: "http://localhost:8081" 15 | }; 16 | 17 | app.use(cors(corsOptions)); 18 | 19 | app.get('/', (req, res) => { 20 | res.json({message: 'alive'}); 21 | }); 22 | 23 | // CRUD Explorers 24 | app.get('/explorers', async (req, res) => { 25 | const allExplorers = await prisma.explorer.findMany({}); 26 | res.json(allExplorers); 27 | }); 28 | 29 | app.get('/explorers/:id', async (req, res) => { 30 | const id = req.params.id; 31 | const explorer = await prisma.explorer.findUnique({where: {id: parseInt(id)}}); 32 | res.json(explorer); 33 | }); 34 | 35 | app.post('/explorers', async (req, res) => { 36 | const explorer = { 37 | name: req.body.name, 38 | username: req.body.username, 39 | mission: req.body.mission 40 | }; 41 | const message = 'Explorer creado.'; 42 | await prisma.explorer.create({data: explorer}); 43 | return res.json({message}); 44 | }); 45 | 46 | app.put('/explorers/:id', async (req, res) => { 47 | const id = parseInt(req.params.id); 48 | 49 | await prisma.explorer.update({ 50 | where: { 51 | id: id 52 | }, 53 | data: { 54 | mission: req.body.mission 55 | } 56 | }) 57 | 58 | return res.json({message: "Actualizado correctamente"}); 59 | }); 60 | 61 | app.delete('/explorers/:id', async (req, res) => { 62 | const id = parseInt(req.params.id); 63 | await prisma.explorer.delete({where: {id: id}}); 64 | return res.json({message: "Eliminado correctamente"}); 65 | }); 66 | 67 | // CRUD Missions 68 | app.get('/missions', async (req, res) => { 69 | const allMissions = await prisma.mission.findMany({}); 70 | res.json(allMissions); 71 | }); 72 | 73 | app.get('/missions/:id', async (req, res) => { 74 | const id = req.params.id; 75 | const mission = await prisma.mission.findUnique({where: {id: parseInt(id)}}); 76 | res.json(mission); 77 | }); 78 | 79 | app.post('/missions', async (req, res) => { 80 | const mission = { 81 | name: req.body.name, 82 | lang: req.body.lang, 83 | missionCommander: req.body.missionCommander, 84 | enrollments: req.body.enrollments 85 | }; 86 | const message = 'Mision creada.'; 87 | await prisma.mission.create({data: mission}); 88 | return res.json({message}); 89 | }); 90 | 91 | app.put('/missions/:id', async (req, res) => { 92 | const id = parseInt(req.params.id); 93 | 94 | await prisma.mission.update({ 95 | where: { 96 | id: id 97 | }, 98 | data: { 99 | name: req.body.name 100 | } 101 | }) 102 | 103 | return res.json({message: "Actualizado correctamente"}); 104 | }); 105 | 106 | app.delete('/missions/:id', async (req, res) => { 107 | const id = parseInt(req.params.id); 108 | await prisma.mission.delete({where: {id: id}}); 109 | return res.json({message: "Eliminado correctamente"}); 110 | }); 111 | 112 | // CRUD MissionCommanders 113 | app.get('/missionCommanders', async (req, res) => { 114 | const allmissionCommanders = await prisma.missionCommander.findMany({}); 115 | res.json(allmissionCommanders); 116 | }); 117 | 118 | app.get('/missionCommanders/:id', async (req, res) => { 119 | const id = req.params.id; 120 | const missionCommander = await prisma.missionCommander.findUnique({where: {id: parseInt(id)}}); 121 | res.json(missionCommander); 122 | }); 123 | 124 | app.post('/missionCommanders', async (req, res) => { 125 | const missionCommander = { 126 | name: req.body.name, 127 | username: req.body.username, 128 | mainStack: req.body.mainStack 129 | }; 130 | const message = 'Mission Commander creado.'; 131 | await prisma.missionCommander.create({data: missionCommander}); 132 | return res.json({message}); 133 | }); 134 | 135 | app.put('/missionCommanders/:id', async (req, res) => { 136 | const id = parseInt(req.params.id); 137 | 138 | await prisma.missionCommander.update({ 139 | where: { 140 | id: id 141 | }, 142 | data: { 143 | mainStack: req.body.mainStack 144 | } 145 | }) 146 | 147 | return res.json({message: "Mission Commander Actualizado correctamente"}); 148 | }); 149 | 150 | app.delete('/missionCommanders/:id', async (req, res) => { 151 | const id = parseInt(req.params.id); 152 | await prisma.missionCommander.delete({where: {id: id}}); 153 | return res.json({message: "Mission Commander eliminado correctamente"}); 154 | }); 155 | 156 | app.listen(port, () => { 157 | console.log(`Listening to requests on port ${port}`); 158 | }); --------------------------------------------------------------------------------