134 lines
3.0 KiB
JavaScript
134 lines
3.0 KiB
JavaScript
const dealDB = require("../../db/deal.db")
|
|
|
|
async function searchDeals(query, page = 1, limit = 10) {
|
|
const skip = (page - 1) * limit
|
|
const where = {
|
|
OR: [
|
|
{ title: { contains: query, mode: "insensitive" } },
|
|
{ description: { contains: query, mode: "insensitive" } },
|
|
],
|
|
}
|
|
|
|
const [deals, total] = await Promise.all([
|
|
dealDB.findDeals(where, {
|
|
skip,
|
|
take: limit,
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
user: { select: { username: true } },
|
|
images: {
|
|
orderBy: { order: "asc" },
|
|
take: 1,
|
|
select: { imageUrl: true },
|
|
},
|
|
},
|
|
}),
|
|
dealDB.countDeals(where),
|
|
])
|
|
|
|
return {
|
|
page,
|
|
total,
|
|
totalPages: Math.ceil(total / limit),
|
|
results: deals,
|
|
}
|
|
}
|
|
|
|
async function getAllDeals(page = 1, limit = 10) {
|
|
const skip = (page - 1) * limit
|
|
|
|
const [deals, total] = await Promise.all([
|
|
dealDB.findDeals({}, {
|
|
skip,
|
|
take: limit,
|
|
orderBy: { createdAt: "desc" },
|
|
include: {
|
|
user: { select: { username: true } },
|
|
images: {
|
|
orderBy: { order: "asc" },
|
|
take: 1,
|
|
select: { imageUrl: true },
|
|
},
|
|
},
|
|
}),
|
|
dealDB.countDeals(),
|
|
])
|
|
|
|
return {
|
|
page,
|
|
total,
|
|
totalPages: Math.ceil(total / limit),
|
|
results: deals,
|
|
}
|
|
}
|
|
|
|
async function getDealById(id) {
|
|
return dealDB.findDeal(
|
|
{ id: Number(id) },
|
|
{
|
|
include: {
|
|
user: { select: { username: true } },
|
|
images: {
|
|
orderBy: { order: "asc" },
|
|
select: { id: true, imageUrl: true, order: true },
|
|
},
|
|
},
|
|
}
|
|
)
|
|
}
|
|
|
|
async function createDeal(data, userId) {
|
|
const payload = {
|
|
title: data.title,
|
|
description: data.description,
|
|
url: data.url,
|
|
price: data.price,
|
|
user: { connect: { id: userId } },
|
|
images: data.images?.length
|
|
? {
|
|
create: data.images.map((imgUrl, index) => ({
|
|
imageUrl: imgUrl,
|
|
order: index,
|
|
})),
|
|
}
|
|
: undefined,
|
|
}
|
|
|
|
return dealDB.createDeal(payload, { include: { images: true } })
|
|
}
|
|
|
|
async function voteDeal(dealId, userId, voteType) {
|
|
if (!dealId || !userId || !voteType) throw new Error("Eksik veri")
|
|
|
|
const existingVote = await dealDB.findVotes({ dealId, userId })
|
|
const vote = existingVote[0]
|
|
|
|
if (vote) {
|
|
await dealDB.updateVote({ id: vote.id }, { voteType })
|
|
} else {
|
|
await dealDB.createVote({ dealId, userId, voteType })
|
|
}
|
|
|
|
const upvotes = await dealDB.countVotes({ dealId, voteType: "UP" })
|
|
const downvotes = await dealDB.countVotes({ dealId, voteType: "DOWN" })
|
|
const score = upvotes - downvotes
|
|
|
|
await dealDB.updateDeal({ id: dealId }, { score })
|
|
return score
|
|
}
|
|
|
|
async function getVotes(dealId) {
|
|
const upvotes = await dealDB.countVotes({ dealId: Number(dealId), voteType: "UP" })
|
|
const downvotes = await dealDB.countVotes({ dealId: Number(dealId), voteType: "DOWN" })
|
|
return { upvotes, downvotes, score: upvotes - downvotes }
|
|
}
|
|
|
|
module.exports = {
|
|
getAllDeals,
|
|
getDealById,
|
|
createDeal,
|
|
voteDeal,
|
|
getVotes,
|
|
searchDeals,
|
|
}
|