Skip to content

Commit

Permalink
Add user watch history endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
n1klaus committed May 14, 2023
1 parent db18215 commit 951b62b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
25 changes: 17 additions & 8 deletions backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { exclude } from './auth.controller';
class UserController {
static async getMeHandler(req: Request, res: Response, next: NextFunction) {
try {
const user = exclude(res.locals.user, ['password']);
const { user } = res.locals;
return res.status(200).json({
status: 'success',
data: {
user: exclude(user, ['password']),
user: exclude(user, ['subcriptions', 'history']),
},
});
} catch (err: any) {
Expand All @@ -22,7 +22,6 @@ class UserController {

static async updateUser(req: Request, resp: Response, next: NextFunction) {
const userId = String(resp.locals.user._id);
console.log(userId);
if (req.params.id === userId) {
try {
const updatedUser = await User.findByIdAndUpdate(
Expand Down Expand Up @@ -69,10 +68,10 @@ class UserController {
const userId = String(resp.locals.user._id);
try {
await User.findByIdAndUpdate(userId, {
$push: { subscriptions: req.params.channelId },
$addToSet: { subscriptions: req.params.channelId },
});
await Channel.findByIdAndUpdate(req.params.channelId, {
$inc: { subscribers: 1 },
$addToSet: { subscribers: userId },
});
return resp.status(200).json('Subscription successfull.');
} catch (err) {
Expand All @@ -87,7 +86,7 @@ class UserController {
$pull: { subscriptions: req.params.channelId },
});
await Channel.findByIdAndUpdate(req.params.channelId, {
$inc: { subscribers: -1 },
$pull: { subscribers: userId },
});
return resp.status(200).json('Unsubscription successfull.');
} catch (err) {
Expand All @@ -97,7 +96,6 @@ class UserController {

static async like(req: Request, resp: Response, next: NextFunction) {
const userId = String(resp.locals.user._id);
console.log(userId);
const { videoId } = req.params;
try {
await Video.findByIdAndUpdate(videoId, {
Expand Down Expand Up @@ -136,7 +134,6 @@ class UserController {
return next(createError(403, 'You are not logged in!'));
}
const subscribedChannels = user.subscriptions;

const list = await Promise.all(
subscribedChannels.map(async (channelId: any) => Channel.findById(channelId)),
);
Expand All @@ -149,6 +146,18 @@ class UserController {
}
}

static async addHistory(req: Request, resp: Response, next: NextFunction) {
const userId = String(resp.locals.user._id);
try {
await User.findByIdAndUpdate(String(userId), {
$addToSet: { history: req.params.videoId },
});
return resp.status(200).json('The video has been added to history.');
} catch (err) {
return next(err);
}
}

static async getHistory(req: Request, resp: Response, next: NextFunction) {
try {
const userId = String(resp.locals.user._id);
Expand Down
12 changes: 10 additions & 2 deletions backend/src/routes/user.route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import express from 'express';
import UserController from '../controllers/user.controller';
import { getAuthToken } from '../middleware/getAuthToken';
import { requireLogin } from '../middleware/requireLogin';
import getAuthToken from '../middleware/getAuthToken';
import requireLogin from '../middleware/requireLogin';

const router = express.Router();

Expand Down Expand Up @@ -57,6 +57,14 @@ router.get(
UserController.getSubscriptions,
); // GET /users/:id/subscriptions

// Add a video to history
router.put(
'/history/:videoId',
getAuthToken,
requireLogin,
UserController.addHistory,
); // PUT /users/history/:videoId

// Get user watched views history
router.get(
'/:id/history',
Expand Down

0 comments on commit 951b62b

Please sign in to comment.