Skip to content

Commit

Permalink
[backend/frontend] Fix editing challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Nov 18, 2024
1 parent d20c8e3 commit a3f475e
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package io.openbas.rest.challenge;

import static io.openbas.config.OpenBASAnonymous.ANONYMOUS;
import static io.openbas.database.model.User.ROLE_ADMIN;
import static io.openbas.helper.StreamHelper.fromIterable;
import static io.openbas.helper.StreamHelper.iterableToSet;

import io.openbas.database.model.Challenge;
import io.openbas.database.model.ChallengeFlag;
import io.openbas.database.model.ChallengeFlag.FLAG_TYPE;
import io.openbas.database.model.Exercise;
import io.openbas.database.model.User;
import io.openbas.database.repository.*;
import io.openbas.rest.challenge.form.ChallengeCreateInput;
import io.openbas.rest.challenge.form.ChallengeInput;
import io.openbas.rest.challenge.form.ChallengeTryInput;
import io.openbas.rest.challenge.form.ChallengeUpdateInput;
import io.openbas.rest.challenge.response.ChallengeInformation;
import io.openbas.rest.challenge.response.ChallengeResult;
import io.openbas.rest.challenge.response.ChallengesReader;
Expand All @@ -22,14 +16,20 @@
import io.openbas.service.ChallengeService;
import jakarta.transaction.Transactional;
import jakarta.validation.Valid;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.util.List;
import java.util.Optional;

import static io.openbas.config.OpenBASAnonymous.ANONYMOUS;
import static io.openbas.database.model.User.ROLE_ADMIN;
import static io.openbas.helper.StreamHelper.fromIterable;
import static io.openbas.helper.StreamHelper.iterableToSet;

@RestController
@RequiredArgsConstructor
public class ChallengeApi extends RestBehavior {
Expand All @@ -53,7 +53,7 @@ public Iterable<Challenge> challenges() {
@PutMapping("/api/challenges/{challengeId}")
@Transactional(rollbackOn = Exception.class)
public Challenge updateChallenge(
@PathVariable String challengeId, @Valid @RequestBody ChallengeUpdateInput input) {
@PathVariable String challengeId, @Valid @RequestBody ChallengeInput input) {
Challenge challenge =
challengeRepository.findById(challengeId).orElseThrow(ElementNotFoundException::new);
challenge.setTags(iterableToSet(tagRepository.findAllById(input.getTagIds())));
Expand Down Expand Up @@ -82,7 +82,7 @@ public Challenge updateChallenge(
@PreAuthorize("isPlanner()")
@PostMapping("/api/challenges")
@Transactional(rollbackOn = Exception.class)
public Challenge createChallenge(@Valid @RequestBody ChallengeCreateInput input) {
public Challenge createChallenge(@Valid @RequestBody ChallengeInput input) {
Challenge challenge = new Challenge();
challenge.setUpdateAttributes(input);
challenge.setTags(iterableToSet(tagRepository.findAllById(input.getTagIds())));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.openbas.rest.challenge.form;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import static io.openbas.config.AppConfig.EMPTY_MESSAGE;
import static io.openbas.config.AppConfig.MANDATORY_MESSAGE;

@Data
public class ChallengeInput {

@NotBlank(message = MANDATORY_MESSAGE)
@JsonProperty("challenge_name")
private String name;

@JsonProperty("challenge_category")
private String category;

@JsonProperty("challenge_content")
private String content;

@JsonProperty("challenge_score")
private Double score;

@JsonProperty("challenge_max_attempts")
private Integer maxAttempts;

@JsonProperty("challenge_tags")
private List<String> tagIds = new ArrayList<>();

@JsonProperty("challenge_documents")
private List<String> documentIds = new ArrayList<>();

@NotEmpty(message = EMPTY_MESSAGE)
@JsonProperty("challenge_flags")
private List<FlagInput> flags = new ArrayList<>();

}

This file was deleted.

2 changes: 1 addition & 1 deletion openbas-front/src/actions/Challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const fetchChallenge = challengeId => (dispatch) => {

export const updateChallenge = (challengeId, data) => (dispatch) => {
const uri = `/api/challenges/${challengeId}`;
return putReferential(challengeId, uri, data)(dispatch);
return putReferential(challenge, uri, data)(dispatch);
};

export const addChallenge = data => dispatch => postReferential(challenge, '/api/challenges', data)(dispatch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const ChallengeExpectation: FunctionComponent<Props> = ({
<ExpectationLine
expectation={expectation}
info={challenge.challenge_category}
title={challenge.challenge_name ?? ''}
title={challenge.challenge_name}
icon={<EmojiEventsOutlined fontSize="small" />}
/>
);
Expand Down
Loading

0 comments on commit a3f475e

Please sign in to comment.