Skip to content

[Marcos Pimentel] - dev_test #96

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
#TODO Configure o Dockerfile
FROM node:18

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]
15 changes: 13 additions & 2 deletions init.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
USE test_db;

--TODO Crie a tabela de user;
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
firstName VARCHAR(100) NOT NULL,
lastName VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL
);

--TODO Crie a tabela de posts;
CREATE TABLE post (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
description VARCHAR(100) NOT NULL,
userId INT NOT NULL,
FOREIGN KEY (userId) REFERENCES user(id)
);
18 changes: 16 additions & 2 deletions src/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from "typeorm";
import { User } from "./User";

//TODO Crie a entidade de Post
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id!: number;

@Column()
title!: string;

@Column()
description!: string;

@ManyToOne(() => User, (user) => user.posts)
user!: User;
}
21 changes: 19 additions & 2 deletions src/entity/User.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { Post } from "./Post";

//TODO Crie a entidade de User
@Entity()
export class User {
@PrimaryGeneratedColumn()
id!: number;

@Column()
firstName!: string;

@Column()
lastName!: string;

@Column()
email!: string;

@OneToMany(() => Post, (post) => post.user)
posts!: Post[];
}
23 changes: 21 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ const initializeDatabase = async () => {
initializeDatabase();

app.post('/users', async (req, res) => {
// Crie o endpoint de users
try {
const user = AppDataSource.getRepository(User).create(req.body);
const result = await AppDataSource.getRepository(User).save(user);
res.status(201).json(result);
} catch (err) {
res.status(500).json({ error: "Failed to create user", details: err });
}
});

app.post('/posts', async (req, res) => {
// Crie o endpoint de posts
try {
const { title, description, userId } = req.body;

const user = await AppDataSource.getRepository(User).findOneBy({ id: userId });
if (!user) {
return res.status(404).json({ error: "User not found" });
}

const post = AppDataSource.getRepository(Post).create({ title, description, user });
const result = await AppDataSource.getRepository(Post).save(post);
res.status(201).json(result);
} catch (err) {
res.status(500).json({ error: "Failed to create post", details: err });
}
});

const PORT = process.env.PORT || 3000;
Expand Down