Skip to content

Commit

Permalink
fix minor bugs in codes
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneleon1 committed May 6, 2024
1 parent df42765 commit 8b1b667
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import route from './router';
import fs from 'fs';
import path from 'path';
import authRoutes from './routes/auth-routes';
import profileRoutes from './routes/profile-routes';
import cookieSession from 'cookie-session';
import passport from 'passport';

Expand Down Expand Up @@ -76,7 +75,6 @@ app.use('/api/v1', route);

// Endpoints for serving social login
app.use('/auth', authRoutes);
app.use('/profile', profileRoutes);

// Endpoint for serving Swagger documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
Expand Down
31 changes: 30 additions & 1 deletion src/controller/user.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { use } from 'passport';
import { Request, Response } from 'express';
import dbConnection from '../database';
import { UserSocial, User } from '../database/models';

Expand All @@ -22,3 +22,32 @@ export const createUser = async (data: UserData) => {
export const allUsers = async () => {
return await userSocialRepository.find();
};

export const deleteUser = async (req: Request, res: Response) => {
try {
// Extract the ID from the request parameters
const id: number = parseInt(req.params.id);

// Find the entity by ID
const entityToDelete = await userSocialRepository.findOne({
where: { id },
});

// If entity not found, send 404 response
if (!entityToDelete) {
return res.status(404).json({ error: 'Entity not found.' });
}

// Delete the entity
await userSocialRepository.remove(entityToDelete);

// Send success response
return res.status(200).json({ message: 'Entity deleted successfully.' });
} catch (error) {
// If an error occurs, send error response
console.error('Error deleting entity:', error);
return res
.status(500)
.json({ error: 'An error occurred while deleting the entity.' });
}
};
5 changes: 4 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Router, Request, Response } from 'express';
import { createUser, allUsers } from './controller/user';
import { createUser, allUsers, deleteUser } from './controller/user';
const route = Router();

route.post('/users', async (req: Request, res: Response) => {
Expand All @@ -9,4 +9,7 @@ route.get('/users', async (req: Request, res: Response) => {
return res.status(200).json({ data: await allUsers() });
});

// Define the route for deleting a record by ID
route.delete('/delete/:id',deleteUser);

export default route;
7 changes: 3 additions & 4 deletions src/routes/auth-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ authRoutes.get(
'/google/redirect',
passport.authenticate('google'),
(req: Request, res: Response) => {
res.redirect('/profile/');
// res.send(req.user);
res.send(req.user);
}
);

Expand All @@ -35,8 +34,8 @@ authRoutes.get(

// auth Logout
authRoutes.get('/logout', async (req: Request, res: Response) => {
// handle with passport
return res.status(200).send('logging out');
req.logout;
res.redirect('/login');
});

export default authRoutes;
10 changes: 0 additions & 10 deletions src/routes/profile-routes.ts

This file was deleted.

0 comments on commit 8b1b667

Please sign in to comment.