使用者資料存取
使用者資料
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number | undefined
@Column()
IdUser: string | undefined
@Column()
Password: string | undefined
@Column()
Permission: number | undefined
@Column()
UserName: string | undefined
@Column()
NickName: string | undefined
@Column()
Gender: number | undefined
@Column()
Phone: string | undefined
@Column()
Birthday: string | undefined
@Column()
Email: string | undefined
@CreateDateColumn()
CreatedTime: Date | undefined
@UpdateDateColumn()
UpdatedTime: Date | undefined
}
export interface LoginData {
IdUser: string;
Password: string;
}
DAO
async findUser(IdUser: string) {
return await this.dataSource.manager.findBy(User, { IdUser: IdUser });
}
async saveUser(user: any) {
return await this.dataSource.manager.save(User, user);
}
async deleteUser(IdUser:string) {
return await this.dataSource.createQueryBuilder().delete()
.from(User)
.where("IdUser = :IdUser", { IdUser: IdUser })
.execute();
}
Api
UserController.post('/User/login', (req, res) => {
if (req.body == null)
{
res.status(400).send({status:"body error"});
return;
}
let loginData:LoginData = req.body;
userDao.findUser(loginData.IdUser).then((users: Array<User>) => {
if (users.length > 0)
{
let user:User = users[0];
if (user.Password == loginData.Password)
{
res.send(user);
}
else
{
res.status(400).send({status:"password error"});
}
}
else
{
res.status(400).send({status:"user not found"});
}
});
});
UserController.post('/User/GetUser', (req, res) => {
if (req.body == null)
{
res.status(400).send({status:"body error"});
return;
}
let IdUser = req.body["IdUser"];
userDao.findUser(IdUser).then((users: Array<User>) => {
if (users.length > 0)
{
let user:User = users[0];
res.status(200).send(user);
}
else
{
res.status(400).send({status:"user not found"});
}
});
});
UserController.post('/User/SaveUser', (req, res) => {
if (req.body == null)
{
res.status(400).send({status:"body error"});
return;
}
userDao.saveUser(req.body).then((data: any) => {
res.status(200).send({status:"user save OK"});
});
});
UserController.delete('/User/DeleteUser/:name', (req, res) => {
let name = req.params["name"];
console.log(name);
userDao.deleteUser(name).then((data: any) => {
res.status(200).send({status:"user delete OK"});
});
});
系統版本資料存取
@Entity()
export class SystemVersion {
@PrimaryGeneratedColumn()
id: number | undefined
@Column()
Version: string | undefined
@Column()
Name: string | undefined
@UpdateDateColumn()
UpdatedTime: Date | undefined
}
DAO
async findNewVersion() {
return await this.dataSource.createQueryBuilder()
.select()
.from(SystemVersion, "SystemVersion")
.orderBy("Id", "DESC")
.getOne();
}
Api
SystemVersionController.get('/SystemVersion', (req, res) => {
systemVersionDao.findNewVersion().then((data: any) => {
res.status(200).send(data);
});
})