Commit 4f6cd08d authored by chinguyen's avatar chinguyen

add_delete_update_api

parent 840e93d9
import * as uuid from 'uuid' import * as uuid from 'uuid'
import { Students } from './../sqlz/models/students'; import { Students } from './../sqlz/models/students'
export function create(students: any): Promise<any>{ export function create(students: any): Promise<any> {
return Students.create({ return Students.create({
id: uuid.v1(), id: uuid.v1(),
code: students.code,
firstname: students.firstname, firstname: students.firstname,
lastname: students.lastname, lastname: students.lastname,
email: students.email, email: students.email,
sdt: students.sdt sdt: students.sdt
}) })
} }
export function findAll(): Promise<any>{ export function findAll(): Promise<any> {
return Students.findAll() return Students.findAll()
} }
export function deleteUser(id: any): Promise<any>{ export function deleteUser(code: any): Promise<any> {
return Students.destroy({ return Students.destroy({
where: {id: id} where:{code}
})
}
export function updateUser(code: any, students: any): Promise<any> {
return Students.findOne({
where: {code}
}).then(function(student) {
if (student) {
student.update({
firstname: students.firstname,
lastname: students.lastname,
email: students.email,
sdt: students.sdt
})
}
}) })
} }
import * as StudentsGet from './students.get' import * as StudentsGet from './students.get'
import * as StudentsPost from './students.post' import * as StudentsPost from './students.post'
import * as StudentsDelete from './students.delete' import * as StudentsDelete from './students.delete'
import * as StudentsPut from './students.put'
export { StudentsGet, StudentsPost, StudentsDelete } export { StudentsGet, StudentsPost, StudentsDelete, StudentsPut }
\ No newline at end of file \ No newline at end of file
import { Request, Response } from 'express' import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index' import { StudentDao } from '../../dao/_index'
export function deleteUser(req: Request, res: Response){ export function deleteUser(req: Request, res: Response) {
return StudentDao.deleteUser(req.params.id).then(res.status(202).send({ return StudentDao.deleteUser(req.params.code).then(res.status(200).json({success: true, message: 'delete success'}))
message: 'delete successfull'
})).catch(error => res.boom.badRequest(error))
} }
...@@ -2,7 +2,7 @@ import { Request, Response } from 'express' ...@@ -2,7 +2,7 @@ import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index' import { StudentDao } from '../../dao/_index'
export function getAllUsers(req: Request, res: Response){ export function getAllUsers(req: Request, res: Response) {
return StudentDao return StudentDao
.findAll() .findAll()
.then(students => res.status(200).send(students)) .then(students => res.status(200).send(students))
......
import { Request, Response } from 'express' import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index' import { StudentDao } from '../../dao/_index'
export function Add(req: Request, res: Response){ export function Add(req: Request, res: Response) {
return StudentDao return StudentDao
.create(req.body) .create(req.body)
.then(students => res.status(201).send(students)) .then(students => res.status(200).send(students))
.catch(error => res.boom.badRequest(error)) .catch(error => res.boom.badRequest(error))
} }
import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index'
export function updateUser(req: Request, res: Response) {
return StudentDao.updateUser(req.params.code, req.body).then(res.status(200).json({success: true, message: 'update successful'}))
}
\ No newline at end of file
import { Express } from 'express' import { Express } from 'express'
import { StudentsController } from '../endpoints/_index' import { StudentsController } from '../endpoints/_index'
export function routes(app: Express){ export function routes(app: Express) {
app.get('/api/getStudents', StudentsController.StudentsGet.getAllUsers) app.get('/api/getStudents', StudentsController.StudentsGet.getAllUsers)
app.post('/api/addStudent', StudentsController.StudentsPost.Add) app.post('/api/addStudent', StudentsController.StudentsPost.Add)
app.delete('/api/deleteStudent', StudentsController.StudentsDelete.deleteUser) app.delete('/api/deleteStudent/:code', StudentsController.StudentsDelete.deleteUser)
app.put('/api/updateStudent/:code',StudentsController.StudentsPut.updateUser)
} }
\ No newline at end of file
'use strict'; 'use strict';
module.exports = { module.exports = {
up: async (queryInterface, Sequelize) => { up: async(queryInterface, Sequelize) => {
await queryInterface.createTable('Students', { await queryInterface.createTable('Students', {
id: { id: {
allowNull: false, allowNull: false,
autoIncrement: true,
primaryKey: true, primaryKey: true,
type: Sequelize.INTEGER type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV1
},
code: {
type: Sequelize.STRING
}, },
firstname: { firstname: {
type: Sequelize.STRING type: Sequelize.STRING
...@@ -18,19 +21,19 @@ module.exports = { ...@@ -18,19 +21,19 @@ module.exports = {
type: Sequelize.STRING type: Sequelize.STRING
}, },
sdt: { sdt: {
type: Sequelize.NUMBER type: Sequelize.STRING
}, },
createdAt: { createdAt: {
allowNull: false, allowNull: true,
type: Sequelize.DATE type: Sequelize.DATE
}, },
updatedAt: { updatedAt: {
allowNull: false, allowNull: true,
type: Sequelize.DATE type: Sequelize.DATE
} }
}); });
}, },
down: async (queryInterface, Sequelize) => { down: async(queryInterface, Sequelize) => {
await queryInterface.dropTable('Students'); await queryInterface.dropTable('Students');
} }
}; };
\ No newline at end of file
import { Model, STRING, UUID, Deferrable,NUMBER } from 'sequelize' import { Model, STRING, UUID, Deferrable, NUMBER, DATE, INTEGER, BIGINT } from 'sequelize'
import sequelize from './_index' import sequelize from './_index'
export class Students extends Model { export class Students extends Model {
...@@ -7,16 +7,20 @@ export class Students extends Model { ...@@ -7,16 +7,20 @@ export class Students extends Model {
export class StudentsModel { export class StudentsModel {
id: string id: string
code: string
firstname: string firstname: string
lastname: string lastname: string
email: string email: string
sdt: number sdt: string
createdAt: Date
updatedAt: Date
} }
Students.init({ Students.init({
firstname: STRING(50), code: STRING,
lastname: STRING(50), firstname: STRING,
email: STRING(50), lastname: STRING,
sdt: NUMBER email: STRING,
sdt: STRING
}, { }, {
sequelize, sequelize,
modelName: 'Students', modelName: 'Students',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment