Skip to content

Commit

Permalink
migrate cocktails all and cocktail visit to new api
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Nov 21, 2024
1 parent 3005d7d commit 1bc65a5
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/other.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export const updateRating = (slug, value) => {
}
})
}

export const updateVisit = (slug) => {
$fetch(`https://newapi.mixdrinks.org/api/cocktail/${slug}/visit`, {
$fetch(`/api/cocktail/${slug}/visit`, {
method: 'POST'
})
}

export const getListSearch = () =>
$fetch('https://newapi.mixdrinks.org/api/cocktails/all')
$fetch('/api/cocktails/all')
29 changes: 29 additions & 0 deletions server/api/cocktail/[slug]/visit.post.js
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 }
}
})
14 changes: 14 additions & 0 deletions server/api/cocktails/all.get.js
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;
})
50 changes: 50 additions & 0 deletions server/api/glassware/[slug].get.js
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),
},
}
})
50 changes: 50 additions & 0 deletions server/api/tools/[slug].get.js
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),
},
}
})

0 comments on commit 1bc65a5

Please sign in to comment.