Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend/frontend] Fix editing challenge #1886

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
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 Down Expand Up @@ -53,11 +52,11 @@
@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())));
challenge.setDocuments(fromIterable(documentRepository.findAllById(input.getDocumentIds())));
challenge.setTags(iterableToSet(tagRepository.findAllById(input.tagIds())));
challenge.setDocuments(fromIterable(documentRepository.findAllById(input.documentIds())));

Check warning on line 59 in openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java#L58-L59

Added lines #L58 - L59 were not covered by tests
challenge.setUpdateAttributes(input);
challenge.setUpdatedAt(Instant.now());
// Clear all flags
Expand All @@ -66,7 +65,7 @@
challengeFlags.clear();
// Add new ones
input
.getFlags()
.flags()

Check warning on line 68 in openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java#L68

Added line #L68 was not covered by tests
.forEach(
flagInput -> {
ChallengeFlag challengeFlag = new ChallengeFlag();
Expand All @@ -82,13 +81,13 @@
@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())));
challenge.setDocuments(fromIterable(documentRepository.findAllById(input.getDocumentIds())));
challenge.setTags(iterableToSet(tagRepository.findAllById(input.tagIds())));
challenge.setDocuments(fromIterable(documentRepository.findAllById(input.documentIds())));

Check warning on line 88 in openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java#L87-L88

Added lines #L87 - L88 were not covered by tests
List<ChallengeFlag> challengeFlags =
input.getFlags().stream()
input.flags().stream()

Check warning on line 90 in openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/ChallengeApi.java#L90

Added line #L90 was not covered by tests
.map(
flagInput -> {
ChallengeFlag challengeFlag = new ChallengeFlag();
Expand Down

This file was deleted.

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

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

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

public record ChallengeInput(
@NotBlank(message = MANDATORY_MESSAGE) @JsonProperty("challenge_name") String name,
@JsonProperty("challenge_category") String category,
@JsonProperty("challenge_content") String content,
@JsonProperty("challenge_score") Double score,
@JsonProperty("challenge_max_attempts") Integer maxAttempts,
@JsonProperty("challenge_tags") List<String> tagIds,
@JsonProperty("challenge_documents") List<String> documentIds,
@NotEmpty(message = EMPTY_MESSAGE) @JsonProperty("challenge_flags") List<FlagInput> flags) {

public ChallengeInput {

Check warning on line 21 in openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java#L21

Added line #L21 was not covered by tests
if (tagIds == null) {
tagIds = List.of();

Check warning on line 23 in openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java#L23

Added line #L23 was not covered by tests
}
if (documentIds == null) {
documentIds = List.of();

Check warning on line 26 in openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java#L26

Added line #L26 was not covered by tests
}
if (flags == null) {
flags = List.of();

Check warning on line 29 in openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java#L29

Added line #L29 was not covered by tests
}
}

Check warning on line 31 in openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java

View check run for this annotation

Codecov / codecov/patch

openbas-api/src/main/java/io/openbas/rest/challenge/form/ChallengeInput.java#L31

Added line #L31 was not covered by tests
}

This file was deleted.

Loading