🔧 Refactored methods into utils
This commit is contained in:
@@ -100,7 +100,7 @@ export default class ApiService extends Service {
|
||||
this.logger.info("Client connected via websocket!");
|
||||
|
||||
client.on(
|
||||
"call",
|
||||
"importComicsInDB",
|
||||
async ({ action, params, opts }, done) => {
|
||||
this.logger.info(
|
||||
"Received request from client! Action:",
|
||||
@@ -117,11 +117,27 @@ export default class ApiService extends Service {
|
||||
extractionOptions,
|
||||
folder
|
||||
);
|
||||
const dbImportResult =
|
||||
await this.broker.call(
|
||||
"import.rawImportToDB",
|
||||
{
|
||||
importStatus: {
|
||||
isImported: true,
|
||||
tagged: false,
|
||||
matchedResult: {
|
||||
score: "0",
|
||||
},
|
||||
},
|
||||
rawFileDetails:
|
||||
comicBookCoverMetadata,
|
||||
},
|
||||
{}
|
||||
);
|
||||
|
||||
client.emit(
|
||||
"comicBookCoverMetadata",
|
||||
comicBookCoverMetadata
|
||||
);
|
||||
client.emit("comicBookCoverMetadata", {
|
||||
comicBookCoverMetadata,
|
||||
dbImportResult,
|
||||
});
|
||||
});
|
||||
|
||||
case "single":
|
||||
|
||||
62
services/imagetransformation.service.ts
Normal file
62
services/imagetransformation.service.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
"use strict";
|
||||
import {
|
||||
Context,
|
||||
Service,
|
||||
ServiceBroker,
|
||||
ServiceSchema,
|
||||
Errors,
|
||||
} from "moleculer";
|
||||
import { resizeImage } from "../utils/imagetransformation.utils";
|
||||
|
||||
export default class ProductsService extends Service {
|
||||
// @ts-ignore
|
||||
public constructor(
|
||||
public broker: ServiceBroker,
|
||||
schema: ServiceSchema<{}> = { name: "imagetransformation" }
|
||||
) {
|
||||
super(broker);
|
||||
this.parseServiceSchema(
|
||||
Service.mergeSchemas(
|
||||
{
|
||||
name: "imagetransformation",
|
||||
mixins: [],
|
||||
settings: {
|
||||
// Available fields in the responses
|
||||
fields: ["_id", "name", "quantity", "price"],
|
||||
|
||||
// Validator for the `create` & `insert` actions.
|
||||
entityValidator: {
|
||||
name: "string|min:3",
|
||||
price: "number|positive",
|
||||
},
|
||||
},
|
||||
hooks: {},
|
||||
actions: {
|
||||
resize: {
|
||||
rest: "POST /resizeImage",
|
||||
params: {},
|
||||
async handler(
|
||||
ctx: Context<{
|
||||
path: string;
|
||||
newWidth: number;
|
||||
newHeight: number;
|
||||
outputPath: string;
|
||||
}>
|
||||
) {
|
||||
const resizeResult = await resizeImage(
|
||||
ctx.params.path,
|
||||
ctx.params.outputPath,
|
||||
ctx.params.newWidth,
|
||||
ctx.params.newHeight
|
||||
);
|
||||
return { resizeOperationStatus: resizeResult };
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: {},
|
||||
},
|
||||
schema
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
"use strict";
|
||||
import { Context, Service, ServiceBroker, ServiceSchema } from "moleculer";
|
||||
import {
|
||||
Context,
|
||||
Service,
|
||||
ServiceBroker,
|
||||
ServiceSchema,
|
||||
Errors,
|
||||
} from "moleculer";
|
||||
import { DbMixin } from "../mixins/db.mixin";
|
||||
import Comic from "../models/comic.model";
|
||||
import { walkFolder } from "../utils/uncompression.utils";
|
||||
import { walkFolder } from "../utils/file.utils";
|
||||
import { convertXMLToJSON } from "../utils/xml.utils";
|
||||
|
||||
export default class ProductsService extends Service {
|
||||
@@ -51,22 +57,43 @@ export default class ProductsService extends Service {
|
||||
},
|
||||
rawImportToDB: {
|
||||
rest: "POST /rawImportToDB",
|
||||
params: { payload: "object" },
|
||||
params: {},
|
||||
async handler(ctx: Context<{ payload: object }>) {
|
||||
return new Promise((resolve, reject) => {
|
||||
Comic.create(
|
||||
ctx.params.payload,
|
||||
(error, data) => {
|
||||
if (data) {
|
||||
resolve(data);
|
||||
} else if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
Comic.create(ctx.params, (error, data) => {
|
||||
if (data) {
|
||||
resolve(data);
|
||||
} else if (error) {
|
||||
throw new Errors.MoleculerError(
|
||||
"Failed to import comic book",
|
||||
400,
|
||||
"IMS_FAILED_COMIC_BOOK_IMPORT",
|
||||
data
|
||||
);
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
},
|
||||
getRecentlyImportedComicBooks: {
|
||||
rest: "POST /getRecentlyImportedComicBooks",
|
||||
params: {},
|
||||
async handler(
|
||||
ctx: Context<{ paginationOptions: object }>
|
||||
) {
|
||||
return await Comic.paginate(
|
||||
{},
|
||||
ctx.params.paginationOptions
|
||||
);
|
||||
},
|
||||
},
|
||||
getComicBookById: {
|
||||
rest: "POST /getComicBookById",
|
||||
params: { id: "string" },
|
||||
async handler(ctx: Context<{ id: string }>) {
|
||||
return await Comic.findById(ctx.params.id);
|
||||
},
|
||||
},
|
||||
},
|
||||
methods: {},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user