-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from egorarud/module8-task1
- Loading branch information
Showing
11 changed files
with
217 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ApiService from '../framework/api-service.js'; | ||
|
||
export default class DestinationsApi extends ApiService { | ||
get destinations() { | ||
return this._load({url: 'destinations'}) | ||
.then(ApiService.parseResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import ApiService from '../framework/api-service.js'; | ||
|
||
export default class OffersApi extends ApiService { | ||
get offers() { | ||
return this._load({url: 'offers'}) | ||
.then(ApiService.parseResponse); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import ApiService from '../framework/api-service.js'; | ||
|
||
const Method = { | ||
GET: 'GET', | ||
PUT: 'PUT', | ||
}; | ||
|
||
export default class PointsApi extends ApiService { | ||
get points() { | ||
return this._load({url: 'points'}) | ||
.then(ApiService.parseResponse); | ||
} | ||
|
||
async updatePoint(point) { | ||
const response = await this._load({ | ||
url: `points/${point.id}`, | ||
method: Method.PUT, | ||
body: JSON.stringify(this.#adaptToServer(point)), | ||
headers: new Headers({'Content-Type': 'application/json'}), | ||
}); | ||
|
||
const parsedResponse = await ApiService.parseResponse(response); | ||
|
||
return parsedResponse; | ||
} | ||
|
||
#adaptToServer(point) { | ||
const adaptedTask = {...point, | ||
'base_price': point.basePrice, | ||
'date_from': point.dateFrom.toISOString(), | ||
'date_to': point.dateTo.toISOString(), | ||
'is-favorite': point.isFavorite, | ||
}; | ||
|
||
delete adaptedTask.basePrice; | ||
delete adaptedTask.dateFrom; | ||
delete adaptedTask.dateTo; | ||
delete adaptedTask.isFavorite; | ||
|
||
return adaptedTask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
import { destinations } from '../mock/destination.js'; | ||
|
||
export default class DestinationsModel { | ||
#destinations = destinations; | ||
#destinations = []; | ||
#destinationsApi = null; | ||
|
||
constructor({destinationsApi}) { | ||
this.#destinationsApi = destinationsApi; | ||
} | ||
|
||
get destinations(){ | ||
return this.#destinations; | ||
} | ||
|
||
async init() { | ||
try { | ||
this.#destinations = await this.#destinationsApi.destinations; | ||
} catch(err) { | ||
this.#destinations = []; | ||
} | ||
} | ||
|
||
getCityNames() { | ||
return this.#destinations.map((item) => item.name); | ||
} | ||
|
||
getDestinationById(id) { | ||
return this.destinations.find((destination) => destination.id === id); | ||
return this.#destinations.find((destination) => destination.id === id); | ||
} | ||
|
||
getDestinationByName(name) { | ||
return this.destinations.find((destination) => destination.name === name); | ||
return this.#destinations.find((destination) => destination.name === name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.