Skip to content

Commit

Permalink
feat: Update backend to filter challenges by difficulty (#1030)
Browse files Browse the repository at this point in the history
* Add Challenge.difficulty to OA description

* Fix EnumConverterConfiguration

* Minor update

* Add property and filter for incentive

* Comment out incentives because it's an array

* Discard incentive for now

* Scaffold custom challenge repository for advanced search

* Add Pageable support to custom repository

* Implement status and difficulty filter with querydsl

* Connect new querydsl code

* Cleanup

* Minor update to OA description
  • Loading branch information
tschaffter authored Dec 21, 2022
1 parent 70d0b7c commit cc1bb06
Show file tree
Hide file tree
Showing 36 changed files with 436 additions and 105 deletions.
3 changes: 0 additions & 3 deletions apps/challenge-service/.openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,3 @@ README.md
**/application.properties
**/model/dto/*AllOf*.java
**/OpenApiGeneratorApplication.java

# The template of this file has a bug: the Dto suffix is not included
**/EnumConverterConfiguration.java
1 change: 1 addition & 0 deletions apps/challenge-service/.openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ src/main/java/org/sagebionetworks/challenge/configuration/HomeController.java
src/main/java/org/sagebionetworks/challenge/configuration/SpringDocConfiguration.java
src/main/java/org/sagebionetworks/challenge/model/dto/BasicErrorDto.java
src/main/java/org/sagebionetworks/challenge/model/dto/ChallengeCreateRequestDto.java
src/main/java/org/sagebionetworks/challenge/model/dto/ChallengeDifficultyDto.java
src/main/java/org/sagebionetworks/challenge/model/dto/ChallengeDto.java
src/main/java/org/sagebionetworks/challenge/model/dto/ChallengeStatusDto.java
src/main/java/org/sagebionetworks/challenge/model/dto/ChallengesPageDto.java
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.validation.Valid;
import javax.validation.constraints.*;
import org.sagebionetworks.challenge.model.dto.BasicErrorDto;
import org.sagebionetworks.challenge.model.dto.ChallengeDifficultyDto;
import org.sagebionetworks.challenge.model.dto.ChallengeStatusDto;
import org.sagebionetworks.challenge.model.dto.ChallengesPageDto;
import org.springframework.http.ResponseEntity;
Expand All @@ -35,7 +36,9 @@ default ChallengeApiDelegate getDelegate() {
*
* @param pageNumber The page number (optional, default to 0)
* @param pageSize The number of items in a single page (optional, default to 100)
* @param status Array of challenge status used to filter the results. (optional)
* @param status An array of challenge status used to filter the results. (optional)
* @param difficulty An array of challenge difficulty levels used to filter the results.
* (optional)
* @return Success (status code 200) or Invalid request (status code 400) or The request cannot be
* fulfilled due to an unexpected server error (status code 500)
*/
Expand Down Expand Up @@ -95,10 +98,16 @@ default ResponseEntity<ChallengesPageDto> listChallenges(
Integer pageSize,
@Parameter(
name = "status",
description = "Array of challenge status used to filter the results.")
description = "An array of challenge status used to filter the results.")
@Valid
@RequestParam(value = "status", required = false)
List<ChallengeStatusDto> status) {
return getDelegate().listChallenges(pageNumber, pageSize, status);
List<ChallengeStatusDto> status,
@Parameter(
name = "difficulty",
description = "An array of challenge difficulty levels used to filter the results.")
@Valid
@RequestParam(value = "difficulty", required = false)
List<ChallengeDifficultyDto> difficulty) {
return getDelegate().listChallenges(pageNumber, pageSize, status, difficulty);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import java.util.Optional;
import javax.annotation.Generated;
import org.sagebionetworks.challenge.model.dto.ChallengeDifficultyDto;
import org.sagebionetworks.challenge.model.dto.ChallengeStatusDto;
import org.sagebionetworks.challenge.model.dto.ChallengesPageDto;
import org.springframework.http.HttpStatus;
Expand All @@ -26,13 +27,18 @@ default Optional<NativeWebRequest> getRequest() {
*
* @param pageNumber The page number (optional, default to 0)
* @param pageSize The number of items in a single page (optional, default to 100)
* @param status Array of challenge status used to filter the results. (optional)
* @param status An array of challenge status used to filter the results. (optional)
* @param difficulty An array of challenge difficulty levels used to filter the results.
* (optional)
* @return Success (status code 200) or Invalid request (status code 400) or The request cannot be
* fulfilled due to an unexpected server error (status code 500)
* @see ChallengeApi#listChallenges
*/
default ResponseEntity<ChallengesPageDto> listChallenges(
Integer pageNumber, Integer pageSize, List<ChallengeStatusDto> status) {
Integer pageNumber,
Integer pageSize,
List<ChallengeStatusDto> status,
List<ChallengeDifficultyDto> difficulty) {
getRequest()
.ifPresent(
request -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sagebionetworks.challenge.api;

import java.util.List;
import org.sagebionetworks.challenge.model.dto.ChallengeDifficultyDto;
import org.sagebionetworks.challenge.model.dto.ChallengeStatusDto;
import org.sagebionetworks.challenge.model.dto.ChallengesPageDto;
import org.sagebionetworks.challenge.service.ChallengeService;
Expand All @@ -15,8 +16,12 @@ public class ChallengeApiDelegateImpl implements ChallengeApiDelegate {

@Override
public ResponseEntity<ChallengesPageDto> listChallenges(
Integer pageNumber, Integer pageSize, List<ChallengeStatusDto> status) {
return ResponseEntity.ok(challengeService.listChallenges(pageNumber, pageSize, status));
Integer pageNumber,
Integer pageSize,
List<ChallengeStatusDto> status,
List<ChallengeDifficultyDto> difficulty) {
return ResponseEntity.ok(
challengeService.listChallenges(pageNumber, pageSize, status, difficulty));
}

// @Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sagebionetworks.challenge.configuration;

import org.sagebionetworks.challenge.model.dto.ChallengeDifficultyDto;
import org.sagebionetworks.challenge.model.dto.ChallengeStatusDto;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -8,6 +9,16 @@
@Configuration
public class EnumConverterConfiguration {

@Bean
Converter<String, ChallengeDifficultyDto> challengeDifficultyConverter() {
return new Converter<String, ChallengeDifficultyDto>() {
@Override
public ChallengeDifficultyDto convert(String source) {
return ChallengeDifficultyDto.fromValue(source);
}
};
}

@Bean
Converter<String, ChallengeStatusDto> challengeStatusConverter() {
return new Converter<String, ChallengeStatusDto>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class ChallengeCreateRequestDto {
@JsonProperty("status")
private ChallengeStatusDto status;

@JsonProperty("difficulty")
private ChallengeDifficultyDto difficulty;

public ChallengeCreateRequestDto name(String name) {
this.name = name;
return this;
Expand Down Expand Up @@ -64,6 +67,26 @@ public void setStatus(ChallengeStatusDto status) {
this.status = status;
}

public ChallengeCreateRequestDto difficulty(ChallengeDifficultyDto difficulty) {
this.difficulty = difficulty;
return this;
}

/**
* Get difficulty
*
* @return difficulty
*/
@Valid
@Schema(name = "difficulty", required = false)
public ChallengeDifficultyDto getDifficulty() {
return difficulty;
}

public void setDifficulty(ChallengeDifficultyDto difficulty) {
this.difficulty = difficulty;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -74,12 +97,13 @@ public boolean equals(Object o) {
}
ChallengeCreateRequestDto challengeCreateRequest = (ChallengeCreateRequestDto) o;
return Objects.equals(this.name, challengeCreateRequest.name)
&& Objects.equals(this.status, challengeCreateRequest.status);
&& Objects.equals(this.status, challengeCreateRequest.status)
&& Objects.equals(this.difficulty, challengeCreateRequest.difficulty);
}

@Override
public int hashCode() {
return Objects.hash(name, status);
return Objects.hash(name, status, difficulty);
}

@Override
Expand All @@ -88,6 +112,7 @@ public String toString() {
sb.append("class ChallengeCreateRequestDto {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" status: ").append(toIndentedString(status)).append("\n");
sb.append(" difficulty: ").append(toIndentedString(difficulty)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.sagebionetworks.challenge.model.dto;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import java.util.*;
import javax.annotation.Generated;
import javax.validation.constraints.*;

/** The difficulty level of a challenge. */
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen")
public enum ChallengeDifficultyDto {
GOOD_FOR_BEGINNERS("good_for_beginners"),

INTERMEDIATE("intermediate"),

ADVANCED("advanced");

private String value;

ChallengeDifficultyDto(String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}

@Override
public String toString() {
return String.valueOf(value);
}

@JsonCreator
public static ChallengeDifficultyDto fromValue(String value) {
for (ChallengeDifficultyDto b : ChallengeDifficultyDto.values()) {
if (b.value.equals(value)) {
return b;
}
}
throw new IllegalArgumentException("Unexpected value '" + value + "'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ChallengeDto {
@JsonProperty("status")
private ChallengeStatusDto status;

@JsonProperty("difficulty")
private ChallengeDifficultyDto difficulty;

@JsonProperty("id")
private Long id;

Expand Down Expand Up @@ -77,6 +80,26 @@ public void setStatus(ChallengeStatusDto status) {
this.status = status;
}

public ChallengeDto difficulty(ChallengeDifficultyDto difficulty) {
this.difficulty = difficulty;
return this;
}

/**
* Get difficulty
*
* @return difficulty
*/
@Valid
@Schema(name = "difficulty", required = false)
public ChallengeDifficultyDto getDifficulty() {
return difficulty;
}

public void setDifficulty(ChallengeDifficultyDto difficulty) {
this.difficulty = difficulty;
}

public ChallengeDto id(Long id) {
this.id = id;
return this;
Expand Down Expand Up @@ -154,14 +177,15 @@ public boolean equals(Object o) {
ChallengeDto challenge = (ChallengeDto) o;
return Objects.equals(this.name, challenge.name)
&& Objects.equals(this.status, challenge.status)
&& Objects.equals(this.difficulty, challenge.difficulty)
&& Objects.equals(this.id, challenge.id)
&& Objects.equals(this.createdAt, challenge.createdAt)
&& Objects.equals(this.updatedAt, challenge.updatedAt);
}

@Override
public int hashCode() {
return Objects.hash(name, status, id, createdAt, updatedAt);
return Objects.hash(name, status, difficulty, id, createdAt, updatedAt);
}

@Override
Expand All @@ -170,6 +194,7 @@ public String toString() {
sb.append("class ChallengeDto {\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append(" status: ").append(toIndentedString(status)).append("\n");
sb.append(" difficulty: ").append(toIndentedString(difficulty)).append("\n");
sb.append(" id: ").append(toIndentedString(id)).append("\n");
sb.append(" createdAt: ").append(toIndentedString(createdAt)).append("\n");
sb.append(" updatedAt: ").append(toIndentedString(updatedAt)).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public class ChallengeEntity {
@Column(nullable = false)
private String status;

@Column(nullable = false)
private String difficulty;

// @Column(nullable = false)
// private String email;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sagebionetworks.challenge.model.mapper;

import org.sagebionetworks.challenge.model.dto.ChallengeDifficultyDto;
import org.sagebionetworks.challenge.model.dto.ChallengeDto;
import org.sagebionetworks.challenge.model.dto.ChallengeStatusDto;
import org.sagebionetworks.challenge.model.entity.ChallengeEntity;
Expand All @@ -22,6 +23,7 @@ public ChallengeDto convertToDto(ChallengeEntity entity, Object... args) {
if (entity != null) {
BeanUtils.copyProperties(entity, user);
user.setStatus(ChallengeStatusDto.fromValue(entity.getStatus()));
user.setDifficulty(ChallengeDifficultyDto.fromValue(entity.getDifficulty()));
}
return user;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.sagebionetworks.challenge.model.repository;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ChallengeFilter {
private List<String> status;
private List<String> difficulty;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@
import org.springframework.data.querydsl.QuerydslPredicateExecutor;

public interface ChallengeRepository
extends JpaRepository<ChallengeEntity, Long>, QuerydslPredicateExecutor<ChallengeEntity> {

// Optional<ChallengeEntity> findByLogin(String login);
}
extends JpaRepository<ChallengeEntity, Long>, QuerydslPredicateExecutor<ChallengeEntity> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.sagebionetworks.challenge.model.repository;

import org.sagebionetworks.challenge.model.entity.ChallengeEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

public interface ChallengeRepositoryCustom {

Page<ChallengeEntity> findAll(Pageable pageable, ChallengeFilter filter);
}
Loading

0 comments on commit cc1bb06

Please sign in to comment.