#13 -- add model, repository and service for members

This commit is contained in:
Anton Romanov 2022-04-26 16:30:07 +04:00
parent 4862f26258
commit 77c24704c6
4 changed files with 84 additions and 0 deletions

View 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;
}
}

View File

@ -0,0 +1,6 @@
package ru.ulstu.members;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MembersRepository extends JpaRepository<Member, Integer> {
}

View 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);
}
}

View File

@ -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