Добавил репозиторий, реализовал добавление, вывод и фильтрацию сообщений.
https://www.youtube.com/watch?v=nyFLX3q3poY&list=PLU2ftbIeotGoGSEUf54LQH-DgiQPF2XRO&index=3
This commit is contained in:
10
compose.yml
Normal file
10
compose.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
restart: on-failure
|
||||||
|
image: postgres:13
|
||||||
|
ports:
|
||||||
|
- "5454:5432"
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: user
|
||||||
|
POSTGRES_PASSWORD: 123456
|
||||||
|
POSTGRES_DB: sweater_db
|
||||||
16
pom.xml
16
pom.xml
@@ -43,6 +43,22 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-docker-compose</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package net.sytes.kashey.sweater;
|
package net.sytes.kashey.sweater;
|
||||||
|
|
||||||
|
import net.sytes.kashey.sweater.domain.Message;
|
||||||
|
import net.sytes.kashey.sweater.repository.MessageRepository;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -9,6 +12,12 @@ import java.util.Map;
|
|||||||
@Controller
|
@Controller
|
||||||
public class GreetingController {
|
public class GreetingController {
|
||||||
|
|
||||||
|
private final MessageRepository messageRepository;
|
||||||
|
|
||||||
|
public GreetingController(MessageRepository messageRepository) {
|
||||||
|
this.messageRepository = messageRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/greeting")
|
@GetMapping("/greeting")
|
||||||
public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name,
|
public String greeting(@RequestParam(name = "name", required = false, defaultValue = "World") String name,
|
||||||
Map<String, Object> model) {
|
Map<String, Object> model) {
|
||||||
@@ -16,9 +25,29 @@ public class GreetingController {
|
|||||||
return "greeting";
|
return "greeting";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping()
|
@GetMapping
|
||||||
public String root(Map<String, Object> model) {
|
public String findAllMessages(Map<String, Object> model) {
|
||||||
model.put("some", "Hello from main!!!");
|
|
||||||
|
model.put("messages", messageRepository.findAll());
|
||||||
|
return "main";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public String addMessage(@RequestParam String text, @RequestParam String tag, Map<String, Object> model) {
|
||||||
|
|
||||||
|
messageRepository.save(new Message(text, tag));
|
||||||
|
model.put("messages", messageRepository.findAll());
|
||||||
|
return "main";
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("filter")
|
||||||
|
public String filterByTag(@RequestParam String tag, Map<String, Object> model) {
|
||||||
|
|
||||||
|
if (tag == null || tag.isEmpty()) {
|
||||||
|
model.put("messages", messageRepository.findAll());
|
||||||
|
} else {
|
||||||
|
model.put("messages", messageRepository.findByTag(tag));
|
||||||
|
}
|
||||||
return "main";
|
return "main";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
47
src/main/java/net/sytes/kashey/sweater/domain/Message.java
Normal file
47
src/main/java/net/sytes/kashey/sweater/domain/Message.java
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
package net.sytes.kashey.sweater.domain;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
import jakarta.persistence.GenerationType;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Message {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy= GenerationType.AUTO)
|
||||||
|
private Integer id;
|
||||||
|
private String text;
|
||||||
|
private String tag;
|
||||||
|
|
||||||
|
public Message() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message(String text, String tag) {
|
||||||
|
this.text = text;
|
||||||
|
this.tag = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getText() {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setText(String text) {
|
||||||
|
this.text = text;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTag() {
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTag(String tag) {
|
||||||
|
this.tag = tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package net.sytes.kashey.sweater.repository;
|
||||||
|
|
||||||
|
import net.sytes.kashey.sweater.domain.Message;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface MessageRepository extends CrudRepository<Message, Integer> {
|
||||||
|
List<Message> findByTag(String tag);
|
||||||
|
}
|
||||||
6
src/main/resources/application.properties
Normal file
6
src/main/resources/application.properties
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
spring.jpa.hibernate.ddl-auto = update
|
||||||
|
spring.datasource.url=jdbc:postgresql://localhost:5454/sweater_db
|
||||||
|
spring.datasource.username=user
|
||||||
|
spring.datasource.password=123456
|
||||||
|
spring.datasource.driver-class-name=org.postgresql.Driver
|
||||||
|
spring.jpa.show-sql: true
|
||||||
@@ -1,5 +1,29 @@
|
|||||||
<html>
|
<html>
|
||||||
<body>
|
<body>
|
||||||
<div>{{some}}<div>
|
<div>
|
||||||
|
<form method="post">
|
||||||
|
<input type="text" name="text" placeholder="Enter message"> </input>
|
||||||
|
<input type="text" name="tag" placeholder="tag"></input>
|
||||||
|
<button type="submit">Add message</button>
|
||||||
|
</form>
|
||||||
|
</div
|
||||||
|
<div>
|
||||||
|
<form method="post" action="filter">
|
||||||
|
Tag to filer
|
||||||
|
<br>
|
||||||
|
<input type="text" name="tag">
|
||||||
|
<button>Find</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>Messages list</div>
|
||||||
|
{{#messages}}
|
||||||
|
<div>
|
||||||
|
<b>{{id}}</b>
|
||||||
|
<span>{{text}}</span>
|
||||||
|
<i>{{tag}}</i>
|
||||||
|
</div>
|
||||||
|
{{/messages}}
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user