From 77c24704c6ebf9e3556c0aa9bcf26d3315a1f8e0 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Tue, 26 Apr 2022 16:30:07 +0400 Subject: [PATCH] #13 -- add model, repository and service for members --- src/main/java/ru/ulstu/members/Member.java | 40 +++++++++++++++++++ .../ru/ulstu/members/MembersRepository.java | 6 +++ .../java/ru/ulstu/members/MembersService.java | 37 +++++++++++++++++ src/main/resources/application.properties | 1 + 4 files changed, 84 insertions(+) create mode 100644 src/main/java/ru/ulstu/members/Member.java create mode 100644 src/main/java/ru/ulstu/members/MembersRepository.java create mode 100644 src/main/java/ru/ulstu/members/MembersService.java diff --git a/src/main/java/ru/ulstu/members/Member.java b/src/main/java/ru/ulstu/members/Member.java new file mode 100644 index 0000000..f868bd6 --- /dev/null +++ b/src/main/java/ru/ulstu/members/Member.java @@ -0,0 +1,40 @@ +package ru.ulstu.members; + +import org.springframework.web.multipart.MultipartFile; +import ru.ulstu.model.BaseEntity; + +import javax.persistence.Entity; +import javax.persistence.Transient; + +@Entity +public class Member extends BaseEntity { + private String name; + private String imageFileName; + @Transient + private MultipartFile imageFile; + + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getImageFileName() { + return imageFileName; + } + + public void setImageFileName(String imageFileName) { + this.imageFileName = imageFileName; + } + + public MultipartFile getImageFile() { + return imageFile; + } + + public void setImageFile(MultipartFile imageFile) { + this.imageFile = imageFile; + } +} diff --git a/src/main/java/ru/ulstu/members/MembersRepository.java b/src/main/java/ru/ulstu/members/MembersRepository.java new file mode 100644 index 0000000..6a4347a --- /dev/null +++ b/src/main/java/ru/ulstu/members/MembersRepository.java @@ -0,0 +1,6 @@ +package ru.ulstu.members; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MembersRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/members/MembersService.java b/src/main/java/ru/ulstu/members/MembersService.java new file mode 100644 index 0000000..ce09cfb --- /dev/null +++ b/src/main/java/ru/ulstu/members/MembersService.java @@ -0,0 +1,37 @@ +package ru.ulstu.members; + +import org.springframework.stereotype.Service; +import ru.ulstu.files.FileSystemStorageService; +import ru.ulstu.files.FileUtil; + +import java.io.IOException; + +@Service +public class MembersService { + + private final MembersRepository membersRepository; + + public MembersService(MembersRepository membersRepository) { + this.membersRepository = membersRepository; + } + + public Member save(Member member) throws IOException { + String fileName = System.currentTimeMillis() + ""; + if (!member.getImageFile().isEmpty()) { + member.setImageFileName(fileName); + FileUtil.saveFile(FileSystemStorageService.UPLOAD_DIR, fileName, member.getImageFile()); + } else { + member.setImageFileName(member.getImageFileName().isEmpty() ? "user.png" : member.getImageFileName()); + } + + if (member.getId() != null && (member.getId() != 0)) { + return membersRepository.save(member); + } else { + return create(member); + } + } + + public Member create(Member member) { + return membersRepository.save(member); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7c55a71..38bbb30 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -13,3 +13,4 @@ spring.datasource.password=password spring.jpa.database-platform=org.hibernate.dialect.H2Dialect spring.h2.console.enabled=true spring.jpa.hibernate.ddl-auto=update +spring.jpa.open-in-view=false -- 2.34.1