#13 -- add model, repository and service for members
This commit is contained in:
parent
4862f26258
commit
77c24704c6
40
src/main/java/ru/ulstu/members/Member.java
Normal file
40
src/main/java/ru/ulstu/members/Member.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
6
src/main/java/ru/ulstu/members/MembersRepository.java
Normal file
6
src/main/java/ru/ulstu/members/MembersRepository.java
Normal file
@ -0,0 +1,6 @@
|
||||
package ru.ulstu.members;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface MembersRepository extends JpaRepository<Member, Integer> {
|
||||
}
|
37
src/main/java/ru/ulstu/members/MembersService.java
Normal file
37
src/main/java/ru/ulstu/members/MembersService.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user