diff --git a/README.md b/README.md new file mode 100644 index 0000000..021bf68 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# CineAllo +## Setup +### Env file +- DATABASE_URL +- MYSQL_DATABASE +- MYSQL_ROOT_PASSWORD +- CINEALLO_PORT + +``DATABASE_URL`` shoud look like this: +```html +mysql://root:@db:3306/ +``` +## Usage +### Docker +```bash +docker-compose up --build +``` +### From scratch +You'll need your own MySql DB running. +```bash +npm ci +``` +```bash +npm run start +``` \ No newline at end of file diff --git a/app.js b/app.js index c2826dc..3515702 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,19 @@ import express from 'express' import logger from 'morgan' import * as dotenv from 'dotenv' -import { indexRouter } from './routes/index.js' -import { usersRouter } from './routes/users.js' +import { showsRouter } from './routes/shows.js' dotenv.config() const app = express() -const port = process.env.CINEALLO_PORT +const port = 3000 app.use(logger('dev')) app.use(express.json()) app.use(express.urlencoded({ extended: false })) -app.use('/', indexRouter) -app.use('/users', usersRouter) +app.get('/', async (req, res, next) => { + res.send({message:"voila ton index"}) +}) +app.use('/shows', showsRouter) app.listen(port, () => console.log(`Listening on port ${port}`)) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index b0b5a9c..ff7fcae 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,4 @@ services: - backend: - build: . - image: cineallo-backend - env_file: - - .env - networks: - - back-net - ports: - - 3000:3000 db: image: mysql:8.0.31 env_file: @@ -16,9 +7,26 @@ services: - back-net environment: - "MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}" - - "MYSQL_DATABASE=cineallo" + - "MYSQL_DATABASE=${MYSQL_DATABASE}" volumes: - ./mysql-data:/var/lib/mysql + healthcheck: + test: [ "CMD", "curl", "-f", "http://localhost:3306" ] + interval: 3s + timeout: 30s + retries: 10 + backend: + build: . + image: cineallo-backend + depends_on: + db: + condition: service_healthy + env_file: + - .env + networks: + - back-net + ports: + - "${CINEALLO_PORT}:3000" networks: - back-net: \ No newline at end of file + back-net: diff --git a/package.json b/package.json index e1e91b6..1c369b2 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "private": true, "type": "module", "scripts": { - "start": "node app.js" + "start": "npx prisma migrate deploy && node app.js", + "dev": "node app.js" }, "dependencies": { "@prisma/client": "^4.6.0", diff --git a/prisma/migrations/20221110100453_/migration.sql b/prisma/migrations/20221110100453_/migration.sql new file mode 100644 index 0000000..ab83a49 --- /dev/null +++ b/prisma/migrations/20221110100453_/migration.sql @@ -0,0 +1,18 @@ +/* + Warnings: + + - You are about to drop the `Article` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropTable +DROP TABLE `Article`; + +-- CreateTable +CREATE TABLE `Serie` ( + `title` VARCHAR(191) NOT NULL, + `date` INTEGER NOT NULL, + `description` VARCHAR(2000) NOT NULL, + `thumbmailURL` VARCHAR(2048) NOT NULL, + + PRIMARY KEY (`title`) +) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; diff --git a/prisma/migrations/20221110122130_/migration.sql b/prisma/migrations/20221110122130_/migration.sql new file mode 100644 index 0000000..64cd81a --- /dev/null +++ b/prisma/migrations/20221110122130_/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE `Serie` ADD COLUMN `likes` INTEGER NOT NULL DEFAULT 0; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 5929a04..49bc9c2 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -2,15 +2,18 @@ // learn more about it in the docs: https://pris.ly/d/prisma-schema generator client { - provider = "prisma-client-js" + provider = "prisma-client-js" } datasource db { - provider = "mysql" - url = env("DATABASE_URL") + provider = "mysql" + url = env("DATABASE_URL") } -model Article { - id Int @id @default(autoincrement()) - title String +model Serie { + title String @id + date Int + description String @db.VarChar(2000) + thumbmailURL String @db.VarChar(2048) + likes Int @default(0) } diff --git a/routes/index.js b/routes/index.js deleted file mode 100644 index 398059c..0000000 --- a/routes/index.js +++ /dev/null @@ -1,17 +0,0 @@ -import express from 'express' -import { PrismaClient } from '@prisma/client' - -const prisma = new PrismaClient() -export const indexRouter = express.Router() - -indexRouter.get('/', async (req, res, next) => { - res.send({message:"voila ton index"}) -}) - -indexRouter.get('/articles', async (req, res, next) => { - const articles = await prisma.article.findMany({}) - res.send({ - message:"voila tes articles", - articles - }) -}) diff --git a/routes/shows.js b/routes/shows.js new file mode 100644 index 0000000..1081df1 --- /dev/null +++ b/routes/shows.js @@ -0,0 +1,61 @@ +import express from 'express' +import { Prisma, PrismaClient } from '@prisma/client' +import createHttpError from 'http-errors' + +const prisma = new PrismaClient() +export const showsRouter = express.Router() + +showsRouter.get('/', function (req, res, next) { + prisma.serie.findMany({ + orderBy: { + likes: 'desc' + } + }) + .then(shows => res.send({ shows })) +}) + +showsRouter.get('/:id', function (req, res, next) { + prisma.serie.findUniqueOrThrow({ + where: { + title: req.params.id + } + }) + .then(show => res.send({ show })) + .catch(error => { + if (error instanceof Prisma.NotFoundError) { + next(createHttpError(404, 'Show not found.')) + } + }) +}) + +showsRouter.post('/', function (req, res, next) { + prisma.serie.create({ + data: { + title: req.body.title, + date: req.body.date, + description: req.body.description, + thumbmailURL: req.body.thumbmailURL + } + }) + .then(() => res.send({ message: 'Show posted!' })) + .catch(() => { + next(createHttpError(400, 'Check your show model/content.')) + }) +}) + +showsRouter.patch('/like/:id', function (req, res, next) { + prisma.serie.update({ + where: { + title: req.params.id + }, + data: { + likes: { + increment: 1 + } + } + }) + .then(() => res.send({ message: 'Show liked!' })) + .catch(() => { + next(createHttpError(400, 'Show not found.')) + }) +}) \ No newline at end of file diff --git a/routes/users.js b/routes/users.js deleted file mode 100644 index a33aafb..0000000 --- a/routes/users.js +++ /dev/null @@ -1,7 +0,0 @@ -import express from 'express' - -export const usersRouter = express.Router() - -usersRouter.get('/', function(req, res, next) { - res.send({message:"voila tes users"}) -})