Skip to content

Commit fe4f862

Browse files
author
Khabib Murtuzaaliev
committed
save food to drf
1 parent b628075 commit fe4f862

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

src/app/food/food.service.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,8 @@ export class FoodService {
6363
else {
6464
// Загрузка из REST
6565
this.loadRest().subscribe(
66-
(resp: Response) => {
67-
let respfood = resp.json()["food"]
68-
if (respfood != null) {
69-
this.food = respfood;
70-
} else {
71-
this.food = {};
72-
}
73-
respfood != null ? this.food = respfood : this.food = {};
74-
66+
(food) => {
67+
food != null ? this.food = food : this.food = {};
7568
this.saveFood();
7669
this.foodsChanged.emit();
7770
},
@@ -89,22 +82,36 @@ export class FoodService {
8982

9083

9184
private getUrl(): string {
92-
return `https://pushreceiver-26e46.firebaseio.com/food.json?auth=${this.tk}`
85+
return `http://localhost:8000/api/v1/menu/`
9386
}
9487

9588
loadRest() {
96-
return this.http.get(this.getUrl());
89+
return this.http.get(this.getUrl())
90+
.map((response:Response)=>{
91+
let f = {}
92+
let weeks = response.json()["results"]
93+
for(let i = 0; i<weeks.length; i++){
94+
let week = weeks[i];
95+
let num = week['num'];
96+
delete week['num'];
97+
f[num]=week;
98+
}
99+
return f;
100+
});
97101
}
98102

99103
saveRest() {
100-
let temp = {}
101-
temp["food"] = this.food;
102-
103-
this.http.put(this.getUrl(), temp).subscribe(
104-
(resp: Response) => {
105-
console.log(resp.json());
104+
for (var week in this.food) {
105+
if (this.food.hasOwnProperty(week)) {
106+
var menu = this.food[week];
107+
menu['num']=week;
108+
console.log(JSON.stringify(menu));
109+
this.http.post(this.getUrl(), menu).subscribe(
110+
(response:Response)=>{console.log(response)},
111+
(error)=>{console.log(error)},
112+
)
106113
}
107-
)
114+
}
108115
}
109116
getFoodForDay(code: string) {
110117
let food = this.food[this.weekNum][code];

0 commit comments

Comments
 (0)