1
0

feat(web): 优化统一接口和类的定义

This commit is contained in:
2024-11-28 10:39:53 +08:00
parent 66bf1b9242
commit 499b7dc597
9 changed files with 227 additions and 173 deletions

View File

@@ -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<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
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<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
AmisResponse<Long> save(SAVE_ITEM item) throws Exception;
private final SimpleService<ENTITY> service;
AmisResponse<ImmutableSet<LIST_ITEM>> list() throws Exception;
public SimpleController(SimpleService<ENTITY> service) {
this.service = service;
}
AmisResponse<DETAIL_ITEM> detail(Long id) throws Exception;
@PostMapping(SAVE)
public AmisResponse<Long> save(@RequestBody SAVE_ITEM item) throws Exception {
return AmisResponse.responseSuccess(service.save(fromSaveItem(item)));
}
@GetMapping(LIST)
public AmisResponse<ImmutableSet<LIST_ITEM>> list() {
return AmisResponse.responseSuccess(service.list().collect(entity -> {
try {
return toListItem(entity);
} catch (Exception e) {
throw new RuntimeException(e);
}
}));
}
@GetMapping(DETAIL)
public AmisResponse<DETAIL_ITEM> detail(@PathVariable Long id) throws Exception {
return AmisResponse.responseSuccess(toDetailItem(service.detail(id)));
}
@GetMapping(REMOVE)
public AmisResponse<Object> 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<Object> remove(Long id) throws Exception;
}

View File

@@ -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<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
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<ENTITY> service;
public SimpleControllerSupport(SimpleServiceSupport<ENTITY> service) {
this.service = service;
}
@PostMapping(SAVE)
@Override
public AmisResponse<Long> save(@RequestBody SAVE_ITEM item) throws Exception {
return AmisResponse.responseSuccess(service.save(fromSaveItem(item)));
}
@GetMapping(LIST)
@Override
public AmisResponse<ImmutableSet<LIST_ITEM>> 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_ITEM> detail(@PathVariable Long id) throws Exception {
return AmisResponse.responseSuccess(toDetailItem(service.detail(id)));
}
@GetMapping(REMOVE)
@Override
public AmisResponse<Object> 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;
}

View File

@@ -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<ENTITY extends SimpleEntity> {
private final SimpleRepository<ENTITY, Long> repository;
private final UserService userService;
public SimpleService(SimpleRepository<ENTITY, Long> 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<ENTITY> list() {
return Sets.immutable.ofAll(repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> 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<ENTITY> list(ImmutableSet<Long> ids) {
return Sets.immutable.ofAll(repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> 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<ENTITY> detailOptional(Long id) {
if (ObjectUtil.isNull(id)) {
return Optional.empty();
}
return repository.findOne(
(root, query, builder) -> {
MutableList<Predicate> 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<ENTITY extends SimpleEntity> {
Long save(ENTITY entity) throws Exception;
ImmutableSet<ENTITY> list() throws Exception;
ImmutableSet<ENTITY> list(ImmutableSet<Long> ids) throws Exception;
Optional<ENTITY> 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;
}

View File

@@ -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<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
private final SimpleRepository<ENTITY, Long> repository;
private final UserService userService;
public SimpleServiceSupport(SimpleRepository<ENTITY, Long> 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<ENTITY> list() {
return Sets.immutable.ofAll(repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> 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<ENTITY> list(ImmutableSet<Long> ids) {
return Sets.immutable.ofAll(repository.findAll(
(root, query, builder) -> {
MutableList<Predicate> 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<ENTITY> detailOptional(Long id) {
if (ObjectUtil.isNull(id)) {
return Optional.empty();
}
return repository.findOne(
(root, query, builder) -> {
MutableList<Predicate> 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));
}
}
}

View File

@@ -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<Confirmation, ConfirmationController.SaveItem, ConfirmationController.ListItem, ConfirmationController.DetailItem> {
public class ConfirmationController extends SimpleControllerSupport<Confirmation, ConfirmationController.SaveItem, ConfirmationController.ListItem, ConfirmationController.DetailItem> {
private final ConfirmationService confirmationService;
private final DataResourceService dataResourceService;
private final DataFileService dataFileService;

View File

@@ -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<Confirmation> {
public class ConfirmationService extends SimpleServiceSupport<Confirmation> {
private final ConfirmationRepository confirmationRepository;
private final UserService userService;

View File

@@ -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<DataResource, DataResourceController.SaveItem, DataResourceController.ListItem, DataResourceController.DetailItem> {
public class DataResourceController extends SimpleControllerSupport<DataResource, DataResourceController.SaveItem, DataResourceController.ListItem, DataResourceController.DetailItem> {
private final ObjectMapper mapper;
private final DataFileService dataFileService;

View File

@@ -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<DataResource> {
public class DataResourceService extends SimpleServiceSupport<DataResource> {
private final ResourceTypeRepository resourceTypeRepository;
private final ResourceFormatRepository resourceFormatRepository;

View File

@@ -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<DataFile> {
public class DataFileService extends SimpleServiceSupport<DataFile> {
private final DataFileRepository dataFileRepository;
private final UserService userService;