Skip to content

Commit

Permalink
editUser viewUser allUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavobarsan committed Feb 14, 2022
1 parent c81580e commit 3e12304
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 28 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PORT=
MONGO_DB=
HASH_KEY=
SECRET=
38 changes: 16 additions & 22 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,32 @@ import helmet from "helmet";
import cors from "cors";
import { config } from "dotenv";
import { join } from "path";
import Users from "./models/users.model";
import {
allUsers,
createUser,
editUser,
login,
viewUser,
} from "./controllers/users.controlers";

config({ path: join(__dirname, "../.env") });

const PORT = process.env.PORT;
const app = express();

app.use(cors());
app.use(helmet());
app.use(morgan("combined"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(helmet());
app.use(express.urlencoded({ extended: true }));
app.disable("x-powered-by");
const users = new Users();

// ROUTES
app.get("/", (req, res) => res.json({ message: "Virtual Wallet" }));

app.post("/users", async (req, res) => {
try {
const resp = await users.createUser(req.body);
return res.status(201).send(resp);
} catch (err: any) {
res.status(500).json({ message: "Deu ruim" });
}
});

app.get("/users", async (req, res) => {
try {
const resp = await users.allUsers();
return res.status(200).send(resp);
} catch (error) {
res.status(500).json({ message: "Deu ruim" });
}
});
app.post("/users", createUser);
app.patch("/users/:id", editUser);
app.post("/login", login);
app.get("/users/:id", viewUser);
app.get("/users", allUsers)

app.listen(PORT, () => console.log(`Running on <http://localhost:${PORT}>`));
46 changes: 42 additions & 4 deletions src/controllers/users.controlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,59 @@ export const createUser = async (req: Request, res: Response) => {
}
};

export const editUser = async (req: Request, res: Response) => {
try {
const updatedUser = await users.updateUser(req.params.id, req.body);
console.log(updatedUser);
delete updatedUser.value.password;
delete updatedUser.value._id;
return res.status(200).send(updatedUser.value);
} catch (error) {
res.status(500).json(error);
}
};

export const viewUser = async (req: Request, res: Response) => {
try {
const user = await users.findUser(req.params.id);
delete user.password;
return res.status(200).send(user);
} catch (error) {
res.status(500).json(error);
}
};

export const allUsers = async (req: Request, res: Response) => {
try {
const resp = await users.filterUser({});

return res.status(200).send(
resp.map((user) => {
delete user.password;
return user;
})
);
} catch (error) {
res.status(500).json(error);
}
};

export const login = async (req: Request, res: Response) => {
try {
const auth = await users.filterUser({
email: req.body.email,
password: hashPassword(req.body.password),
});

if (length > 0) {
if (auth.length > 0) {
const token = jwt.sign({ id: auth[0]._id }, SECRET, {
algorithm: "RS256",
expiresIn: 300, // expires /s
});
return res.status(200).send(token);
await users.updateUser(auth[0]._id, { isLogged: true });
return res.status(200).send({ token: token });
}

return res.status(400).json({ message: "Campos inválidos" });
return res.status(400).json({ message: "Úsuario ou senha inválidos" });
} catch (error: any) {
res.status(500).json(error);
}
Expand Down
1 change: 1 addition & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface IQueryUsers {
password?: string;
cpf?: string;
tel?: string;
isLogged?: boolean;
}
3 changes: 1 addition & 2 deletions src/models/users.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ class Users {

async updateUser(userId: string, body: IQueryUsers) {
try {
const user = await cursor
return await cursor
.collection("users")
.findOneAndUpdate({ _id: ObjectId(userId) }, { $set: body });
return user;
} catch (error: any) {
console.error(error);
throw new Error(error.message);
Expand Down

0 comments on commit 3e12304

Please sign in to comment.