Skip to content

Commit

Permalink
Merge pull request #48 from Mango-Entertainment/ts-example-stuff
Browse files Browse the repository at this point in the history
refactoring db calls and removing ts ignore
  • Loading branch information
AlexVCS authored Dec 27, 2023
2 parents dbeb26f + 4027b00 commit a855404
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 45 deletions.
68 changes: 24 additions & 44 deletions app/lib/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@ const sql = postgres(connectionString)

export const getTrending = async () => {
try {
const result: TrendingData[] = []
const data =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, trending_thumbs.large, trending_thumbs.small
const data: TrendingData[] = await sql<
TrendingData[]
>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, trending_thumbs.large, trending_thumbs.small
FROM selections
JOIN trending_thumbs ON trending_thumbs.selection_id = selections.id`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
data.forEach((item) => result.push(item))
return result
return data
} catch (error) {
console.error('Database Error:', error)
throw new Error('Failed to fetch trending data.')
Expand All @@ -23,16 +20,13 @@ export const getTrending = async () => {

export const getRecommended = async () => {
try {
const result: RegularData[] = []
const data =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
const data: RegularData[] = await sql<
RegularData[]
>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.is_trending != true`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
data.forEach((item) => result.push(item))
return result
return data
} catch (error) {
console.error('Database Error:', error)
throw new Error('Failed to fetch recommended data.')
Expand All @@ -41,16 +35,13 @@ export const getRecommended = async () => {

export const getMovies = async () => {
try {
const result: RegularData[] = []
const data =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
const data: RegularData[] = await sql<
RegularData[]
>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.category = 'Movie'`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
data.forEach((item) => result.push(item))
return result
return data
} catch (error) {
console.error('Database Error:', error)
throw new Error('Failed to fetch movie data.')
Expand All @@ -59,16 +50,13 @@ export const getMovies = async () => {

export const getSeries = async () => {
try {
const result: RegularData[] = []
const data =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
const data: RegularData[] = await sql<
RegularData[]
>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.category = 'TV Series'`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
data.forEach((item) => result.push(item))
return result
return data
} catch (error) {
console.error('Database Error:', error)
throw new Error('Failed to fetch series data.')
Expand All @@ -77,26 +65,18 @@ export const getSeries = async () => {

export const getBookmarks = async () => {
try {
const movieResult: RegularData[] = []
const seriesResult: RegularData[] = []
const movieData =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.category = 'Movie'
AND selections.is_bookmarked = true`
const seriesData =
await sql`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
const movieResult: RegularData[] =
await sql<RegularData[]>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.category = 'Movie'
AND selections.is_bookmarked = true`
const seriesResult: RegularData[] =
await sql<RegularData[]>`SELECT selections.id, selections.title, selections.rating, selections.year, selections.category, selections.is_bookmarked, regular_thumbs.large, regular_thumbs.medium, regular_thumbs.small, selections.is_trending
FROM selections
JOIN regular_thumbs ON regular_thumbs.selection_id = selections.id
WHERE selections.category = 'TV Series'
AND selections.is_bookmarked = true`
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
movieData.forEach((item) => movieResult.push(item))
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
seriesData.forEach((item) => seriesResult.push(item))
return { movies: movieResult, series: seriesResult }
} catch (error) {
console.error('Database Error:', error)
Expand Down
2 changes: 1 addition & 1 deletion app/lib/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ export type RegularData = {
large: string
small: string
medium: string
}
}

0 comments on commit a855404

Please sign in to comment.