Skip to content

Instantly share code, notes, and snippets.

@GautamPanickar
Created September 5, 2022 06:00
Show Gist options
  • Save GautamPanickar/4d7174b143ef285beb597ba4639d5ab3 to your computer and use it in GitHub Desktop.
Save GautamPanickar/4d7174b143ef285beb597ba4639d5ab3 to your computer and use it in GitHub Desktop.
import { NextFunction, Request, Response, Router } from 'express';
import { UserInfoDTO } from '../dtos/user-info-dto';
import UserService from '../services/user-service';
import Controller from '../types/controller';
import HttpResponse from '../utils/response/httpresponse';
import UserResponse from '../utils/response/userresponse';
class UserController implements Controller {
public path: string = '/user';
public router: Router = Router();
private userService: UserService;
public constructor() {
this.userService = new UserService();
this.initializeRoutes();
}
/**
* Routes for pos actions.
*/
private initializeRoutes(): void {
this.router.get(`${this.path}/info`, this.getInfo);
this.router.post(`${this.path}/save`, this.saveInfo);
}
public getInfo = async(request: Request, response: Response, next: NextFunction) => {
try {
const info: UserInfoDTO = this.userService.getUserInfo();
return response.send(new UserResponse({
status: 200,
info: info,
message: `User details ${info ? 'found' : 'could not be found'}`,
result: info ? true : false
}));
} catch (error) {
next(error);
}
}
public saveInfo = async(request: Request, response: Response, next: NextFunction) => {
try {
const saved = this.userService.saveUserInfo(request.body);
return response.send(new HttpResponse(
200,
`User details ${saved ? 'saved' : 'could not be saved'}`,
saved ? true : false
));
} catch (error) {
next(error);
}
}
}
export default UserController;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment