|
@@ -0,0 +1,112 @@
|
|
|
+let allUsers = [];
|
|
|
+let allFilms = [];
|
|
|
+let url = 'https://api-f22.herokuapp.com/';
|
|
|
+let userUrl = 'users/';
|
|
|
+let films = 'films/';
|
|
|
+let addFilm = 'addFilm/';
|
|
|
+
|
|
|
+
|
|
|
+class User {
|
|
|
+ constructor(name, filmId){
|
|
|
+ this.name = name;
|
|
|
+ this.films = tagFilm(this.id, filmId)
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const createUser = function(name, filmId){
|
|
|
+ return new User(name, filmId)
|
|
|
+};
|
|
|
+
|
|
|
+const addNewUser = function(name, filmId){
|
|
|
+ axios
|
|
|
+ .post(url+userUrl, createUser(name, filmId))
|
|
|
+ .catch(error => console.log(error));
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
+const updateUser = function(id, updatedInfo){
|
|
|
+ axios
|
|
|
+ .patch(url + userUrl, {
|
|
|
+ name: updatedInfo,
|
|
|
+ id: id
|
|
|
+ })
|
|
|
+};
|
|
|
+
|
|
|
+const showUserById = function(id){
|
|
|
+ return axios
|
|
|
+ .get(url+userUrl+id)
|
|
|
+};
|
|
|
+showUserById(2).then((response=>{
|
|
|
+ console.log(response.data);
|
|
|
+}));
|
|
|
+
|
|
|
+const deleteUser = function (id) {
|
|
|
+ axios.delete(url + userUrl + id)
|
|
|
+ .then(res => {
|
|
|
+ console.log('Deleted:', res.message)
|
|
|
+ return res
|
|
|
+ })
|
|
|
+ .catch(err => console.error(err))
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+const updateServersData = function (){
|
|
|
+ addNewUser('Ruslan', 1);
|
|
|
+ deleteUser(2);
|
|
|
+ updateUser(2, 'Igor');
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+class Film {
|
|
|
+ constructor(title, author){
|
|
|
+ this.title = title;
|
|
|
+ this.author = author;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+const createFilm = function(title, author){
|
|
|
+ axios.post(url+films, new Film(title, author))
|
|
|
+};
|
|
|
+
|
|
|
+const tagFilm = function (userId, filmId) {
|
|
|
+ axios.post(url+ userUrl + addFilm, {
|
|
|
+ userId: userId,
|
|
|
+ filmId: filmId
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const getAllFilms = function (){
|
|
|
+ return axios.get(url+films).then((res=>{
|
|
|
+ allFilms.push(...res.data);
|
|
|
+ }))
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const renderUsers = users =>{
|
|
|
+ document.body.innerText = '';
|
|
|
+ for (let user of users){
|
|
|
+ document.body.innerHTML += `
|
|
|
+ <div class="card">
|
|
|
+ <h2>user: ${user.name}</h2>
|
|
|
+ <p>userId: ${user.id}</p>
|
|
|
+ <p>List of films: ${user.films}</p>
|
|
|
+ </div>`
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function getNewUsers(){
|
|
|
+ await updateServersData();
|
|
|
+ axios.get(url+userUrl)
|
|
|
+ .then(res =>
|
|
|
+ {
|
|
|
+ renderUsers(res.data)
|
|
|
+ allUsers.push(res.data)
|
|
|
+ })
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+createFilm('Бумер', 'Кто-то');
|
|
|
+getAllFilms();
|
|
|
+console.log(allFilms);
|
|
|
+getNewUsers();
|