changes to search by tag endpoints

This commit is contained in:
Lacey Pevey
2023-05-17 09:27:13 -05:00
parent 2b7e9ce077
commit 6888c339f6
4 changed files with 63 additions and 59 deletions
+8
View File
@@ -1,5 +1,13 @@
# Change Log
## 0.0.4
### Patch Changes
- Changes to retrieve by tag endpoints
# Change Log
## 0.0.3
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "medusa-plugin-blog",
"version": "0.0.3",
"version": "0.0.4",
"description": "Basic blog for Medusa",
"repository": {
"type": "git",
+7 -11
View File
@@ -62,18 +62,14 @@ export default (rootDirectory: string): Router | Router[] => {
})
// GET ALL BLOG POSTS TAGGED WITH A TAG OR AN ARRAY OF TAGS
router.get("/store/blog/tags", cors(storeCorsOptions), async (req, res) => {
const tags = req.query.tags
// Ex: /store/blog/tags/posts?tag=tag1&tag=tag2&tag=tag3
router.get("/store/blog/tags/posts", cors(storeCorsOptions), async (req, res) => {
if (!req.query.tag) { return res.json([]) }
const tag_ids = (typeof req.query.tag === "string")? [req.query.tag] : req.query.tag
const blogService = req.scope.resolve("blogService")
if (Array.isArray(tags)) {
blogService.getBlogPostsAllTags(tags).then((blog_posts) => {
return res.json(blog_posts)
})
} else {
blogService.getBlogPostsByTag(tags).then((blog_posts) => {
return res.json(blog_posts)
})
}
blogService.getBlogPostsByTags(tag_ids).then((blog_posts) => {
return res.json(blog_posts)
})
})
// GET ALL BLOG POSTS TAGGED WITH A PRODUCT
+47 -47
View File
@@ -4,7 +4,6 @@ import { ProductCollectionRepository } from '@medusajs/medusa/dist/repositories/
import { BlogCategoryRepository } from '../repositories/blog-category'
import { BlogPostRepository } from '../repositories/blog-post'
import { BlogTagRepository } from '../repositories/blog-tag'
import { ArrayContains, ArrayOverlap } from 'typeorm'
export default class BlogService extends TransactionBaseService {
protected readonly productRepository_: typeof ProductRepository
@@ -126,65 +125,66 @@ export default class BlogService extends TransactionBaseService {
async getBlogPostsByTag(tag_id) {
/* @ts-ignore */
const blogPostRepository = this.activeManager_.withRepository(this.blogPostRepository_)
return await blogPostRepository.find({
relations: {
category: true,
tags: true,
products: true,
collections: true
},
where: {
tags: ArrayContains(tag_id)
}
})
return await blogPostRepository.createQueryBuilder('blog_post')
.leftJoinAndSelect('blog_post.tags', 'tags')
.leftJoinAndSelect('blog_post.category', 'category')
.leftJoinAndSelect('blog_post.products', 'products')
.leftJoinAndSelect('blog_post.collections', 'collections')
.where('tags.id = :tag_id)', { tag_id })
.getMany()
}
async getBlogPostsAllTags(tag_ids) {
/* @ts-ignore */
const blogPostRepository = this.activeManager_.withRepository(this.blogPostRepository_)
return await blogPostRepository.find({
relations: {
category: true,
tags: true,
products: true,
collections: true
},
where: {
tags: ArrayOverlap(tag_ids)
}
})
// return await blogPostRepository.createQueryBuilder('blog_post')
// .leftJoinAndSelect('blog_post.category', 'category')
// .leftJoinAndSelect('blog_post.products', 'products')
// .leftJoinAndSelect('blog_post.collections', 'collections')
// .innerJoinAndSelect('blog_post.tags', 'tags', 'tags.id IN (:...tag_ids)', { tag_ids })
// .getMany()
// return await blogPostRepository.createQueryBuilder('blog_post')
// .leftJoinAndSelect('blog_post.tags', 'tags')
// .leftJoinAndSelect('blog_post.category', 'category')
// .leftJoinAndSelect('blog_post.products', 'products')
// .leftJoinAndSelect('blog_post.collections', 'collections')
// .where('tags.id IN (:...tag_ids)', { tag_ids })
// .getMany()
}
async getBlogPostsByProduct(product_id) {
async getBlogPostsByTags(tag_ids:number[]) {
/* @ts-ignore */
const blogPostRepository = this.activeManager_.withRepository(this.blogPostRepository_)
return await blogPostRepository.find({
relations: {
category: true,
tags: true,
products: true,
collections: true
},
where: {
products: ArrayContains(product_id)
}
})
return await blogPostRepository.createQueryBuilder('blog_post')
.innerJoinAndSelect('blog_post.tags', 'tags', 'tags.id IN (:...tag_ids)', { tag_ids })
.leftJoinAndSelect('blog_post.category', 'category')
.leftJoinAndSelect('blog_post.products', 'products')
.leftJoinAndSelect('blog_post.collections', 'collections')
.getMany()
}
async getBlogPostsByProduct(product_id:string) {
/* @ts-ignore */
const blogPostRepository = this.activeManager_.withRepository(this.blogPostRepository_)
return await blogPostRepository.createQueryBuilder('blog_post')
.leftJoinAndSelect('blog_post.tags', 'tags')
.leftJoinAndSelect('blog_post.category', 'category')
.leftJoinAndSelect('blog_post.products', 'products')
.leftJoinAndSelect('blog_post.collections', 'collections')
.where('products.id = :product_id', { product_id })
.getMany()
}
async getBlogPostsByCollection(collection_id) {
/* @ts-ignore */
const blogPostRepository = this.activeManager_.withRepository(this.blogPostRepository_)
return await blogPostRepository.find({
relations: {
category: true,
tags: true,
products: true,
collections: true
},
where: {
collections: ArrayContains(collection_id)
}
})
return await blogPostRepository.createQueryBuilder('blog_post')
.leftJoinAndSelect('blog_post.tags', 'tags')
.leftJoinAndSelect('blog_post.category', 'category')
.leftJoinAndSelect('blog_post.products', 'products')
.leftJoinAndSelect('blog_post.collections', 'collections')
.where('collections.id = :collection_id)', { collection_id })
.getMany()
}