Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

course for community #429

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
240 changes: 162 additions & 78 deletions api/src/controllers/communityController.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const getCommunities = async (req, res) => {
// const order = req.query.order || 'DESC'
// const ordervalue = order && [['name', order]]
try {
// const userId = req.user.id
const communities = await db.Community.findAndCountAll({
offset: (page - 1) * pageSize,
limit: pageSize,
Expand All @@ -40,11 +41,12 @@ const getCommunities = async (req, res) => {
CASE WHEN "creatorId"=${req.user.id} THEN 'true'
ELSE 'false'
END
`), 'isCreator'
`),
'isCreator'
],
[
sequelize.literal(`(
SELECT COUNT("userId")
SELECT COUNT("userId")
FROM communities_users
WHERE "communityId" = communities.id AND active = true AND "userId" = ${req.user.id}
)`),
Expand All @@ -55,13 +57,15 @@ const getCommunities = async (req, res) => {
}
})
const totalPages = Math.ceil(communities.count / pageSize)
res.json({
communities: communities.rows.map(rec => ({ ...rec.dataValues })),
totalItems: communities.count,
totalPages,
page,
pageSize
}).status(200)
res
.json({
communities: communities.rows.map((rec) => ({ ...rec.dataValues })),
totalItems: communities.count,
totalPages,
page,
pageSize
})
.status(200)
} catch (error) {
res.json(error)
}
Expand Down Expand Up @@ -96,11 +100,12 @@ const getUserCommunities = async (req, res) => {
CASE WHEN "creatorId"=${req.user.id} THEN 'true'
ELSE 'false'
END
`), 'isCreator'
`),
'isCreator'
],
[
sequelize.literal(`(
SELECT COUNT("userId")
SELECT COUNT("userId")
FROM communities_users
WHERE "communityId" = communities.id AND active = true AND "userId" = ${req.user.id}
)`),
Expand All @@ -111,23 +116,30 @@ const getUserCommunities = async (req, res) => {
},
order: [['createdAt', 'DESC']],
where: { deleted: false },
include: [{
model: db.User,
as: 'followers',
attributes: [],
where: { id: req.user.id },
through: { attributes: [] }
}]
include: [
{
model: db.User,
as: 'followers',
attributes: [],
where: { id: req.user.id },
through: { attributes: [] }
}
]
})
.then(communities => {
.then((communities) => {
const totalPages = Math.ceil(communities.count / pageSize)
res.json({
communities: communities.rows.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) })),
totalItems: communities.count,
totalPages,
page,
pageSize
}).status(200)
res
.json({
communities: communities.rows.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
})),
totalItems: communities.count,
totalPages,
page,
pageSize
})
.status(200)
})
.catch((err) => res.json({ err }).status(400))
}
Expand All @@ -147,8 +159,19 @@ const createCommunity = async (req, res) => {
// auto follow through transactions
if (req.body.auto_follow === 'true') {
const result = await sequelize.transaction(async (t) => {
const community = await db.Community.create({ ...req.body, creatorId: req.user.id, slug: '', attachment: 'community/' + filename }, { transaction: t })
const idArrays = await db.User.findAll({ attributes: ['id'] }, { transaction: t })
const community = await db.Community.create(
{
...req.body,
creatorId: req.user.id,
slug: '',
attachment: 'community/' + filename
},
{ transaction: t }
)
const idArrays = await db.User.findAll(
{ attributes: ['id'] },
{ transaction: t }
)
const allFollow = []

for (let i = 0; i < idArrays.length; i++) {
Expand All @@ -166,11 +189,24 @@ const createCommunity = async (req, res) => {
return res.json({ message: result })
} else {
const followCommunity = await sequelize.transaction(async (t) => {
const community = await db.Community.create({ ...req.body, creatorId: req.user.id, slug: '', attachment: 'community/' + filename }, { transaction: t })
await db.CommunityUser.create({ userId: req.user.id, communityId: community.id }, { transaction: t })
const community = await db.Community.create(
{
...req.body,
creatorId: req.user.id,
slug: '',
attachment: 'community/' + filename
},
{ transaction: t }
)
await db.CommunityUser.create(
{ userId: req.user.id, communityId: community.id },
{ transaction: t }
)
return true
})
if (followCommunity) return res.json({ message: 'Community is Created !!!' }).status(200)
if (followCommunity) {
return res.json({ message: 'Community is Created !!!' }).status(200)
}
}
} catch (error) {
return res.json({ error: error.message }).status(400)
Expand All @@ -184,9 +220,12 @@ const getCommunityById = async (req, res) => {
const id = req.params.id

db.Community.findByPk(id)
.then(communities => {
.then((communities) => {
if (communities) {
res.json({ ...communities.dataValues, attachment: changeFormat(communities.dataValues.attachment) })
res.json({
...communities.dataValues,
attachment: changeFormat(communities.dataValues.attachment)
})
} else {
res.status(404)
throw new Error('Community not found')
Expand Down Expand Up @@ -214,14 +253,25 @@ const deleteCommunity = async (req, res) => {
}

const result = await sequelize.transaction(async (t) => {
const communityUserIds = await db.CommunityUser.findAll({ where: { communityId: id } }, { transaction: t })
const communityUserIds = await db.CommunityUser.findAll(
{ where: { communityId: id } },
{ transaction: t }
)

communityUserIds.forEach(async function (communityId) {
const { id } = communityId
await db.CommunityUser.update({ active: false }, { where: { id } }, { transaction: t })
await db.CommunityUser.update(
{ active: false },
{ where: { id } },
{ transaction: t }
)
})

await db.Community.update({ deleted: true }, { where: { id } }, { transaction: t })
await db.Community.update(
{ deleted: true },
{ where: { id } },
{ transaction: t }
)

return 'Community Deleted with links.'
})
Expand All @@ -241,9 +291,7 @@ const deleteCommunity = async (req, res) => {
// @access Private
const updateCommunity = async (req, res) => {
try {
const {
name, description, file, toggleActive
} = req.body
const { name, description, file, toggleActive } = req.body

let filename = ''
if (req.file) {
Expand Down Expand Up @@ -274,11 +322,11 @@ const updateCommunity = async (req, res) => {

// converting them to the array of ids not(object with ids)
// to check console.log(followIdArrays) and newFollowIds
const newFollowIds = followIdArrays.map(item => item.userId)
const newUserIds = userIdArrays.map(item => item.id)
const newFollowIds = followIdArrays.map((item) => item.userId)
const newUserIds = userIdArrays.map((item) => item.id)

// this is removing any duplicate items between newFollowIds and newUserIds
const idArrays = newUserIds.filter(item => {
const idArrays = newUserIds.filter((item) => {
return newFollowIds.indexOf(item) === -1
})

Expand All @@ -295,33 +343,41 @@ const updateCommunity = async (req, res) => {
await db.CommunityUser.bulkCreate(allFollow, { transaction: t })

if (!req.file) {
await db.Community.update({ ...req.body, slug: '' },
{ where: { id } })
await db.Community.update(
{ ...req.body, slug: '' },
{ where: { id } }
)
return 'Community is updated with autoFollow'
}

await db.Community.update({ ...req.body, slug: '', attachment: 'community/' + filename },
{ where: { id } })
await db.Community.update(
{ ...req.body, slug: '', attachment: 'community/' + filename },
{ where: { id } }
)
return 'Community is updated with autoFollow'
})

return res.json({ message: result })
} else {
if (!req.file) {
await db.Community.update({
name,
description
},
{ where: { id }, returning: true, attributes: ['id'] })
await db.Community.update(
{
name,
description
},
{ where: { id }, returning: true, attributes: ['id'] }
)
return res.json({ message: 'Community Updated !!!' }).status(200)
}

await db.Community.update({
name,
description,
attachment: 'community/' + filename
},
{ where: { id }, returning: true, attributes: ['id'] })
await db.Community.update(
{
name,
description,
attachment: 'community/' + filename
},
{ where: { id }, returning: true, attributes: ['id'] }
)
return res.json({ message: 'Community Updated !!!' }).status(200)
}
} else {
Expand All @@ -340,11 +396,21 @@ const searchCommunityName = (req, res) => {
const { name } = req.query
const order = req.query.order || 'ASC'

db.Community.findAll({ where: { name: { [Op.iLike]: '%' + name + '%' } }, order: [['name', order]] })
.then(communities => res.json({
communities: communities.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) }))
}).status(200))
.catch(err => res.json({ error: err }).status(400))
db.Community.findAll({
where: { name: { [Op.iLike]: '%' + name + '%' } },
order: [['name', order]]
})
.then((communities) =>
res
.json({
communities: communities.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
}))
})
.status(200)
)
.catch((err) => res.json({ error: err }).status(400))
}

// @desc Search usercommunity name
Expand All @@ -355,26 +421,44 @@ const searchUserCommunityName = (req, res) => {
const order = req.query.order || 'ASC'

db.Community.findAll({
include: [{
model: db.User,
as: 'creator' && 'followers',
attributes: ['id'],
where: { id: req.user.id },
through: {
attributes: ['active'],
as: 'followStatus',
where: {
active: true
include: [
{
model: db.User,
as: 'creator' && 'followers',
attributes: ['id'],
where: { id: req.user.id },
through: {
attributes: ['active'],
as: 'followStatus',
where: {
active: true
}
}
}
}],
],
where: { name: { [Op.iLike]: '%' + name + '%' } },
order: [['name', order]]
})
.then(communities => res.json({
communities: communities.map(rec => ({ ...rec.dataValues, attachment: changeFormat(rec.attachment) }))
}).status(200))
.catch(err => res.json({ error: err }).status(400))
.then((communities) =>
res
.json({
communities: communities.map((rec) => ({
...rec.dataValues,
attachment: changeFormat(rec.attachment)
}))
})
.status(200)
)
.catch((err) => res.json({ error: err }).status(400))
}

module.exports = { getCommunities, getUserCommunities, searchUserCommunityName, createCommunity, getCommunityById, deleteCommunity, updateCommunity, searchCommunityName }
module.exports = {
getCommunities,
getUserCommunities,
searchUserCommunityName,
createCommunity,
getCommunityById,
deleteCommunity,
updateCommunity,
searchCommunityName
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict'

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.addColumn('courses', 'communityId', {
type: Sequelize.INTEGER,
references: {
model: 'communities',
key: 'id'
}
})
await queryInterface.addColumn('courses', 'isPublic', Sequelize.BOOLEAN)
},
down: async (queryInterface, Sequelize) => {
await queryInterface.removeColumn('courses', 'communityId')
await queryInterface.removeColumn('courses', 'isPublic')
}
}
Loading