Skip to content

Commit

Permalink
init structure, add liquibase
Browse files Browse the repository at this point in the history
  • Loading branch information
teract10s committed Oct 19, 2023
1 parent c3bbe60 commit 017c32c
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,26 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -68,6 +84,22 @@
<linkXRef>false</linkXRef>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mate.academy.rickandmorty.controller;

import java.util.List;
import lombok.RequiredArgsConstructor;
import mate.academy.rickandmorty.model.Personage;
import mate.academy.rickandmorty.repository.PersonageRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController("/rick-and-morty")
@RequiredArgsConstructor
public class RickAndMortyController {
private final PersonageRepository characterRepository;

@GetMapping("/random-personage")
public Object getRandomPersonage() {
return characterRepository;
}

@GetMapping("/search")
public List<Personage> getPersonageByName(@RequestParam String name) {
return characterRepository.findAllByNameLike(name);
}
}
29 changes: 29 additions & 0 deletions src/main/java/mate/academy/rickandmorty/model/Personage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mate.academy.rickandmorty.model;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;

@Entity
@Data
@Table(name = "personages")
public class Personage {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "external_id", nullable = false)
private Long externalId;

@Column(nullable = false)
private String name;

@Column(nullable = false)
private String status;

private String gender;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.rickandmorty.repository;

import java.util.List;
import mate.academy.rickandmorty.model.Personage;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PersonageRepository extends JpaRepository<Personage, Long> {
List<Personage> findAllByNameLike(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mate.academy.rickandmorty.service;

import org.springframework.stereotype.Service;

@Service
public class RickAndMortyApiService {

}
8 changes: 8 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
spring.datasource.url=jdbc:mysql://localhost/rick_and_morty?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true

spring.jpa.open-in-view=false
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
databaseChangeLog:
- changeSet:
id: create-characters-table
author: teract10s
changes:
- createTable:
tableName: characters
columns:
- column:
name: id
type: bigint
autoIncrement: true
constraints:
primaryKey: true
nullable: false
- column:
name: external_id
type: bigint
constraints:
nullable: false
- column:
name: name
type: varchar(255)
constraints:
nullable: false
- column:
name: status
type: varchar(255)
constraints:
nullable: false
- column:
name: gender
type: varchar(255)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
databaseChangeLog:
- changeSet:
id: rename-characters-to-personages
author: teract10s
changes:
- renameTable:
oldTableName: characters
newTableName: personages
5 changes: 5 additions & 0 deletions src/main/resources/db/changelog/db.changelog-master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
databaseChangeLog:
- include:
file: db/changelog/changes/01-create-characters-table.yaml
- include:
file: db/changelog/changes/02-rename-characters-to-personages.yaml
5 changes: 5 additions & 0 deletions src/main/resources/liquibase.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
url=jdbc:mysql://localhost/rick_and_morty?serverTimezone=UTC
username=root
password=1234
changeLogFile=db/changelog/db.changelog-master.yaml
driver=com.mysql.cj.jdbc.Driver

0 comments on commit 017c32c

Please sign in to comment.