-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ff0ae4
commit 10ec296
Showing
48 changed files
with
611 additions
and
2,151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Use uma imagem Node.js como base | ||
FROM node:alpine | ||
|
||
# Defina o diretório de trabalho no contêiner | ||
WORKDIR /usr/src/app/backend/portfolio | ||
|
||
# Copie o conteúdo do diretório para dentro do contêiner | ||
COPY . . | ||
|
||
# Instale as dependências do aplicativo | ||
RUN npm install | ||
|
||
# Instale o nodemon globalmente | ||
RUN npm install -g nodemon | ||
|
||
# Instale o ts-node globalmente | ||
RUN npm install -g ts-node | ||
|
||
# Exponha a porta 4001 do contêiner (se necessário) | ||
EXPOSE 4001 | ||
|
||
# Comando para iniciar o aplicativo quando o contêiner for iniciado | ||
CMD ["npm", "start"] |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,131 +1,116 @@ | ||
import { Request, Response } from "express" | ||
import prisma from "../libs/prisma" | ||
import { Request, Response } from "express"; | ||
import prisma from "../libs/prisma"; | ||
import path from "path"; | ||
|
||
//Get list all Posts | ||
export const getAllPosts = async (req:Request, res:Response) =>{ | ||
// Get list all Posts | ||
export const getAllPosts = async (req: Request, res: Response) => { | ||
try { | ||
const posts = await prisma.post.findMany({ orderBy: { date: 'desc' }}) | ||
if(posts.length === 0) { | ||
return res | ||
.status(404) | ||
.json({error: 'Não a nenhum post cadastrado'}) | ||
const posts = await prisma.post.findMany({ orderBy: { date: "desc" } }); | ||
if (posts.length === 0) { | ||
return res.status(404).json({ error: "Não há nenhum post cadastrado" }); | ||
} | ||
return res.json({success: true, posts}) | ||
return res.json({ success: true, posts }); | ||
} catch (error) { | ||
console.log(error) | ||
return res.status(500).json({ server: 'Erro interno', error }) | ||
console.log(error); | ||
return res.status(500).json({ server: "Erro interno", error }); | ||
} | ||
} | ||
|
||
}; | ||
|
||
//Get all posts in the category | ||
// Get all posts in the category | ||
export const getPostsByCategory = async (req: Request, res: Response) => { | ||
try { | ||
const { name } = req.params | ||
const { name } = req.params; | ||
|
||
const category = await prisma.category.findUnique({ | ||
where: { | ||
name | ||
} | ||
}) | ||
name, | ||
}, | ||
}); | ||
|
||
if (!category) { | ||
return res.status(404).json({ error: 'Categoria não encontrada' }) | ||
return res.status(404).json({ error: "Categoria não encontrada" }); | ||
} | ||
|
||
const posts = await prisma.post.findMany({ | ||
where: { | ||
categoryId: category.id | ||
categoryId: category.id, | ||
}, | ||
orderBy: { date: 'desc' } | ||
}) | ||
orderBy: { date: "desc" }, | ||
}); | ||
|
||
if (posts.length === 0) { | ||
return res.status(404).json({ error: 'Não há nenhum post cadastrado para esta categoria' }) | ||
return res | ||
.status(404) | ||
.json({ error: "Não há nenhum post cadastrado para esta categoria" }); | ||
} | ||
|
||
return res.json({ success: true, posts }) | ||
return res.json({ success: true, posts }); | ||
} catch (error) { | ||
console.error(error) | ||
return res.status(500).json({ server: 'Erro interno', error }) | ||
} finally { | ||
await prisma.$disconnect() | ||
console.error(error); | ||
return res.status(500).json({ server: "Erro interno", error }); | ||
} | ||
} | ||
|
||
}; | ||
|
||
//Get Post in category | ||
// Get Post in category | ||
export const getPostInCategory = async (req: Request, res: Response) => { | ||
try { | ||
const { name, slug } = req.params | ||
const { name, slug } = req.params; | ||
const category = await prisma.category.findUnique({ | ||
where: { | ||
name: name, | ||
}, | ||
}) | ||
}); | ||
if (!category) { | ||
return res.status(404).json({ error: 'Categoria não encontrada' }) | ||
return res.status(404).json({ error: "Categoria não encontrada" }); | ||
} | ||
|
||
const post = await prisma.post.findUnique({ | ||
where: { | ||
slug: slug, | ||
categoryId: category.id, | ||
}, | ||
}) | ||
}); | ||
if (!post) { | ||
return res.status(404).json({ error: 'Post não encontrado nesta categoria' }) | ||
return res | ||
.status(404) | ||
.json({ error: "Post não encontrado nesta categoria" }); | ||
} | ||
return res.json({ success: true, post }) | ||
return res.json({ success: true, post }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ server: 'Erro interno', error }) | ||
} finally { | ||
await prisma.$disconnect() | ||
return res.status(500).json({ server: "Erro interno", error }); | ||
} | ||
} | ||
}; | ||
|
||
export const newPost = async (req: Request, res: Response) => { | ||
interface UploadedFile { | ||
fieldname: string; | ||
originalname: string; | ||
encoding: string; | ||
mimetype: string; | ||
destination: string; | ||
filename: string; | ||
path: string; | ||
size: number; | ||
} | ||
//Accpts only image | ||
if(req.file){ | ||
const file = req.file as UploadedFile | ||
}else{ | ||
res.status(404).json({error: 'File not sent'}) | ||
return | ||
} | ||
//New Post | ||
try { | ||
const { title, slug, content, body, url, author, categoryId } = req.body | ||
const photo: string = `${process.env.BASE}/${(req.file as Express.MulterS3.File)?.key}`; | ||
if(!title || !slug || !content || !body || !author || !categoryId|| !photo) { | ||
return res.json({Sever: 'Todos os campos são obrigatorios'}) | ||
}else{ | ||
try { | ||
// Verifica se o arquivo foi enviado | ||
if (!req.file) { | ||
return res.status(400).json({ error: "File not sent" }); | ||
} | ||
|
||
const { title, slug, content, body, author, categoryId } = req.body; | ||
|
||
const photoPath = req.file.path; // Caminho do arquivo local | ||
|
||
if (!title || !slug || !content || !body || !author || !categoryId || !photoPath) { | ||
return res.json({ Sever: "Todos os campos são obrigatórios" }); | ||
} else { | ||
await prisma.post.create({ | ||
data: { | ||
title, | ||
slug, | ||
url: photo, | ||
url: photoPath, // Atualiza o caminho do arquivo para o caminho local | ||
content, | ||
body, | ||
author, | ||
categoryId: parseInt(categoryId, 10), | ||
}, | ||
}) | ||
return res.json({ success: 'Post criado com sucesso' }); | ||
}); | ||
return res.json({ success: "Post criado com sucesso" }); | ||
} | ||
} catch (error) { | ||
console.log('Erro ao criar post:', error) | ||
return res.status(500).json({ Server: 'Erro interno do servidor' }) | ||
} | ||
|
||
} | ||
} catch (error) { | ||
console.log("Erro ao criar post:", error); | ||
return res.status(500).json({ Server: "Erro interno do servidor" }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,27 @@ | ||
import { Router, Request, Response } from 'express' | ||
import * as posts from '../controller/posts.controller' | ||
import multer, { Multer } from 'multer' | ||
import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3' | ||
import multerS3 from 'multer-s3' | ||
import { Auth } from '../middleware/auth' | ||
import { Router } from 'express'; | ||
import * as posts from '../controller/posts.controller'; | ||
import multer, { Multer } from 'multer'; | ||
import path from 'path'; | ||
|
||
|
||
const s3Client = new S3Client({ | ||
region: process.env.AWS_REGION || 'sa-east-1', | ||
credentials: { | ||
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '', | ||
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '', | ||
const storage = multer.diskStorage({ | ||
destination: function (req, file, cb) { | ||
cb(null, 'public/blog'); // Define a pasta de destino como 'public/blog' | ||
}, | ||
filename: function (req, file, cb) { | ||
const ext = path.extname(file.originalname); | ||
cb(null, file.fieldname + '-' + Date.now() + ext); // Define o nome do arquivo | ||
}, | ||
}) | ||
}); | ||
|
||
const upload: Multer = multer({ | ||
storage: multerS3({ | ||
s3: s3Client, | ||
bucket: process.env.BUCKET_NAME || '', | ||
acl: 'public-read', | ||
contentType: multerS3.AUTO_CONTENT_TYPE, | ||
key: (req: Request, file, cb) => { | ||
console.log('File Object:', file) | ||
let randomNamePhoto = Math.floor(Math.random() * 9999999); | ||
const originalname = file.originalname || 'default' | ||
cb(null, 'uploads/' + originalname + randomNamePhoto + Date.now() + '.jpg') | ||
}, | ||
}), | ||
fileFilter: (req: Request, file: Express.Multer.File, cb) => { | ||
const allowed: string[] = ['image/jpg', 'image/jpeg', 'image/png'] | ||
if (allowed.includes(file.mimetype)) { | ||
cb(null, true) | ||
} else { | ||
cb(new Error('Invalid file type')) | ||
} | ||
} | ||
}) | ||
const upload: Multer = multer({ storage: storage }); | ||
|
||
const router = Router() | ||
const router = Router(); | ||
|
||
router.get('/posts', posts.getAllPosts) | ||
router.get('/posts/:name', posts.getPostsByCategory) | ||
router.get('/posts/:name/:slug', posts.getPostInCategory) | ||
router.get('/posts', posts.getAllPosts); | ||
router.get('/posts/:name', posts.getPostsByCategory); | ||
router.get('/posts/:name/:slug', posts.getPostInCategory); | ||
|
||
router.post('/admin/newpost', upload.single('photo'), posts.newPost) | ||
// Altera o endpoint para /admin/newpost e utiliza o middleware de upload | ||
router.post('/admin/newpost', upload.single('photo'), posts.newPost); | ||
|
||
export default router; | ||
export default router; |
Oops, something went wrong.