Как получить вложенные объекты с MongoDb с помощью Spring Boot?
У меня есть 3 DTO, BoardResponse, ColumnsResponse, CardResponse.
public class BoardResponse {
//id, name, createdBy,createdDate,updatedBy,updatedDate, getters
List<ColumnResponse> columns = new ArrayList<ColumnResponse>();
public BoardResponse(KanbanBoard board, List<ColumnResponse> columns) {
super();
this.id = board.getId();
this.name = board.getName();
this.createdBy = board.getCreatedBy();
this.createdDate = board.getCreatedDate();
this.updatedBy = board.getUpdatedBy();
this.updatedDate = board.getUpdatedDate();
this.columns = columns;
}
public class ColumnResponse {
//id, name, createdBy,createdDate,updatedBy,updatedDate, getters
private ObjectId idBoard;
List<CardResponse> cards = new ArrayList<>();
public ColumnResponse(KanbanColumn column, List<CardResponse> cards) {
super();
this.id = column.getId();
this.name = column.getName();
this.createdBy = column.getCreatedBy();
this.createdDate = column.getCreatedDate();
this.updatedBy = column.getUpdatedBy();
this.updatedDate = column.getUpdatedDate();
this.id = column.getIdBoard();
this.idBoard = column.getIdBoard();
this.cards = cards;
}
public class CardResponse {
//id, name, createdBy,createdDate,updatedBy,updatedDate, getters
private ObjectId idColumn;
public CardResponse(KanbanCard card) {
super();
this.id = card.getId();
this.name = card.getName();
this.createdBy = card.getCreatedBy();
this.createdDate = card.getCreatedDate();
this.updatedBy = card.getUpdatedBy();
this.updatedDate = card.getUpdatedDate();
this.idColumn = card.getIdColumn();
}
Я хочу, как сделать вложенные с MongoTemplate, я получил Он использует бизнес-логи c if board exist find by column with board id, if column exist find by card with card id.
Я не знаю, насколько это хорошо.
KanbanBoard, KanbanColumn, такой же, как сущность Board и Column, те же свойства.
public BoardResponse findBoardById(ObjectId id, UserPrincipal currentUser) {
KanbanBoard board = this.kanbanBoardRepository.findById(id).orElse(null);//find board by id
//find all columns by board ID within the entity @document KanbanColumn
List<KanbanColumn> columns = this.kanbanColumnRepository.findAllByIdBoard(board.getId());
//return list of ColumnResponse DTO
List<ColumnResponse> columnResponse = columns.stream().map(column -> {
return new ColumnResponse(column);
}).collect(Collectors.toList());
//List Column map
List<ColumnResponse> columnMap = new ArrayList<>();
for (ColumnResponse column : columnResponse) {
List<CardResponse> cards = this.kanbanCardRepository.findAllByIdColumn(column.getId()).stream()
.map(card -> new CardResponse(card)).collect(Collectors.toList());
column.setCards(cards); //column set list of cards
columnMap.add(column);//add columns with list of cards inside list of column map
}
//return board with list of column map, with list of cards
return new BoardResponse(board, columnMap);
}
Результат
{
"id": "5e717d6d6e7cbf226074c3fe",
"name": null,
"createdBy": "admin",
"createdDate": 1584495981290,
"updatedBy": "admin",
"updatedDate": 1584495981290,
"columns": [
{
"id": "5e72bfa6cc3ff9000ae93c92",
"name": null,
"createdBy": "admin",
"createdDate": 1584578470269,
"updatedBy": "admin",
"updatedDate": 1584578470269,
"idBoard": null,
"cards": [
{
"id": "5e72de720715f131878b4ed2",
"name": "esse é o card",
"createdBy": "admin",
"createdDate": 1584586354958,
"updatedBy": "admin",
"updatedDate": 1584586354958,
"idColumn": "5e72bfa6cc3ff9000ae93c92"
}
]
},
{
"id": "5e72bfefcc3ff9000ae93c95",
"name": "coluna criada com sucesso.",
"createdBy": "admin",
"createdDate": 1584578543201,
"updatedBy": "admin",
"updatedDate": 1584578543201,
"idBoard": null,
"cards": [
{
"id": "5e72de550715f131878b4ed0",
"name": "esse é o card",
"createdBy": "admin",
"createdDate": 1584586325485,
"updatedBy": "admin",
"updatedDate": 1584586325485,
"idColumn": "5e72bfefcc3ff9000ae93c95"
},
{
"id": "5e72de630715f131878b4ed1",
"name": "esse é o card",
"createdBy": "admin",
"createdDate": 1584586339140,
"updatedBy": "admin",
"updatedDate": 1584586339140,
"idColumn": "5e72bfefcc3ff9000ae93c95"
}
]
}
]
}