Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0cb9fd8de5 | ||
|
|
0738464e7d |
BIN
LabWork3Report.docx
Normal file
564
demo/ANSWERS.md
Normal file
@@ -0,0 +1,564 @@
|
||||
# Ответы на вопросы по персистентности, ORM, JPA и Hibernate
|
||||
|
||||
## 1. Что такое персистентность?
|
||||
|
||||
**Персистентность** — это способность данных сохраняться после завершения работы приложения. В нашем проекте данные сохраняются в файле базы данных H2.
|
||||
|
||||
**Пример из проекта:**
|
||||
- Файл `demo/src/main/resources/application.properties`, строки 6-11:
|
||||
```properties
|
||||
spring.datasource.url=jdbc:h2:file:./data
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
```
|
||||
Настройка `ddl-auto=update` позволяет сохранять данные между перезапусками приложения в файл `./data.mv.db`.
|
||||
|
||||
---
|
||||
|
||||
## 2. Каким образом можно добавить поддержку персистентности в приложение?
|
||||
|
||||
В нашем проекте персистентность добавлена через:
|
||||
1. **Spring Data JPA** — зависимость в `build.gradle`
|
||||
2. **H2 Database** — встроенная БД
|
||||
3. **JPA аннотации** на сущностях
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/build.gradle`, строки 30-31:
|
||||
```gradle
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation "com.h2database:h2:${h2Version}"
|
||||
```
|
||||
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 9-10:
|
||||
```java
|
||||
@Entity
|
||||
@Table(name = "artists")
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Что такое сущность и атрибут сущности?
|
||||
|
||||
**Сущность** — это объект предметной области, который имеет идентичность и может быть сохранен в БД. **Атрибут** — это свойство сущности.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`:
|
||||
- **Сущность**: `ArtistEntity` (строка 11)
|
||||
- **Атрибуты**: `name` (строка 13), `description` (строка 15), `epoch` (строка 20), `country` (строка 24)
|
||||
|
||||
---
|
||||
|
||||
## 4. Как сущности и их атрибуты представлены в реляционной модели данных?
|
||||
|
||||
В реляционной модели:
|
||||
- **Сущность** → **Таблица**
|
||||
- **Атрибут** → **Колонка**
|
||||
- **Связь** → **Внешний ключ (Foreign Key)**
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 9-24:
|
||||
```java
|
||||
@Entity
|
||||
@Table(name = "artists") // Таблица "artists"
|
||||
public class ArtistEntity extends BaseEntity {
|
||||
@Column(nullable = false) // Колонка "name"
|
||||
private String name;
|
||||
|
||||
@Column(columnDefinition = "text") // Колонка "description"
|
||||
private String description;
|
||||
|
||||
@JoinColumn(name = "epoch_id", nullable = false) // Внешний ключ "epoch_id"
|
||||
@ManyToOne
|
||||
private EpochEntity epoch;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Как сущности и их атрибуты представлены в ООП?
|
||||
|
||||
В ООП:
|
||||
- **Сущность** → **Класс**
|
||||
- **Атрибут** → **Поле класса**
|
||||
- **Связь** → **Ссылка на другой объект**
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 11-24:
|
||||
```java
|
||||
public class ArtistEntity extends BaseEntity { // Класс (сущность)
|
||||
private String name; // Поле (атрибут)
|
||||
private String description; // Поле (атрибут)
|
||||
private EpochEntity epoch; // Ссылка на другой объект (связь)
|
||||
private CountryEntity country; // Ссылка на другой объект (связь)
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Как соотносятся понятия ООП и реляционной модели данных?
|
||||
|
||||
**ORM (Object-Relational Mapping)** преобразует:
|
||||
- Класс ↔ Таблица
|
||||
- Поле ↔ Колонка
|
||||
- Объект ↔ Строка таблицы
|
||||
- Ссылка ↔ Внешний ключ
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`:
|
||||
- Класс `ArtistEntity` → таблица `artists` (строка 10)
|
||||
- Поле `name` → колонка `name` (строка 12-13)
|
||||
- Поле `epoch` → внешний ключ `epoch_id` (строка 18-20)
|
||||
|
||||
---
|
||||
|
||||
## 7. Что такое ORM?
|
||||
|
||||
**ORM (Object-Relational Mapping)** — технология, которая автоматически преобразует объекты в записи БД и обратно.
|
||||
|
||||
**Пример из проекта:**
|
||||
- Используется через Spring Data JPA и Hibernate
|
||||
- `demo/src/main/java/com/example/demo/repository/ArtistRepository.java`, строка 12:
|
||||
```java
|
||||
public interface ArtistRepository extends JpaRepository<ArtistEntity, Long>
|
||||
```
|
||||
JpaRepository автоматически реализует методы для работы с БД через ORM.
|
||||
|
||||
---
|
||||
|
||||
## 8. Когда следует использовать ORM?
|
||||
|
||||
ORM следует использовать когда:
|
||||
- Приложение использует ООП
|
||||
- Нужна быстрая разработка
|
||||
- Бизнес-логика сложная
|
||||
- Нужна переносимость между БД
|
||||
|
||||
**Пример из проекта:**
|
||||
- Проект использует Spring Boot с JPA для быстрой разработки
|
||||
- `demo/src/main/resources/application.properties`, строка 10:
|
||||
```properties
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
```
|
||||
Можно легко сменить БД, изменив только настройки.
|
||||
|
||||
---
|
||||
|
||||
## 9. Когда НЕ следует использовать ORM?
|
||||
|
||||
ORM НЕ следует использовать когда:
|
||||
- Критична производительность (высоконагруженные системы)
|
||||
- Нужны сложные SQL-запросы
|
||||
- Работа с большими объемами данных (batch-обработка)
|
||||
- Уже есть оптимизированные SQL-запросы
|
||||
|
||||
**Пример из проекта:**
|
||||
- Для статистики используются JPQL запросы, но для очень сложных запросов может понадобиться нативный SQL
|
||||
- `demo/src/main/java/com/example/demo/repository/ArtistRepository.java`, строки 19-23:
|
||||
```java
|
||||
@Query("select count(a) as totalArtists, ...")
|
||||
ArtistStatsProjection getArtistsStatistics();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. Что такое ассоциации в ООП?
|
||||
|
||||
**Ассоциация** — это связь между классами, когда один объект ссылается на другой.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 18-24:
|
||||
```java
|
||||
@ManyToOne
|
||||
private EpochEntity epoch; // Ассоциация с EpochEntity
|
||||
|
||||
@ManyToOne
|
||||
private CountryEntity country; // Ассоциация с CountryEntity
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 11. Перечислите свойства ассоциаций в ООП.
|
||||
|
||||
Свойства ассоциаций:
|
||||
- **Кратность** (1:1, 1:N, N:M)
|
||||
- **Направленность** (однонаправленная/двунаправленная)
|
||||
- **Обязательность** (обязательная/опциональная)
|
||||
- **Каскадность** (cascade)
|
||||
|
||||
**Пример из проекта:**
|
||||
- **Кратность**: `@ManyToOne` (строка 19) — многие артисты к одной эпохе
|
||||
- **Направленность**: Двунаправленная связь (ArtistEntity ↔ EpochEntity)
|
||||
- **Обязательность**: `nullable = false` (строка 18) — обязательная связь
|
||||
- **Каскадность**: Не используется (по умолчанию нет каскада)
|
||||
|
||||
---
|
||||
|
||||
## 12. Какими двумя способами можно представить связи между сущностями в реляционной БД?
|
||||
|
||||
1. **Внешний ключ (Foreign Key)** — в таблице "многих"
|
||||
2. **Связующая таблица (Join Table)** — для связи многие-ко-многим
|
||||
|
||||
**Пример из проекта:**
|
||||
- Используется внешний ключ:
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 18-20:
|
||||
```java
|
||||
@JoinColumn(name = "epoch_id", nullable = false) // Внешний ключ в таблице artists
|
||||
@ManyToOne
|
||||
private EpochEntity epoch;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 13. Что такое JPA?
|
||||
|
||||
**JPA (Java Persistence API)** — стандарт Java для работы с персистентностью, часть спецификации Jakarta EE.
|
||||
|
||||
**Пример из проекта:**
|
||||
- Используются JPA аннотации из пакета `jakarta.persistence`:
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 3-6:
|
||||
```java
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 14. Что такое Hibernate?
|
||||
|
||||
**Hibernate** — реализация JPA, ORM-фреймворк для Java.
|
||||
|
||||
**Пример из проекта:**
|
||||
- Hibernate используется через Spring Boot:
|
||||
- `demo/src/main/resources/application.properties`, строка 10:
|
||||
```properties
|
||||
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 15. Чем JPA отличается от Hibernate?
|
||||
|
||||
- **JPA** — это спецификация (стандарт)
|
||||
- **Hibernate** — это реализация JPA
|
||||
|
||||
**Пример из проекта:**
|
||||
- Используются JPA аннотации (`@Entity`, `@Column`), но работает через Hibernate
|
||||
- `demo/src/main/java/com/example/demo/entity/BaseEntity.java`, строки 11-13:
|
||||
```java
|
||||
@Id // JPA аннотация
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE) // JPA
|
||||
@SequenceGenerator(name = "hibernate_sequence") // Hibernate
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 16. Что такое Spring Data?
|
||||
|
||||
**Spring Data** — проект Spring для упрощения работы с данными, предоставляет репозитории.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/repository/ArtistRepository.java`, строка 12:
|
||||
```java
|
||||
public interface ArtistRepository extends JpaRepository<ArtistEntity, Long>
|
||||
```
|
||||
`JpaRepository` — часть Spring Data JPA, автоматически реализует CRUD операции.
|
||||
|
||||
---
|
||||
|
||||
## 17. Что такое соглашение по умолчанию (convention over configuration)?
|
||||
|
||||
**Convention over Configuration** — принцип, когда фреймворк использует разумные значения по умолчанию, уменьшая необходимость в конфигурации.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/repository/ArtistRepository.java`, строки 13-17:
|
||||
```java
|
||||
List<ArtistEntity> findByEpochId(Long epochId);
|
||||
```
|
||||
Spring Data автоматически создает SQL-запрос по имени метода, не нужно писать реализацию.
|
||||
|
||||
---
|
||||
|
||||
## 18. Для чего используется аннотация @Entity?
|
||||
|
||||
`@Entity` помечает класс как JPA сущность, которая будет отображаться в таблицу БД.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строка 9:
|
||||
```java
|
||||
@Entity
|
||||
@Table(name = "artists")
|
||||
public class ArtistEntity extends BaseEntity {
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 19. Для чего используется аннотация @SecondaryTable?
|
||||
|
||||
`@SecondaryTable` используется для отображения сущности на несколько таблиц (в нашем проекте не используется).
|
||||
|
||||
---
|
||||
|
||||
## 20. Для чего используются аннотации @Embeddable и @Embedded?
|
||||
|
||||
`@Embeddable` и `@Embedded` используются для встраивания одного объекта в другой без отдельной таблицы (в нашем проекте не используется).
|
||||
|
||||
---
|
||||
|
||||
## 21. Для чего используются аннотации @Id и @GeneratedValue?
|
||||
|
||||
`@Id` помечает поле как первичный ключ, `@GeneratedValue` указывает стратегию генерации ID.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/BaseEntity.java`, строки 11-13:
|
||||
```java
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
@SequenceGenerator(name = "hibernate_sequence")
|
||||
protected Long id;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 22. Для чего используются аннотации @Embeddable и @EmbeddedId?
|
||||
|
||||
Используются для составных первичных ключей через встраиваемый класс (в нашем проекте не используется).
|
||||
|
||||
---
|
||||
|
||||
## 23. Для чего используется аннотация @IdClass?
|
||||
|
||||
`@IdClass` используется для составных первичных ключей через отдельный класс (в нашем проекте не используется).
|
||||
|
||||
---
|
||||
|
||||
## 24. В чем разница между @EmbeddedId и @IdClass?
|
||||
|
||||
- `@EmbeddedId` — составной ключ как встроенный объект
|
||||
- `@IdClass` — составной ключ как отдельный класс
|
||||
|
||||
В нашем проекте используется простой `@Id` (Long), составные ключи не используются.
|
||||
|
||||
---
|
||||
|
||||
## 25. Для чего используются аннотации @Basic и @Column?
|
||||
|
||||
`@Basic` указывает базовое отображение поля, `@Column` настраивает колонку в таблице.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 12-16:
|
||||
```java
|
||||
@Column(nullable = false) // Колонка не может быть null
|
||||
private String name;
|
||||
|
||||
@Column(columnDefinition = "text") // Тип колонки - text
|
||||
private String description;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 26. Для чего используются аннотации @Lob, @Basic, @Column, @Temporal и @Transient?
|
||||
|
||||
- `@Lob` — для больших объектов (BLOB/CLOB)
|
||||
- `@Basic` — базовое отображение
|
||||
- `@Column` — настройка колонки
|
||||
- `@Temporal` — для дат/времени
|
||||
- `@Transient` — поле не сохраняется в БД
|
||||
|
||||
**Пример из проекта:**
|
||||
- `@Column` используется в `ArtistEntity.java`, строка 15:
|
||||
```java
|
||||
@Column(columnDefinition = "text") // Текстовое поле
|
||||
private String description;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 27. Для чего используется аннотация @Enumerated?
|
||||
|
||||
`@Enumerated` используется для сохранения enum в БД (в нашем проекте не используется).
|
||||
|
||||
---
|
||||
|
||||
## 28. Для чего используется аннотация @Access?
|
||||
|
||||
`@Access` определяет способ доступа к полям (FIELD или PROPERTY) (в нашем проекте используется FIELD по умолчанию).
|
||||
|
||||
---
|
||||
|
||||
## 29. Какие аннотации используются для отображения разных типов связей?
|
||||
|
||||
- `@OneToOne` — один к одному
|
||||
- `@OneToMany` — один ко многим
|
||||
- `@ManyToOne` — многие к одному
|
||||
- `@ManyToMany` — многие ко многим
|
||||
|
||||
**Примеры из проекта:**
|
||||
- `@ManyToOne`: `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строка 19:
|
||||
```java
|
||||
@ManyToOne
|
||||
private EpochEntity epoch;
|
||||
```
|
||||
|
||||
- `@OneToMany`: `demo/src/main/java/com/example/demo/entity/EpochEntity.java`, строка 18:
|
||||
```java
|
||||
@OneToMany(mappedBy = "epoch")
|
||||
private Set<ArtistEntity> artists = new HashSet<>();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 30. Приведите пример реализации однонаправленной one to one связи.
|
||||
|
||||
В нашем проекте нет one-to-one связи. Пример был бы:
|
||||
```java
|
||||
@Entity
|
||||
public class ArtistEntity {
|
||||
@OneToOne
|
||||
@JoinColumn(name = "profile_id")
|
||||
private ProfileEntity profile;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 31. Приведите пример реализации однонаправленной one to many связи.
|
||||
|
||||
В нашем проекте все связи двунаправленные. Пример однонаправленной:
|
||||
```java
|
||||
@Entity
|
||||
public class EpochEntity {
|
||||
@OneToMany
|
||||
@JoinColumn(name = "epoch_id")
|
||||
private List<ArtistEntity> artists;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 32. Приведите пример реализации двунаправленной one to many связи.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/EpochEntity.java`, строки 18-20:
|
||||
```java
|
||||
@OneToMany(mappedBy = "epoch")
|
||||
@OrderBy("id ASC")
|
||||
private Set<ArtistEntity> artists = new HashSet<>();
|
||||
```
|
||||
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строки 18-20:
|
||||
```java
|
||||
@JoinColumn(name = "epoch_id", nullable = false)
|
||||
@ManyToOne
|
||||
private EpochEntity epoch;
|
||||
```
|
||||
|
||||
Связь двунаправленная: `EpochEntity` имеет коллекцию `artists`, а `ArtistEntity` имеет ссылку `epoch`.
|
||||
|
||||
---
|
||||
|
||||
## 33. Приведите пример реализации двунаправленной many to many связи.
|
||||
|
||||
В нашем проекте нет many-to-many связи. Пример:
|
||||
```java
|
||||
@Entity
|
||||
public class ArtistEntity {
|
||||
@ManyToMany
|
||||
@JoinTable(name = "artist_genre",
|
||||
joinColumns = @JoinColumn(name = "artist_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "genre_id"))
|
||||
private Set<GenreEntity> genres;
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 34. Какие стратегии выборки данных для связей можно использовать?
|
||||
|
||||
- **LAZY** — ленивая загрузка (по умолчанию для `@OneToMany`, `@ManyToMany`)
|
||||
- **EAGER** — жадная загрузка (по умолчанию для `@OneToOne`, `@ManyToOne`)
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/EpochEntity.java`, строка 18:
|
||||
```java
|
||||
@OneToMany(mappedBy = "epoch") // LAZY по умолчанию
|
||||
private Set<ArtistEntity> artists;
|
||||
```
|
||||
|
||||
- `demo/src/main/java/com/example/demo/entity/ArtistEntity.java`, строка 19:
|
||||
```java
|
||||
@ManyToOne // EAGER по умолчанию
|
||||
private EpochEntity epoch;
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 35. Чем отличаются стратегии Lazy и Eager?
|
||||
|
||||
- **LAZY** — данные загружаются только при обращении (ленивая загрузка)
|
||||
- **EAGER** — данные загружаются сразу вместе с родительским объектом (жадная загрузка)
|
||||
|
||||
**Пример из проекта:**
|
||||
- При загрузке `ArtistEntity` эпоха загружается сразу (EAGER)
|
||||
- При загрузке `EpochEntity` артисты загружаются только при обращении к `getArtists()` (LAZY)
|
||||
|
||||
---
|
||||
|
||||
## 36. Как принимается решение об используемой стратегии выборки данных?
|
||||
|
||||
Решение принимается на основе:
|
||||
- Частоты использования связи
|
||||
- Производительности
|
||||
- Размера связанных данных
|
||||
|
||||
**Пример из проекта:**
|
||||
- `@ManyToOne` — EAGER, так как обычно нужна эпоха/страна артиста
|
||||
- `@OneToMany` — LAZY, так как коллекция может быть большой
|
||||
|
||||
---
|
||||
|
||||
## 37. Для чего используются аннотации @OrderBy и @OrderColumn?
|
||||
|
||||
`@OrderBy` сортирует коллекцию при загрузке, `@OrderColumn` сохраняет порядок в отдельной колонке.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/EpochEntity.java`, строки 18-20:
|
||||
```java
|
||||
@OneToMany(mappedBy = "epoch")
|
||||
@OrderBy("id ASC") // Сортировка по id при загрузке
|
||||
private Set<ArtistEntity> artists = new HashSet<>();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 38. Когда для работы со связями следует использовать изменение данных в коллекции объекта?
|
||||
|
||||
Когда связь управляется владельцем (owner side), изменения в коллекции синхронизируются с БД.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/entity/EpochEntity.java`, строки 43-50:
|
||||
```java
|
||||
public void addArtist(ArtistEntity artist) {
|
||||
if (!artists.contains(artist)) {
|
||||
artists.add(artist); // Изменение коллекции
|
||||
if (artist.getEpoch() != this) {
|
||||
artist.setEpoch(this); // Синхронизация обратной связи
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 39. Когда для работы со связями следует использовать DAO?
|
||||
|
||||
DAO (Data Access Object) используется для сложных операций, которые нельзя выразить через методы сущности.
|
||||
|
||||
**Пример из проекта:**
|
||||
- `demo/src/main/java/com/example/demo/repository/ArtistRepository.java`, строки 19-23:
|
||||
```java
|
||||
@Query("select count(a) as totalArtists, ...")
|
||||
ArtistStatsProjection getArtistsStatistics();
|
||||
```
|
||||
Сложные запросы со статистикой выполняются через репозиторий (аналог DAO), а не через методы сущности.
|
||||
|
||||
209
demo/HOW_TO_RUN.md
Normal file
@@ -0,0 +1,209 @@
|
||||
# Инструкция по запуску приложения
|
||||
|
||||
## Требования
|
||||
|
||||
- **Java 21** (JDK 21)
|
||||
- **Gradle** (обычно входит в проект через Gradle Wrapper)
|
||||
|
||||
Проверьте версию Java:
|
||||
```bash
|
||||
java -version
|
||||
```
|
||||
|
||||
Должно быть что-то вроде:
|
||||
```
|
||||
openjdk version "21.x.x"
|
||||
```
|
||||
|
||||
## Способы запуска
|
||||
|
||||
### 1. Через Gradle Wrapper (рекомендуется)
|
||||
|
||||
#### Windows:
|
||||
```bash
|
||||
cd demo
|
||||
.\gradlew.bat bootRun
|
||||
```
|
||||
|
||||
#### Linux/Mac:
|
||||
```bash
|
||||
cd demo
|
||||
./gradlew bootRun
|
||||
```
|
||||
|
||||
### 2. Через IDE (IntelliJ IDEA / Eclipse)
|
||||
|
||||
1. Откройте проект в IDE
|
||||
2. Найдите класс `DemoApplication.java` в пакете `com.example.demo`
|
||||
3. Правой кнопкой мыши → `Run 'DemoApplication'`
|
||||
|
||||
### 3. Сборка и запуск JAR файла
|
||||
|
||||
#### Сборка:
|
||||
```bash
|
||||
cd demo
|
||||
.\gradlew.bat bootJar # Windows
|
||||
# или
|
||||
./gradlew bootJar # Linux/Mac
|
||||
```
|
||||
|
||||
#### Запуск:
|
||||
```bash
|
||||
java -jar build/libs/demo-0.0.1-SNAPSHOT.jar
|
||||
```
|
||||
|
||||
## Что происходит при запуске
|
||||
|
||||
1. **Создается база данных H2** в файле `./data.mv.db` (в папке `demo`)
|
||||
2. **Автоматически создаются таблицы** (благодаря `ddl-auto=create`):
|
||||
- `epochs`
|
||||
- `countries`
|
||||
- `artists`
|
||||
3. **Автоматически создаются начальные данные** (если база пустая):
|
||||
- **Страны**: Россия, США, Великобритания, Германия, Франция, Япония
|
||||
- **Эпохи**: 1970-е, 1980-е, 1990-е, 2000-е, 2010-е, 2020-е
|
||||
4. **Приложение запускается** на порту **8080**
|
||||
|
||||
> **Примечание**: Начальные данные создаются только если база данных пустая. При последующих запусках данные сохраняются (если не удалить файл `data.mv.db`).
|
||||
|
||||
## Проверка работы
|
||||
|
||||
### 1. Проверка через браузер
|
||||
|
||||
Откройте в браузере:
|
||||
- **Swagger UI**: http://localhost:8080/swagger-ui.html
|
||||
- **API Docs**: http://localhost:8080/api-docs
|
||||
|
||||
### 2. Проверка через H2 Console
|
||||
|
||||
1. Откройте в браузере: http://localhost:8080/h2-console
|
||||
2. Настройки подключения:
|
||||
- **JDBC URL**: `jdbc:h2:file:./data`
|
||||
- **User Name**: `sa`
|
||||
- **Password**: `sa`
|
||||
3. Нажмите "Connect"
|
||||
4. Выполните SQL запрос:
|
||||
```sql
|
||||
SELECT * FROM artists;
|
||||
SELECT * FROM epochs;
|
||||
SELECT * FROM countries;
|
||||
```
|
||||
|
||||
### 3. Проверка через API
|
||||
|
||||
Используйте Swagger UI или любой HTTP клиент (Postman, curl):
|
||||
|
||||
#### Примеры запросов:
|
||||
|
||||
**Создать эпоху:**
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/epochs \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name": "1980-е"}'
|
||||
```
|
||||
|
||||
**Создать страну:**
|
||||
```bash
|
||||
curl -X POST http://localhost:8080/api/countries \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name": "Россия"}'
|
||||
```
|
||||
|
||||
**Получить всех артистов:**
|
||||
```bash
|
||||
curl http://localhost:8080/api/artists
|
||||
```
|
||||
|
||||
## Запуск тестов
|
||||
|
||||
### Через Gradle:
|
||||
```bash
|
||||
cd demo
|
||||
.\gradlew.bat test # Windows
|
||||
# или
|
||||
./gradlew test # Linux/Mac
|
||||
```
|
||||
|
||||
### Через IDE:
|
||||
Правой кнопкой на папке `test` → `Run Tests`
|
||||
|
||||
## Структура базы данных
|
||||
|
||||
После первого запуска в папке `demo` появится файл:
|
||||
- `data.mv.db` - файл базы данных H2
|
||||
|
||||
**Важно**: При каждом запуске с `ddl-auto=create` таблицы пересоздаются заново, поэтому данные не сохраняются между перезапусками.
|
||||
|
||||
Если нужно сохранять данные между запусками, измените в `application.properties`:
|
||||
```properties
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
```
|
||||
|
||||
## Возможные проблемы
|
||||
|
||||
### 1. Порт 8080 занят
|
||||
|
||||
**Ошибка**: `Port 8080 is already in use`
|
||||
|
||||
**Решение**:
|
||||
- Измените порт в `application.properties`:
|
||||
```properties
|
||||
server.port=8081
|
||||
```
|
||||
- Или остановите процесс, использующий порт 8080
|
||||
|
||||
### 2. Java версия не подходит
|
||||
|
||||
**Ошибка**: `Unsupported class file major version`
|
||||
|
||||
**Решение**: Установите Java 21
|
||||
|
||||
### 3. База данных не создается
|
||||
|
||||
**Решение**:
|
||||
- Убедитесь, что у приложения есть права на запись в папку `demo`
|
||||
- Проверьте настройки в `application.properties`
|
||||
|
||||
### 4. Ошибки компиляции
|
||||
|
||||
**Решение**:
|
||||
```bash
|
||||
cd demo
|
||||
.\gradlew.bat clean build
|
||||
```
|
||||
|
||||
## Полезные команды
|
||||
|
||||
### Очистка проекта:
|
||||
```bash
|
||||
.\gradlew.bat clean
|
||||
```
|
||||
|
||||
### Пересборка:
|
||||
```bash
|
||||
.\gradlew.bat clean build
|
||||
```
|
||||
|
||||
### Просмотр зависимостей:
|
||||
```bash
|
||||
.\gradlew.bat dependencies
|
||||
```
|
||||
|
||||
### Запуск с отладкой:
|
||||
Добавьте в `application.properties`:
|
||||
```properties
|
||||
logging.level.com.example.demo=DEBUG
|
||||
```
|
||||
|
||||
## Остановка приложения
|
||||
|
||||
- В терминале: `Ctrl + C`
|
||||
- В IDE: нажмите кнопку "Stop" в панели запуска
|
||||
|
||||
## Следующие шаги
|
||||
|
||||
1. Откройте Swagger UI: http://localhost:8080/swagger-ui.html
|
||||
2. Протестируйте API через Swagger
|
||||
3. Проверьте данные в H2 Console
|
||||
4. Запустите тесты для проверки функциональности
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
apply plugin: "com.github.node-gradle.node"
|
||||
|
||||
logger.quiet("Configure front builder")
|
||||
|
||||
ext {
|
||||
frontDir = file("${project.projectDir}/punkrock-react")
|
||||
staticDir = file("${project.projectDir}/src/main/resources/static")
|
||||
if (!frontDir.exists()) {
|
||||
throw new GradleException("Frontend app directory is not exists")
|
||||
}
|
||||
logger.quiet("Webapp dir is {}", frontDir.toString())
|
||||
}
|
||||
|
||||
node {
|
||||
version = "22.17.1"
|
||||
npmVersion = "10.9.2"
|
||||
download = true
|
||||
}
|
||||
|
||||
tasks.register("frontDepsInstall", NpmTask) {
|
||||
group = "front"
|
||||
description = "Installs dependencies from package.json"
|
||||
logger.quiet(description)
|
||||
workingDir = frontDir
|
||||
args = ["install"]
|
||||
}
|
||||
|
||||
tasks.register("frontBuild", NpmTask) {
|
||||
group = "front"
|
||||
description = "Build frontend webapp"
|
||||
logger.quiet(description)
|
||||
workingDir = frontDir
|
||||
dependsOn frontDepsInstall
|
||||
args = ["run", "build"]
|
||||
}
|
||||
|
||||
tasks.register("copyFrontend", org.gradle.api.tasks.Copy) {
|
||||
group = "front"
|
||||
description = "Copy built frontend to static resources"
|
||||
dependsOn frontBuild
|
||||
from("${frontDir}/build")
|
||||
into(staticDir)
|
||||
includeEmptyDirs = false
|
||||
}
|
||||
|
||||
if (frontDir.exists()) {
|
||||
processResources.dependsOn copyFrontend
|
||||
bootJar.dependsOn copyFrontend
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.2.5'
|
||||
id 'io.spring.dependency-management' version '1.1.5'
|
||||
id 'com.github.node-gradle.node' version '7.1.0' apply false
|
||||
id 'org.liquibase.gradle' version '2.2.2' apply false
|
||||
}
|
||||
|
||||
group = 'com.example'
|
||||
@@ -21,87 +19,21 @@ repositories {
|
||||
}
|
||||
|
||||
ext {
|
||||
springdocVersion = "2.2.0"
|
||||
h2Version = "2.4.240"
|
||||
postgresVersion = "42.7.8"
|
||||
liquibaseVersion = "4.33.0"
|
||||
thymeleafLayoutVersion = "3.4.0"
|
||||
bootstrapVersion = "5.3.3"
|
||||
bootstrapIconsVersion = "1.11.3"
|
||||
|
||||
springProfiles = []
|
||||
if (project.hasProperty("front")) {
|
||||
springProfiles.add("front")
|
||||
}
|
||||
if (project.hasProperty("prod")) {
|
||||
springProfiles.add("prod")
|
||||
} else {
|
||||
springProfiles.add("dev")
|
||||
}
|
||||
currentProfiles = springProfiles.join(",")
|
||||
logger.quiet("Current profiles are: " + currentProfiles)
|
||||
}
|
||||
|
||||
if (springProfiles.contains("front")) {
|
||||
apply from: "build.front.gradle"
|
||||
}
|
||||
if (springProfiles.contains("dev")) {
|
||||
apply from: "build.migrations.gradle"
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-security'
|
||||
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0'
|
||||
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
runtimeOnly "org.liquibase:liquibase-core:${liquibaseVersion}"
|
||||
|
||||
if (springProfiles.contains("prod")) {
|
||||
runtimeOnly "org.postgresql:postgresql:${postgresVersion}"
|
||||
} else {
|
||||
runtimeOnly "org.postgresql:postgresql:${postgresVersion}"
|
||||
runtimeOnly "com.h2database:h2:${h2Version}"
|
||||
}
|
||||
|
||||
if (!springProfiles.contains("front")) {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
|
||||
implementation "nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:${thymeleafLayoutVersion}"
|
||||
implementation "org.thymeleaf.extras:thymeleaf-extras-springsecurity6"
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
runtimeOnly "org.webjars.npm:bootstrap:${bootstrapVersion}"
|
||||
runtimeOnly "org.webjars.npm:bootstrap-icons:${bootstrapIconsVersion}"
|
||||
// Добавляем implementation для OpenAPI, чтобы Swagger UI был доступен в runtime
|
||||
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springdocVersion}"
|
||||
} else {
|
||||
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springdocVersion}"
|
||||
}
|
||||
implementation "com.h2database:h2:${h2Version}"
|
||||
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
}
|
||||
|
||||
bootRun {
|
||||
def currentArgs = ["--spring.profiles.active=" + currentProfiles]
|
||||
if (project.hasProperty("args")) {
|
||||
currentArgs.addAll(project.args.split(","))
|
||||
}
|
||||
args currentArgs
|
||||
}
|
||||
|
||||
bootJar {
|
||||
archiveFileName = String.format("%s-%s.jar", rootProject.name, version)
|
||||
}
|
||||
|
||||
test {
|
||||
systemProperty "spring.profiles.active", currentProfiles
|
||||
tasks.named('test') {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
processResources {
|
||||
filesMatching("**/application.yml") {
|
||||
filter { line ->
|
||||
line.replace("active: dev", "active: ${currentProfiles}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
apply plugin: "org.liquibase.gradle"
|
||||
|
||||
logger.quiet("Configure migrations generator")
|
||||
|
||||
ext {
|
||||
picocliVersion = "4.7.7"
|
||||
timestamp = new Date().format("yyyy-MM-dd-HHmmss")
|
||||
}
|
||||
|
||||
liquibase {
|
||||
activities {
|
||||
main {
|
||||
changelogFile "db/master.yml"
|
||||
url "jdbc:h2:file:./data"
|
||||
username "sa"
|
||||
password "sa"
|
||||
referenceUrl "hibernate:spring:com.example.demo.entity?dialect=org.hibernate.dialect.H2Dialect"
|
||||
logLevel "warn"
|
||||
}
|
||||
}
|
||||
}
|
||||
update.dependsOn processResources
|
||||
|
||||
dependencies {
|
||||
liquibaseRuntime "org.liquibase.ext:liquibase-hibernate6:${liquibaseVersion}"
|
||||
liquibaseRuntime "info.picocli:picocli:${picocliVersion}"
|
||||
liquibaseRuntime sourceSets.main.runtimeClasspath
|
||||
liquibaseRuntime sourceSets.main.output
|
||||
}
|
||||
|
||||
tasks.register("generateFull") {
|
||||
group = "migrations"
|
||||
description = "Generate changelog from existing database"
|
||||
doFirst(){
|
||||
liquibase {
|
||||
activities {
|
||||
main {
|
||||
changeLogFile "src/main/resources/db/generated-full-${timestamp}.yml"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finalizedBy generateChangelog
|
||||
}
|
||||
|
||||
tasks.register("generateDiff") {
|
||||
group = "liquibase"
|
||||
description = "Generate diff between DB and JPA entities"
|
||||
doFirst(){
|
||||
liquibase {
|
||||
activities {
|
||||
main {
|
||||
changeLogFile "src/main/resources/db/generated-diff-${timestamp}.yml"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
finalizedBy diffChangelog
|
||||
}
|
||||
diffChangelog.dependsOn compileJava
|
||||
|
||||
BIN
demo/data.mv.db
@@ -1,4 +1,60 @@
|
||||
2025-12-12 17:34:35.260933+04:00 jdbc[3]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Таблица "DATABASECHANGELOGLOCK" не найдена
|
||||
Table "DATABASECHANGELOGLOCK" not found; SQL statement:
|
||||
SELECT COUNT(*) FROM PUBLIC.DATABASECHANGELOGLOCK [42102-240]
|
||||
2025-11-14 20:15:51.893939+04:00 jdbc[13]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Синтаксическая ошибка в выражении SQL "[*]DROPTABLE COUNTRIES"; ожидалось "DELETE, DROP"
|
||||
Syntax error in SQL statement "[*]DROPTABLE COUNTRIES"; expected "DELETE, DROP"; SQL statement:
|
||||
DROPTABLE COUNTRIES [42001-240]
|
||||
2025-11-14 20:16:02.103322+04:00 jdbc[13]: exception
|
||||
org.h2.jdbc.JdbcSQLSyntaxErrorException: Невозможно удалить "COUNTRIES", пока существует зависимый объект "FK7SDOJ1330RH52SHDCVIL7SD4J"
|
||||
Cannot drop "COUNTRIES" because "FK7SDOJ1330RH52SHDCVIL7SD4J" depends on it; SQL statement:
|
||||
DROP TABLE COUNTRIES [90107-240]
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:644)
|
||||
at org.h2.message.DbException.getJdbcSQLException(DbException.java:489)
|
||||
at org.h2.message.DbException.get(DbException.java:223)
|
||||
at org.h2.command.ddl.DropTable.prepareDrop(DropTable.java:104)
|
||||
at org.h2.command.ddl.DropTable.update(DropTable.java:129)
|
||||
at org.h2.command.CommandContainer.update(CommandContainer.java:139)
|
||||
at org.h2.command.Command.executeUpdate(Command.java:306)
|
||||
at org.h2.command.Command.executeUpdate(Command.java:250)
|
||||
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:262)
|
||||
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:231)
|
||||
at org.h2.server.web.WebApp.getResult(WebApp.java:1344)
|
||||
at org.h2.server.web.WebApp.query(WebApp.java:1142)
|
||||
at org.h2.server.web.WebApp.query(WebApp.java:1118)
|
||||
at org.h2.server.web.WebApp.process(WebApp.java:244)
|
||||
at org.h2.server.web.WebApp.processRequest(WebApp.java:176)
|
||||
at org.h2.server.web.JakartaWebServlet.doGet(JakartaWebServlet.java:129)
|
||||
at org.h2.server.web.JakartaWebServlet.doPost(JakartaWebServlet.java:166)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:590)
|
||||
at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:206)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
|
||||
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
|
||||
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
|
||||
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
|
||||
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
|
||||
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:116)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:175)
|
||||
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:150)
|
||||
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:167)
|
||||
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:90)
|
||||
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:482)
|
||||
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:115)
|
||||
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:93)
|
||||
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
|
||||
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:344)
|
||||
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:391)
|
||||
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63)
|
||||
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:896)
|
||||
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
|
||||
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
|
||||
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
|
||||
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:63)
|
||||
at java.base/java.lang.Thread.run(Thread.java:1583)
|
||||
|
||||
251
demo/gradlew
vendored
@@ -0,0 +1,251 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
CLASSPATH="\\\"\\\""
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-classpath "$CLASSPATH" \
|
||||
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
|
||||
6
demo/package-lock.json
generated
@@ -1,6 +0,0 @@
|
||||
{
|
||||
"name": "demo",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.example.demo.api;
|
||||
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
|
||||
public class PageHelper {
|
||||
private PageHelper() {
|
||||
}
|
||||
|
||||
public static Pageable toPageable(int page, int size) {
|
||||
return PageRequest.of(page - 1, size, Sort.by("id"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.example.demo.api;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
|
||||
public record PageRs<D>(
|
||||
List<D> items,
|
||||
int itemsCount,
|
||||
int currentPage,
|
||||
int currentSize,
|
||||
int totalPages,
|
||||
long totalItems,
|
||||
boolean isFirst,
|
||||
boolean isLast,
|
||||
boolean hasNext,
|
||||
boolean hasPrevious) {
|
||||
|
||||
public List<D> items() {
|
||||
return Optional.ofNullable(items).orElse(Collections.emptyList());
|
||||
}
|
||||
|
||||
public static <D, E> PageRs<D> from(Page<E> page, Function<E, D> mapper) {
|
||||
return new PageRs<>(
|
||||
page.getContent().stream().map(mapper::apply).toList(),
|
||||
page.getNumberOfElements(),
|
||||
page.getNumber() + 1,
|
||||
page.getSize(),
|
||||
page.getTotalPages(),
|
||||
page.getTotalElements(),
|
||||
page.isFirst(),
|
||||
page.isLast(),
|
||||
page.hasNext(),
|
||||
page.hasPrevious());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
package com.example.demo.config;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import io.swagger.v3.oas.models.info.Contact;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -20,12 +18,6 @@ public class OpenApiConfig {
|
||||
.description("REST API для управления данными о панк-рок исполнителях")
|
||||
.contact(new Contact()
|
||||
.name("Developer")
|
||||
.email("developer@example.com")))
|
||||
.addSecurityItem(new SecurityRequirement().addList("basicAuth"))
|
||||
.components(new io.swagger.v3.oas.models.Components()
|
||||
.addSecuritySchemes("basicAuth", new SecurityScheme()
|
||||
.type(SecurityScheme.Type.HTTP)
|
||||
.scheme("basic")
|
||||
.description("HTTP Basic Authentication")));
|
||||
.email("developer@example.com")));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package com.example.demo.configuration;
|
||||
public class Constants {
|
||||
public static final String DEV_ORIGIN = "http://localhost:5173";
|
||||
public static final String API_URL = "/api";
|
||||
public static final String MVC_REDIRECT = "redirect:";
|
||||
public static final String LOGIN_URL = "/login";
|
||||
|
||||
private Constants() {
|
||||
}
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
package com.example.demo.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Profile("!front")
|
||||
@Configuration
|
||||
public class MvcWebConfiguration implements WebMvcConfigurer {
|
||||
@Override
|
||||
public void addViewControllers(@NonNull ViewControllerRegistry registry) {
|
||||
registry.addRedirectViewController("/", "/page-artists");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -7,13 +9,9 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import com.example.demo.api.PageHelper;
|
||||
import com.example.demo.api.PageRs;
|
||||
import com.example.demo.configuration.Constants;
|
||||
import com.example.demo.dto.ArtistRq;
|
||||
import com.example.demo.dto.ArtistRs;
|
||||
@@ -30,10 +28,8 @@ public class ArtistController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public PageRs<ArtistRs> getAll(
|
||||
@RequestParam(defaultValue = "1") @Min(1) int page,
|
||||
@RequestParam(defaultValue = "3") @Min(1) int size) {
|
||||
return artistService.getAll(PageHelper.toPageable(page, size));
|
||||
public List<ArtistRs> getAll() {
|
||||
return artistService.getAll();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
||||
@Profile("front")
|
||||
@Controller
|
||||
public class SpaController {
|
||||
@GetMapping(value = "/{path:^(?!api|assets|images|swagger-ui|.*\\.[a-zA-Z0-9]{2,10}).*}/**")
|
||||
public String forwardToIndex(@PathVariable(required = false) String path) {
|
||||
return "forward:/index.html";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.context.annotation.ScopedProxyMode;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
@Component
|
||||
@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
|
||||
public class ArtistFilterSession {
|
||||
private Long countryId;
|
||||
private Long epochId;
|
||||
|
||||
public Long getCountryId() {
|
||||
return countryId;
|
||||
}
|
||||
|
||||
public void setCountryId(Long countryId) {
|
||||
this.countryId = countryId;
|
||||
}
|
||||
|
||||
public Long getEpochId() {
|
||||
return epochId;
|
||||
}
|
||||
|
||||
public void setEpochId(Long epochId) {
|
||||
this.epochId = epochId;
|
||||
}
|
||||
|
||||
public void clearFilters() {
|
||||
this.countryId = null;
|
||||
this.epochId = null;
|
||||
}
|
||||
|
||||
public boolean hasFilters() {
|
||||
return countryId != null || epochId != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import static com.example.demo.configuration.Constants.MVC_REDIRECT;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import com.example.demo.api.PageHelper;
|
||||
import com.example.demo.api.PageRs;
|
||||
import com.example.demo.dto.ArtistRs;
|
||||
import com.example.demo.dto.mvc.ArtistFormDto;
|
||||
import com.example.demo.service.ArtistService;
|
||||
import com.example.demo.service.CountryService;
|
||||
import com.example.demo.service.EpochService;
|
||||
|
||||
@Profile("!front")
|
||||
@SessionAttributes(ArtistMvcController.ATR_COUNTRIES)
|
||||
@Controller
|
||||
@RequestMapping("/" + ArtistMvcController.VIEW)
|
||||
public class ArtistMvcController {
|
||||
public static final String VIEW = "page-artists";
|
||||
private static final String VIEW_EDIT = "page-artists-edit";
|
||||
|
||||
private static final String ATR_ARTIST = "artist";
|
||||
public static final String ATR_COUNTRIES = "countries";
|
||||
public static final String ATR_EPOCHS = "epochs";
|
||||
private static final String ATR_PAGE = "page";
|
||||
private static final String ATR_ARTISTS = "artists";
|
||||
|
||||
private final ArtistService artistService;
|
||||
private final CountryService countryService;
|
||||
private final EpochService epochService;
|
||||
private final ArtistFilterSession filterSession;
|
||||
|
||||
public ArtistMvcController(
|
||||
ArtistService artistService,
|
||||
CountryService countryService,
|
||||
EpochService epochService,
|
||||
ArtistFilterSession filterSession) {
|
||||
this.artistService = artistService;
|
||||
this.countryService = countryService;
|
||||
this.epochService = epochService;
|
||||
this.filterSession = filterSession;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getAll(
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "3") int size,
|
||||
@RequestParam(required = false) Long countryId,
|
||||
@RequestParam(required = false) Long epochId,
|
||||
SessionStatus sessionStatus,
|
||||
Model model) {
|
||||
if (countryId != null) {
|
||||
filterSession.setCountryId(countryId);
|
||||
}
|
||||
if (epochId != null) {
|
||||
filterSession.setEpochId(epochId);
|
||||
}
|
||||
|
||||
final PageRs<ArtistRs> artists = artistService.getAllFiltered(
|
||||
filterSession.getCountryId(),
|
||||
filterSession.getEpochId(),
|
||||
PageHelper.toPageable(page, size));
|
||||
|
||||
if (artists.itemsCount() == 0 && page > 1) {
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
model.addAttribute(ATR_ARTISTS, artists);
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
model.addAttribute(ATR_COUNTRIES, countryService.getAll());
|
||||
model.addAttribute(ATR_EPOCHS, epochService.getAll());
|
||||
model.addAttribute("selectedCountryId", filterSession.getCountryId());
|
||||
model.addAttribute("selectedEpochId", filterSession.getEpochId());
|
||||
sessionStatus.setComplete();
|
||||
return VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/filter")
|
||||
public String applyFilter(
|
||||
@RequestParam(required = false) Long countryId,
|
||||
@RequestParam(required = false) Long epochId,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
filterSession.setCountryId(countryId);
|
||||
filterSession.setEpochId(epochId);
|
||||
redirectAttributes.addAttribute(ATR_PAGE, 1);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/filter/clear")
|
||||
public String clearFilter(RedirectAttributes redirectAttributes) {
|
||||
filterSession.clearFilters();
|
||||
redirectAttributes.addAttribute(ATR_PAGE, 1);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@GetMapping(value = { "/edit", "/edit/" })
|
||||
public String create(@RequestParam(defaultValue = "1") int page, Model model) {
|
||||
final ArtistFormDto artist = ArtistFormDto.empty();
|
||||
model.addAttribute(ATR_ARTIST, artist);
|
||||
model.addAttribute(ATR_COUNTRIES, countryService.getAll());
|
||||
model.addAttribute(ATR_EPOCHS, epochService.getAll());
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
|
||||
@PostMapping(value = { "/edit", "/edit/" })
|
||||
public String create(
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@ModelAttribute(name = ATR_ARTIST) @Valid ArtistFormDto dto,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(ATR_COUNTRIES, countryService.getAll());
|
||||
model.addAttribute(ATR_EPOCHS, epochService.getAll());
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
artistService.create(dto.toRq());
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
redirectAttributes.addFlashAttribute("successSave", true);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@GetMapping("/edit/{id:\\d+}")
|
||||
public String update(@PathVariable Long id, @RequestParam(defaultValue = "1") int page, Model model) {
|
||||
final ArtistFormDto artist = ArtistFormDto.fromRs(artistService.get(id));
|
||||
model.addAttribute(ATR_ARTIST, artist);
|
||||
model.addAttribute(ATR_COUNTRIES, countryService.getAll());
|
||||
model.addAttribute(ATR_EPOCHS, epochService.getAll());
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
|
||||
@PostMapping("/edit/{id:\\d+}")
|
||||
public String update(
|
||||
@PathVariable Long id,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@ModelAttribute(name = ATR_ARTIST) @Valid ArtistFormDto dto,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(ATR_COUNTRIES, countryService.getAll());
|
||||
model.addAttribute(ATR_EPOCHS, epochService.getAll());
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
artistService.update(id, dto.toRq());
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
redirectAttributes.addFlashAttribute("successSave", true);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/delete/{id:\\d+}")
|
||||
public String delete(
|
||||
@PathVariable Long id,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
artistService.delete(id);
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import static com.example.demo.configuration.Constants.MVC_REDIRECT;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||
import org.springframework.web.bind.support.SessionStatus;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import com.example.demo.api.PageHelper;
|
||||
import com.example.demo.api.PageRs;
|
||||
import com.example.demo.dto.CountryRs;
|
||||
import com.example.demo.dto.mvc.CountryFormDto;
|
||||
import com.example.demo.service.CountryService;
|
||||
|
||||
@Profile("!front")
|
||||
@SessionAttributes(CountryMvcController.ATR_COUNTRIES)
|
||||
@Controller
|
||||
@RequestMapping("/" + CountryMvcController.VIEW)
|
||||
public class CountryMvcController {
|
||||
public static final String VIEW = "page-countries";
|
||||
private static final String VIEW_EDIT = "page-countries-edit";
|
||||
|
||||
private static final String ATR_COUNTRY = "country";
|
||||
public static final String ATR_COUNTRIES = "countries";
|
||||
private static final String ATR_PAGE = "page";
|
||||
|
||||
private final CountryService countryService;
|
||||
|
||||
public CountryMvcController(CountryService countryService) {
|
||||
this.countryService = countryService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getAll(
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "10") int size,
|
||||
SessionStatus sessionStatus,
|
||||
Model model) {
|
||||
final PageRs<CountryRs> countries = countryService.getAll(PageHelper.toPageable(page, size));
|
||||
if (countries.itemsCount() == 0 && page > 1) {
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
model.addAttribute(ATR_COUNTRIES, countries);
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
sessionStatus.setComplete();
|
||||
return VIEW;
|
||||
}
|
||||
|
||||
@GetMapping(value = { "/edit", "/edit/" })
|
||||
public String create(@RequestParam(defaultValue = "1") int page, Model model) {
|
||||
final CountryFormDto country = CountryFormDto.empty();
|
||||
model.addAttribute(ATR_COUNTRY, country);
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
|
||||
@PostMapping(value = { "/edit", "/edit/" })
|
||||
public String create(
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@ModelAttribute(name = ATR_COUNTRY) @Valid CountryFormDto dto,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
countryService.create(dto.toRq());
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
redirectAttributes.addFlashAttribute("successSave", true);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@GetMapping("/edit/{id:\\d+}")
|
||||
public String update(@PathVariable Long id, @RequestParam(defaultValue = "1") int page, Model model) {
|
||||
final CountryFormDto country = CountryFormDto.fromRs(countryService.get(id));
|
||||
model.addAttribute(ATR_COUNTRY, country);
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
|
||||
@PostMapping("/edit/{id:\\d+}")
|
||||
public String update(
|
||||
@PathVariable Long id,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@ModelAttribute(name = ATR_COUNTRY) @Valid CountryFormDto dto,
|
||||
BindingResult bindingResult,
|
||||
Model model,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
model.addAttribute(ATR_PAGE, page);
|
||||
return VIEW_EDIT;
|
||||
}
|
||||
countryService.update(id, dto.toRq());
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
redirectAttributes.addFlashAttribute("successSave", true);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
|
||||
@PostMapping("/delete/{id:\\d+}")
|
||||
public String delete(
|
||||
@PathVariable Long id,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
RedirectAttributes redirectAttributes) {
|
||||
countryService.delete(id);
|
||||
redirectAttributes.addAttribute(ATR_PAGE, page);
|
||||
return MVC_REDIRECT + "/" + VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.example.demo.configuration.Constants;
|
||||
|
||||
@Profile("!front")
|
||||
@Controller
|
||||
@RequestMapping(Constants.LOGIN_URL)
|
||||
public class LoginMvcController {
|
||||
public static final String VIEW = "login";
|
||||
|
||||
@GetMapping
|
||||
public String getLogin() {
|
||||
return VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.example.demo.dto.UserRs;
|
||||
import com.example.demo.security.UserPrincipal;
|
||||
import com.example.demo.service.UserService;
|
||||
|
||||
@Profile("!front")
|
||||
@Controller
|
||||
@RequestMapping("/page-user")
|
||||
public class UserMvcController {
|
||||
public static final String VIEW = "page-user";
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public UserMvcController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getUserProfile(Authentication authentication, Model model) {
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserPrincipal) {
|
||||
UserPrincipal principal = (UserPrincipal) authentication.getPrincipal();
|
||||
String username = principal.getUsername();
|
||||
|
||||
UserRs user = userService.get(username);
|
||||
|
||||
model.addAttribute("user", user);
|
||||
model.addAttribute("username", user.login());
|
||||
model.addAttribute("role", user.role());
|
||||
}
|
||||
return VIEW;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package com.example.demo.controller.mvc;
|
||||
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import com.example.demo.configuration.Constants;
|
||||
import com.example.demo.dto.mvc.UserSignupFormDto;
|
||||
import com.example.demo.error.AlreadyExistsException;
|
||||
import com.example.demo.error.PasswordConfirmationException;
|
||||
import com.example.demo.service.UserService;
|
||||
|
||||
@Profile("!front")
|
||||
@Controller
|
||||
@RequestMapping("/" + UserSignupMvcController.VIEW)
|
||||
public class UserSignupMvcController {
|
||||
public static final String VIEW = "signup";
|
||||
|
||||
private static final String ATR_USER = "user";
|
||||
|
||||
private UserService userService;
|
||||
|
||||
public UserSignupMvcController(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getSignup(Model model) {
|
||||
model.addAttribute(ATR_USER, UserSignupFormDto.empty());
|
||||
return VIEW;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String signup(
|
||||
@ModelAttribute(name = ATR_USER) @Valid UserSignupFormDto dto,
|
||||
BindingResult bindingResult,
|
||||
Model model) {
|
||||
if (bindingResult.hasErrors()) {
|
||||
return VIEW;
|
||||
}
|
||||
try {
|
||||
userService.create(dto.toRq());
|
||||
} catch (AlreadyExistsException ae) {
|
||||
bindingResult.rejectValue("login", "signup:login", ae.getMessage());
|
||||
model.addAttribute(ATR_USER, dto);
|
||||
return VIEW;
|
||||
} catch (PasswordConfirmationException pce) {
|
||||
bindingResult.rejectValue("password", "signup:password", pce.getMessage());
|
||||
bindingResult.rejectValue("passwordConfirm", "signup:passwordConfirm", pce.getMessage());
|
||||
model.addAttribute(ATR_USER, dto);
|
||||
return VIEW;
|
||||
}
|
||||
return "redirect:" + Constants.LOGIN_URL + "?signup";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.example.demo.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
|
||||
public record UserRq(
|
||||
@NotBlank @Size(min = 3, max = 20) String login,
|
||||
@NotBlank @Size(min = 3, max = 60) String password,
|
||||
@NotBlank @Size(min = 3, max = 60) String passwordConfirm) {
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
package com.example.demo.dto;
|
||||
|
||||
import com.example.demo.entity.UserEntity;
|
||||
import com.example.demo.entity.UserRole;
|
||||
|
||||
public record UserRs(Long id, String login, UserRole role) {
|
||||
public static UserRs from(UserEntity entity) {
|
||||
return new UserRs(entity.getId(), entity.getLogin(), entity.getRole());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
package com.example.demo.dto.mvc;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import com.example.demo.dto.ArtistRq;
|
||||
import com.example.demo.dto.ArtistRs;
|
||||
import com.example.demo.dto.CountryRs;
|
||||
import com.example.demo.dto.EpochRs;
|
||||
|
||||
public record ArtistFormDto(
|
||||
Long id,
|
||||
@NotBlank String name,
|
||||
String description,
|
||||
@NotNull @Min(1) Long epochId,
|
||||
@NotNull @Min(1) Long countryId) {
|
||||
|
||||
public static ArtistFormDto empty() {
|
||||
return new ArtistFormDto(null, null, null, -1L, -1L);
|
||||
}
|
||||
|
||||
public static ArtistFormDto fromRs(ArtistRs item) {
|
||||
return new ArtistFormDto(
|
||||
item.getId(),
|
||||
item.getName(),
|
||||
item.getDescription(),
|
||||
Optional.ofNullable(item.getEpoch()).map(EpochRs::getId).orElse(-1L),
|
||||
Optional.ofNullable(item.getCountry()).map(CountryRs::getId).orElse(-1L));
|
||||
}
|
||||
|
||||
public ArtistRq toRq() {
|
||||
ArtistRq rq = new ArtistRq();
|
||||
rq.setName(name);
|
||||
rq.setDescription(description);
|
||||
rq.setEpochId(epochId);
|
||||
rq.setCountryId(countryId);
|
||||
return rq;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.example.demo.dto.mvc;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import com.example.demo.dto.CountryRq;
|
||||
import com.example.demo.dto.CountryRs;
|
||||
|
||||
public record CountryFormDto(
|
||||
Long id,
|
||||
@NotBlank String name) {
|
||||
|
||||
public static CountryFormDto empty() {
|
||||
return new CountryFormDto(null, null);
|
||||
}
|
||||
|
||||
public static CountryFormDto fromRs(CountryRs item) {
|
||||
return new CountryFormDto(item.getId(), item.getName());
|
||||
}
|
||||
|
||||
public CountryRq toRq() {
|
||||
CountryRq rq = new CountryRq();
|
||||
rq.setName(name);
|
||||
return rq;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.example.demo.dto.mvc;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import com.example.demo.dto.UserRq;
|
||||
|
||||
public record UserSignupFormDto(
|
||||
@NotBlank @Size(min = 3, max = 20) String login,
|
||||
@NotBlank @Size(min = 3, max = 60) String password,
|
||||
@NotBlank @Size(min = 3, max = 60) String passwordConfirm) {
|
||||
|
||||
public static UserSignupFormDto empty() {
|
||||
return new UserSignupFormDto(null, null, null);
|
||||
}
|
||||
|
||||
public UserRq toRq() {
|
||||
return new UserRq(login, password, passwordConfirm);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
package com.example.demo.entity;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.EnumType;
|
||||
import jakarta.persistence.Enumerated;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "sec_users")
|
||||
public class UserEntity extends BaseEntity {
|
||||
@Column(nullable = false, unique = true, length = 20)
|
||||
private String login;
|
||||
@Column(nullable = false, length = 255)
|
||||
private String password;
|
||||
@Enumerated(EnumType.STRING)
|
||||
private UserRole role;
|
||||
|
||||
public UserEntity() {
|
||||
}
|
||||
|
||||
public UserEntity(String login, String password) {
|
||||
this.login = login;
|
||||
this.password = password;
|
||||
this.role = UserRole.USER;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public UserRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(UserRole role) {
|
||||
this.role = role;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.example.demo.entity;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
public enum UserRole implements GrantedAuthority {
|
||||
ADMIN,
|
||||
USER;
|
||||
|
||||
@Override
|
||||
public String getAuthority() {
|
||||
return "ROLE_" + this.name();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.example.demo.error;
|
||||
|
||||
public class AlreadyExistsException extends RuntimeException {
|
||||
public AlreadyExistsException(Class<?> entityClass, String identifier) {
|
||||
super(String.format("%s with identifier '%s' already exists", entityClass.getSimpleName(), identifier));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,5 @@ public class NotFoundException extends RuntimeException {
|
||||
public <T> NotFoundException(Class<T> entClass, Long id) {
|
||||
super(String.format("%s with id %s is not found", entClass.getSimpleName(), id));
|
||||
}
|
||||
|
||||
public <T> NotFoundException(Class<T> entClass, String field, String value) {
|
||||
super(String.format("%s with %s '%s' is not found", entClass.getSimpleName(), field, value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package com.example.demo.error;
|
||||
|
||||
public class PasswordConfirmationException extends RuntimeException {
|
||||
public PasswordConfirmationException() {
|
||||
super("Passwords do not match");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ package com.example.demo.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@@ -17,12 +15,6 @@ public interface ArtistRepository extends JpaRepository<ArtistEntity, Long> {
|
||||
List<ArtistEntity> findByCountryId(Long countryId);
|
||||
|
||||
List<ArtistEntity> findByEpochIdAndCountryId(Long epochId, Long countryId);
|
||||
|
||||
Page<ArtistEntity> findByEpochId(Long epochId, Pageable pageable);
|
||||
|
||||
Page<ArtistEntity> findByCountryId(Long countryId, Pageable pageable);
|
||||
|
||||
Page<ArtistEntity> findByEpochIdAndCountryId(Long epochId, Long countryId, Pageable pageable);
|
||||
|
||||
@Query("select count(a) as totalArtists, " +
|
||||
"sum(case when a.description is not null and length(a.description) > 0 then 1 else 0 end) as artistsWithDescription, " +
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.example.demo.repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import com.example.demo.entity.UserEntity;
|
||||
|
||||
public interface UserRepository extends JpaRepository<UserEntity, Long> {
|
||||
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||
}
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.example.demo.security;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
@Configuration
|
||||
public class PasswordEncoderConfiguration {
|
||||
@Bean
|
||||
PasswordEncoder passwordEncoder() {
|
||||
return new BCryptPasswordEncoder();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
package com.example.demo.security;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.security.config.Customizer;
|
||||
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
|
||||
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
import org.springframework.security.web.authentication.HttpStatusEntryPoint;
|
||||
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
||||
import org.springframework.security.web.util.matcher.RequestMatcher;
|
||||
|
||||
import com.example.demo.configuration.Constants;
|
||||
import com.example.demo.controller.mvc.UserSignupMvcController;
|
||||
import com.example.demo.entity.UserRole;
|
||||
import com.example.demo.service.UserService;
|
||||
|
||||
@Profile("!front")
|
||||
@Configuration
|
||||
@EnableMethodSecurity
|
||||
public class SecurityMvcConfiguration {
|
||||
|
||||
private final UserService userService;
|
||||
|
||||
public SecurityMvcConfiguration(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
@Bean
|
||||
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
|
||||
httpSecurity.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
|
||||
httpSecurity.csrf(AbstractHttpConfigurer::disable);
|
||||
httpSecurity.cors(Customizer.withDefaults());
|
||||
|
||||
// Настраиваем обработку исключений для API - возвращаем 401 вместо редиректа на логин
|
||||
RequestMatcher apiMatcher = new AntPathRequestMatcher(Constants.API_URL + "/**");
|
||||
httpSecurity.exceptionHandling(exceptions -> exceptions
|
||||
.defaultAuthenticationEntryPointFor(
|
||||
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED),
|
||||
apiMatcher)
|
||||
.defaultAccessDeniedHandlerFor(
|
||||
(request, response, accessDeniedException) -> {
|
||||
response.setStatus(HttpStatus.FORBIDDEN.value());
|
||||
},
|
||||
apiMatcher));
|
||||
|
||||
// Настраиваем HTTP Basic аутентификацию для API endpoints
|
||||
httpSecurity.httpBasic(basic -> basic.authenticationEntryPoint(
|
||||
new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED)));
|
||||
|
||||
httpSecurity.authorizeHttpRequests(requests -> requests
|
||||
.requestMatchers("/css/**", "/js/**", "/webjars/**", "/images/**", "/icon.svg", "/favicon.ico")
|
||||
.permitAll()
|
||||
.requestMatchers("/swagger-ui.html", "/swagger-ui/**", "/v3/api-docs/**", "/api-docs/**", "/api/swagger-ui.html")
|
||||
.hasAuthority(UserRole.ADMIN.getAuthority())
|
||||
.requestMatchers(Constants.LOGIN_URL, "/login/**").permitAll()
|
||||
.requestMatchers("/" + UserSignupMvcController.VIEW).anonymous()
|
||||
.requestMatchers("/error").permitAll()
|
||||
.requestMatchers("/api/**").hasAuthority(UserRole.ADMIN.getAuthority())
|
||||
.requestMatchers("/h2-console/**").hasAuthority(UserRole.ADMIN.getAuthority())
|
||||
.anyRequest().authenticated());
|
||||
|
||||
httpSecurity.userDetailsService(userService);
|
||||
|
||||
httpSecurity.formLogin(formLogin -> formLogin
|
||||
.loginPage(Constants.LOGIN_URL)
|
||||
.defaultSuccessUrl("/", true)
|
||||
.permitAll());
|
||||
|
||||
httpSecurity.rememberMe(rememberMe -> rememberMe.key("some-key"));
|
||||
|
||||
httpSecurity.logout(logout -> logout
|
||||
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "POST"))
|
||||
.deleteCookies("JSESSIONID")
|
||||
.permitAll());
|
||||
|
||||
return httpSecurity.build();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
package com.example.demo.security;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
import com.example.demo.entity.UserEntity;
|
||||
import com.example.demo.entity.UserRole;
|
||||
|
||||
public class UserPrincipal implements UserDetails {
|
||||
private final Long id;
|
||||
private final String username;
|
||||
private final String password;
|
||||
private final Set<? extends GrantedAuthority> roles;
|
||||
private final boolean active;
|
||||
|
||||
private UserPrincipal(
|
||||
Long id, String username, String password, Set<? extends GrantedAuthority> roles, boolean active) {
|
||||
this.id = id;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.roles = roles;
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public UserPrincipal(UserEntity user) {
|
||||
this.id = user.getId();
|
||||
this.username = user.getLogin();
|
||||
this.password = user.getPassword();
|
||||
this.roles = Set.of(user.getRole());
|
||||
this.active = true;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<? extends GrantedAuthority> getAuthorities() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return active;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonExpired() {
|
||||
return isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAccountNonLocked() {
|
||||
return isEnabled();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCredentialsNonExpired() {
|
||||
return isEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,10 @@ package com.example.demo.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.api.PageRs;
|
||||
import com.example.demo.dto.ArtistRq;
|
||||
import com.example.demo.dto.ArtistRs;
|
||||
import com.example.demo.entity.ArtistEntity;
|
||||
@@ -40,23 +37,8 @@ public class ArtistService {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public PageRs<ArtistRs> getAll(Pageable pageable) {
|
||||
return PageRs.from(repository.findAll(pageable), mapper::toRsDto);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public PageRs<ArtistRs> getAllFiltered(Long countryId, Long epochId, Pageable pageable) {
|
||||
Page<ArtistEntity> page;
|
||||
if (countryId != null && epochId != null) {
|
||||
page = repository.findByEpochIdAndCountryId(epochId, countryId, pageable);
|
||||
} else if (countryId != null) {
|
||||
page = repository.findByCountryId(countryId, pageable);
|
||||
} else if (epochId != null) {
|
||||
page = repository.findByEpochId(epochId, pageable);
|
||||
} else {
|
||||
page = repository.findAll(pageable);
|
||||
}
|
||||
return PageRs.from(page, mapper::toRsDto);
|
||||
public List<ArtistRs> getAll() {
|
||||
return mapper.toRsDtoList(repository.findAll());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
|
||||
@@ -2,12 +2,10 @@ package com.example.demo.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.api.PageRs;
|
||||
import com.example.demo.dto.CountryRq;
|
||||
import com.example.demo.dto.CountryRs;
|
||||
import com.example.demo.entity.CountryEntity;
|
||||
@@ -36,11 +34,6 @@ public class CountryService {
|
||||
return mapper.toRsDtoList(repository.findAll());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public PageRs<CountryRs> getAll(Pageable pageable) {
|
||||
return PageRs.from(repository.findAll(pageable), mapper::toRsDto);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public CountryRs get(Long id) {
|
||||
final CountryEntity entity = getEntity(id);
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
package com.example.demo.service;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import com.example.demo.dto.UserRq;
|
||||
import com.example.demo.dto.UserRs;
|
||||
import com.example.demo.entity.UserEntity;
|
||||
import com.example.demo.error.AlreadyExistsException;
|
||||
import com.example.demo.error.NotFoundException;
|
||||
import com.example.demo.error.PasswordConfirmationException;
|
||||
import com.example.demo.repository.UserRepository;
|
||||
import com.example.demo.security.UserPrincipal;
|
||||
|
||||
@Service
|
||||
public class UserService implements UserDetailsService {
|
||||
private final UserRepository repository;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
public UserService(UserRepository repository, PasswordEncoder passwordEncoder) {
|
||||
this.repository = repository;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.MANDATORY)
|
||||
public UserEntity getEntity(Long id) {
|
||||
return repository.findById(id)
|
||||
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.MANDATORY)
|
||||
public UserEntity getEntityByLogin(String login) {
|
||||
return repository.findByLoginIgnoreCase(login)
|
||||
.orElseThrow(() -> new NotFoundException(UserEntity.class, "login", login));
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public UserRs get(String login) {
|
||||
final UserEntity entity = getEntityByLogin(login);
|
||||
return UserRs.from(entity);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public UserRs create(UserRq dto) {
|
||||
if (repository.findByLoginIgnoreCase(dto.login()).isPresent()) {
|
||||
throw new AlreadyExistsException(UserEntity.class, dto.login());
|
||||
}
|
||||
if (!Objects.equals(dto.password(), dto.passwordConfirm())) {
|
||||
throw new PasswordConfirmationException();
|
||||
}
|
||||
UserEntity entity = new UserEntity(dto.login(), passwordEncoder.encode(dto.password()));
|
||||
entity = repository.save(entity);
|
||||
return UserRs.from(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||
try {
|
||||
final UserEntity existsUser = getEntityByLogin(username);
|
||||
return new UserPrincipal(existsUser);
|
||||
} catch (NotFoundException e) {
|
||||
throw new UsernameNotFoundException(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
# Available levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
example:
|
||||
demo: DEBUG
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:file:./data
|
||||
username: sa
|
||||
password: sa
|
||||
driver-class-name: org.h2.Driver
|
||||
h2:
|
||||
console:
|
||||
enabled: true
|
||||
jpa:
|
||||
show-sql: false
|
||||
properties:
|
||||
hibernate:
|
||||
format-sql: false
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
# Available levels: TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
example:
|
||||
demo: INFO
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:postgresql://127.0.0.1/demo
|
||||
username: postgres
|
||||
password: postgres
|
||||
driver-class-name: org.postgresql.Driver
|
||||
jpa:
|
||||
show-sql: false
|
||||
properties:
|
||||
hibernate:
|
||||
jdbc:
|
||||
lob:
|
||||
non_contextual_creation: true
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
spring:
|
||||
main:
|
||||
banner-mode: off
|
||||
application:
|
||||
name: demo
|
||||
profiles:
|
||||
active: dev
|
||||
datasource:
|
||||
url: jdbc:h2:file:./data
|
||||
username: sa
|
||||
password: sa
|
||||
driver-class-name: org.h2.Driver
|
||||
h2:
|
||||
console:
|
||||
enabled: true
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: validate
|
||||
open-in-view: false
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.H2Dialect
|
||||
jdbc:
|
||||
lob:
|
||||
non_contextual_creation: true
|
||||
liquibase:
|
||||
enabled: true
|
||||
drop-first: false
|
||||
change-log: classpath:db/master.yml
|
||||
server:
|
||||
port: 8080
|
||||
springdoc:
|
||||
api-docs:
|
||||
path: /api-docs
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
persist-authorization: true
|
||||
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: create-sequences
|
||||
author: user
|
||||
preConditions:
|
||||
- onFail: MARK_RAN
|
||||
- not:
|
||||
- sequenceExists:
|
||||
sequenceName: hibernate_sequence
|
||||
schemaName: public
|
||||
changes:
|
||||
- createSequence:
|
||||
sequenceName: hibernate_sequence
|
||||
schemaName: public
|
||||
startValue: 1
|
||||
incrementBy: 50
|
||||
minValue: 1
|
||||
cycle: false
|
||||
dataType: bigint
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: create-tables
|
||||
author: user
|
||||
preConditions:
|
||||
- onFail: MARK_RAN
|
||||
- not:
|
||||
- tableExists:
|
||||
tableName: epochs
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: epochs
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: pk_epochs
|
||||
- column:
|
||||
name: name
|
||||
type: VARCHAR(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- createTable:
|
||||
tableName: countries
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: pk_countries
|
||||
- column:
|
||||
name: name
|
||||
type: VARCHAR(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- createTable:
|
||||
tableName: artists
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: pk_artists
|
||||
- column:
|
||||
name: name
|
||||
type: VARCHAR(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: description
|
||||
type: TEXT
|
||||
- column:
|
||||
name: epoch_id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
nullable: false
|
||||
foreignKeyName: fk_artists_epoch
|
||||
references: epochs(id)
|
||||
- column:
|
||||
name: country_id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
nullable: false
|
||||
foreignKeyName: fk_artists_country
|
||||
references: countries(id)
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
databaseChangeLog:
|
||||
- changeSet:
|
||||
id: create-users-table
|
||||
author: user
|
||||
preConditions:
|
||||
- onFail: MARK_RAN
|
||||
- not:
|
||||
- tableExists:
|
||||
tableName: sec_users
|
||||
changes:
|
||||
- createTable:
|
||||
tableName: sec_users
|
||||
columns:
|
||||
- column:
|
||||
name: id
|
||||
type: BIGINT
|
||||
constraints:
|
||||
primaryKey: true
|
||||
nullable: false
|
||||
primaryKeyName: pk_sec_users
|
||||
- column:
|
||||
name: login
|
||||
type: VARCHAR(20)
|
||||
constraints:
|
||||
nullable: false
|
||||
unique: true
|
||||
- column:
|
||||
name: password
|
||||
type: VARCHAR(255)
|
||||
constraints:
|
||||
nullable: false
|
||||
- column:
|
||||
name: role
|
||||
type: VARCHAR(20)
|
||||
constraints:
|
||||
nullable: false
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
databaseChangeLog:
|
||||
- includeAll:
|
||||
path: changes/
|
||||
relativeToChangelogFile: true
|
||||
errorIfMissingOrEmpty: false
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
:root {
|
||||
--punk-primary: blueviolet;
|
||||
--punk-dark: #121212;
|
||||
}
|
||||
|
||||
.text-punk {
|
||||
color: var(--punk-primary) !important;
|
||||
}
|
||||
|
||||
.border-punk {
|
||||
border-color: var(--punk-primary) !important;
|
||||
}
|
||||
|
||||
.bg-punk {
|
||||
background-color: var(--punk-primary) !important;
|
||||
}
|
||||
|
||||
.page-link {
|
||||
background-color: transparent;
|
||||
border-color: var(--punk-primary);
|
||||
color: var(--punk-primary);
|
||||
}
|
||||
|
||||
.page-link:hover {
|
||||
background-color: var(--punk-primary);
|
||||
color: white;
|
||||
border-color: var(--punk-primary);
|
||||
}
|
||||
|
||||
.page-item.active .page-link {
|
||||
background-color: var(--punk-primary);
|
||||
border-color: var(--punk-primary);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.nav-link.active {
|
||||
color: var(--punk-primary) !important;
|
||||
}
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link href="/styles.css" rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
|
||||
<title>Панкуха</title>
|
||||
</head>
|
||||
<body class="bg-dark text-light">
|
||||
<div id="root"></div>
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Панк-рок исполнители</title>
|
||||
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
|
||||
<script type="text/javascript" src="/webjars/bootstrap/5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="/webjars/bootstrap/5.3.3/dist/css/bootstrap.min.css" />
|
||||
<link rel="stylesheet" href="/webjars/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" />
|
||||
<link rel="stylesheet" href="/css/style.css" />
|
||||
<th:block layout:fragment="css"></th:block>
|
||||
</head>
|
||||
<body class="bg-dark text-light">
|
||||
<div class="d-flex flex-column min-vh-100">
|
||||
<header>
|
||||
<nav class="navbar navbar-expand-md navbar-dark bg-dark border-bottom border-punk">
|
||||
<div class="container-fluid">
|
||||
<a class="navbar-brand text-punk" th:href="@{/}">
|
||||
<i class="bi bi-music-note-beamed"></i>
|
||||
Панк-рок
|
||||
</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="navbar-collapse collapse justify-content-end" id="navbarNav">
|
||||
<div class="navbar-nav">
|
||||
<a class="nav-link" th:href="@{/page-artists}">
|
||||
Исполнители
|
||||
</a>
|
||||
<a class="nav-link" th:href="@{/page-countries}">
|
||||
Страны
|
||||
</a>
|
||||
<a class="nav-link" th:href="@{/swagger-ui.html}" target="_blank" sec:authorize="hasRole('ADMIN')">Swagger</a>
|
||||
<th:block sec:authorize="isAuthenticated()">
|
||||
<div class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="userDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
<i class="bi bi-person-circle"></i>
|
||||
<span sec:authentication="name"></span>
|
||||
</a>
|
||||
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="userDropdown">
|
||||
<li>
|
||||
<a class="dropdown-item" th:href="@{/page-user}">Профиль</a>
|
||||
</li>
|
||||
<li>
|
||||
<form th:action="@{/logout}" method="post" class="d-inline">
|
||||
<button class="dropdown-item" type="submit">Выйти</button>
|
||||
</form>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</th:block>
|
||||
<a class="nav-link" th:href="@{/login}" sec:authorize="!isAuthenticated()">Войти</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
</header>
|
||||
<main class="flex-grow-1 container-fluid p-4" layout:fragment="content"></main>
|
||||
<footer class="d-flex flex-shrink-0 align-items-center justify-content-center bg-dark border-top border-punk py-3">
|
||||
<span class="text-muted">Панк-рок исполнители, [[${#dates.year(#dates.createNow())}]]</span>
|
||||
</footer>
|
||||
</div>
|
||||
<th:block layout:fragment="js"></th:block>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<th:block th:fragment="draw (items, totalPages, currentPage)">
|
||||
<th:block th:with="isEmpty=${items.size() == 0}">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-dark table-hover table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>№</th>
|
||||
<th>Название</th>
|
||||
<th>Описание</th>
|
||||
<th>Эпоха</th>
|
||||
<th>Страна</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<th:block th:if="${isEmpty}">
|
||||
<tr>
|
||||
<td class="text-center" colspan="100">
|
||||
<h5 class="text-center text-muted">Данные отсутствуют</h5>
|
||||
</td>
|
||||
</tr>
|
||||
</th:block>
|
||||
<th:block th:unless="${isEmpty}">
|
||||
<tr th:each="artist : ${items}">
|
||||
<td th:text="${artist.id}"></td>
|
||||
<td th:text="${artist.name}"></td>
|
||||
<td>
|
||||
<span th:if="${artist.description != null and artist.description.length() > 0}" th:text="${#strings.abbreviate(artist.description, 50)}"></span>
|
||||
<span th:unless="${artist.description != null and artist.description.length() > 0}" class="text-muted">-</span>
|
||||
</td>
|
||||
<td th:text="${artist.epoch?.name}"></td>
|
||||
<td th:text="${artist.country?.name}"></td>
|
||||
<td class="p-1">
|
||||
<a
|
||||
class="btn btn-warning btn-sm"
|
||||
th:href="@{/page-artists/edit/{id}(id=${artist.id},page=${currentPage})}"
|
||||
>
|
||||
<i class="bi bi-pencil-fill"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td class="p-1">
|
||||
<form
|
||||
th:action="@{/page-artists/delete/{id}(id=${artist.id},page=${currentPage})}"
|
||||
method="post"
|
||||
>
|
||||
<button
|
||||
class="btn btn-danger btn-sm"
|
||||
onclick="return confirm('Вы уверены?')"
|
||||
>
|
||||
<i class="bi bi-trash-fill"></i>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</th:block>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<th:block
|
||||
th:replace="~{ f-pagination :: draw (
|
||||
path='page-artists',
|
||||
totalPages=${totalPages},
|
||||
currentPage=${currentPage}) }"
|
||||
/>
|
||||
</th:block>
|
||||
</th:block>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<th:block th:fragment="draw (items, totalPages, currentPage)">
|
||||
<th:block th:with="isEmpty=${items.size() == 0}">
|
||||
<div class="table-responsive">
|
||||
<table class="table table-dark table-hover table-sm">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>№</th>
|
||||
<th>Название</th>
|
||||
<th></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<th:block th:if="${isEmpty}">
|
||||
<tr>
|
||||
<td class="text-center" colspan="100">
|
||||
<h5 class="text-center text-muted">Данные отсутствуют</h5>
|
||||
</td>
|
||||
</tr>
|
||||
</th:block>
|
||||
<th:block th:unless="${isEmpty}">
|
||||
<tr th:each="country : ${items}">
|
||||
<td th:text="${country.id}"></td>
|
||||
<td th:text="${country.name}"></td>
|
||||
<td class="p-1">
|
||||
<a
|
||||
class="btn btn-warning btn-sm"
|
||||
th:href="@{/page-countries/edit/{id}(id=${country.id},page=${currentPage})}"
|
||||
>
|
||||
<i class="bi bi-pencil-fill"></i>
|
||||
</a>
|
||||
</td>
|
||||
<td class="p-1">
|
||||
<form
|
||||
th:action="@{/page-countries/delete/{id}(id=${country.id},page=${currentPage})}"
|
||||
method="post"
|
||||
>
|
||||
<button
|
||||
class="btn btn-danger btn-sm"
|
||||
onclick="return confirm('Вы уверены?')"
|
||||
>
|
||||
<i class="bi bi-trash-fill"></i>
|
||||
</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
</th:block>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<th:block
|
||||
th:replace="~{ f-pagination :: draw (
|
||||
path='page-countries',
|
||||
totalPages=${totalPages},
|
||||
currentPage=${currentPage}) }"
|
||||
/>
|
||||
</th:block>
|
||||
</th:block>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<title></title>
|
||||
</head>
|
||||
<body>
|
||||
<th:block th:fragment="draw (path, totalPages, currentPage)">
|
||||
<nav th:if="${totalPages > 1}" th:with="maxPage=2">
|
||||
<ul
|
||||
class="pagination justify-content-center"
|
||||
th:with="seqFrom=${currentPage - maxPage < 1 ? 1 : currentPage - maxPage},
|
||||
seqTo=${currentPage + maxPage > totalPages ? totalPages : currentPage + maxPage}"
|
||||
>
|
||||
<th:block th:if="${currentPage > maxPage + 1}">
|
||||
<li class="page-item">
|
||||
<a class="page-link" aria-label="Previous" th:href="@{/{url}(url=${path})}">
|
||||
<span aria-hidden="true">«</span>
|
||||
</a>
|
||||
</li>
|
||||
<li class="page-item disabled">
|
||||
<span class="page-link" aria-label="Previous">
|
||||
<span aria-hidden="true">…</span>
|
||||
</span>
|
||||
</li>
|
||||
</th:block>
|
||||
<li
|
||||
class="page-item"
|
||||
th:each="page : ${#numbers.sequence(seqFrom, seqTo)}"
|
||||
th:classappend="${page == currentPage} ? 'active' : ''"
|
||||
>
|
||||
<a class="page-link" th:href="@{/{url}?page={page}(url=${path},page=${page})}">
|
||||
<span th:text="${page}" />
|
||||
</a>
|
||||
</li>
|
||||
<th:block th:if="${currentPage < totalPages - maxPage}">
|
||||
<li class="page-item disabled">
|
||||
<span class="page-link" aria-label="Previous">
|
||||
<span aria-hidden="true">…</span>
|
||||
</span>
|
||||
</li>
|
||||
<li class="page-item">
|
||||
<a
|
||||
class="page-link"
|
||||
aria-label="Next"
|
||||
th:href="@{/{url}?page={page}(url=${path},page=${totalPages})}"
|
||||
>
|
||||
<span aria-hidden="true">»</span>
|
||||
</a>
|
||||
</li>
|
||||
</th:block>
|
||||
</ul>
|
||||
</nav>
|
||||
</th:block>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,51 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Вход</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="row justify-content-center">
|
||||
<form action="#" th:action="@{/login}" method="post" class="col col-md-6">
|
||||
<div th:if="${param.error}" class="alert alert-danger">Неверный логин или пароль</div>
|
||||
<div th:if="${param.logout}" class="alert alert-success">Выход успешно произведен</div>
|
||||
<div th:if="${param.signup}" class="alert alert-success">Пользователь успешно создан</div>
|
||||
<div class="mb-3">
|
||||
<label for="username" class="form-label">Имя пользователя</label>
|
||||
<input
|
||||
type="text"
|
||||
id="username"
|
||||
name="username"
|
||||
class="form-control"
|
||||
minlength="3"
|
||||
maxlength="20"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="password" class="form-label">Пароль</label>
|
||||
<input
|
||||
type="password"
|
||||
id="password"
|
||||
name="password"
|
||||
class="form-control"
|
||||
minlength="3"
|
||||
maxlength="60"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div class="form-check mb-3">
|
||||
<input class="form-check-input" type="checkbox" id="remember-me" name="remember-me" checked />
|
||||
<label class="form-check-label" for="remember-me">Запомнить меня</label>
|
||||
</div>
|
||||
<div class="mb-3 d-flex flex-row">
|
||||
<button class="btn btn-primary me-2" type="submit">Войти</button>
|
||||
<a class="btn btn-secondary" th:href="@{/signup}">Регистрация</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Редактировать исполнителя</title>
|
||||
</head>
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<form
|
||||
th:action="@{/page-artists/edit/{id}(id=${artist.id},page=${page})}"
|
||||
th:object="${artist}"
|
||||
method="post"
|
||||
>
|
||||
<div th:if="${successSave}" class="alert alert-success mb-2" role="alert">
|
||||
Данные успешно записаны
|
||||
</div>
|
||||
<div class="mb-2" th:with="hasError=${#fields.hasErrors('name')}">
|
||||
<label for="name" class="form-label">Название</label>
|
||||
<input
|
||||
type="text"
|
||||
th:field="*{name}"
|
||||
id="name"
|
||||
class="form-control bg-dark text-light"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
/>
|
||||
<div th:if="${hasError}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-2" th:with="hasError=${#fields.hasErrors('description')}">
|
||||
<label for="description" class="form-label">Описание</label>
|
||||
<textarea
|
||||
th:field="*{description}"
|
||||
id="description"
|
||||
class="form-control bg-dark text-light"
|
||||
rows="4"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
></textarea>
|
||||
<div th:if="${hasError}" th:errors="*{description}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-2" th:with="hasError=${#fields.hasErrors('epochId')}">
|
||||
<label for="epochId" class="form-label">Эпоха</label>
|
||||
<select
|
||||
th:field="*{epochId}"
|
||||
id="epochId"
|
||||
class="form-select bg-dark text-light"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
>
|
||||
<option selected value="">Выберите эпоху</option>
|
||||
<option
|
||||
th:each="epoch : ${epochs}"
|
||||
th:value="${epoch.id}"
|
||||
th:selected="${epoch.id==epochId}"
|
||||
th:text="${epoch.name}"
|
||||
></option>
|
||||
</select>
|
||||
<div th:if="${hasError}" th:errors="*{epochId}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-2" th:with="hasError=${#fields.hasErrors('countryId')}">
|
||||
<label for="countryId" class="form-label">Страна</label>
|
||||
<select
|
||||
th:field="*{countryId}"
|
||||
id="countryId"
|
||||
class="form-select bg-dark text-light"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
>
|
||||
<option selected value="">Выберите страну</option>
|
||||
<option
|
||||
th:each="country : ${countries}"
|
||||
th:value="${country.id}"
|
||||
th:selected="${country.id==countryId}"
|
||||
th:text="${country.name}"
|
||||
></option>
|
||||
</select>
|
||||
<div th:if="${hasError}" th:errors="*{countryId}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<button class="btn btn-primary" type="submit">Сохранить</button>
|
||||
<a class="btn btn-secondary mx-2" th:href="@{/page-artists(page=${page})}">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Исполнители</title>
|
||||
</head>
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="mb-3">
|
||||
<a class="btn btn-primary" th:href="@{/page-artists/edit(page=${page})}">
|
||||
<i class="bi bi-plus-circle-fill me-2"></i>Создать нового исполнителя
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="card bg-dark border-punk mb-3">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title text-punk">Фильтры</h5>
|
||||
<form th:action="@{/page-artists/filter}" method="post" class="row g-3">
|
||||
<div class="col-md-4">
|
||||
<label for="countryId" class="form-label">Страна</label>
|
||||
<select id="countryId" name="countryId" class="form-select bg-dark text-light">
|
||||
<option value="">Все страны</option>
|
||||
<option
|
||||
th:each="country : ${countries}"
|
||||
th:value="${country.id}"
|
||||
th:selected="${country.id == selectedCountryId}"
|
||||
th:text="${country.name}"
|
||||
></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<label for="epochId" class="form-label">Эпоха</label>
|
||||
<select id="epochId" name="epochId" class="form-select bg-dark text-light">
|
||||
<option value="">Все эпохи</option>
|
||||
<option
|
||||
th:each="epoch : ${epochs}"
|
||||
th:value="${epoch.id}"
|
||||
th:selected="${epoch.id == selectedEpochId}"
|
||||
th:text="${epoch.name}"
|
||||
></option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-md-4 d-flex align-items-end">
|
||||
<button type="submit" class="btn btn-outline-primary me-2">Применить</button>
|
||||
<a th:href="@{/page-artists/filter/clear}" class="btn btn-outline-secondary">Сбросить</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<th:block
|
||||
th:replace="~{ f-artists :: draw (
|
||||
items=${artists.items},
|
||||
totalPages=${artists.totalPages},
|
||||
currentPage=${artists.currentPage}) }"
|
||||
/>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Редактировать страну</title>
|
||||
</head>
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<form
|
||||
th:action="@{/page-countries/edit/{id}(id=${country.id},page=${page})}"
|
||||
th:object="${country}"
|
||||
method="post"
|
||||
>
|
||||
<div th:if="${successSave}" class="alert alert-success mb-2" role="alert">
|
||||
Данные успешно записаны
|
||||
</div>
|
||||
<div class="mb-2" th:with="hasError=${#fields.hasErrors('name')}">
|
||||
<label for="name" class="form-label">Название</label>
|
||||
<input
|
||||
type="text"
|
||||
th:field="*{name}"
|
||||
id="name"
|
||||
class="form-control bg-dark text-light"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
/>
|
||||
<div th:if="${hasError}" th:errors="*{name}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="text-center">
|
||||
<button class="btn btn-primary" type="submit">Сохранить</button>
|
||||
<a class="btn btn-secondary mx-2" th:href="@{/page-countries(page=${page})}">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Страны</title>
|
||||
</head>
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="mb-2">
|
||||
<a class="btn btn-primary" th:href="@{/page-countries/edit(page=${page})}">
|
||||
<i class="bi bi-plus-circle-fill me-2"></i>Создать новую страну
|
||||
</a>
|
||||
</div>
|
||||
<th:block
|
||||
th:replace="~{ f-countries :: draw (
|
||||
items=${countries.items},
|
||||
totalPages=${countries.totalPages},
|
||||
currentPage=${countries.currentPage}) }"
|
||||
/>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Профиль пользователя</title>
|
||||
</head>
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="container py-4">
|
||||
<div class="card bg-dark border-punk">
|
||||
<div class="card-body">
|
||||
<h3 class="text-punk mb-4">
|
||||
<i class="bi bi-person-circle"></i> Профиль пользователя
|
||||
</h3>
|
||||
|
||||
<div class="row">
|
||||
<div class="col-md-8">
|
||||
<div class="mb-4">
|
||||
<label class="form-label text-light fw-bold">
|
||||
<i class="bi bi-person-fill me-2"></i>Имя пользователя:
|
||||
</label>
|
||||
<div class="text-light fs-5 ms-4">
|
||||
<span th:text="${username}">username</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-4">
|
||||
<label class="form-label text-light fw-bold">
|
||||
<i class="bi bi-shield-check me-2"></i>Роль:
|
||||
</label>
|
||||
<div class="ms-4">
|
||||
<span class="badge bg-punk text-dark fs-6" th:text="${role}">USER</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
|
||||
<head>
|
||||
<title>Регистрация</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main layout:fragment="content">
|
||||
<div class="row justify-content-center">
|
||||
<form action="#" th:action="@{/signup}" th:object="${user}" method="post" class="col col-md-6">
|
||||
<div class="mb-3" th:with="hasError=${#fields.hasErrors('login')}">
|
||||
<label for="login" class="form-label">Имя пользователя</label>
|
||||
<input
|
||||
type="text"
|
||||
th:field="*{login}"
|
||||
id="login"
|
||||
class="form-control"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
/>
|
||||
<div th:if="${hasError}" th:errors="*{login}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3" th:with="hasError=${#fields.hasErrors('password')}">
|
||||
<label for="password" class="form-label">Пароль</label>
|
||||
<input
|
||||
type="password"
|
||||
th:field="*{password}"
|
||||
id="password"
|
||||
class="form-control"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
/>
|
||||
<div th:if="${hasError}" th:errors="*{password}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3" th:with="hasError=${#fields.hasErrors('passwordConfirm')}">
|
||||
<label for="passwordConfirm" class="form-label">Пароль (подтверждение)</label>
|
||||
<input
|
||||
type="password"
|
||||
th:field="*{passwordConfirm}"
|
||||
id="passwordConfirm"
|
||||
class="form-control"
|
||||
th:classappend="${hasError ? 'is-invalid' : ''}"
|
||||
/>
|
||||
<div th:if="${hasError}" th:errors="*{passwordConfirm}" class="invalid-feedback"></div>
|
||||
</div>
|
||||
<div class="mb-3 d-flex flex-row">
|
||||
<button class="btn btn-primary me-2" type="submit">Регистрация</button>
|
||||
<a class="btn btn-secondary" href="/login">Отмена</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
driver-class-name: org.h2.Driver
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:postgresql://127.0.0.1/demo_test
|
||||
username: postgres
|
||||
password: postgres
|
||||
driver-class-name: org.postgresql.Driver
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
logging:
|
||||
level:
|
||||
com:
|
||||
example:
|
||||
demo: DEBUG
|
||||
spring:
|
||||
profiles:
|
||||
active: prod
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
liquibase:
|
||||
enabled: false
|
||||
|
||||
931
punkrock-react/package-lock.json
generated
@@ -12,7 +12,6 @@
|
||||
"bootstrap": "^5.3.6",
|
||||
"bootstrap-icons": "^1.13.1",
|
||||
"react": "^18.2.0",
|
||||
"react-bootstrap": "^2.10.2",
|
||||
"react-bootstrap-icons": "^1.11.6",
|
||||
"react-dom": "^18.2.0",
|
||||
"react-router-dom": "^7.6.0",
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
.punk-pagination {
|
||||
--bs-pagination-color: var(--punk-primary);
|
||||
--bs-pagination-bg: transparent;
|
||||
--bs-pagination-border-color: var(--punk-primary);
|
||||
--bs-pagination-hover-color: white;
|
||||
--bs-pagination-hover-bg: var(--punk-primary);
|
||||
--bs-pagination-hover-border-color: var(--punk-primary);
|
||||
--bs-pagination-focus-color: white;
|
||||
--bs-pagination-focus-bg: var(--punk-primary);
|
||||
--bs-pagination-focus-border-color: var(--punk-primary);
|
||||
--bs-pagination-active-color: white;
|
||||
--bs-pagination-active-bg: var(--punk-primary);
|
||||
--bs-pagination-active-border-color: var(--punk-primary);
|
||||
--bs-pagination-disabled-color: #6c757d;
|
||||
--bs-pagination-disabled-bg: transparent;
|
||||
--bs-pagination-disabled-border-color: #6c757d;
|
||||
}
|
||||
|
||||
.punk-pagination-item {
|
||||
background-color: transparent !important;
|
||||
border-color: var(--punk-primary) !important;
|
||||
color: var(--punk-primary) !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.punk-pagination-item:hover:not(.active) {
|
||||
background-color: var(--punk-primary) !important;
|
||||
color: white !important;
|
||||
border-color: var(--punk-primary) !important;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(138, 43, 226, 0.4);
|
||||
}
|
||||
|
||||
.punk-pagination-item.active {
|
||||
background-color: var(--punk-primary) !important;
|
||||
color: white !important;
|
||||
border-color: var(--punk-primary) !important;
|
||||
box-shadow: 0 0 10px rgba(138, 43, 226, 0.6);
|
||||
}
|
||||
|
||||
.punk-pagination-nav {
|
||||
background-color: transparent !important;
|
||||
border-color: var(--punk-primary) !important;
|
||||
color: var(--punk-primary) !important;
|
||||
transition: all 0.3s ease !important;
|
||||
}
|
||||
|
||||
.punk-pagination-nav:hover:not(:disabled) {
|
||||
background-color: var(--punk-primary) !important;
|
||||
color: white !important;
|
||||
border-color: var(--punk-primary) !important;
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(138, 43, 226, 0.4);
|
||||
}
|
||||
|
||||
.punk-pagination-nav:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed !important;
|
||||
}
|
||||
|
||||
.punk-pagination .page-link {
|
||||
background-color: transparent;
|
||||
border-color: var(--punk-primary);
|
||||
color: var(--punk-primary);
|
||||
}
|
||||
|
||||
.punk-pagination .page-link:hover {
|
||||
background-color: var(--punk-primary);
|
||||
color: white;
|
||||
border-color: var(--punk-primary);
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
import React from 'react';
|
||||
import { Pagination as BSPagination } from 'react-bootstrap';
|
||||
import './Pagination.css';
|
||||
|
||||
const Pagination = ({ currentPage, totalPages, onPageChange }) => {
|
||||
if (totalPages <= 1) return null;
|
||||
|
||||
const items = [];
|
||||
for (let number = 1; number <= totalPages; number++) {
|
||||
items.push(
|
||||
<BSPagination.Item
|
||||
key={number}
|
||||
active={number === currentPage}
|
||||
onClick={() => onPageChange(number)}
|
||||
className="punk-pagination-item"
|
||||
>
|
||||
{number}
|
||||
</BSPagination.Item>,
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<BSPagination className="justify-content-center punk-pagination">
|
||||
<BSPagination.Prev
|
||||
onClick={() => onPageChange(currentPage - 1)}
|
||||
disabled={currentPage === 1}
|
||||
className="punk-pagination-nav"
|
||||
/>
|
||||
{items}
|
||||
<BSPagination.Next
|
||||
onClick={() => onPageChange(currentPage + 1)}
|
||||
disabled={currentPage === totalPages}
|
||||
className="punk-pagination-nav"
|
||||
/>
|
||||
</BSPagination>
|
||||
);
|
||||
};
|
||||
|
||||
export default Pagination;
|
||||
|
||||
@@ -3,7 +3,6 @@ import Header from '../components/Header';
|
||||
import Footer from '../components/Footer';
|
||||
import ArtistList from '../components/ArtistList';
|
||||
import ArtistForm from '../components/ArtistForm';
|
||||
import Pagination from '../components/Pagination';
|
||||
import { getArtists, getCountries, getEpochs, createArtist, updateArtist, deleteArtist } from '../services/api';
|
||||
|
||||
const PunkRockPage = () => {
|
||||
@@ -12,28 +11,20 @@ const PunkRockPage = () => {
|
||||
const [epochs, setEpochs] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [editingArtist, setEditingArtist] = useState(null);
|
||||
const [sortDirection, setSortDirection] = useState('asc');
|
||||
const [currentPage, setCurrentPage] = useState(1);
|
||||
const [pageSize] = useState(3);
|
||||
const [pagination, setPagination] = useState(null);
|
||||
const [sortDirection, setSortDirection] = useState('asc'); // 'asc' для А-Я, 'desc' для Я-А
|
||||
|
||||
useEffect(() => {
|
||||
const fetchData = async () => {
|
||||
try {
|
||||
const [artistsData, countriesData, epochsData] = await Promise.all([
|
||||
getArtists(currentPage, pageSize),
|
||||
getArtists(),
|
||||
getCountries(),
|
||||
getEpochs()
|
||||
]);
|
||||
console.log('Fetched Artists:', artistsData);
|
||||
console.log('Fetched Countries:', countriesData);
|
||||
console.log('Fetched Epochs:', epochsData);
|
||||
setArtists(artistsData.items || []);
|
||||
setPagination({
|
||||
currentPage: artistsData.currentPage,
|
||||
totalPages: artistsData.totalPages,
|
||||
totalItems: artistsData.totalItems
|
||||
});
|
||||
setArtists(artistsData);
|
||||
setCountries(countriesData);
|
||||
setEpochs(epochsData);
|
||||
setLoading(false);
|
||||
@@ -43,24 +34,24 @@ const PunkRockPage = () => {
|
||||
}
|
||||
};
|
||||
fetchData();
|
||||
}, [currentPage, pageSize]);
|
||||
}, []);
|
||||
|
||||
const handleAddArtist = async (artistData) => {
|
||||
try {
|
||||
// Проверка уникальности имени
|
||||
const isDuplicate = artists.some(artist => artist.name.toLowerCase() === artistData.name.toLowerCase());
|
||||
if (isDuplicate) {
|
||||
alert('Исполнитель с таким именем уже существует!');
|
||||
return;
|
||||
}
|
||||
|
||||
const newArtist = await createArtist(artistData);
|
||||
console.log('Added Artist:', newArtist);
|
||||
// Получаем обновленные данные для определения последней страницы
|
||||
const artistsData = await getArtists(1, pageSize);
|
||||
const newTotalPages = artistsData.totalPages;
|
||||
// Переходим на последнюю страницу, где будет новый исполнитель
|
||||
const lastPageData = await getArtists(newTotalPages, pageSize);
|
||||
setArtists(lastPageData.items || []);
|
||||
setPagination({
|
||||
currentPage: lastPageData.currentPage,
|
||||
totalPages: lastPageData.totalPages,
|
||||
totalItems: lastPageData.totalItems
|
||||
setArtists(prevArtists => {
|
||||
return [...prevArtists, newArtist].sort((a, b) =>
|
||||
sortDirection === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)
|
||||
);
|
||||
});
|
||||
setCurrentPage(newTotalPages);
|
||||
} catch (error) {
|
||||
console.error('Error adding artist:', error);
|
||||
throw error;
|
||||
@@ -69,15 +60,22 @@ const PunkRockPage = () => {
|
||||
|
||||
const handleUpdateArtist = async (artistData) => {
|
||||
try {
|
||||
// Проверка уникальности имени (кроме текущего исполнителя)
|
||||
const isDuplicate = artists.some(artist =>
|
||||
artist.name.toLowerCase() === artistData.name.toLowerCase() && artist.id !== editingArtist.id
|
||||
);
|
||||
if (isDuplicate) {
|
||||
alert('Исполнитель с таким именем уже существует!');
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedArtist = await updateArtist(editingArtist.id, artistData);
|
||||
console.log('Updated Artist:', updatedArtist);
|
||||
// Перезагружаем текущую страницу
|
||||
const artistsData = await getArtists(currentPage, pageSize);
|
||||
setArtists(artistsData.items || []);
|
||||
setPagination({
|
||||
currentPage: artistsData.currentPage,
|
||||
totalPages: artistsData.totalPages,
|
||||
totalItems: artistsData.totalItems
|
||||
setArtists(prevArtists => {
|
||||
return prevArtists.map(a => (a.id === updatedArtist.id ? updatedArtist : a))
|
||||
.sort((a, b) =>
|
||||
sortDirection === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)
|
||||
);
|
||||
});
|
||||
setEditingArtist(null);
|
||||
} catch (error) {
|
||||
@@ -89,18 +87,10 @@ const PunkRockPage = () => {
|
||||
const handleDeleteArtist = async (id) => {
|
||||
try {
|
||||
await deleteArtist(id);
|
||||
// Перезагружаем текущую страницу
|
||||
const artistsData = await getArtists(currentPage, pageSize);
|
||||
setArtists(artistsData.items || []);
|
||||
setPagination({
|
||||
currentPage: artistsData.currentPage,
|
||||
totalPages: artistsData.totalPages,
|
||||
totalItems: artistsData.totalItems
|
||||
});
|
||||
// Если текущая страница стала пустой и это не первая страница, переходим на предыдущую
|
||||
if (artistsData.items.length === 0 && currentPage > 1) {
|
||||
setCurrentPage(currentPage - 1);
|
||||
}
|
||||
setArtists(artists.filter(artist => artist.id !== id)
|
||||
.sort((a, b) =>
|
||||
sortDirection === 'asc' ? a.name.localeCompare(b.name) : b.name.localeCompare(a.name)
|
||||
));
|
||||
} catch (error) {
|
||||
console.error('Error deleting artist:', error);
|
||||
throw error;
|
||||
@@ -111,10 +101,6 @@ const PunkRockPage = () => {
|
||||
setSortDirection(prev => prev === 'asc' ? 'desc' : 'asc');
|
||||
};
|
||||
|
||||
const handlePageChange = (page) => {
|
||||
setCurrentPage(page);
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return <div className="text-center text-punk my-5">Загрузка...</div>;
|
||||
}
|
||||
@@ -147,15 +133,6 @@ const PunkRockPage = () => {
|
||||
onEdit={setEditingArtist}
|
||||
onDelete={handleDeleteArtist}
|
||||
/>
|
||||
{pagination && (
|
||||
<div className="mt-4">
|
||||
<Pagination
|
||||
currentPage={pagination.currentPage}
|
||||
totalPages={pagination.totalPages}
|
||||
onPageChange={handlePageChange}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const API_URL = 'http://localhost:8080/api';
|
||||
|
||||
export const getArtists = async (page = 1, size = 3) => {
|
||||
const response = await fetch(`${API_URL}/artists?page=${page}&size=${size}`);
|
||||
export const getArtists = async () => {
|
||||
const response = await fetch(`${API_URL}/artists`);
|
||||
if (!response.ok) throw new Error('Ошибка загрузки исполнителей');
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
BIN
~$6lab.docx
@@ -50,13 +50,12 @@ out/
|
||||
|
||||
# Compiled output
|
||||
/dist
|
||||
dist/
|
||||
/tmp
|
||||
/out-tsc
|
||||
/bazel-out
|
||||
|
||||
# Node
|
||||
node_modules/
|
||||
node_modules
|
||||
npm-debug.log
|
||||
yarn-error.log
|
||||
|
||||
@@ -72,6 +71,3 @@ testem.log
|
||||
# System files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
*.mv.db
|
||||
*.trace.db
|
||||
@@ -6,7 +6,9 @@
|
||||
"request": "launch",
|
||||
"cwd": "${workspaceFolder}",
|
||||
"mainClass": "ru.ulstu.is.server.ServerApplication",
|
||||
"projectName": "lec6"
|
||||
"projectName": "lec2",
|
||||
"envFile": "${workspaceFolder}/.env",
|
||||
"args": "--populate"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -8,8 +8,8 @@
|
||||
"editor.tabSize": 4,
|
||||
"editor.insertSpaces": true,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": "explicit"
|
||||
// "source.sortImports": "explicit"
|
||||
"source.organizeImports": "explicit",
|
||||
"source.sortImports": "explicit"
|
||||
},
|
||||
"editor.snippetSuggestions": "bottom",
|
||||
"debug.toolBarLocation": "commandCenter",
|
||||
@@ -36,34 +36,19 @@
|
||||
"[html]": {
|
||||
"editor.defaultFormatter": "esbenp.prettier-vscode"
|
||||
},
|
||||
"java.compile.nullAnalysis.mode": "automatic",
|
||||
"java.configuration.updateBuildConfiguration": "automatic",
|
||||
"[java]": {
|
||||
"editor.semanticHighlighting.enabled": true,
|
||||
"editor.pasteAs.enabled": false,
|
||||
"editor.codeActionsOnSave": {
|
||||
"source.organizeImports": "explicit"
|
||||
// "source.sortImports": "explicit"
|
||||
"source.organizeImports": "never",
|
||||
"source.sortImports": "explicit"
|
||||
}
|
||||
},
|
||||
"gradle.nestedProjects": true,
|
||||
"java.configuration.updateBuildConfiguration": "automatic",
|
||||
"java.saveActions.organizeImports": true,
|
||||
"java.dependency.packagePresentation": "hierarchical",
|
||||
"java.codeGeneration.hashCodeEquals.useJava7Objects": true,
|
||||
"java.import.gradle.enabled": true,
|
||||
"java.import.gradle.offline.enabled": false,
|
||||
"java.import.gradle.wrapper.enabled": true,
|
||||
"java.compile.nullAnalysis.mode": "disabled",
|
||||
"java.import.generatesMetadataFilesAtProjectRoot": false,
|
||||
"java.completion.favoriteStaticMembers": [
|
||||
"org.junit.Assert.*",
|
||||
"org.junit.Assume.*",
|
||||
"org.junit.jupiter.api.Assertions.*",
|
||||
"org.mockito.Mockito.*",
|
||||
"org.mockito.ArgumentMatchers.*",
|
||||
"org.mockito.Answers.*",
|
||||
"org.hamcrest.MatcherAssert.*",
|
||||
"org.hamcrest.Matchers.*"
|
||||
],
|
||||
"spring-boot.ls.problem.boot2.JAVA_CONSTRUCTOR_PARAMETER_INJECTION": "WARNING",
|
||||
"spring.initializr.defaultLanguage": "Java"
|
||||
}
|
||||
5
Лекция 2 (3)/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
Swagger UI URL:
|
||||
http://localhost:8080/swagger-ui/index.html
|
||||
|
||||
MVN Repository:
|
||||
https://mvnrepository.com/
|
||||
60
Лекция 2 (3)/build.gradle
Normal file
@@ -0,0 +1,60 @@
|
||||
plugins {
|
||||
id "java"
|
||||
id "org.springframework.boot" version "3.5.5"
|
||||
id "io.spring.dependency-management" version "1.1.7"
|
||||
}
|
||||
|
||||
group = "ru.ulstu.is"
|
||||
version = "0.0.1-SNAPSHOT"
|
||||
description = "My demo server app"
|
||||
def jdkVersion = "21"
|
||||
|
||||
defaultTasks "bootRun"
|
||||
|
||||
jar {
|
||||
enabled = false
|
||||
}
|
||||
|
||||
bootJar {
|
||||
archiveFileName = String.format("%s-%s.jar", rootProject.name, version)
|
||||
}
|
||||
|
||||
assert System.properties["java.specification.version"] == jdkVersion
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(jdkVersion)
|
||||
}
|
||||
}
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
ext {
|
||||
springdocVersion = "2.8.11"
|
||||
mockitoVersion = "5.19.0"
|
||||
}
|
||||
|
||||
configurations {
|
||||
mockitoAgent
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.springframework.boot:spring-boot-starter-web"
|
||||
implementation 'org.springframework.boot:spring-boot-starter-validation'
|
||||
implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${springdocVersion}"
|
||||
|
||||
testImplementation "org.springframework.boot:spring-boot-starter-test"
|
||||
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
|
||||
|
||||
testImplementation "org.mockito:mockito-core:${mockitoVersion}"
|
||||
mockitoAgent("org.mockito:mockito-core:${mockitoVersion}") {
|
||||
transitive = false
|
||||
}
|
||||
}
|
||||
|
||||
tasks.named("test") {
|
||||
useJUnitPlatform()
|
||||
jvmArgs += "-Xshare:off"
|
||||
jvmArgs += "-javaagent:${configurations.mockitoAgent.asPath}"
|
||||
}
|
||||
@@ -5,7 +5,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"start": "vite",
|
||||
"build": "vite build --mode prod",
|
||||
"build": "vite build",
|
||||
"serve": "http-server -p 3000 ./dist/",
|
||||
"prod": "npm-run-all build serve",
|
||||
"lint": "eslint ."
|
||||
|
Before Width: | Height: | Size: 5.7 KiB After Width: | Height: | Size: 5.7 KiB |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
|
Before Width: | Height: | Size: 640 KiB After Width: | Height: | Size: 640 KiB |
|
Before Width: | Height: | Size: 812 KiB After Width: | Height: | Size: 812 KiB |
|
Before Width: | Height: | Size: 767 KiB After Width: | Height: | Size: 767 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
@@ -11,12 +11,12 @@ export const useStudents = () => {
|
||||
const [students, setStudents] = useState(null);
|
||||
const [page, setPage] = useState(parseInt(searchParams.get(PAGE_PARAM)) || 1);
|
||||
|
||||
const total = students?.totalPages ?? 0;
|
||||
const total = students?.pages ?? 0;
|
||||
const current = Math.min(Math.max(1, page), total);
|
||||
const pages = { current, total };
|
||||
|
||||
const getStudents = (page) => {
|
||||
return getAllItems(PATH, `page=${page}&size=${PAGE_SIZE}`);
|
||||
return getAllItems(PATH, `_page=${page}&_per_page=${PAGE_SIZE}`);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
@@ -48,10 +48,10 @@ export const useStudents = () => {
|
||||
await deleteItem(PATH, id);
|
||||
const newStudents = await getStudents(page);
|
||||
setStudents(newStudents);
|
||||
if (newStudents.totalPages < page) {
|
||||
changePage(newStudents.totalPages);
|
||||
if (newStudents.pages < page) {
|
||||
changePage(newStudents.pages);
|
||||
}
|
||||
};
|
||||
|
||||
return { students: students?.items ?? null, deleteStudent, pages, changePage };
|
||||
return { students: students ?? null, deleteStudent, pages, changePage };
|
||||
};
|
||||