Skip to content
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

Feat/express add user creation route #1932

Open
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import dataSource from '../database/data-source';
await dataSource.initialize();
const args = process.argv.slice(2);

const [username, password] = args;
const [email, password] = args;

if (!username || !password) {
console.error('Usage: createUser [username] [password]');
if (!email || !password) {
console.error('Usage: createUser [email] [password]');
process.exit(1);
}

await createUser({ username, password })({ userRepository });
await createUser({ email, password })({ userRepository });
})().catch((e) => console.error(e));
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Request, Response, Router } from 'express';
import { User } from '../../domain/User';
import createUser from '../../useCases/createUser';
import listUsers from '../../useCases/listUsers';
import { userRepository } from '../database';
import { mapUser } from '../mappers';
Expand Down Expand Up @@ -30,4 +31,13 @@ router.get('/', async (req: Request, res: Response) => {
res.json(users.map(mapUser));
});

router.post('/', async (req: Request, res: Response) => {
if (req.user) {
const user = await createUser(req.body)({ userRepository });
res.send(mapUser(user));
} else {
res.sendStatus(404);
}
});

export default router;
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { User, UserRepository } from '../domain/User';

interface Arg {
username: string,
email: string,
password: string,
}

interface Context {
userRepository: UserRepository;
}

const createUser = ({ username, password }: Arg) => async ({ userRepository }: Context): Promise<User> => {
const user = new User(username);
const createUser = ({ email, password }: Arg) => async ({ userRepository }: Context): Promise<User> => {
const user = new User(email);
await user.updatePassword(password);

await userRepository.save(user);
Expand Down
Loading