From b126afac5eddfa854a0a43171b14bfbbc2193d50 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Thu, 28 Oct 2021 17:07:25 +0400 Subject: [PATCH 1/6] crud implementation --- build.gradle | 2 + src/main/java/email/EmailController.java | 16 +++- src/main/java/email/core/BaseEntity.java | 83 +++++++++++++++++++ src/main/java/email/model/Email.java | 46 ++++++++++ .../email/repository/EmailRepository.java | 10 +++ src/main/java/email/service/EmailService.java | 33 ++++++++ src/main/resources/application.properties | 14 ++++ src/main/resources/templates/list.html | 42 ++++++++++ 8 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 src/main/java/email/core/BaseEntity.java create mode 100644 src/main/java/email/model/Email.java create mode 100644 src/main/java/email/repository/EmailRepository.java create mode 100644 src/main/java/email/service/EmailService.java create mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/templates/list.html diff --git a/build.gradle b/build.gradle index 44f4bf0..6b4ba46 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,8 @@ dependencies { compile("org.springframework.boot:spring-boot-starter-web") compile("org.springframework.boot:spring-boot-starter-thymeleaf") compile("org.springframework.boot:spring-boot-devtools") + compile("org.springframework.boot:spring-boot-starter-data-jpa") + compile("com.h2database:h2") testCompile("junit:junit") } diff --git a/src/main/java/email/EmailController.java b/src/main/java/email/EmailController.java index 1249fc4..6d1fbf3 100644 --- a/src/main/java/email/EmailController.java +++ b/src/main/java/email/EmailController.java @@ -1,6 +1,8 @@ package email; +import email.model.Email; import email.model.EmailForm; +import email.service.EmailService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; @@ -9,6 +11,11 @@ import org.springframework.web.bind.annotation.PostMapping; @Controller public class EmailController { + private final EmailService emailService; + + public EmailController(EmailService emailService) { + this.emailService = emailService; + } @GetMapping("/") public String indexForm(Model model) { @@ -22,7 +29,14 @@ public class EmailController { model.addAttribute("error", "'Кому' не должно быть пустым"); return "index"; } - return "result"; + emailService.save(new Email(emailForm.getTo(), emailForm.getSubject(), emailForm.getMessage())); + model.addAttribute("emails", emailService.getAllEmails()); + return "list"; } + @GetMapping("/list") + public String list(Model model) { + model.addAttribute("emails", emailService.getAllEmails()); + return "list"; + } } diff --git a/src/main/java/email/core/BaseEntity.java b/src/main/java/email/core/BaseEntity.java new file mode 100644 index 0000000..fced11c --- /dev/null +++ b/src/main/java/email/core/BaseEntity.java @@ -0,0 +1,83 @@ +package email.core; + +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.MappedSuperclass; +import javax.persistence.Version; +import javax.validation.constraints.NotNull; +import java.io.Serializable; + +@MappedSuperclass +public abstract class BaseEntity implements Serializable, Comparable { + @Id + @GeneratedValue(strategy = GenerationType.TABLE) + private Integer id; + + @Version + private Integer version; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public Integer getVersion() { + return version; + } + + public void setVersion(Integer version) { + this.version = version; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (!getClass().isAssignableFrom(obj.getClass())) { + return false; + } + BaseEntity other = (BaseEntity) obj; + if (id == null) { + if (other.id != null) { + return false; + } + } else if (!id.equals(other.id)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + (id == null ? 0 : id.hashCode()); + return result; + } + + @Override + public String toString() { + return getClass().getSimpleName() + "{" + + "id=" + id + + ", version=" + version + + '}'; + } + + @Override + public int compareTo(@NotNull BaseEntity o) { + return id != null ? id.compareTo(o.getId()) : -1; + } + + public void reset() { + this.id = null; + this.version = null; + } +} \ No newline at end of file diff --git a/src/main/java/email/model/Email.java b/src/main/java/email/model/Email.java new file mode 100644 index 0000000..d2eefe3 --- /dev/null +++ b/src/main/java/email/model/Email.java @@ -0,0 +1,46 @@ +package email.model; + +import email.core.BaseEntity; + +import javax.persistence.Entity; + +@Entity +public class Email extends BaseEntity { + private String to; + private String subject; + private String message; + + public Email() { + + } + + public Email(String to, String subject, String message) { + this.to = to; + this.subject = subject; + this.message = message; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getSubject() { + return subject; + } + + public void setSubject(String subject) { + this.subject = subject; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/src/main/java/email/repository/EmailRepository.java b/src/main/java/email/repository/EmailRepository.java new file mode 100644 index 0000000..80be2c6 --- /dev/null +++ b/src/main/java/email/repository/EmailRepository.java @@ -0,0 +1,10 @@ +package email.repository; + +import email.model.Email; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EmailRepository extends JpaRepository { +} + diff --git a/src/main/java/email/service/EmailService.java b/src/main/java/email/service/EmailService.java new file mode 100644 index 0000000..237a6ea --- /dev/null +++ b/src/main/java/email/service/EmailService.java @@ -0,0 +1,33 @@ +package email.service; + +import email.model.Email; +import email.repository.EmailRepository; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class EmailService { + private final EmailRepository emailRepository; + + public EmailService(EmailRepository emailRepository) { + this.emailRepository = emailRepository; + } + + public List getAllEmails() { + return emailRepository.findAll(); + } + + public Email save(Email email) { + return emailRepository.save(email); + } + + public Email getEmailById(Integer id) { + return emailRepository.getOne(id); + } + + public void delete(Integer setId) { + emailRepository.deleteById(setId); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..fc200ed --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,14 @@ +# Server Settings +spring.main.banner-mode=off +server.port=8080 +# Log settings (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF) +logging.level.ru.ulstu=DEBUG +#JPA +# go to http://localhost:8080/h2-console +spring.datasource.url=jdbc:h2:file:./data/emails +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file diff --git a/src/main/resources/templates/list.html b/src/main/resources/templates/list.html new file mode 100644 index 0000000..675aaae --- /dev/null +++ b/src/main/resources/templates/list.html @@ -0,0 +1,42 @@ + + + + Список записей в БД + + + + + + + + + + +

Список записей в БД:

+ + + + + + + + + + + + + + + +
КомуТемаСообщение
+ + +
+ +Отправить другое сообщение + + -- 2.34.1 From 46f904d893b03479e37a0a76f5f70b02e9c7d553 Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Thu, 28 Oct 2021 17:33:49 +0400 Subject: [PATCH 2/6] fix dependencies --- build.gradle | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build.gradle b/build.gradle index 6b4ba46..636ed5d 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,8 @@ dependencies { compile("org.springframework.boot:spring-boot-devtools") compile("org.springframework.boot:spring-boot-starter-data-jpa") compile("com.h2database:h2") + compile("javax.xml.bind:jaxb-api:2.3.0") + compile("org.javassist:javassist:3.23.1-GA") testCompile("junit:junit") } -- 2.34.1 From fecef79d769da830815b3733dc8cfe9217ddb554 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 14 Sep 2022 14:31:22 +0400 Subject: [PATCH 3/6] fix build.gradle --- build.gradle | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 7337770..7198a5c 100644 --- a/build.gradle +++ b/build.gradle @@ -13,10 +13,6 @@ apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' -bootJar { - baseName = 'gs-serving-web-content' - version = '0.1.0' -} repositories { mavenCentral() @@ -26,13 +22,14 @@ sourceCompatibility = 17 targetCompatibility = 17 dependencies { - compile("org.springframework.boot:spring-boot-starter-web") - compile("org.springframework.boot:spring-boot-starter-thymeleaf") - compile("org.springframework.boot:spring-boot-devtools") - compile("org.springframework.boot:spring-boot-starter-data-jpa") - compile("com.h2database:h2") - compile("javax.xml.bind:jaxb-api:2.3.0") - compile("org.javassist:javassist:3.23.1-GA") - testCompile("junit:junit") + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web' + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-thymeleaf' + implementation group: 'org.springframework.boot', name: 'spring-boot-devtools' + implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa' + implementation group: 'com.h2database', name: 'h2' + implementation group: 'javax.xml.bind', name: 'jaxb-api' + implementation group: 'javax.validation', name: 'validation-api', version: '2.0.1.Final' + implementation group: 'javassist', name: 'javassist', version: '3.12.1.GA' + implementation group: 'junit', name: 'junit' } -- 2.34.1 From 2ec0ca57161f8b355bae9107d3442642986162dd Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Wed, 14 Sep 2022 15:58:54 +0400 Subject: [PATCH 4/6] add bootstrap --- src/main/resources/templates/index.html | 92 +++++++++++++----- src/main/resources/templates/list.html | 122 +++++++++++++++++------- 2 files changed, 160 insertions(+), 54 deletions(-) diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index f37aa8f..16df476 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -2,29 +2,77 @@ Простая обработка формы на Spring MVC - + + + -

Форма

-
-

- - - - - - - - - - - -
Тема:
Кому:
Сообщение: