item-detail.service.ts 1.91 KB
Newer Older
1 2
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
3
import { ItemDetail, SearchItemDetailModel } from '../models/itemDetail.model';
Your Name committed
4
import { Observable } from 'rxjs';
5 6

@Injectable({ providedIn: 'root' })
7
export class ItemDetailService {
8 9

  constructor(private http: HttpClient) { }
Your Name committed
10
  url: string = "http://915e-49-0-64-28.ngrok.io";
Your Name committed
11
  listItemDetail: ItemDetail[] = []
12

13 14 15 16
  addItemDetail(model: ItemDetail) {
    console.log(this.listItemDetail);
    this.listItemDetail.push(model);
    this.listItemDetail.reverse
17 18
  }

Your Name committed
19 20
  getListItemDetail(): Observable<ItemDetail[]> {
    return this.http.get<ItemDetail[]>(this.url + "/lend/lists");
21
  }
22

23
  searchListItemDetail(searchModel: SearchItemDetailModel) {
24 25
    this.http.get(this.url + '&startDate=' + searchModel.sDate.split("-")[2] + '-' + searchModel.sDate.split("-")[1] + '-' + searchModel.sDate.split("-")[0] + '&endDate=' + searchModel.eDate.split("-")[2] + '-' + searchModel.eDate.split("-")[1] + '-' + searchModel.eDate.split("-")[0] + '&startTime=' + searchModel.sTime + ':00' + '&endTime=' + searchModel.eTime + ':00').subscribe((response: any) => {
      console.log(response);
26
      this.listItemDetail = response;
27 28 29 30 31 32
    }, error => {
      console.log(error)
      alert("ไม่สามารถค้นหาอุปกรณ์ที่ว่างได้ เนื่องจาก :\n" + error.message)
    })
  }

Your Name committed
33 34
  deleteItemDetail(model: ItemDetail) {
    this.http.delete(this.url + "/" + model.id).subscribe(response => {
35
      console.log(response);
36
      this.getListItemDetail()
37 38 39 40
      alert("ลบข้อมูลอุปกรณ์สำเร็จ")
    })
  }

Your Name committed
41 42 43 44 45
  updateItemDetail(model: ItemDetail) {
    this.http.put(this.url + "/lend/" +model.id, model).subscribe(response => {
      console.log(response);
      alert("อัพเดทข้อมูลอุปกรณ์สำเร็จ")
    })
46 47 48
  }

}