Skip to content

Commit

Permalink
concept cloud plugin template
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghanzheng committed Jun 5, 2023
1 parent 588e710 commit a6bb9a5
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
public interface Sample extends DomainEntity {

String getSample();

User getUser();

Users getUsers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class SampleImpl implements Sample {

protected String sample;

protected User user;

protected Users users;

public static class Builder extends AbstractDomainBuilder<SampleImpl> {

@NotEmpty
Expand All @@ -31,6 +35,12 @@ public static class Builder extends AbstractDomainBuilder<SampleImpl> {
@NotNull
protected String sample;

@NotNull
protected User user;

@NotNull
protected Users users;

public Builder id(String id) {
this.id = id;
return this;
Expand All @@ -41,14 +51,24 @@ public Builder sample(String sample) {
return this;
}

public Builder user(User user) {
this.user = user;
return this;
}

public Builder users(Users users) {
this.users = users;
return this;
}

@Override
protected void init() {

}

@Override
protected SampleImpl build() {
return new SampleImpl(id, sample);
return new SampleImpl(id, sample, user, users);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import $PACKAGE$.domain.sample.SampleService;
import $PACKAGE$.domain.user.User;
import $PACKAGE$.module.sample.domain.sample.view.SampleCreateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleUpdateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleDeleteCommand;
import com.github.linyuzai.domain.core.exception.DomainNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* 沸点应用服务
* 示例应用服务
*/
@Service
public class SampleApplicationService {
Expand All @@ -29,10 +30,22 @@ public class SampleApplicationService {
* 创建
*/
public void create(SampleCreateCommand create, User user) {
Sample sample = sampleFacadeAdapter.from(create, user);
Sample sample = sampleFacadeAdapter.from(create);
sampleService.create(sample, user);
}

/**
* 更新
*/
public void update(SampleUpdateCommand update, User user) {
Sample oldSample = sampleRepository.get(update.getId());
if (oldSample == null) {
throw new DomainNotFoundException(Sample.class, update.getId());
}
Sample newSample = sampleFacadeAdapter.from(update, oldSample);
sampleService.update(newSample, oldSample, user);
}

/**
* 删除
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import $PACKAGE$.domain.user.User;
import $PACKAGE$.login.Login;
import $PACKAGE$.module.sample.domain.sample.view.SampleCreateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleUpdateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleDeleteCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleQuery;
import $PACKAGE$.module.sample.domain.sample.view.SampleVO;
Expand All @@ -13,6 +14,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "示例")
@RestController
@RequestMapping("/sample")
Expand All @@ -30,6 +33,12 @@ public void create(@RequestBody SampleCreateCommand create, @Login User user) {
sampleApplicationService.create(create, user);
}

@Operation(summary = "更新")
@PutMapping
public void update(@RequestBody SampleUpdateCommand update, @Login User user) {
sampleApplicationService.update(update, user);
}

@Operation(summary = "删除")
@DeleteMapping
public void delete(@RequestBody SampleDeleteCommand delete, @Login User user) {
Expand All @@ -42,6 +51,12 @@ public SampleVO get(@Parameter(description = "ID") @PathVariable String id) {
return sampleSearcher.get(id);
}

@Operation(summary = "列表查询")
@GetMapping("list")
public List<SampleVO> list(SampleQuery query) {
return sampleSearcher.list(query);
}

@Operation(summary = "分页查询")
@GetMapping("/page")
public Pages<SampleVO> page(SampleQuery query, Pages.Args page) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package $PACKAGE$.module.sample.domain.sample;

import $PACKAGE$.domain.sample.Sample;
import $PACKAGE$.domain.user.User;
import $PACKAGE$.module.sample.domain.sample.view.SampleCreateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleUpdateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleQuery;
import $PACKAGE$.module.sample.domain.sample.view.SampleVO;
import com.github.linyuzai.domain.core.condition.Conditions;
Expand All @@ -15,7 +15,12 @@ public interface SampleFacadeAdapter {
/**
* 创建视图转领域模型
*/
Sample from(SampleCreateCommand create, User user);
Sample from(SampleCreateCommand create);

/**
* 更新视图转领域模型
*/
Sample from(SampleUpdateCommand update, Sample old);

/**
* 领域模型转视图
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import $PACKAGE$.domain.sample.SampleImpl;
import $PACKAGE$.domain.user.User;
import $PACKAGE$.module.sample.domain.sample.view.SampleCreateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleUpdateCommand;
import $PACKAGE$.module.sample.domain.sample.view.SampleQuery;
import $PACKAGE$.module.sample.domain.sample.view.SampleUserVO;
import $PACKAGE$.module.sample.domain.sample.view.SampleVO;
Expand All @@ -15,7 +16,7 @@
import org.springframework.stereotype.Component;

/**
* 沸点领域模型转换适配器实现
* 领域模型转换适配器实现
*/
@Component
public class SampleFacadeAdapterImpl implements SampleFacadeAdapter {
Expand All @@ -30,17 +31,35 @@ public class SampleFacadeAdapterImpl implements SampleFacadeAdapter {
private DomainValidator validator;

@Override
public Sample from(SampleCreateCommand create, User user) {
public Sample from(SampleCreateCommand create) {
String id = sampleIdGenerator.generateId(create);
return new SampleImpl.Builder()
.id(id)
.user(null)
.users(factory.createEmptyCollection(Users.class))
.build(validator);
}

@Override
public Sample from(SampleUpdateCommand update, Sample old) {
return new SampleImpl.Builder()
.id(old.getId())
.sample(update.getSample())
.user(old.getUser())
.users(old.getUsers())
.build(validator);
}

@Override
public SampleVO do2vo(Sample sample) {
SampleVO vo = new SampleVO();
vo.setId(sample.getId());
vo.setUser(getUser(sample.getUser()));
vo.setUsers(sample.getUsers()
.list()
.stream()
.map(this::getUser)
.collect(Collectors.toList()));
return vo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import $PACKAGE$.module.sample.domain.sample.view.SampleVO;
import com.github.linyuzai.domain.core.page.Pages;

import java.util.List;

/**
* 沸点搜索
* 搜索
*/
public interface SampleSearcher {

Expand All @@ -14,6 +16,11 @@ public interface SampleSearcher {
*/
SampleVO get(String id);

/**
* 列表查询
*/
List<SampleVO> list(SampleQuery query);

/**
* 分页查询
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
* 查询实现
*/
Expand All @@ -29,6 +32,15 @@ public SampleVO get(String id) {
return sampleFacadeAdapter.do2vo(sample);
}

@Override
public List<SampleVO> list(SampleQuery query) {
return sampleRepository.select(sampleFacadeAdapter.toConditions(query))
.list()
.stream()
.map(sampleFacadeAdapter::do2vo)
.collect(Collectors.toList());
}

@Override
public Pages<SampleVO> page(SampleQuery query, Pages.Args page) {
return sampleRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Data;

@Data
@Schema(description = "沸点修改命令")
@Schema(description = "修改命令")
public class SampleUpdateCommand {

@Schema(description = "ID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
import java.util.List;

@Data
@Schema(description = "沸点视图")
@Schema(description = "示例视图")
public class SampleVO {

@Schema(description = "沸点ID")
@Schema(description = "示例ID")
private String id;

@Schema(description = "沸点内容")
@Schema(description = "示例内容")
private String sample;

@Schema(description = "用户")
private SampleUserVO user;

@Schema(description = "用户列表")
private List<SampleUserVO> users;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package $PACKAGE$.module.sample.infrastructure.sample.mbp;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import $PACKAGE$.basic.boot.mbp.MBPBaseRepository;
import $PACKAGE$.domain.sample.Sample;
import $PACKAGE$.domain.sample.SampleImpl;
import $PACKAGE$.domain.sample.SampleRepository;
import $PACKAGE$.domain.sample.Samples;
import $PACKAGE$.domain.user.User;
import $PACKAGE$.domain.user.Users;
import com.github.linyuzai.domain.core.DomainFactory;
import com.github.linyuzai.domain.core.DomainValidator;
import com.github.linyuzai.domain.core.condition.LambdaConditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* 基于 MBP 的示例存储实现
Expand All @@ -22,26 +29,64 @@ public class MBPSampleRepository extends MBPBaseRepository<Sample, Samples, Samp
@Autowired
private SampleMapper sampleMapper;

@Autowired
private SampleUserMapper sampleUserMapper;

@Autowired
private DomainFactory factory;

@Autowired
private DomainValidator validator;

@Override
public SamplePO do2po(Sample pin) {
public SamplePO do2po(Sample sample) {
SamplePO po = new SamplePO();
po.setId(pin.getId());
po.setId(sample.getId());
if (sample.getUser() != null) {
po.setUserId(sample.getUser().getId());
}
return po;
}

@Override
public Sample po2do(SamplePO po) {
User user = po.getUserId() == null ? null : factory.createObject(User.class, po.getUserId());
List<SampleUserPO> sampleUsers = sampleUserMapper
.selectList(Wrappers.<SampleUserPO>lambdaQuery()
.eq(SampleUserPO::getId, po.getId()));
Set<String> userIds = sampleUsers.stream()
.map(SampleUserPO::getUserId)
.collect(Collectors.toSet());
Users users = factory.createCollection(Users.class,
new LambdaConditions().in(User::getId, userIds));
return new SampleImpl.Builder()
.id(po.getId())
.user(user)
.users(users)
.build(validator);
}

@Transactional
@Override
public void create(Sample sample) {
super.create(sample);
sample.getUsers().list().stream().map(it -> {
SampleUserPO sup = new SampleUserPO();
sup.setSampleId(sample.getId());
sup.setUserId(it.getId());
return sup;
}).forEach(sampleUserMapper::insert);

}

@Transactional
@Override
public void delete(Sample sample) {
super.delete(sample);
sampleUserMapper.delete(Wrappers.<SampleUserPO>lambdaQuery()
.eq(SampleUserPO::getSampleId, sample.getId()));
}

@Override
public BaseMapper<SamplePO> getBaseMapper() {
return sampleMapper;
Expand Down
Loading

0 comments on commit a6bb9a5

Please sign in to comment.