diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/internetDev.iml b/.idea/internetDev.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/internetDev.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..20f033c --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..73f753c --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/demo/src/main/java/com/example/demo/controller/ArtistController.java b/demo/src/main/java/com/example/demo/controller/ArtistController.java index b24164c..6316532 100644 --- a/demo/src/main/java/com/example/demo/controller/ArtistController.java +++ b/demo/src/main/java/com/example/demo/controller/ArtistController.java @@ -10,17 +10,10 @@ import java.util.concurrent.atomic.AtomicInteger; @CrossOrigin(origins = "*") public class ArtistController { - private final Map artists = new LinkedHashMap<>(); + private final Map artists = new LinkedHashMap<>(); private final AtomicInteger idCounter = new AtomicInteger(1); - public ArtistController() { - // Инициализация предопределенными данными - artists.put("9daa", new ArtistDto("9daa", "testlabуцк", "sadууу", 2, 3)); - artists.put("98ad", new ArtistDto("98ad", "ыфвфы", "выфв", 1, 1)); - artists.put("dd28", new ArtistDto("dd28", "аааа", "ааа", 2, 3)); - artists.put("b499", new ArtistDto("b499", "бб", "уафв", 1, 3)); - artists.put("0f62", new ArtistDto("0f62", "Максим", "2", 2, 1)); - } + public ArtistController() {} @GetMapping("/artists") public List getAllArtists() { @@ -28,20 +21,20 @@ public class ArtistController { } @GetMapping("/artists/{id}") - public ArtistDto getArtistById(@PathVariable String id) { + public ArtistDto getArtistById(@PathVariable Integer id) { return artists.get(id); } @PostMapping("/artists") public ArtistDto createArtist(@RequestBody ArtistDto artist) { - String newId = String.valueOf(idCounter.getAndIncrement()); + Integer newId = idCounter.getAndIncrement(); artist.setId(newId); artists.put(newId, artist); return artist; } @PutMapping("/artists/{id}") - public ArtistDto updateArtist(@PathVariable String id, @RequestBody ArtistDto artist) { + public ArtistDto updateArtist(@PathVariable Integer id, @RequestBody ArtistDto artist) { if (artists.containsKey(id)) { artist.setId(id); artists.put(id, artist); @@ -51,7 +44,7 @@ public class ArtistController { } @PatchMapping("/artists/{id}") - public ArtistDto patchArtist(@PathVariable String id, @RequestBody Map updates) { + public ArtistDto patchArtist(@PathVariable Integer id, @RequestBody Map updates) { ArtistDto existingArtist = artists.get(id); if (existingArtist != null) { if (updates.containsKey("name")) { @@ -73,7 +66,7 @@ public class ArtistController { } @DeleteMapping("/artists/{id}") - public boolean deleteArtist(@PathVariable String id) { + public boolean deleteArtist(@PathVariable Integer id) { return artists.remove(id) != null; } } diff --git a/demo/src/main/java/com/example/demo/controller/CountryController.java b/demo/src/main/java/com/example/demo/controller/CountryController.java index 87e992b..cc1767b 100644 --- a/demo/src/main/java/com/example/demo/controller/CountryController.java +++ b/demo/src/main/java/com/example/demo/controller/CountryController.java @@ -14,7 +14,7 @@ public class CountryController { private final AtomicInteger idCounter = new AtomicInteger(1); public CountryController() { - // Инициализация предопределенными данными + countries.put(1, new CountryDto(1, "Россия")); countries.put(2, new CountryDto(2, "США")); countries.put(3, new CountryDto(3, "Тайга")); diff --git a/demo/src/main/java/com/example/demo/controller/EpochController.java b/demo/src/main/java/com/example/demo/controller/EpochController.java index a1cc98c..3d76ece 100644 --- a/demo/src/main/java/com/example/demo/controller/EpochController.java +++ b/demo/src/main/java/com/example/demo/controller/EpochController.java @@ -14,7 +14,6 @@ public class EpochController { private final AtomicInteger idCounter = new AtomicInteger(1); public EpochController() { - // Инициализация предопределенными данными epochs.put(1, new EpochDto(1, "1980-е")); epochs.put(2, new EpochDto(2, "1990-е")); } diff --git a/demo/src/main/java/com/example/demo/dto/ArtistDto.java b/demo/src/main/java/com/example/demo/dto/ArtistDto.java index 560507c..7560e08 100644 --- a/demo/src/main/java/com/example/demo/dto/ArtistDto.java +++ b/demo/src/main/java/com/example/demo/dto/ArtistDto.java @@ -1,7 +1,7 @@ package com.example.demo.dto; public class ArtistDto { - private String id; + private Integer id; private String name; private String description; private Integer epochId; @@ -9,7 +9,7 @@ public class ArtistDto { public ArtistDto() {} - public ArtistDto(String id, String name, String description, Integer epochId, Integer countryId) { + public ArtistDto(Integer id, String name, String description, Integer epochId, Integer countryId) { this.id = id; this.name = name; this.description = description; @@ -17,11 +17,11 @@ public class ArtistDto { this.countryId = countryId; } - public String getId() { + public Integer getId() { return id; } - public void setId(String id) { + public void setId(Integer id) { this.id = id; } diff --git a/punkrock-react/punkrock.html b/punkrock-react/punkrock.html index fb5a8a0..550b9a3 100644 --- a/punkrock-react/punkrock.html +++ b/punkrock-react/punkrock.html @@ -91,7 +91,7 @@

Добавить исполнителя

-
+
@@ -153,12 +153,12 @@
- +
diff --git a/punkrock-react/src/js/main.js b/punkrock-react/src/js/main.js new file mode 100644 index 0000000..10dd1bc --- /dev/null +++ b/punkrock-react/src/js/main.js @@ -0,0 +1,208 @@ +// Основной JavaScript код для работы с исполнителями +const API_URL = 'http://localhost:8080/api'; + +// Загрузка данных при загрузке страницы +document.addEventListener('DOMContentLoaded', function() { + loadArtists(); + loadCountries(); + loadEpochs(); +}); + +// Загрузка исполнителей +async function loadArtists() { + try { + const response = await fetch(`${API_URL}/artists`); + const artists = await response.json(); + displayArtists(artists); + } catch (error) { + console.error('Ошибка загрузки исполнителей:', error); + } +} + +// Загрузка стран +async function loadCountries() { + try { + const response = await fetch(`${API_URL}/countries`); + const countries = await response.json(); + console.log('Загружены страны:', countries); + } catch (error) { + console.error('Ошибка загрузки стран:', error); + } +} + +// Загрузка эпох +async function loadEpochs() { + try { + const response = await fetch(`${API_URL}/epochs`); + const epochs = await response.json(); + console.log('Загружены эпохи:', epochs); + } catch (error) { + console.error('Ошибка загрузки эпох:', error); + } +} + +// Отображение исполнителей +function displayArtists(artists) { + const container = document.getElementById('artistsContainer'); + container.innerHTML = ''; + + artists.forEach(artist => { + const artistCard = createArtistCard(artist); + container.appendChild(artistCard); + }); +} + +// Создание карточки исполнителя +function createArtistCard(artist) { + const col = document.createElement('div'); + col.className = 'col-md-4 mb-4'; + + col.innerHTML = ` +
+
+
${artist.name}
+

${artist.description || 'Нет описания'}

+

Эпоха ID: ${artist.epochId || 'Не указана'}

+

Страна ID: ${artist.countryId || 'Не указана'}

+ + +
+
+ `; + + return col; +} + +// Редактирование исполнителя +function editArtist(id) { + // ID теперь числовой + console.log('Редактирование исполнителя с ID:', id, 'тип:', typeof id); + + // Заполняем форму данными исполнителя + document.getElementById('editArtistId').value = id; + + // Здесь можно загрузить данные исполнителя и заполнить форму + // Пока что просто открываем модальное окно + const modal = new bootstrap.Modal(document.getElementById('editArtistModal')); + modal.show(); +} + +// Удаление исполнителя +async function deleteArtist(id) { + // ID теперь числовой + console.log('Удаление исполнителя с ID:', id, 'тип:', typeof id); + + if (confirm('Удалить исполнителя?')) { + try { + const response = await fetch(`${API_URL}/artists/${id}`, { + method: 'DELETE' + }); + + if (response.ok) { + loadArtists(); // Перезагружаем список + } else { + alert('Ошибка удаления исполнителя'); + } + } catch (error) { + console.error('Ошибка удаления:', error); + alert('Ошибка удаления исполнителя'); + } + } +} + +// Сохранение изменений исполнителя +async function saveEditArtist() { + const id = parseInt(document.getElementById('editArtistId').value); + const name = document.getElementById('editArtistName').value; + const description = document.getElementById('editDescription').value; + const year = document.getElementById('editArtistYear').value; + const country = document.getElementById('editArtistCountry').value; + + console.log('Сохранение исполнителя с ID:', id, 'тип:', typeof id); + + if (!name || !description) { + alert('Все поля обязательны!'); + return; + } + + try { + const response = await fetch(`${API_URL}/artists/${id}`, { + method: 'PATCH', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: name, + description: description, + epochId: 1, // Пока что статическое значение + countryId: 1 // Пока что статическое значение + }) + }); + + if (response.ok) { + const modal = bootstrap.Modal.getInstance(document.getElementById('editArtistModal')); + modal.hide(); + loadArtists(); // Перезагружаем список + } else { + alert('Ошибка сохранения исполнителя'); + } + } catch (error) { + console.error('Ошибка сохранения:', error); + alert('Ошибка сохранения исполнителя'); + } +} + +// Добавление нового исполнителя +async function addArtist() { + const name = document.getElementById('artistName').value; + const description = document.getElementById('description').value; + const year = document.getElementById('artistYear').value; + const country = document.getElementById('artistCountry').value; + + if (!name || !description) { + alert('Все поля обязательны!'); + return; + } + + try { + const response = await fetch(`${API_URL}/artists`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({ + name: name, + description: description, + epochId: 1, // Пока что статическое значение + countryId: 1 // Пока что статическое значение + }) + }); + + if (response.ok) { + document.getElementById('artistForm').reset(); + loadArtists(); // Перезагружаем список + } else { + alert('Ошибка добавления исполнителя'); + } + } catch (error) { + console.error('Ошибка добавления:', error); + alert('Ошибка добавления исполнителя'); + } +} + +// Привязка событий +document.addEventListener('DOMContentLoaded', function() { + // Кнопка сохранения редактирования + document.getElementById('saveEditArtist').addEventListener('click', saveEditArtist); + + // Кнопка добавления исполнителя + document.getElementById('artistForm').addEventListener('submit', function(e) { + e.preventDefault(); + addArtist(); + }); +}); +