Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Mariotti committed Jan 9, 2025
1 parent 246e701 commit c3550d0
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,19 @@ public void getBoardResourceIds(HttpServerRequest request) {
});
}

@Post("/boards/imageUrl")
@ApiDoc("Get imageUrl of boards")
@ResourceFilter(ViewRight.class)
@SecuredAction(value = "", type = ActionType.RESOURCE)
public void getAllBoardImages(HttpServerRequest request) {
RequestUtils.bodyToJson(request, pathPrefix + "boardsIds", boards ->
UserUtils.getUserInfos(eb, request, user -> {
List<String> boardIds = boards.getJsonArray(Field.BOARDIDS).getList();
boardService.getAllBoardImages(boardIds)
.onFailure(err -> renderError(request))
.onSuccess(result -> renderJson(request, result));
}));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import fr.cgi.magneto.model.share.SharedElem;
import fr.cgi.magneto.model.statistics.StatisticsPayload;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.entcore.common.user.UserInfos;

Expand Down Expand Up @@ -151,4 +152,12 @@ Future<JsonObject> getAllBoards(UserInfos user, Integer page, String searchText,
Future<List<String>> getOwnedBoardsIds(List<String> boardsIds,String userId );

Future<List<Board>> getBoardsWithNbCards(List<String> resultIds);

/**
* Get all images of boards
*
* @param boardIds List of board identifiers
* @return Future {@link Future <JsonArray>} containing list of board identifiers and their image
*/
Future<JsonArray> getAllBoardImages(List<String> boardIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -879,4 +879,42 @@ public Future<List<Board>> getBoardsWithNbCards(List<String> resultIds) {

return promise.future();
}

@Override
public Future<JsonArray> getAllBoardImages(List<String> boardIds) {
Promise<JsonArray> promise = Promise.promise();

JsonObject query = new MongoQuery(this.collection)
.match(new JsonObject()
.put(Field._ID, new JsonObject().put(Mongo.IN, new JsonArray(boardIds))))
.project(new JsonObject()
.put(Field._ID, 1)
.put(Field.IMAGEURL, 1))
.getAggregate();

mongoDb.command(query.toString(), MongoDbResult.validResultHandler(either -> {
if (either.isLeft()) {
log.error("[Magneto@%s::getAllBoardImages] Failed to get board images",
this.getClass().getSimpleName(), either.left().getValue());
promise.fail(either.left().getValue());
} else {
JsonArray result = either.right().getValue()
.getJsonObject(Field.CURSOR, new JsonObject())
.getJsonArray(Field.FIRSTBATCH, new JsonArray());

JsonArray boardImages = new JsonArray();
result.stream()
.filter(JsonObject.class::isInstance)
.map(JsonObject.class::cast)
.forEach(board -> {
boardImages.add(new JsonObject()
.put(Field._ID, board.getString(Field._ID))
.put(Field.IMAGEURL, board.getString(Field.IMAGEURL)));
});
promise.complete(boardImages);
}
}));

return promise.future();
}
}
18 changes: 18 additions & 0 deletions backend/src/main/resources/jsonschema/boardsIds.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"additionalProperties": false,
"properties": {
"boardIds": {
"type": "array",
"items": {
"anyOf": [
{
"type": "string"
}
]
}
}
},
"required": ["boardIds"]
}
23 changes: 23 additions & 0 deletions frontend/src/providers/BoardProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { useEntcoreBehaviours } from "~/hooks/useEntcoreBehaviours";
import { Board, IBoardItemResponse } from "~/models/board.model";
import { Card } from "~/models/card.model";
import { useGetBoardDataQuery } from "~/services/api/boardData.service";
import { useGetAllBoardImagesQuery } from "~/services/api/boards.service";
import { useGetDocumentsQuery } from "~/services/api/workspace.service";

const BoardContext = createContext<BoardContextType | null>(null);
Expand Down Expand Up @@ -82,6 +83,28 @@ export const BoardProvider: FC<BoardProviderProps> = ({ children }) => {
: new Board();
}, [boardData]);

const boardResourceIds = useMemo(() => {
const cards = board?.isLayoutFree()
? board.cards
: board.sections.flatMap((section) => section.cards || []);
if (!cards) return [];
return cards
.filter(
(card) => card.resourceType === "board" && card.resourceUrl !== null,
)
.map((card) => card.resourceUrl);
}, [board]);

const { data: boardImages } = useGetAllBoardImagesQuery(boardResourceIds, {
skip: !boardResourceIds?.length,
});

console.log(boardImages);

useEffect(() => {
console.log("cc", boardImages);
}, [boardImages]);

const documents = useMemo(() => {
return documentsData ?? [];
}, [documentsData]);
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/services/api/boards.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ export const boardsApi = emptySplitApi.injectEndpoints({
getAllBoardsEditable: builder.query<Boards, void>({
query: () => `boards/editable`,
}),
getAllBoardImages: builder.query({
query: (boardIds: string[]) => ({
url: "boards/imageUrl",
method: "POST",
body: { boardIds: boardIds },
}),
providesTags: ["Boards"],
}),
}),
});

Expand All @@ -142,4 +150,5 @@ export const {
useRestorePreDeleteBoardsMutation,
useGetUrlQuery,
useGetAllBoardsEditableQuery,
useGetAllBoardImagesQuery,
} = boardsApi;

0 comments on commit c3550d0

Please sign in to comment.