Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh Raj committed Jan 26, 2024
1 parent dd958c4 commit 80903c6
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/models/comments.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// comments-model.ts - A mongoose model
//
// See http://mongoosejs.com/docs/models.html
// for more of what you can do here.
import { Application } from '../declarations';
import { Model, Mongoose } from 'mongoose';

export default function (app: Application): Model<any> {
const modelName = 'comments';
const mongooseClient: Mongoose = app.get('mongooseClient');
const { Schema } = mongooseClient;
const schema = new Schema({
text: { type: String, required: true }
}, {
timestamps: true
});

// This is necessary to avoid model compilation errors in watch mode
// see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
if (mongooseClient.modelNames().includes(modelName)) {
(mongooseClient as any).deleteModel(modelName);
}
return mongooseClient.model<any>(modelName, schema);
}
9 changes: 9 additions & 0 deletions src/services/comments/comments.class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Service, MongooseServiceOptions } from 'feathers-mongoose';
import { Application } from '../../declarations';

export class Comments extends Service {
//eslint-disable-next-line @typescript-eslint/no-unused-vars
constructor(options: Partial<MongooseServiceOptions>, app: Application) {
super(options);
}
}
37 changes: 37 additions & 0 deletions src/services/comments/comments.hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { HooksObject } from '@feathersjs/feathers';
import * as authentication from '@feathersjs/authentication';
// Don't remove this comment. It's needed to format import lines nicely.

const { authenticate } = authentication.hooks;

export default {
before: {
all: [ authenticate('jwt') ],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

after: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
},

error: {
all: [],
find: [],
get: [],
create: [],
update: [],
patch: [],
remove: []
}
};
28 changes: 28 additions & 0 deletions src/services/comments/comments.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Initializes the `comments` service on path `/comments`
import { ServiceAddons } from '@feathersjs/feathers';
import { Application } from '../../declarations';
import { Comments } from './comments.class';
import createModel from '../../models/comments.model';
import hooks from './comments.hooks';

// Add this service to the service type index
declare module '../../declarations' {
interface ServiceTypes {
'comments': Comments & ServiceAddons<any>;
}
}

export default function (app: Application): void {
const options = {
Model: createModel(app),
paginate: app.get('paginate')
};

// Initialize our service with any options it requires
app.use('/comments', new Comments(options, app));

// Get our initialized service so that we can register hooks
const service = app.service('comments');

service.hooks(hooks);
}
2 changes: 2 additions & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Application } from '../declarations';
import users from './users/users.service';
import reviews from './reviews/reviews.service';
import faculties from './faculties/faculties.service';
import comments from './comments/comments.service';
// Don't remove this comment. It's needed to format import lines nicely.

export default function (app: Application): void {
app.configure(users);
app.configure(reviews);
app.configure(faculties);
app.configure(comments);
}
8 changes: 8 additions & 0 deletions test/services/comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import app from '../../src/app';

describe('\'comments\' service', () => {
it('registered the service', () => {
const service = app.service('comments');
expect(service).toBeTruthy();
});
});

0 comments on commit 80903c6

Please sign in to comment.