-
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.
migrate cocktails all and cocktail visit to new api
- Loading branch information
1 parent
3005d7d
commit 1bc65a5
Showing
5 changed files
with
147 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { defineEventHandler } from 'h3' | ||
import { db } from '~/server/utils/mongo' | ||
|
||
async function addVisitToCocktail(slug) { | ||
return db.collection('cocktails').updateOne( | ||
{ | ||
'slug': slug | ||
}, | ||
{ | ||
$inc: { | ||
'visitCount': 1 | ||
} | ||
} | ||
); | ||
} | ||
|
||
export default defineEventHandler(async (req) => { | ||
const slug = req.context.params.slug | ||
|
||
const scoreInfo = await addVisitToCocktail(slug); | ||
|
||
if (!scoreInfo) { | ||
return { | ||
error: 'Cocktail not found' | ||
} | ||
} else { | ||
return { 'slug': slug } | ||
} | ||
}) |
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,14 @@ | ||
import { defineEventHandler } from 'h3' | ||
import { db } from '~/server/utils/mongo' | ||
|
||
async function getAllCocktails() { | ||
return db.collection('cocktails').find( | ||
{}, | ||
) | ||
.project({ name: 1, slug: 1, _id: 0 }) | ||
.toArray(); | ||
} | ||
export default defineEventHandler(async (req) => { | ||
const cocktails = await getAllCocktails(); | ||
return cocktails; | ||
}) |
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,50 @@ | ||
import { defineEventHandler } from 'h3' | ||
import { db } from '~/server/utils/mongo' | ||
import * as dotenv from 'dotenv' | ||
|
||
dotenv.config() | ||
|
||
const imageDomain = process.env.IMAGE_DOMAIN | ||
|
||
const formats = ['webp', 'jpg'] | ||
|
||
const buildOgImage = (id) => { | ||
return `${imageDomain}/goods/${id}/256/${id}.jpg`; | ||
}; | ||
|
||
const buildGlasswareDetailsImage = (slug) => { | ||
const sizes = [ | ||
{ responseSize: "414px", imageSize: "500" }, | ||
{ responseSize: "0", imageSize: "334" } | ||
]; | ||
|
||
return formats.flatMap(format => | ||
sizes.map(size => ({ | ||
srcset: `${imageDomain}/v2/glasswares/${slug}/${size.imageSize}.${format}`, | ||
media: `screen and (min-width: ${size.responseSize})`, | ||
type: `image/${format}` | ||
})) | ||
); | ||
} | ||
|
||
async function getGlasswarebySlug(slug) { | ||
return db | ||
.collection('glassware') | ||
.findOne({ slug: slug }) | ||
} | ||
|
||
export default defineEventHandler(async (req) => { | ||
const slug = req.context.params.slug; | ||
|
||
const glassware = await getGlasswarebySlug(slug); | ||
return { | ||
id: glassware.id, | ||
slug: glassware.slug, | ||
name: glassware.name, | ||
about: glassware.about, | ||
images: buildGlasswareDetailsImage(glassware.slug), | ||
meta: { | ||
ogImage: buildOgImage(glassware.id), | ||
}, | ||
} | ||
}) |
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,50 @@ | ||
import { defineEventHandler } from 'h3' | ||
import { db } from '~/server/utils/mongo' | ||
import * as dotenv from 'dotenv' | ||
|
||
dotenv.config() | ||
|
||
const imageDomain = process.env.IMAGE_DOMAIN | ||
|
||
const formats = ['webp', 'jpg'] | ||
|
||
const buildOgImage = (id) => { | ||
return `${imageDomain}/goods/${id}/256/${id}.jpg`; | ||
}; | ||
|
||
const buildToolDetailsImage = (slug) => { | ||
const sizes = [ | ||
{ responseSize: "414px", imageSize: "500" }, | ||
{ responseSize: "0", imageSize: "334" } | ||
]; | ||
|
||
return formats.flatMap(format => | ||
sizes.map(size => ({ | ||
srcset: `${imageDomain}/v2/tools/${slug}/${size.imageSize}.${format}`, | ||
media: `screen and (min-width: ${size.responseSize})`, | ||
type: `image/${format}` | ||
})) | ||
); | ||
} | ||
|
||
async function getToolBySlug(slug) { | ||
return db | ||
.collection('tools') | ||
.findOne({ slug: slug }) | ||
} | ||
|
||
export default defineEventHandler(async (req) => { | ||
const slug = req.context.params.slug; | ||
|
||
const tool = await getToolBySlug(slug); | ||
return { | ||
id: tool.id, | ||
slug: tool.slug, | ||
name: tool.name, | ||
about: tool.about, | ||
images: buildToolDetailsImage(tool.slug), | ||
meta: { | ||
ogImage: buildOgImage(tool.id), | ||
}, | ||
} | ||
}) |