diff --git a/Dockerfile b/Dockerfile index baec7ba2..c8da8c09 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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"] + diff --git a/init.sql b/init.sql index e2c36c6f..679faf26 100644 --- a/init.sql +++ b/init.sql @@ -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) +); \ No newline at end of file diff --git a/src/entity/Post.ts b/src/entity/Post.ts index a1f68038..e8a4183c 100644 --- a/src/entity/Post.ts +++ b/src/entity/Post.ts @@ -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; +} diff --git a/src/entity/User.ts b/src/entity/User.ts index a8e22632..ac7b94ff 100644 --- a/src/entity/User.ts +++ b/src/entity/User.ts @@ -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[]; +} diff --git a/src/index.ts b/src/index.ts index 1af52a84..ab2c0843 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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;