Skip to content
Snippets Groups Projects
Commit cd435495 authored by AF7626's avatar AF7626
Browse files

started server test 1

parent 1cac5878
No related branches found
No related tags found
No related merge requests found
......@@ -69,3 +69,10 @@
## 13. Emergency Assistance
- Users can contact emergency services or request emergency medical assistance directly from the app in case of critical situations.
User Management
POST /users/register → Register new users (patients, doctors, admins)
POST /users/login → Authenticate users
GET /users/:id → Get user details
PUT /users/:id → Update user profile
import app from './app.js';
Deno.serve(app.fetch);
\ No newline at end of file
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
import * as userController from "./userController.js";
const app = new Hono();
app.get("/v1/users", userController.showForm);
app.get("/v1/users/:id", userController.showUser);
app.post("/v1/users", userController.createUser);
app.post("/v1/users/:id", userController.updateUser);
app.post("/v1/users/:id/delete", userController.deleteUser);
export default app;
{
"name": "backend",
"version": "1.0.0",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"bcrypt": "^5.1.1",
"cors": "^2.8.5",
"dotenv": "^16.4.7",
"express": "^4.21.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.1.9",
"validator": "^13.12.0"
}
}
import express from 'express';
import cors from 'cors';
import 'dotenv/config';
const app = express();
const port = process.env.port || 3000;
// middlewares
app.use(express.json());
app.use(cors());
//api endpoints
app.get("/", (req,res) => {
res.send("Hello its time to ride");
})
app.listen(port, () => {
console.log("server is running",port);
})
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Users</title>
</head>
<body>
<form method="POST" action="/v1/users/<%= it.user.id %>">
<label for="name">Name:</label>
<input type="text" id="name" name="name" value="<%= it.user.name %>" /><br/>
<label for="email">Email:</label>
<input type="email" id="email" name="email" value="<%= it.user.email %>" /><br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" value="<%= it.user.isbn %>" /><br/>
<input type="submit" value="Update" />
</form>
<p><a href="/v1/users">Back to users</a></p>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>Register User</h2>
<form method="POST" action="/v1/users">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required /><br/>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required /><br/>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required /><br/>
<input type="submit" value="Register" />
</form>
<ul>
<h2>Existing Users</h2>
<ul>
<% it.users.forEach((user) => { %>
<li>
<a href="/v1/users/<%= user.id %>"><%= user.name %> (<%= user.email %>)</a>
<form method="POST" action="/v1/users/<%= user.id %>/delete" style="display:inline;">
<input type="submit" value="Delete" />
</form>
</li>
<% }); %>
</ul>
</body>
</html>
\ No newline at end of file
import { Eta } from "https://deno.land/x/eta@v3.4.0/src/index.ts";
import * as userService from "./userService.js";
const eta = new Eta({ views: `${Deno.cwd()}/templates/` });
const showForm = async (c) => {
return c.html(
eta.render("users.eta", { users: await userService.listUsers() }),
);
};
const createUser = async (c) => {
const body = await c.req.parseBody();
await userService.createUser(body);
return c.redirect("/v1/users");
};
const showUser = async (c) => {
const id = c.req.param("id");
return c.html(
eta.render("user.eta", { user: await userService.getUser(id) }),
);
};
const updateUser = async (c) => {
const id = c.req.param("id");
const body = await c.req.parseBody();
await userService.updateUser(id, body);
return c.redirect(`/v1/users/${id}`);
};
const deleteUser = async (c) => {
const id = c.req.param("id");
await userService.deleteUser(id);
return c.redirect("/v1/users");
}
export { createUser, showForm, showUser, updateUser,deleteUser };
\ No newline at end of file
const createUser = async (user) => {
user.id = crypto.randomUUID();
const kv = await Deno.openKv();
await kv.set(["users", user.id], user);
};
const listUsers = async () => {
const kv = await Deno.openKv();
const userEntries = await kv.list({ prefix: ["users"] });
const users = [];
for await (const entry of userEntries) {
users.push(entry.value);
}
return users;
};
const getUser = async (id) => {
const kv = await Deno.openKv();
const user = await kv.get(["users", id]);
return user?.value ?? {};
};
const updateUser = async (id, user) => {
user.id = id;
const kv = await Deno.openKv();
await kv.set(["users", id], user);
};
const deleteUser = async (id) => {
const kv = await Deno.openKv();
await kv.delete(["users",id]);
}
export {createUser,listUsers,getUser,updateUser,deleteUser}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment