Skip to content

Commit

Permalink
Implemented crud over user table on mysql db
Browse files Browse the repository at this point in the history
  • Loading branch information
ashrash committed Oct 15, 2022
1 parent 9e005a1 commit cd4f884
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dist
18 changes: 18 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach by Process ID",
"processId": "${command:PickProcess}",
"request": "attach",
"skipFiles": [
"<node_internals>/**"
],
"type": "node"
}

]
}
5 changes: 3 additions & 2 deletions DDL.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
-- DB DDL script for OSlash clone

CREATE TABLE `user`
CREATE TABLE `users`
(
`id` bigint NOT NULL ,
`id` bigint NOT NULL AUTO_INCREMENT,
`email` varchar(100) NOT NULL ,
`name` varchar(100) NOT NULL ,
`password` varchar(100) NOT NULL ,
`created_dttm` datetime NOT NULL ,
`updated_dttm` datetime NOT NULL ,
Expand Down
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import express from 'express';
import morgan from 'morgan';
import sequelize from './config/mysql';
import databaseObj from './models';
import swaggerJSDoc from 'swagger-jsdoc';
import swaggerUi from 'swagger-ui-express';
import { Routes } from './interfaces/routes.interface';
Expand Down Expand Up @@ -38,6 +38,7 @@ class App {
}

private connectToDatabase() {
const { sequelize } = databaseObj;
sequelize.authenticate().then(() => {
console.log('Connection has been established successfully.');
}).catch((error) => {
Expand Down
6 changes: 0 additions & 6 deletions src/config/mysql.ts

This file was deleted.

20 changes: 20 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextFunction, Request, Response } from 'express';
import { UserData } from '../interfaces/user.interface';
import userService from '../services/user.service';

class UsersController {
public userService = new userService();

public getUserByEmail = async (req: Request, res: Response, next: NextFunction) => {
try {
const email: string = req.params.email;
const findOneUserData: UserData = await this.userService.findUserDataByEmail(email);

res.status(200).json({ data: findOneUserData, message: 'findOne' });
} catch (error) {
next(error);
}
};
}

export default UsersController;
17 changes: 17 additions & 0 deletions src/dtos/users.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { IsString, IsNotEmpty } from 'class-validator';

export class CreateUserDto {

@IsString()
@IsNotEmpty()
public email: string;

@IsString()
@IsNotEmpty()
public password: string;
}





8 changes: 8 additions & 0 deletions src/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface UserData {
_id: number;
email: string;
password: string;
created_dttm: string;
updated_dttm: string;
}

13 changes: 13 additions & 0 deletions src/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Sequelize, DataTypes } from 'sequelize';
import User from './user.model';
import { DB_HOST, DB_PASSWORD, DB_USERNAME } from '../config/index';

const sequelize = new Sequelize(`mysql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/slash`);

const databaseObj = {
Sequelize,
sequelize,
User : User(sequelize, Sequelize, DataTypes),
};

export default databaseObj;
22 changes: 22 additions & 0 deletions src/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const User = (sequelize, Sequelize, DataTypes) => {
const User = sequelize.define(
"users",
{
email: {
type: DataTypes.STRING,
unique: true,
primaryKey: true
},
password: {
type: DataTypes.STRING
}
},
{
createdAt: "created_dttm",
updatedAt: "updated_dttm"
}
);
return User;
};

export default User;
19 changes: 19 additions & 0 deletions src/routes/auth.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import { Router } from 'express';
import { Routes } from '../interfaces/routes.interface';

class HealthRoute implements Routes {
public route = '/health';
public router = Router();
public healthController = new HealthController();

constructor() {
this.initializeRoutes();
}

private initializeRoutes() {
this.router.get(`${this.route}`, this.healthController.healthCheck);
}
}

export default HealthRoute;
19 changes: 19 additions & 0 deletions src/routes/user.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Router } from 'express';
import UsersController from '../controllers/user.controller';
import { Routes } from '../interfaces/routes.interface';

class UsersRoute implements Routes {
public route = '/user';
public router = Router();
public usersController = new UsersController();

constructor() {
this.initializeRoutes();
}

private initializeRoutes() {
this.router.get(`${this.route}/:email`, this.usersController.getUserByEmail);
}
}

export default UsersRoute;
3 changes: 2 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import App from './app';
import HealthRoute from './routes/health.route';
import UserRoute from './routes/user.route';

const app = new App([new HealthRoute()]);
const app = new App([new HealthRoute(), new UserRoute()]);

app.listen();

Expand Down
20 changes: 20 additions & 0 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { UserData } from '../interfaces/user.interface';
import { HttpException } from '../utils/exception';
import Db from '../models';
import * as R from 'ramda';
import { nullCheck } from '../utils/ramda';

class UserService {

public async findUserDataByEmail(email: string): Promise<UserData> {
if (nullCheck(email)) throw new HttpException(400, "email is undefined");

const userResult: UserData = await Db.User.findByPk(email);
if (!userResult) throw new HttpException(204, '');

return userResult;
}

}

export default UserService;

0 comments on commit cd4f884

Please sign in to comment.