Skip to content

Commit a3b570a

Browse files
committed
updates
1 parent 5b223b4 commit a3b570a

File tree

8 files changed

+234
-89
lines changed

8 files changed

+234
-89
lines changed

Diff for: package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"fastify": "^3.0.2",
2323
"fastify-cors": "^4.0.0",
2424
"fastify-mongodb": "^2.0.1",
25+
"mongodb": "^3.5.9",
2526
"pino": "^6.3.2"
2627
},
2728
"devDependencies": {

Diff for: src/modules/base/base-index.ts

+30-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import BaseService from "./base-service";
22
import BaseSchema from "./base-schema";
3-
import { FastifyInstance } from "fastify";
3+
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
44

55
export default class BaseIndex {
66
private prefix: string;
@@ -15,27 +15,41 @@ export default class BaseIndex {
1515
}
1616

1717
public register() {
18+
this.fastifyInstance.post(
19+
this.prefix,
20+
{ schema: this.schema.createSchema },
21+
async (request: FastifyRequest, reply: FastifyReply) => this.create(request, reply)
22+
);
23+
this.fastifyInstance.get(
24+
this.prefix,
25+
{ schema: this.schema.getAllSchema },
26+
async (request: FastifyRequest, reply: FastifyReply) => this.index(request, reply)
27+
);
28+
this.fastifyInstance.get(
29+
this.prefix + '/:id',
30+
{ schema: this.schema.getOneSchema },
31+
async (request: FastifyRequest, reply: FastifyReply) => this.show(request, reply)
32+
);
33+
}
1834

19-
this.fastifyInstance.post(this.prefix, { schema: this.schema.createSchema }, this.create);
20-
//this.fastifyInstance.get(this.prefix, { schema: this.schema.getOneSchema }, this.show);
21-
this.fastifyInstance.get(this.prefix, { schema: this.schema.getAllSchema }, async (request: any, reply: any) => {
22-
const resp = await this.service.index();
23-
return reply.send(resp);
24-
});
25-
this.fastifyInstance.get(this.prefix + '/:id', { schema: this.schema.getOneSchema }, async (request: any, reply: any) => {
26-
const resp = await this.service.show(request.params.id);
27-
return reply.send(resp);
28-
});
29-
35+
public async create(request: any, reply: any) {
36+
const resp = await this.service.create(request.body);
37+
return reply.send(resp.toResponse());
3038
}
3139

32-
public async create(req: any, res: any) {
33-
return {};
40+
public async index(request: any, reply: any) {
41+
const resp = await this.service.index();
42+
return reply.send(resp);
43+
//return reply.send({ "name": "u" });
3444
}
3545

3646
public async show(request: any, reply: any) {
3747
const resp = await this.service.show(request.params.id);
38-
return reply.send(resp);
39-
//return reply.send({ "name": "u" });
48+
if (resp === undefined || resp == null) {
49+
return reply.code(404).send(new Error("Item not found"));
50+
}
51+
else {
52+
return reply.send(resp);
53+
}
4054
}
4155
}

Diff for: src/modules/base/base-model.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { FastifyInstance } from "fastify";
22

33
export default class BaseModel {
4-
private fastifyInstance: any;
5-
public static collectionName: string;
4+
private static _collectionName: string;
5+
66
public _id!: string;
77

8-
constructor(fastifyInstance: any) {
9-
this.fastifyInstance = fastifyInstance;
8+
get collectionName() {
9+
return BaseModel._collectionName;
1010
}
1111

1212
get id(): string {
1313
return this._id;
1414
}
1515

1616
get pathPrefix() {
17-
return BaseModel.collectionName;
17+
return BaseModel._collectionName;
18+
}
19+
20+
public toResponse() {
21+
//delete this._collectionName;
22+
return this;
1823
}
24+
1925
}

Diff for: src/modules/base/base-schema.ts

+24-19
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
export default class BaseSchema {
33
public properties = {};
4-
5-
get getOneSchema() {
4+
public required: string[] = [];
5+
get getOneSchema(): any {
66
return {
77
response: {
88
200: {
99
type: 'object',
10-
properties: this.properties
11-
}
12-
}
10+
properties: this.properties,
11+
},
12+
},
1313
};
1414
}
1515

@@ -20,22 +20,27 @@ export default class BaseSchema {
2020
type: 'array',
2121
items: {
2222
type: 'object',
23-
properties: this.properties
24-
}
25-
}
26-
}
23+
properties: this.properties,
24+
},
25+
},
26+
},
2727
};
2828
}
2929

3030
get createSchema() {
3131
return {
32-
body: this.properties,
32+
body: {
33+
type: 'object',
34+
required: this.required,
35+
properties:
36+
this.properties,
37+
},
3338
response: {
3439
201: {
3540
type: 'object',
36-
properties: this.properties
37-
}
38-
}
41+
properties: this.properties,
42+
},
43+
},
3944
};
4045
}
4146

@@ -45,9 +50,9 @@ export default class BaseSchema {
4550
response: {
4651
200: {
4752
type: 'object',
48-
properties: this.properties
49-
}
50-
}
53+
properties: this.properties,
54+
},
55+
},
5156
};
5257
}
5358

@@ -56,9 +61,9 @@ export default class BaseSchema {
5661
response: {
5762
204: {
5863
type: 'object',
59-
properties: this.properties
60-
}
61-
}
64+
properties: this.properties,
65+
},
66+
},
6267
}
6368
}
6469
}

Diff for: src/modules/base/base-service.ts

+44-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
1-
import BaseModel from "./base-model";
2-
import { ObjectId } from "mongodb";
1+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2+
/* eslint-disable no-underscore-dangle */
3+
import { ObjectId } from 'mongodb';
4+
import BaseModel from './base-model';
35

46
export default class BaseService {
57
private modelType: any;
8+
69
private fastifyInstance: any;
7-
protected _collection: string = "";
10+
11+
protected _collection = '';
12+
813
constructor(modelType: any, fastifyInstance: any) {
9-
this.modelType = modelType.constructor;
14+
this.modelType = modelType;
1015
this.fastifyInstance = fastifyInstance;
1116
}
1217

13-
get collectionName() {
18+
get collectionName(): string {
1419
return this._collection;
1520
}
1621

17-
public create(body: any) {
18-
//this.fastifyInstance.mongo.db
19-
//.fastifyInstance.mongo.db.collection(this.model.collectionName)
22+
public async create(body: any): Promise<BaseModel> {
23+
try {
24+
const resp = await this.fastifyInstance.mongo.db.collection(this.collectionName).insertOne(body);
25+
if (resp == null || resp === undefined || resp.ops.length < 1) {
26+
throw new Error("Insert didn't work");
27+
}
28+
else {
29+
return Object.assign(new this.modelType(), resp.ops[0]);
30+
}
31+
}
32+
catch (e) {
33+
console.log(e);
34+
throw new Error(e);
35+
}
2036
}
2137

22-
public async show(id: string) {
23-
const resp = await this.fastifyInstance.mongo.db.collection(this.collectionName).findOne({ _id: new ObjectId(id) });
24-
return resp;
38+
public async show(id: string): Promise<BaseModel> {
39+
try {
40+
const resp = await this.fastifyInstance.mongo.db.collection(this.collectionName)
41+
.findOne({ _id: new ObjectId(id) });
42+
if (resp !== null && resp !== undefined) {
43+
return Object.assign(new this.modelType(), resp);
44+
}
45+
return resp;
46+
} catch (e) {
47+
console.log(e);
48+
throw new Error(e);
49+
}
2550
}
2651

27-
public async index() {
28-
const resp = await this.fastifyInstance.mongo.db.collection(this.collectionName).find({});
29-
return resp.toArray();
52+
public async index(): Promise<BaseModel[]> {
53+
try {
54+
const resp = await this.fastifyInstance.mongo.db.collection(this.collectionName).find({});
55+
return resp.toArray();
56+
} catch (e) {
57+
console.log(e);
58+
throw new Error(e);
59+
}
3060
}
3161
}
+90-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,95 @@
11
import BaseModel from "../base/base-model";
22

33
export default class ProductCategory extends BaseModel {
4-
collectionName = "product_categories";
4+
_collectionName: string = "product_categories";
5+
// private _name!: string;
6+
// private _abstract?: string | null = null;
7+
// private _description?: string | null = null;
8+
// private _image_path?: string | null = null;
9+
// private _meta_title?: string | null = null;
10+
// private _meta_keywords?: string | null = null;
11+
// private _meta_description?: string | null = null;
12+
// private _url_friendly_name?: string | null = null;
13+
// private _published: boolean = true;
14+
515
public name!: string;
6-
public abstract?: string;
7-
public description?: string;
8-
public image_path?: string;
9-
public meta_title?: string;
10-
public meta_keywords?: string;
11-
public meta_description?: string;
12-
public url_friendly_name?: string;
13-
public published?: boolean;
16+
public abstract?: string | null = null;
17+
public description?: string | null = null;
18+
public image_path?: string | null = null;
19+
public meta_title?: string | null = null;
20+
public meta_keywords?: string | null = null;
21+
public meta_description?: string | null = null;
22+
public url_friendly_name?: string | null = null;
23+
public published: boolean = true;
24+
25+
// set name(value: string) {
26+
// this._name = value;
27+
// }
28+
29+
// get name() {
30+
// return this._name;
31+
// }
32+
33+
// set abstract(value: string | undefined | null) {
34+
// this._abstract = value;
35+
// }
36+
37+
// get abstract() {
38+
// return this._abstract;
39+
// }
40+
41+
// get description() {
42+
// return this._description;
43+
// }
44+
45+
// set description(value: string | undefined | null) {
46+
// this._description = value;
47+
// }
48+
49+
// get image_path() {
50+
// return this._image_path;
51+
// }
52+
53+
// set image_path(value: string | undefined | null) {
54+
// this._image_path = value;
55+
// }
56+
57+
// get meta_title() {
58+
// return this._meta_title;
59+
// }
60+
61+
// set meta_title(value: string | undefined | null) {
62+
// this._meta_title = value;
63+
// }
64+
65+
// get meta_keywords() {
66+
// return this._meta_keywords;
67+
// }
68+
69+
// set meta_keywords(value: string | undefined | null) {
70+
// this._meta_keywords = value;
71+
// }
72+
73+
// get meta_description() {
74+
// return this._meta_description;
75+
// }
76+
// set meta_description(value: string | undefined | null) {
77+
// this._meta_description = value;
78+
// }
79+
80+
// get url_friendly_name() {
81+
// return this._url_friendly_name;
82+
// }
83+
84+
// set url_friendly_name(value: string | undefined | null) {
85+
// this._url_friendly_name = value;
86+
// }
87+
88+
// get published() {
89+
// return this._published;
90+
// }
91+
92+
// set published(value: boolean) {
93+
// this._published = value;
94+
// }
1495
}

0 commit comments

Comments
 (0)