-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
221 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './collections.constant'; | ||
export * from './regex.constant'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const MONGO_ID_REGEX = /^[0-9a-fA-F]{24}$/; | ||
|
||
export const FILENAME_REGEX = | ||
/^[a-zA-Z0-9]+(\.png|\.webp|\.jpg|\.jpeg|\.gif|\.bmp)$/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './request.schema'; | ||
export * from './response.schema'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import vine from '@vinejs/vine'; | ||
import { FILENAME_REGEX, MONGO_ID_REGEX } from '../constants'; | ||
|
||
const cleanStringSchema = vine.string().trim().escape().toLowerCase(); | ||
|
||
export const updateVoteBodySchema = vine.object({ | ||
vote: vine.enum(['positive', 'negative']), | ||
}); | ||
|
||
export const updateVoteBodyValidator = vine.compile(updateVoteBodySchema); | ||
|
||
export const updateVoteParamsSchema = vine.object({ | ||
id: cleanStringSchema.regex(MONGO_ID_REGEX), | ||
}); | ||
|
||
export const updateVoteParamsValidator = vine.compile(updateVoteParamsSchema); | ||
|
||
export const getImageByFilenameParamsSchema = vine.object({ | ||
filename: cleanStringSchema.regex(FILENAME_REGEX), | ||
}); | ||
|
||
export const getImageByFilenameParamsValidator = vine.compile( | ||
getImageByFilenameParamsSchema | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import vine from '@vinejs/vine'; | ||
import { MONGO_ID_REGEX } from '../constants'; | ||
|
||
export const updateVoteResponseSchema = vine.object({ | ||
celebrityId: vine.string().regex(MONGO_ID_REGEX), | ||
votes: vine.object({ | ||
positive: vine.number(), | ||
negative: vine.number(), | ||
}), | ||
name: vine.string(), | ||
picture: vine.string(), | ||
description: vine.string().optional(), | ||
lastUpdated: vine.date(), | ||
active: vine.boolean(), | ||
}); | ||
|
||
export const updateVoteResponseValidator = vine.compile( | ||
updateVoteResponseSchema | ||
); |
39 changes: 0 additions & 39 deletions
39
libs/backend/src/infrastructure/controllers/celebrities.controller.ts
This file was deleted.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
libs/backend/src/infrastructure/controllers/get-celebrities.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { CelebritiesType } from '@libs/shared'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { findCelebritiesCollection } from '../../application'; | ||
|
||
export async function getCelebritiesController( | ||
_req: NextApiRequest, | ||
res: NextApiResponse<CelebritiesType> | ||
) { | ||
const celebrities = await findCelebritiesCollection(); | ||
|
||
res.status(200).json(celebrities); | ||
} |
34 changes: 34 additions & 0 deletions
34
libs/backend/src/infrastructure/controllers/get-cover-by-name.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import sharp from 'sharp'; | ||
import { findOneByNameBucket } from '../../application'; | ||
import { getImageByFilenameParamsValidator } from '../../domain/schemas'; | ||
|
||
export async function getCoverByNameController( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
// Validate query params | ||
const [params] = await Promise.all([ | ||
getImageByFilenameParamsValidator.validate(req.query), | ||
]); | ||
|
||
const { filename } = params; | ||
|
||
const file = await findOneByNameBucket<NodeJS.ReadableStream>( | ||
filename.replace(/.webp$/, '.png') | ||
); | ||
|
||
const output = sharp() | ||
.webp({ quality: 80 }) | ||
.resize({ | ||
fit: 'cover', | ||
width: 1280, | ||
height: 720, | ||
}) | ||
.extend({ | ||
background: { r: 255, g: 255, b: 255, alpha: 0.8 }, | ||
}); | ||
|
||
res.setHeader('Content-Type', 'image/webp'); | ||
file.pipe(output).pipe(res); | ||
} |
29 changes: 29 additions & 0 deletions
29
libs/backend/src/infrastructure/controllers/get-image-by-name.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import sharp from 'sharp'; | ||
import { findOneByNameBucket } from '../../application'; | ||
import { getImageByFilenameParamsValidator } from '../../domain/schemas'; | ||
|
||
export async function getImageByNameController( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
// Validate query params | ||
const [params] = await Promise.all([ | ||
getImageByFilenameParamsValidator.validate(req.query), | ||
]); | ||
|
||
const { filename } = params; | ||
|
||
const file = await findOneByNameBucket<NodeJS.ReadableStream>( | ||
filename.replace(/.webp$/, '.png') | ||
); | ||
|
||
const output = sharp().webp({ quality: 80 }).resize({ | ||
fit: 'contain', | ||
width: 300, | ||
height: 300, | ||
}); | ||
|
||
res.setHeader('Content-Type', 'image/webp'); | ||
file.pipe(output).pipe(res); | ||
} |
58 changes: 0 additions & 58 deletions
58
libs/backend/src/infrastructure/controllers/images.controller.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './celebrities.controller'; | ||
export * from './images.controller'; | ||
export * from './get-celebrities.controller'; | ||
export * from './get-cover-by-name.controller'; | ||
export * from './update-vote.controller'; |
32 changes: 32 additions & 0 deletions
32
libs/backend/src/infrastructure/controllers/update-vote.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { CelebrityType } from '@libs/shared'; | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { updateCelebrityByIdCollection } from '../../application'; | ||
import { | ||
updateVoteBodyValidator, | ||
updateVoteParamsValidator, | ||
} from '../../domain/schemas'; | ||
|
||
export async function updateVoteController( | ||
req: NextApiRequest, | ||
res: NextApiResponse<CelebrityType> | ||
) { | ||
// Validate body and params | ||
const [body, params] = await Promise.all([ | ||
updateVoteBodyValidator.validate(req.body), | ||
updateVoteParamsValidator.validate(req.query), | ||
]); | ||
|
||
const { vote } = body; | ||
const { id } = params; | ||
|
||
const increment: Record<string, Record<string, number>> = { | ||
positive: { 'votes.positive': 1 }, | ||
negative: { 'votes.negative': 1 }, | ||
}; | ||
|
||
const result = await updateCelebrityByIdCollection(id, { | ||
$inc: increment[vote], | ||
}); | ||
|
||
res.status(200).json(result); | ||
} |
8 changes: 1 addition & 7 deletions
8
libs/frontend/src/application/services/get-celebrities.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.