Skip to content

Commit

Permalink
end task
Browse files Browse the repository at this point in the history
  • Loading branch information
Rseuret committed Oct 10, 2023
1 parent f51ea6b commit ea6adfb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 39 deletions.
16 changes: 11 additions & 5 deletions src/main/java/fr/cgi/magneto/controller/SectionController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import fr.cgi.magneto.model.boards.BoardPayload;
import fr.cgi.magneto.model.cards.Card;
import fr.cgi.magneto.security.DuplicateCardRight;
import fr.cgi.magneto.security.ManageBoardRight;
import fr.cgi.magneto.security.ViewRight;
import fr.cgi.magneto.security.WriteBoardRight;
import fr.cgi.magneto.service.BoardService;
Expand All @@ -17,16 +18,19 @@
import fr.wseduc.rs.*;
import fr.wseduc.security.ActionType;
import fr.wseduc.security.SecuredAction;
import fr.wseduc.webutils.http.Binding;
import fr.wseduc.webutils.request.RequestUtils;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.entcore.common.controller.ControllerHelper;
import org.entcore.common.events.EventStore;
import org.entcore.common.events.EventStoreFactory;
import org.entcore.common.http.filter.ResourceFilter;
import org.entcore.common.user.UserInfos;
import org.entcore.common.user.UserUtils;

import java.util.Collections;
Expand All @@ -50,20 +54,22 @@ public SectionController(ServiceFactory serviceFactory) {
this.eventStore = EventStoreFactory.getFactory().getEventStore(Magneto.class.getSimpleName());
}

@Get("/sections/:boardId")
@Get("/sections/:id")
@ApiDoc("Get sections by board id")
@ResourceFilter(ViewRight.class)
@SecuredAction(value = "", type = ActionType.RESOURCE)
public void getSectionsByBoardId(HttpServerRequest request) {
String boardId = request.getParam(Field.BOARDID);
boardService.getBoards(Collections.singletonList(boardId))
String boardId = request.getParam(Field.ID);
UserUtils.getUserInfos(eb, request, user -> new ManageBoardRight().authorize(request, null, user, readOnly ->
boardService.getBoards(Collections.singletonList(boardId))
.compose(boards -> {
if (boards.isEmpty()) {
String message = String.format("[Magneto@%s::getSectionsByBoardId] Failed to get boards with board id : %s",
this.getClass().getSimpleName(), boardId);
return Future.failedFuture(message);
} else {
return sectionService.getSectionsByBoard(boards.get(0));

return sectionService.getSectionsByBoard(boards.get(0), !readOnly);
}
})
.onFailure(err -> {
Expand All @@ -79,7 +85,7 @@ public void getSectionsByBoardId(HttpServerRequest request) {
.collect(Collectors.toList()));
renderJson(request, new JsonObject()
.put(Field.ALL, sectionsResult));
});
})));
}

@Post("/section")
Expand Down
53 changes: 29 additions & 24 deletions src/main/java/fr/cgi/magneto/model/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ public class Section implements Model {
private String title;
private List<String> cardIds;
private String boardId;
private boolean displayed;
private Boolean displayed;

@SuppressWarnings("unchecked")
public Section(JsonObject section) {
this._id = section.getString(Field._ID, null);
this.title = section.getString(Field.TITLE);
this.cardIds = section.getJsonArray(Field.CARDIDS, new JsonArray()).getList();
this.boardId = section.getString(Field.BOARDID);
this.displayed = section.getBoolean(Field.DISPLAYED);
if (section.containsKey(Field.DISPLAYED))
this.displayed = section.getBoolean(Field.DISPLAYED);
}

public Section() {

}
Expand All @@ -30,52 +32,55 @@ public String getId() {
return _id;
}

public Section setId(String id) {
this._id = id;
return this;
}

public String getTitle() {
return title;
}

public Section setTitle(String title) {
this.title = title;
return this;
}

public List<String> getCardIds() {
return cardIds;
}

public Section setCardIds(List<String> cardIds) {
this.cardIds = cardIds;
return this;
}

public Section removeCardIds(List<String> cardIds) {
this.cardIds = this.cardIds.stream()
.filter(s -> !cardIds.contains(s))
.collect(Collectors.toList());
return this;
}


public String getBoardId() {
return boardId;
}

public Section addCardIds(List<String> cardIds) {
this.cardIds.addAll(0, cardIds);
return this;
}

public Section setId(String id) {
this._id = id;
return this;
}

public Section setTitle(String title) {
this.title = title;
return this;
}

public Section setBoardId(String boardId) {
this.boardId = boardId;
return this;
}

public Section setCardIds(List<String> cardIds) {
this.cardIds = cardIds;
public Section addCardIds(List<String> cardIds) {
this.cardIds.addAll(0, cardIds);
return this;
}

public boolean getDisplayed() {
return displayed;
if (this.displayed != null)
return displayed;
else
return true;
}

public void setDisplayed(boolean displayed) {
Expand All @@ -88,8 +93,9 @@ public JsonObject toJson() {
.put(Field._ID, this.getId())
.put(Field.TITLE, this.getTitle())
.put(Field.CARDIDS, this.getCardIds())
.put(Field.BOARDID, this.getBoardId())
.put(Field.DISPLAYED, this.getDisplayed());
.put(Field.BOARDID, this.getBoardId());
if (this.displayed != null)
json.put(Field.DISPLAYED, this.getDisplayed());
return json;
}

Expand All @@ -99,5 +105,4 @@ public Section model(JsonObject section) {
}



}
4 changes: 2 additions & 2 deletions src/main/java/fr/cgi/magneto/model/SectionPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class SectionPayload implements Model {
private String title;
private List<String> cardIds;
private String boardId;
private boolean displayed;
private Boolean displayed;

@SuppressWarnings("unchecked")
public SectionPayload(JsonObject section) {
Expand Down Expand Up @@ -115,7 +115,7 @@ public JsonObject toJson() {
json.put(Field.CARDIDS, new JsonArray());
}
if (this.displayed() != null ) {
json.put(Field.DISPLAYED, new JsonArray());
json.put(Field.DISPLAYED, this.displayed());
}

return json;
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/fr/cgi/magneto/service/SectionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ public interface SectionService {
*/
Future<List<Section>> getSectionsByBoardId(String boardId);

Future<List<Section>> getSectionsByBoard(Board board);
/**
*
* @param board board of the sections
* @param isReadOnly
* @return
*/
Future<List<Section>> getSectionsByBoard(Board board, boolean isReadOnly);

/**
* Create a section
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import fr.cgi.magneto.model.boards.Board;
import fr.cgi.magneto.model.boards.BoardPayload;
import fr.cgi.magneto.model.cards.Card;
import fr.cgi.magneto.service.BoardService;
import fr.cgi.magneto.service.CardService;
import fr.cgi.magneto.service.SectionService;
import fr.cgi.magneto.service.ServiceFactory;
import fr.wseduc.mongodb.MongoDb;
Expand Down Expand Up @@ -80,7 +78,7 @@ public Future<List<Section>> getSectionsByBoardId(String boardId) {
}

@Override
public Future<List<Section>> getSectionsByBoard(Board board) {
public Future<List<Section>> getSectionsByBoard(Board board, boolean isReadOnly) {
Promise<List<Section>> promise = Promise.promise();
JsonObject query = this.getAllSectionsByBoardQuery(board);
mongoDb.command(query.toString(), MongoDbResult.validResultHandler(either -> {
Expand All @@ -92,7 +90,12 @@ public Future<List<Section>> getSectionsByBoard(Board board) {
JsonArray result = either.right().getValue()
.getJsonObject(Field.CURSOR, new JsonObject())
.getJsonArray(Field.FIRSTBATCH, new JsonArray());
promise.complete(ModelHelper.toList(result, Section.class));
if (isReadOnly) {
promise.complete(((List<Section>)ModelHelper.toList(result, Section.class)).stream()
.filter(Section::getDisplayed).collect(Collectors.toList()));
}
else
promise.complete(ModelHelper.toList(result, Section.class));
}
}));

Expand All @@ -115,7 +118,9 @@ private JsonObject getAllSectionsByBoardQuery(Board board) {
.put(Field._ID, 1)
.put(Field.TITLE, 1)
.put(Field.BOARDID, 1)
.put(Field.CARDIDS, 1));
.put(Field.CARDIDS, 1)
.put(Field.DISPLAYED, 1))
;
return query.getAggregate();
}

Expand All @@ -140,7 +145,6 @@ public Future<JsonObject> update(SectionPayload section) {
Promise<JsonObject> promise = Promise.promise();
JsonObject sectionUpdate = new JsonObject()
.put(Field._ID, section.getId());
log.info(section.toJson());
JsonObject update = new JsonObject().put(Mongo.SET, section.toJson());
mongoDb.update(this.collection, sectionUpdate, update, MongoDbResult.validResultHandler(results -> {
if (results.isLeft()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div id="section-[[vm.section.id]]" ng-show="vm.board.myRights.manager !== undefined || vm.section.displayed " >
<div id="section-[[vm.section.id]]" >
<div class="sections-listDirective-content-container" tabindex="0">
<i class="magneto-hide" aria-hidden="true" ng-show="!vm.section.displayed"></i>
<input ng-readonly="vm.board.myRights.publish === undefined" type="text" class="sections-listDirective-content-title input"
Expand Down

0 comments on commit ea6adfb

Please sign in to comment.