-
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
8f927b1
commit 5c3e053
Showing
9 changed files
with
63 additions
and
49 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Request, Response } from "express"; | ||
import { UserRepository } from "../repositories/UserRepository"; | ||
|
||
export class UserController { | ||
static async create(req: Request, res: Response): Promise<Response> { | ||
const { name, email, password } = req.body; | ||
|
||
if (!name || !email || !password) { | ||
return res.status(400).json({ message: "Missing required fields" }); | ||
} | ||
|
||
const userAlreadyExists = await UserRepository.findOne({where: {email}}); | ||
|
||
if(userAlreadyExists) | ||
throw new Error('Email address already used!'); | ||
|
||
try { | ||
const user = UserRepository.create({name,email,password}); | ||
const userSave = await UserRepository.save(user); | ||
return res.status(201).json(userSave); | ||
} catch (error) { | ||
return res.status(500).json({ message: "Error saving user", error }); | ||
} | ||
} | ||
} |
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,5 @@ | ||
import { Repository } from "typeorm"; | ||
import { User } from "../models/User"; | ||
import { AppDataSource } from "../database"; | ||
|
||
export const UserRepository: Repository<User> = AppDataSource.getRepository(User); |
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,8 +1,9 @@ | ||
import { Router } from "express"; | ||
import { milistoneRouter } from "./milistone.routes"; | ||
import { usersRouter } from "./user.routes"; | ||
|
||
const router = Router(); | ||
|
||
router.use('/milistones',milistoneRouter); | ||
|
||
router.use("/users", usersRouter); | ||
export {router} |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from "express"; | ||
import { UserController } from "../controllers/UserController"; | ||
|
||
const usersRouter = Router(); | ||
|
||
usersRouter.post("/", UserController.create); | ||
|
||
export { usersRouter }; |
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 was deleted.
Oops, something went wrong.