[Added] Tests and some more functionality to the Services
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package com.ninjaws.quiz.models;
|
||||
|
||||
import com.ninjaws.quiz.models.QuizSettings.Difficulty;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import jakarta.validation.constraints.Max;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
public class QuizSettings {
|
||||
|
||||
public enum Type {
|
||||
ANY, BOOLEAN, MULTIPLE;
|
||||
|
||||
public String getApiValue() {
|
||||
return this.name().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public enum Difficulty {
|
||||
ANY, EASY, MEDIUM, HARD;
|
||||
|
||||
public String getApiValue() {
|
||||
return this.name().toLowerCase();
|
||||
}
|
||||
}
|
||||
|
||||
public enum Category {
|
||||
ANY, GEN_KNOWLEDGE, ENT_BOOKS, ENT_FILM, ENT_MUSIC, ENT_MUSICAL_THEATRE, ENT_TELEVISION,
|
||||
ENT_VIDEOGAMES, ENT_BOARDGAMES, SCIENCE_NATURE, SCIENCE_COMPUTERS, SCIENCE_MATH, MYTHOLOGY, SPORTS, GEOGRAPHY, HISTORY, POLITICS, ART, CELEBRITIES,
|
||||
ANIMALS, VEHICLES, ENT_COMICS, SCIENCE_GADGETS, ENT_ANIME_MANGA, ENT_CARTOON_ANIMATIONS;
|
||||
|
||||
/**
|
||||
* For some reason the external API starts with a value of 9 for the General Knowledge category, so we have to adjust
|
||||
* ANY also gets adjusted, but it doesn't matter since that one doesn't get used as parameter anyway
|
||||
* @return The corrected value
|
||||
*/
|
||||
public int getApiValue() {
|
||||
return 8 + this.ordinal();
|
||||
}
|
||||
}
|
||||
|
||||
@Min(1)
|
||||
@Max(50)
|
||||
private int amount = 10;
|
||||
|
||||
private Type type = Type.ANY;
|
||||
|
||||
private Difficulty difficulty = Difficulty.ANY;
|
||||
|
||||
private Category category = Category.ANY;
|
||||
|
||||
|
||||
/**
|
||||
* Ensures the value is between 1 and 50
|
||||
* @Valid doesn't seem to be enforcing it, so this ensures the value is legal
|
||||
*/
|
||||
public void setAmount(@Valid int amount) {
|
||||
this.amount = Math.max(1, Math.min(amount, 50));
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the class to a format that can be provided as arguments for the api request
|
||||
* @return The resulting request arguments as String
|
||||
*/
|
||||
public String toApiQuery() {
|
||||
StringBuilder query = new StringBuilder("?amount=" + amount);
|
||||
|
||||
if (category != null && category != Category.ANY) {
|
||||
query.append("&category=").append(category.getApiValue());
|
||||
}
|
||||
|
||||
if (difficulty != null && difficulty != Difficulty.ANY) {
|
||||
query.append("&difficulty=").append(difficulty.getApiValue());
|
||||
}
|
||||
|
||||
if (type != null && type != Type.ANY) {
|
||||
query.append("&type=").append(type.getApiValue());
|
||||
}
|
||||
|
||||
return query.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user