mirror of
https://github.com/vran-dev/databasir.git
synced 2025-08-08 17:32:14 +08:00
feat: support add / delete / query remark
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.databasir.core.domain.remark.converter;
|
||||
|
||||
import com.databasir.core.domain.remark.data.RemarkResponse;
|
||||
import com.databasir.dao.tables.pojos.DocumentRemarkPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
@Mapper(componentModel = "spring")
|
||||
public interface RemarkResponseConverter {
|
||||
|
||||
@Mapping(target = "remarkBy.userId", source = "remarkBy.id")
|
||||
@Mapping(target = "remarkBy.nickname", source = "remarkBy.nickname")
|
||||
@Mapping(target = "remarkBy.email", source = "remarkBy.email")
|
||||
@Mapping(target = "id", source = "remark.id")
|
||||
@Mapping(target = "createAt", source = "remark.createAt")
|
||||
RemarkResponse of(DocumentRemarkPojo remark,
|
||||
UserPojo remarkBy);
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package com.databasir.core.domain.remark.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Data
|
||||
public class RemarkCreateRequest {
|
||||
|
||||
@NotBlank
|
||||
private String remark;
|
||||
|
||||
@NotNull
|
||||
private String tableName;
|
||||
|
||||
private String columnName;
|
||||
}
|
@@ -0,0 +1,35 @@
|
||||
package com.databasir.core.domain.remark.data;
|
||||
|
||||
import com.databasir.dao.Tables;
|
||||
import lombok.Data;
|
||||
import org.jooq.Condition;
|
||||
import org.jooq.impl.DSL;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class RemarkListCondition {
|
||||
|
||||
@NotBlank
|
||||
private String tableName;
|
||||
|
||||
private String columnName;
|
||||
|
||||
public Condition toCondition(Integer projectId) {
|
||||
List<Condition> conditions = new ArrayList<>();
|
||||
Condition condition = Tables.DOCUMENT_REMARK.TABLE_NAME.eq(tableName);
|
||||
conditions.add(condition);
|
||||
|
||||
Condition columnCondition;
|
||||
if (columnName != null) {
|
||||
columnCondition = Tables.DOCUMENT_REMARK.COLUMN_NAME.eq(columnName);
|
||||
} else {
|
||||
columnCondition = Tables.DOCUMENT_REMARK.COLUMN_NAME.isNull();
|
||||
}
|
||||
conditions.add(columnCondition);
|
||||
conditions.add(Tables.DOCUMENT_REMARK.PROJECT_ID.eq(projectId));
|
||||
return conditions.stream().reduce(Condition::and).orElse(DSL.trueCondition());
|
||||
}
|
||||
}
|
@@ -0,0 +1,30 @@
|
||||
package com.databasir.core.domain.remark.data;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Data
|
||||
public class RemarkResponse {
|
||||
|
||||
private Integer id;
|
||||
|
||||
private Integer projectId;
|
||||
|
||||
private String remark;
|
||||
|
||||
private RemarkUser remarkBy;
|
||||
|
||||
private LocalDateTime createAt;
|
||||
|
||||
@Data
|
||||
public static class RemarkUser {
|
||||
|
||||
private Integer userId;
|
||||
|
||||
private String nickname;
|
||||
|
||||
private String email;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
package com.databasir.core.domain.remark.service;
|
||||
|
||||
import com.databasir.common.exception.Forbidden;
|
||||
import com.databasir.core.domain.remark.converter.RemarkResponseConverter;
|
||||
import com.databasir.core.domain.remark.data.RemarkCreateRequest;
|
||||
import com.databasir.core.domain.remark.data.RemarkListCondition;
|
||||
import com.databasir.core.domain.remark.data.RemarkResponse;
|
||||
import com.databasir.dao.impl.DocumentRemarkDao;
|
||||
import com.databasir.dao.impl.ProjectDao;
|
||||
import com.databasir.dao.impl.UserDao;
|
||||
import com.databasir.dao.tables.pojos.DocumentRemarkPojo;
|
||||
import com.databasir.dao.tables.pojos.UserPojo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class DocumentRemarkService {
|
||||
|
||||
private final DocumentRemarkDao documentRemarkDao;
|
||||
|
||||
private final ProjectDao projectDao;
|
||||
|
||||
private final UserDao userDao;
|
||||
|
||||
private final RemarkResponseConverter remarkResponseConverter;
|
||||
|
||||
public void deleteById(Integer groupId,
|
||||
Integer projectId,
|
||||
Integer remarkId) {
|
||||
if (projectDao.exists(groupId, projectId)) {
|
||||
documentRemarkDao.deleteById(remarkId);
|
||||
} else {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
public Page<RemarkResponse> list(Integer groupId,
|
||||
Integer projectId,
|
||||
Pageable pageable,
|
||||
RemarkListCondition condition) {
|
||||
if (projectDao.exists(groupId, projectId)) {
|
||||
Page<DocumentRemarkPojo> data = documentRemarkDao.selectByPage(pageable, condition.toCondition(projectId));
|
||||
Set<Integer> userIdList = data.getContent()
|
||||
.stream()
|
||||
.map(DocumentRemarkPojo::getUserId)
|
||||
.collect(Collectors.toSet());
|
||||
Map<Integer, UserPojo> userMapById = userDao.selectUserIdIn(userIdList)
|
||||
.stream()
|
||||
.collect(Collectors.toMap(UserPojo::getId, Function.identity()));
|
||||
return data
|
||||
.map(remarkPojo -> {
|
||||
UserPojo userPojo = userMapById.get(remarkPojo.getUserId());
|
||||
return remarkResponseConverter.of(remarkPojo, userPojo);
|
||||
});
|
||||
} else {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
public void create(Integer groupId, Integer projectId, Integer userId, RemarkCreateRequest request) {
|
||||
if (projectDao.exists(groupId, projectId)) {
|
||||
DocumentRemarkPojo pojo = new DocumentRemarkPojo();
|
||||
pojo.setUserId(userId);
|
||||
pojo.setProjectId(projectId);
|
||||
pojo.setRemark(request.getRemark());
|
||||
pojo.setTableName(request.getTableName());
|
||||
pojo.setColumnName(request.getColumnName());
|
||||
documentRemarkDao.insertAndReturnId(pojo);
|
||||
} else {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user