iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0
SideProject30

Electron Angular軟體架構與簡易功能實作學習路程實記系列 第 11

Day 11- 功能規格設計-後端Api開發設計-1

  • 分享至 

  • xImage
  •  

使用者資料存取

使用者資料

@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);
  });
})

上一篇
Day 10 - 功能規格設計-資料庫規劃
下一篇
Day 12- 功能規格設計-後端Api開發設計-2
系列文
Electron Angular軟體架構與簡易功能實作學習路程實記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言