diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleController.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleController.java index 301106d..b92cc2a 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleController.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleController.java @@ -1,60 +1,18 @@ package com.eshore.gringotts.web.domain.base.controller; import com.eshore.gringotts.web.configuration.amis.AmisResponse; -import com.eshore.gringotts.web.domain.base.entity.SimpleEntity; -import com.eshore.gringotts.web.domain.base.service.SimpleService; import org.eclipse.collections.api.set.ImmutableSet; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; /** * @author lanyuanxiaoyao - * @date 2024-11-26 + * @date 2024-11-28 */ -public abstract class SimpleController { - protected static final String SAVE = "/save"; - protected static final String LIST = "/list"; - protected static final String DETAIL = "/detail/{id}"; - protected static final String REMOVE = "/remove/{id}"; +public interface SimpleController { + AmisResponse save(SAVE_ITEM item) throws Exception; - private final SimpleService service; + AmisResponse> list() throws Exception; - public SimpleController(SimpleService service) { - this.service = service; - } + AmisResponse detail(Long id) throws Exception; - @PostMapping(SAVE) - public AmisResponse save(@RequestBody SAVE_ITEM item) throws Exception { - return AmisResponse.responseSuccess(service.save(fromSaveItem(item))); - } - - @GetMapping(LIST) - public AmisResponse> list() { - return AmisResponse.responseSuccess(service.list().collect(entity -> { - try { - return toListItem(entity); - } catch (Exception e) { - throw new RuntimeException(e); - } - })); - } - - @GetMapping(DETAIL) - public AmisResponse detail(@PathVariable Long id) throws Exception { - return AmisResponse.responseSuccess(toDetailItem(service.detail(id))); - } - - @GetMapping(REMOVE) - public AmisResponse remove(@PathVariable Long id) { - service.remove(id); - return AmisResponse.responseSuccess(); - } - - protected abstract ENTITY fromSaveItem(SAVE_ITEM item) throws Exception; - - protected abstract LIST_ITEM toListItem(ENTITY entity) throws Exception; - - protected abstract DETAIL_ITEM toDetailItem(ENTITY entity) throws Exception; + AmisResponse remove(Long id) throws Exception; } diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleControllerSupport.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleControllerSupport.java new file mode 100644 index 0000000..d758a17 --- /dev/null +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/controller/SimpleControllerSupport.java @@ -0,0 +1,64 @@ +package com.eshore.gringotts.web.domain.base.controller; + +import com.eshore.gringotts.web.configuration.amis.AmisResponse; +import com.eshore.gringotts.web.domain.base.entity.SimpleEntity; +import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport; +import org.eclipse.collections.api.set.ImmutableSet; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +/** + * @author lanyuanxiaoyao + * @date 2024-11-26 + */ +public abstract class SimpleControllerSupport implements SimpleController { + protected static final String SAVE = "/save"; + protected static final String LIST = "/list"; + protected static final String DETAIL = "/detail/{id}"; + protected static final String REMOVE = "/remove/{id}"; + + private final SimpleServiceSupport service; + + public SimpleControllerSupport(SimpleServiceSupport service) { + this.service = service; + } + + @PostMapping(SAVE) + @Override + public AmisResponse save(@RequestBody SAVE_ITEM item) throws Exception { + return AmisResponse.responseSuccess(service.save(fromSaveItem(item))); + } + + @GetMapping(LIST) + @Override + public AmisResponse> list() { + return AmisResponse.responseSuccess(service.list().collect(entity -> { + try { + return toListItem(entity); + } catch (Exception e) { + throw new RuntimeException(e); + } + })); + } + + @GetMapping(DETAIL) + @Override + public AmisResponse detail(@PathVariable Long id) throws Exception { + return AmisResponse.responseSuccess(toDetailItem(service.detail(id))); + } + + @GetMapping(REMOVE) + @Override + public AmisResponse remove(@PathVariable Long id) { + service.remove(id); + return AmisResponse.responseSuccess(); + } + + protected abstract ENTITY fromSaveItem(SAVE_ITEM item) throws Exception; + + protected abstract LIST_ITEM toListItem(ENTITY entity) throws Exception; + + protected abstract DETAIL_ITEM toDetailItem(ENTITY entity) throws Exception; +} diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleService.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleService.java index 4119206..26d6c40 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleService.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleService.java @@ -1,125 +1,20 @@ package com.eshore.gringotts.web.domain.base.service; -import cn.hutool.core.bean.BeanUtil; -import cn.hutool.core.bean.copier.CopyOptions; -import cn.hutool.core.util.ObjectUtil; -import cn.hutool.core.util.StrUtil; import com.eshore.gringotts.web.domain.base.entity.SimpleEntity; -import com.eshore.gringotts.web.domain.base.repository.SimpleRepository; -import com.eshore.gringotts.web.domain.user.entity.User; -import com.eshore.gringotts.web.domain.user.service.UserService; import java.util.Optional; -import javax.persistence.criteria.Predicate; -import javax.transaction.Transactional; -import lombok.extern.slf4j.Slf4j; -import org.eclipse.collections.api.factory.Lists; -import org.eclipse.collections.api.factory.Sets; -import org.eclipse.collections.api.list.MutableList; import org.eclipse.collections.api.set.ImmutableSet; -import org.springframework.data.domain.Sort; /** * @author lanyuanxiaoyao - * @date 2024-11-21 + * @date 2024-11-28 */ -@Slf4j -public abstract class SimpleService { - private final SimpleRepository repository; - private final UserService userService; - - public SimpleService(SimpleRepository repository, UserService userService) { - this.repository = repository; - this.userService = userService; - } - - @Transactional(rollbackOn = Throwable.class) - public Long save(ENTITY entity) { - if (ObjectUtil.isNotNull(entity.getId())) { - Long id = entity.getId(); - ENTITY targetEntity = repository.findById(entity.getId()).orElseThrow(() -> new IdNotFoundException(id)); - BeanUtil.copyProperties( - entity, - targetEntity, - CopyOptions.create() - .setIgnoreProperties("id", "createdUser", "createdTime", "modifiedUser", "modifiedTime") - ); - entity = targetEntity; - } - User user = userService.currentLoginUser(); - if (ObjectUtil.isNull(entity.getCreatedUser())) { - entity.setCreatedUser(user); - } - entity.setModifiedUser(user); - entity = repository.save(entity); - return entity.getId(); - } - - public ImmutableSet list() { - return Sets.immutable.ofAll(repository.findAll( - (root, query, builder) -> { - MutableList predicates = Lists.mutable.empty(); - User user = userService.currentLoginUser(); - if (User.isNotAdministrator(user)) { - predicates.add(builder.equal(root.get("createdUser"), user)); - } - return builder.and(predicates.toArray(new Predicate[predicates.size()])); - }, - Sort.by("createdTime").descending() - )); - } - - public ImmutableSet list(ImmutableSet ids) { - return Sets.immutable.ofAll(repository.findAll( - (root, query, builder) -> { - MutableList predicates = Lists.mutable.of( - builder.in(root.get("id")).value(ids.select(ObjectUtil::isNotNull)) - ); - User user = userService.currentLoginUser(); - if (User.isNotAdministrator(user)) { - predicates.add(builder.equal(root.get("createdUser"), user)); - } - return builder.and(predicates.toArray(new Predicate[predicates.size()])); - }, - Sort.by("createdTime").descending() - )); - } - - public Optional detailOptional(Long id) { - if (ObjectUtil.isNull(id)) { - return Optional.empty(); - } - return repository.findOne( - (root, query, builder) -> { - MutableList predicates = Lists.mutable.of( - builder.equal(root.get("id"), id) - ); - User user = userService.currentLoginUser(); - if (User.isNotAdministrator(user)) { - predicates.add(builder.equal(root.get("createdUser"), user)); - } - return builder.and(predicates.toArray(new Predicate[predicates.size()])); - } - ); - } - - public ENTITY detail(Long id) { - return detailOptional(id).orElse(null); - } - - public ENTITY detailOrThrow(Long id) { - return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id)); - } - - @Transactional(rollbackOn = Throwable.class) - public void remove(Long id) { - if (ObjectUtil.isNotNull(id)) { - repository.deleteById(id); - } - } - - public static final class IdNotFoundException extends RuntimeException { - public IdNotFoundException(Long id) { - super(StrUtil.format("ID为{}的资源不存在", id)); - } - } +public interface SimpleService { + Long save(ENTITY entity) throws Exception; + ImmutableSet list() throws Exception; + ImmutableSet list(ImmutableSet ids) throws Exception; + Optional detailOptional(Long id) throws Exception; + ENTITY detail(Long id) throws Exception; + ENTITY detailOrThrow(Long id) throws Exception; + ENTITY detailOrNull(Long id) throws Exception; + void remove(Long id) throws Exception; } diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleServiceSupport.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleServiceSupport.java new file mode 100644 index 0000000..8c587b0 --- /dev/null +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/base/service/SimpleServiceSupport.java @@ -0,0 +1,137 @@ +package com.eshore.gringotts.web.domain.base.service; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.bean.copier.CopyOptions; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; +import com.eshore.gringotts.web.domain.base.entity.SimpleEntity; +import com.eshore.gringotts.web.domain.base.repository.SimpleRepository; +import com.eshore.gringotts.web.domain.user.entity.User; +import com.eshore.gringotts.web.domain.user.service.UserService; +import java.util.Optional; +import javax.persistence.criteria.Predicate; +import javax.transaction.Transactional; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.collections.api.factory.Lists; +import org.eclipse.collections.api.factory.Sets; +import org.eclipse.collections.api.list.MutableList; +import org.eclipse.collections.api.set.ImmutableSet; +import org.springframework.data.domain.Sort; + +/** + * @author lanyuanxiaoyao + * @date 2024-11-21 + */ +@Slf4j +public abstract class SimpleServiceSupport implements SimpleService { + private final SimpleRepository repository; + private final UserService userService; + + public SimpleServiceSupport(SimpleRepository repository, UserService userService) { + this.repository = repository; + this.userService = userService; + } + + @Transactional(rollbackOn = Throwable.class) + @Override + public Long save(ENTITY entity) { + if (ObjectUtil.isNotNull(entity.getId())) { + Long id = entity.getId(); + ENTITY targetEntity = repository.findById(entity.getId()).orElseThrow(() -> new IdNotFoundException(id)); + BeanUtil.copyProperties( + entity, + targetEntity, + CopyOptions.create() + .setIgnoreProperties("id", "createdUser", "createdTime", "modifiedUser", "modifiedTime") + ); + entity = targetEntity; + } + User user = userService.currentLoginUser(); + if (ObjectUtil.isNull(entity.getCreatedUser())) { + entity.setCreatedUser(user); + } + entity.setModifiedUser(user); + entity = repository.save(entity); + return entity.getId(); + } + + @Override + public ImmutableSet list() { + return Sets.immutable.ofAll(repository.findAll( + (root, query, builder) -> { + MutableList predicates = Lists.mutable.empty(); + User user = userService.currentLoginUser(); + if (User.isNotAdministrator(user)) { + predicates.add(builder.equal(root.get("createdUser"), user)); + } + return builder.and(predicates.toArray(new Predicate[predicates.size()])); + }, + Sort.by("createdTime").descending() + )); + } + + @Override + public ImmutableSet list(ImmutableSet ids) { + return Sets.immutable.ofAll(repository.findAll( + (root, query, builder) -> { + MutableList predicates = Lists.mutable.of( + builder.in(root.get("id")).value(ids.select(ObjectUtil::isNotNull)) + ); + User user = userService.currentLoginUser(); + if (User.isNotAdministrator(user)) { + predicates.add(builder.equal(root.get("createdUser"), user)); + } + return builder.and(predicates.toArray(new Predicate[predicates.size()])); + }, + Sort.by("createdTime").descending() + )); + } + + @Override + public Optional detailOptional(Long id) { + if (ObjectUtil.isNull(id)) { + return Optional.empty(); + } + return repository.findOne( + (root, query, builder) -> { + MutableList predicates = Lists.mutable.of( + builder.equal(root.get("id"), id) + ); + User user = userService.currentLoginUser(); + if (User.isNotAdministrator(user)) { + predicates.add(builder.equal(root.get("createdUser"), user)); + } + return builder.and(predicates.toArray(new Predicate[predicates.size()])); + } + ); + } + + @Override + public ENTITY detail(Long id) { + return detailOrNull(id); + } + + @Override + public ENTITY detailOrThrow(Long id) { + return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id)); + } + + @Override + public ENTITY detailOrNull(Long id) { + return detailOptional(id).orElse(null); + } + + @Transactional(rollbackOn = Throwable.class) + @Override + public void remove(Long id) { + if (ObjectUtil.isNotNull(id)) { + repository.deleteById(id); + } + } + + public static final class IdNotFoundException extends RuntimeException { + public IdNotFoundException(Long id) { + super(StrUtil.format("ID为{}的资源不存在", id)); + } + } +} diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/controller/ConfirmationController.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/controller/ConfirmationController.java index c659824..76296e7 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/controller/ConfirmationController.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/controller/ConfirmationController.java @@ -1,7 +1,7 @@ package com.eshore.gringotts.web.domain.confirmation.controller; import com.eshore.gringotts.web.configuration.amis.AmisResponse; -import com.eshore.gringotts.web.domain.base.controller.SimpleController; +import com.eshore.gringotts.web.domain.base.controller.SimpleControllerSupport; import com.eshore.gringotts.web.domain.base.entity.FileInfo; import com.eshore.gringotts.web.domain.base.entity.SimpleListItem; import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem; @@ -27,7 +27,7 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("confirmation") -public class ConfirmationController extends SimpleController { +public class ConfirmationController extends SimpleControllerSupport { private final ConfirmationService confirmationService; private final DataResourceService dataResourceService; private final DataFileService dataFileService; diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/service/ConfirmationService.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/service/ConfirmationService.java index 74f9c08..22f8d60 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/service/ConfirmationService.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/confirmation/service/ConfirmationService.java @@ -1,7 +1,7 @@ package com.eshore.gringotts.web.domain.confirmation.service; import cn.hutool.core.util.ObjectUtil; -import com.eshore.gringotts.web.domain.base.service.SimpleService; +import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport; import com.eshore.gringotts.web.domain.confirmation.entity.Confirmation; import com.eshore.gringotts.web.domain.confirmation.repository.ConfirmationRepository; import com.eshore.gringotts.web.domain.user.service.UserService; @@ -14,7 +14,7 @@ import org.springframework.stereotype.Service; */ @Slf4j @Service -public class ConfirmationService extends SimpleService { +public class ConfirmationService extends SimpleServiceSupport { private final ConfirmationRepository confirmationRepository; private final UserService userService; diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/controller/DataResourceController.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/controller/DataResourceController.java index bb5c38d..83dfdca 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/controller/DataResourceController.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/controller/DataResourceController.java @@ -2,7 +2,7 @@ package com.eshore.gringotts.web.domain.resource.controller; import cn.hutool.core.util.EnumUtil; import cn.hutool.core.util.ObjectUtil; -import com.eshore.gringotts.web.domain.base.controller.SimpleController; +import com.eshore.gringotts.web.domain.base.controller.SimpleControllerSupport; import com.eshore.gringotts.web.domain.base.entity.FileInfo; import com.eshore.gringotts.web.domain.base.entity.SimpleListItem; import com.eshore.gringotts.web.domain.base.entity.SimpleSaveItem; @@ -42,7 +42,7 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController @RequestMapping("/data_resource") -public class DataResourceController extends SimpleController { +public class DataResourceController extends SimpleControllerSupport { private final ObjectMapper mapper; private final DataFileService dataFileService; diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/service/DataResourceService.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/service/DataResourceService.java index 43daf71..6c4e1bf 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/service/DataResourceService.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/resource/service/DataResourceService.java @@ -1,6 +1,6 @@ package com.eshore.gringotts.web.domain.resource.service; -import com.eshore.gringotts.web.domain.base.service.SimpleService; +import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport; import com.eshore.gringotts.web.domain.resource.entity.DataResource; import com.eshore.gringotts.web.domain.resource.entity.format.ResourceFormat; import com.eshore.gringotts.web.domain.resource.entity.type.ResourceType; @@ -18,7 +18,7 @@ import org.springframework.stereotype.Service; */ @Slf4j @Service -public class DataResourceService extends SimpleService { +public class DataResourceService extends SimpleServiceSupport { private final ResourceTypeRepository resourceTypeRepository; private final ResourceFormatRepository resourceFormatRepository; diff --git a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/upload/service/DataFileService.java b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/upload/service/DataFileService.java index 76a5d3a..b966406 100644 --- a/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/upload/service/DataFileService.java +++ b/gringotts-web/src/main/java/com/eshore/gringotts/web/domain/upload/service/DataFileService.java @@ -1,6 +1,6 @@ package com.eshore.gringotts.web.domain.upload.service; -import com.eshore.gringotts.web.domain.base.service.SimpleService; +import com.eshore.gringotts.web.domain.base.service.SimpleServiceSupport; import com.eshore.gringotts.web.domain.upload.entity.DataFile; import com.eshore.gringotts.web.domain.upload.repository.DataFileRepository; import com.eshore.gringotts.web.domain.user.entity.User; @@ -16,7 +16,7 @@ import org.springframework.stereotype.Service; */ @Slf4j @Service -public class DataFileService extends SimpleService { +public class DataFileService extends SimpleServiceSupport { private final DataFileRepository dataFileRepository; private final UserService userService;