1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 03:29:37 +02:00

Backend: auth for bookmarks and categories

This commit is contained in:
Paweł Malak 2021-11-11 16:43:00 +01:00
parent 22471d64c7
commit 0d36c5cf94
6 changed files with 39 additions and 12 deletions

View file

@ -6,8 +6,12 @@ const { Sequelize } = require('sequelize');
// @route GET /api/bookmarks
// @access Public
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
// bookmarks visibility
const where = req.isAuthenticated ? {} : { isPublic: true };
const bookmarks = await Bookmark.findAll({
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
where,
});
res.status(200).json({

View file

@ -6,8 +6,10 @@ const Bookmark = require('../../models/Bookmark');
// @route GET /api/bookmarks/:id
// @access Public
const getSingleBookmark = asyncWrapper(async (req, res, next) => {
const visibility = req.isAuthenticated ? {} : { isPublic: true };
const bookmark = await Bookmark.findOne({
where: { id: req.params.id },
where: { id: req.params.id, ...visibility },
});
if (!bookmark) {

View file

@ -12,15 +12,20 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
let categories;
// categories visibility
const where = req.isAuthenticated ? {} : { isPublic: true };
if (orderType == 'name') {
categories = await Category.findAll({
include: [
{
model: Bookmark,
as: 'bookmarks',
where,
},
],
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
where,
});
} else {
categories = await Category.findAll({
@ -28,9 +33,11 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
{
model: Bookmark,
as: 'bookmarks',
where,
},
],
order: [[orderType, 'ASC']],
where,
});
}

View file

@ -7,12 +7,15 @@ const Bookmark = require('../../models/Bookmark');
// @route GET /api/categories/:id
// @access Public
const getSingleCategory = asyncWrapper(async (req, res, next) => {
const visibility = req.isAuthenticated ? {} : { isPublic: true };
const category = await Category.findOne({
where: { id: req.params.id },
where: { id: req.params.id, ...visibility },
include: [
{
model: Bookmark,
as: 'bookmarks',
where: visibility,
},
],
});