Skip to content

Solução do teste para a Vaga de Desenvolvedor Backend Júnior #104

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 1 commit 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
25 changes: 24 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
#TODO Configure o Dockerfile
# Use the official Node.js LTS image
FROM node:18-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code
COPY . .

# Build the TypeScript code
RUN npm run build

# Expose the application port
EXPOSE 3000

# Define the command to run the application
CMD ["node", "dist/index.js"]

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({ length: 100 })
title!: string;

@Column({ length: 100 })
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({ length: 100 })
firstName!: string;

@Column({ length: 100 })
lastName!: string;

@Column({ length: 100 })
email!: string;

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

app.post('/users', async (req, res) => {
// Crie o endpoint de users
});
try {
const { firstName, lastName, email } = req.body;

// Basic validation
if (!firstName || !lastName || !email) {
return res.status(400).json({ message: 'Please provide firstName, lastName, and email' });
}

const user = new User();
user.firstName = firstName;
user.lastName = lastName;
user.email = email;

// Save the user to the database
await AppDataSource.manager.save(user);

return res.status(201).json(user);
} catch (error) {
console.error('Error creating user:', error);
return res.status(500).json({ message: 'Internal server error' });
}});

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

// Basic validation
if (!title || !description || !userId) {
return res.status(400).json({ message: 'Please provide title, description, and userId' });
}

// Check if the user exists
const user = await AppDataSource.manager.findOneBy(User, { id: userId });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}

const post = new Post();
post.title = title;
post.description = description;
post.user = user; // Associate the post with the user

// Save the post to the database
await AppDataSource.manager.save(post);

return res.status(201).json(post);
} catch (error) {
console.error('Error creating post:', error);
return res.status(500).json({ message: 'Internal server error' });
}
});

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