Skip to content

Commit

Permalink
aligned with current scheme
Browse files Browse the repository at this point in the history
  • Loading branch information
eirikhanasand committed Feb 18, 2025
1 parent 760e98c commit dd9e08d
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 118 deletions.
112 changes: 55 additions & 57 deletions api/src/put/blacklist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,69 @@ type BlacklistUpdateBody = {
oldVersion: string
newVersion: string
ecosystem: string
comment: string
}

export default async function blacklistPutHandler(
req: FastifyRequest<{ Params: BlacklistUpdateBody }>,
res: FastifyReply
) {
const { ecosystem, name, oldVersion, newVersion } = req.params
export default async function blacklistPutHandler(req: FastifyRequest, res: FastifyReply) {
const { ecosystem, name, oldVersion, newVersion, comment } = req.body as BlacklistUpdateBody

if (!ecosystem || !name || !oldVersion || !newVersion) {
return res
.status(400)
.send({ error: "Missing name, oldVersion, newVersion, or ecosystem." })
}
if (!ecosystem || !name || !oldVersion || !newVersion || !comment) {
return res
.status(400)
.send({ error: "Missing name, oldVersion, newVersion, ecosystem, comment." })
}

try {
console.log(
`Replacing blacklist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}`
)
try {
console.log(
`Replacing blacklist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}, comment=${comment}`
)

await runInTransaction(async (client) => {
const checkExists = await client.query(
"SELECT name FROM blacklist WHERE name = $1;",
[name]
)
if (checkExists.rowCount === 0) {
throw new Error("Blacklist entry not found.")
}
await runInTransaction(async (client) => {
const checkExists = await client.query(
"SELECT name FROM blacklist WHERE name = $1;",
[name]
)
if (checkExists.rowCount === 0) {
throw new Error("Blacklist entry not found.")
}

const updateResult = await client.query(
`
UPDATE blacklist_versions
SET version = $3
WHERE name = $1
AND version = $2;
`,
[name, oldVersion, newVersion]
)
const updateResult = await client.query(
`
UPDATE blacklist_versions
SET version = $3
WHERE name = $1
AND version = $2;
`,
[name, oldVersion, newVersion]
)

if (updateResult.rowCount === 0) {
await client.query(
`
INSERT INTO blacklist_versions (name, version)
VALUES ($1, $2);
`,
[name, newVersion]
)
}
if (updateResult.rowCount === 0) {
await client.query(
`
INSERT INTO blacklist_versions (name, version)
VALUES ($1, $2);
`,
[name, newVersion]
)
}

await client.query(
`
INSERT INTO blacklist_ecosystems (name, ecosystem)
VALUES ($1, $2)
ON CONFLICT (name, ecosystem)
DO UPDATE SET ecosystem = EXCLUDED.ecosystem;
`,
[name, ecosystem]
)
})
await client.query(
`
INSERT INTO blacklist_ecosystems (name, ecosystem)
VALUES ($1, $2)
ON CONFLICT (name, ecosystem)
DO UPDATE SET ecosystem = EXCLUDED.ecosystem;
`,
[name, ecosystem]
)
})

return res.send({ message: "Blacklist entry updated successfully." })
} catch (error: any) {
console.error("Database error:", error)
if (error.message.includes("not found")) {
return res.status(404).send({ error: error.message })
return res.send({ message: "Blacklist entry updated successfully." })
} catch (error: any) {
console.error("Database error:", error)
if (error.message.includes("not found")) {
return res.status(404).send({ error: error.message })
}
return res.status(500).send({ error: "Internal Server Error" })
}
return res.status(500).send({ error: "Internal Server Error" })
}
}
120 changes: 59 additions & 61 deletions api/src/put/whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,73 @@ import { FastifyReply, FastifyRequest } from "fastify"
import { runInTransaction } from "../db.js"

type WhitelistUpdateBody = {
name: string
oldVersion: string
newVersion: string
ecosystem: string
name: string
oldVersion: string
newVersion: string
ecosystem: string
comment: string
}

export default async function whitelistPutHandler(
req: FastifyRequest<{ Params: WhitelistUpdateBody }>,
res: FastifyReply
) {
const { ecosystem, name, oldVersion, newVersion } = req.params
export default async function whitelistPutHandler(req: FastifyRequest, res: FastifyReply) {
const { ecosystem, name, oldVersion, newVersion, comment } = req.body as WhitelistUpdateBody

if (!ecosystem || !name || !oldVersion || !newVersion) {
return res
.status(400)
.send({ error: "Missing name, oldVersion, newVersion, or ecosystem." })
}
if (!ecosystem || !name || !oldVersion || !newVersion || !comment) {
return res
.status(400)
.send({ error: "Missing name, oldVersion, newVersion, ecosystem, comment." })
}

try {
console.log(
`Replacing whitelist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}`
)
try {
console.log(
`Replacing whitelist version: name=${name}, oldVersion=${oldVersion}, newVersion=${newVersion}, ecosystem=${ecosystem}, comment=${comment}`
)

await runInTransaction(async (client) => {
const checkExists = await client.query(
"SELECT name FROM white WHERE name = $1;",
[name]
)
if (checkExists.rowCount === 0) {
throw new Error("White entry not found.")
}
await runInTransaction(async (client) => {
const checkExists = await client.query(
"SELECT name FROM whitelist WHERE name = $1;",
[name]
)
if (checkExists.rowCount === 0) {
throw new Error("whitelist entry not found.")
}

const updateResult = await client.query(
`
UPDATE whitelist_versions
SET version = $3
WHERE name = $1
AND version = $2;
`,
[name, oldVersion, newVersion]
)
const updateResult = await client.query(
`
UPDATE whitelist_versions
SET version = $3
WHERE name = $1
AND version = $2;
`,
[name, oldVersion, newVersion]
)

if (updateResult.rowCount === 0) {
await client.query(
`
INSERT INTO whitelist_versions (name, version)
VALUES ($1, $2);
`,
[name, newVersion]
)
}
if (updateResult.rowCount === 0) {
await client.query(
`
INSERT INTO whitelist_versions (name, version)
VALUES ($1, $2);
`,
[name, newVersion]
)
}

await client.query(
`
INSERT INTO whitelist_ecosystems (name, ecosystem)
VALUES ($1, $2)
ON CONFLICT (name, ecosystem)
DO UPDATE SET ecosystem = EXCLUDED.ecosystem;
`,
[name, ecosystem]
)
})
await client.query(
`
INSERT INTO whitelist_ecosystems (name, ecosystem)
VALUES ($1, $2)
ON CONFLICT (name, ecosystem)
DO UPDATE SET ecosystem = EXCLUDED.ecosystem;
`,
[name, ecosystem]
)
})

return res.send({ message: "Whitelist entry updated successfully." })
} catch (error: any) {
console.error("Database error:", error)
if (error.message.includes("not found")) {
return res.status(404).send({ error: error.message })
return res.send({ message: "whitelist entry updated successfully." })
} catch (error: any) {
console.error("Database error:", error)
if (error.message.includes("not found")) {
return res.status(404).send({ error: error.message })
}
return res.status(500).send({ error: "Internal Server Error" })
}
return res.status(500).send({ error: "Internal Server Error" })
}
}

0 comments on commit dd9e08d

Please sign in to comment.