Commit 4f6cd08d authored by chinguyen's avatar chinguyen

add_delete_update_api

parent 840e93d9
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({
id: uuid.v1(),
code: students.code,
firstname: students.firstname,
lastname: students.lastname,
email: students.email,
sdt: students.sdt
})
}
export function findAll(): Promise<any>{
export function findAll(): Promise<any> {
return Students.findAll()
}
export function deleteUser(id: any): Promise<any>{
export function deleteUser(code: any): Promise<any> {
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 StudentsPost from './students.post'
import * as StudentsDelete from './students.delete'
import * as StudentsPut from './students.put'
export { StudentsGet, StudentsPost, StudentsDelete }
\ No newline at end of file
export { StudentsGet, StudentsPost, StudentsDelete, StudentsPut }
\ No newline at end of file
import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index'
export function deleteUser(req: Request, res: Response){
return StudentDao.deleteUser(req.params.id).then(res.status(202).send({
message: 'delete successfull'
})).catch(error => res.boom.badRequest(error))
export function deleteUser(req: Request, res: Response) {
return StudentDao.deleteUser(req.params.code).then(res.status(200).json({success: true, message: 'delete success'}))
}
......@@ -2,7 +2,7 @@ import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index'
export function getAllUsers(req: Request, res: Response){
export function getAllUsers(req: Request, res: Response) {
return StudentDao
.findAll()
.then(students => res.status(200).send(students))
......
import { Request, Response } from 'express'
import { StudentDao } from '../../dao/_index'
export function Add(req: Request, res: Response){
export function Add(req: Request, res: Response) {
return StudentDao
.create(req.body)
.then(students => res.status(201).send(students))
.then(students => res.status(200).send(students))
.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 { StudentsController } from '../endpoints/_index'
export function routes(app: Express){
export function routes(app: Express) {
app.get('/api/getStudents', StudentsController.StudentsGet.getAllUsers)
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';
module.exports = {
up: async (queryInterface, Sequelize) => {
up: async(queryInterface, Sequelize) => {
await queryInterface.createTable('Students', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV1
},
code: {
type: Sequelize.STRING
},
firstname: {
type: Sequelize.STRING
......@@ -18,19 +21,19 @@ module.exports = {
type: Sequelize.STRING
},
sdt: {
type: Sequelize.NUMBER
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
allowNull: true,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
allowNull: true,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
down: async(queryInterface, Sequelize) => {
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'
export class Students extends Model {
......@@ -7,16 +7,20 @@ export class Students extends Model {
export class StudentsModel {
id: string
code: string
firstname: string
lastname: string
email: string
sdt: number
sdt: string
createdAt: Date
updatedAt: Date
}
Students.init({
firstname: STRING(50),
lastname: STRING(50),
email: STRING(50),
sdt: NUMBER
code: STRING,
firstname: STRING,
lastname: STRING,
email: STRING,
sdt: STRING
}, {
sequelize,
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