Skip to content

Commit

Permalink
Refactoring the catalog APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
treoden committed Nov 26, 2023
1 parent 9b5eb26 commit aec67dc
Show file tree
Hide file tree
Showing 32 changed files with 783 additions and 468 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const createProductAttribute = require('../../services/attribute/createProductAt

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const attribute = await createProductAttribute(request.body);
const attribute = await createProductAttribute(request.body, {
routeId: request.currentRoute.id
});
return attribute;
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const createCategory = require('../../services/category/createCategory');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const result = await createCategory(request.body);
const result = await createCategory(request.body, {
routeId: request.currentRoute.id
});
return result;
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const createCollection = require('../../services/collection/createCollection');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const collection = await createCollection(request.body, {
routeId: request.currentRoute.id
});
return collection;
};
Original file line number Diff line number Diff line change
@@ -1,64 +1,27 @@
const {
commit,
rollback,
select
} = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const { buildUrl } = require('@evershop/evershop/src/lib/router/buildUrl');
const {
OK,
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const { OK } = require('@evershop/evershop/src/lib/util/httpStatus');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
const promises = [];
Object.keys(delegate).forEach((id) => {
// Check if middleware is async
if (delegate[id] instanceof Promise) {
promises.push(delegate[id]);
const collection = await delegate.createCollection;
response.status(OK);
response.json({
data: {
...collection,
links: [
{
rel: 'collectionGrid',
href: buildUrl('collectionGrid'),
action: 'GET',
types: ['text/xml']
},
{
rel: 'edit',
href: buildUrl('collectionEdit', { id: collection.uuid }),
action: 'GET',
types: ['text/xml']
}
]
}
});
const result = await delegate.createCollection;
const connection = await delegate.getConnection;
const results = await Promise.allSettled(promises);
const rejected = results.find((r) => r.status === 'rejected');
if (!rejected) {
await commit(connection);
// Load the created collection
const query = select().from('collection');
const collection = await query
.where('collection_id', '=', result.insertId)
.load(pool);
response.status(OK);
response.json({
data: {
...collection,
links: [
{
rel: 'collectionGrid',
href: buildUrl('collectionGrid'),
action: 'GET',
types: ['text/xml']
},
{
rel: 'edit',
href: buildUrl('collectionEdit', { id: collection.uuid }),
action: 'GET',
types: ['text/xml']
}
]
}
});
} else {
await rollback(connection);
response.status(INTERNAL_SERVER_ERROR);
response.json({
data: null,
error: {
status: INTERNAL_SERVER_ERROR,
message: rejected.reason.message
}
});
}
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"code": {
"type": "string"
"type": "string",
"skipEscape": true
}
},
"required": [
"name",
"description",
"code"
],
"additionalProperties": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const createProduct = require('../../services/product/createProduct');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const result = await createProduct(request.body);
const result = await createProduct(request.body, {
routeId: request.currentRoute.id
});
return result;
};
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
/* eslint-disable no-unused-vars */
const { del, select } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
OK,
INTERNAL_SERVER_ERROR,
INVALID_PAYLOAD
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const deleteProductAttribute = require('../../services/attribute/deleteProductAttribute');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
try {
const { id } = request.params;
const attribute = await select()
.from('attribute')
.where('uuid', '=', id)
.load(pool);

if (!attribute) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Invalid attribute id'
}
});
return;
}
await del('attribute').where('uuid', '=', id).execute(pool);

const attribute = await deleteProductAttribute(id, {
routeId: request.currentRoute.id
});
response.status(OK);
response.json({
data: attribute
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
/* eslint-disable no-unused-vars */
const { del, select } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
OK,
INTERNAL_SERVER_ERROR,
INVALID_PAYLOAD
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const deleteCategory = require('../../services/category/deleteCategory');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
try {
const { id } = request.params;
const query = select().from('category');
query
.leftJoin('category_description')
.on(
'category_description.category_description_category_id',
'=',
'category.category_id'
);

const category = await query.where('uuid', '=', id).load(pool);

if (!category) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Category not found'
}
});
return;
}

await del('category').where('uuid', '=', id).execute(pool);
const category = await deleteCategory(id, {
routeId: request.currentRoute.id
});
response.status(OK);
response.json({
data: category
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
/* eslint-disable no-unused-vars */
const { del, select } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
OK,
INTERNAL_SERVER_ERROR,
INVALID_PAYLOAD
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const deleteCollection = require('../../services/collection/deleteCollection');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
try {
const { id } = request.params;
const query = select().from('collection');
const collection = await query.where('uuid', '=', id).load(pool);

if (!collection) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Collection not found'
}
});
return;
}

await del('collection').where('uuid', '=', id).execute(pool);
const collection = await deleteCollection(id, {
routeId: request.currentRoute.id
});
response.status(OK);
response.json({
data: collection
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
const { del, select } = require('@evershop/postgres-query-builder');
const { pool } = require('@evershop/evershop/src/lib/postgres/connection');
const {
OK,
INTERNAL_SERVER_ERROR,
INVALID_PAYLOAD
INTERNAL_SERVER_ERROR
} = require('@evershop/evershop/src/lib/util/httpStatus');
const deleteProduct = require('../../services/product/deleteProduct');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate, next) => {
try {
const { id } = request.params;
const query = select().from('product');
query
.leftJoin('product_description')
.on(
'product_description.product_description_product_id',
'=',
'product.product_id'
);

const product = await query.where('uuid', '=', id).load(pool);

if (!product) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Product not found'
}
});
return;
}

await del('product').where('uuid', '=', id).execute(pool);
const product = await deleteProduct(id, {
routeId: request.currentRoute.id
});
response.status(OK);
response.json({
data: product
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const updateCategory = require('../../services/category/updateCategory');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, delegate) => {
const category = await updateCategory(request.params.id, request.body);
const category = await updateCategory(request.params.id, request.body, {
routeId: request.currentRoute.id
});
return category;
};

This file was deleted.

Loading

0 comments on commit aec67dc

Please sign in to comment.