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

fix inconsistencies with api spec and refactor #22

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/main/java/io/mathdojo/MathDojoQuestionRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.mathdojo;

import java.util.Collection;
import java.util.List;

import org.springframework.data.mongodb.repository.MongoRepository;
Expand All @@ -10,6 +9,8 @@
public interface MathDojoQuestionRepository extends MongoRepository<Question, String> {

List<Question> findByQuestionTitle(String questionTitle);

List<Question> findByDifficulty(String difficulty);


}
5 changes: 5 additions & 0 deletions src/main/java/io/mathdojo/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ public boolean equals(Object obj) {
return true;
}

public boolean isValid() {
//object validation rules to be decided
return true;
}




Expand Down
164 changes: 72 additions & 92 deletions src/main/java/io/mathdojo/QuestionFunction.java
Original file line number Diff line number Diff line change
@@ -1,142 +1,122 @@
package io.mathdojo;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Iterables;
import com.microsoft.azure.functions.ExecutionContext;

@SpringBootApplication
import reactor.core.publisher.Flux;

@Configuration
public class QuestionFunction {
/**
* This class contains all the functions that can be called in this service.
* Functions are called through RequestHandler classes
*/

@Autowired
public MathDojoQuestionRepository repository;
@Autowired
public MathDojoTopicRepository tRepository;

public static void main(String[] args) throws Exception {
SpringApplication.run(QuestionFunction.class, args);
}
public QuestionService questionService;

@Bean
public Function<Question, Question> getQuestion() {
return input -> Iterables.getFirst(
repository.findByQuestionTitle(input.getQuestionTitle()).stream()
.filter(i -> i.getDifficulty().equals(input.getDifficulty())).collect(Collectors.toList()),
Question.EMPTY_DATABASE);
public Function<Flux<Question>, Flux<List<Question>>> getQuestionsByTitleAndDifficulty() {
return questionFluxEntity -> {
return questionFluxEntity.map(question -> {
return questionService.findQuestionsByTitleAndDifficulty(question.getQuestionTitle(),
question.getDifficulty());
});
};
}

@Bean
public Consumer<Question> createQuestion() {
return question -> repository.save(question);
public Function<Flux<Question>, Flux<Question>> createQuestion(ExecutionContext context) {
return questionFluxEntity -> {
return questionFluxEntity.map(question -> {
context.getLogger().info("creating question with title " + question.getQuestionTitle());
return questionService.createQuestion(question);
});
};
}

@Bean
public Function<Question, Question> getQuestionById() {
return question -> repository.findById(question.getId()).isPresent()
? repository.findById(question.getId()).get()
: Question.EMPTY_DATABASE;
public Function<Flux<String>, Flux<Question>> getQuestionById(ExecutionContext context) {
return questionFluxEntity -> {
return questionFluxEntity.map(questionId -> {
context.getLogger().info("retrieving question with id " + questionId);
return questionService.getQuestionById(questionId);
});
};

}

@Bean
public Consumer<Question> updateQuestion() {
return new Consumer<Question>() {

@Override
public void accept(Question question) {
Question oldQuestion = repository.findById(question.getId()).isPresent()
? repository.findById(question.getId()).get()
: null;
Question newQuestion = new Question(question.getId(),
question.getQuestionTitle() != null ? question.getQuestionTitle()
: oldQuestion.getQuestionTitle(),
question.getQuestionBody() != null ? question.getQuestionBody() : oldQuestion.getQuestionBody(),
question.getSampleAnswer() != null ? question.getSampleAnswer() : oldQuestion.getSampleAnswer(),
question.getSuccessRate() != null ? question.getSuccessRate() : oldQuestion.getSuccessRate(),
question.getDifficulty() != null ? question.getDifficulty() : oldQuestion.getDifficulty(),
question.getHints() != null ? question.getHints() : oldQuestion.getHints(),
question.getParentTopicTitle() != null ? question.getParentTopicTitle()
: oldQuestion.getParentTopicTitle(),
question.getQuestionAnswerOptions() != null ? question.getQuestionAnswerOptions()
: oldQuestion.getQuestionAnswerOptions(),
question.getAnswer() != null ? question.getAnswer() : oldQuestion.getAnswer());
;
repository.save(newQuestion);
}

public Function<Flux<Question>, Flux<Question>> updateQuestion(ExecutionContext context) {
return questionFluxEntity -> {
return questionFluxEntity.map(question -> {
context.getLogger().info("updating question with id " + question.getId());
return questionService.updateQuestionById(question);
});
};
}

@Bean
public Consumer<Question> deleteQuestion() {
return question -> repository.deleteById(question.getId());
public Consumer<Question> deleteQuestionById(ExecutionContext context) {
return deletionRequest -> questionService.deleteQuestionById(deletionRequest.getId());
}

@Bean
public Function<Topic, Topic> getTopic() {
return input -> Iterables.getFirst(
tRepository.findByTopicTitle(input.getTopicTitle()).stream().collect(Collectors.toList()),
Topic.EMPTY_DATABASE);
public Function<Flux<Topic>, Flux<List<Topic>>> getTopicsByTitle() {
return topicFluxEntity -> {
return topicFluxEntity.map(topic -> {
return questionService.findTopicsByTitle(topic.getTopicTitle());
});
};
}

@Bean
public Consumer<Topic> createTopic() {
return topic -> tRepository.save(topic);
public Function<Flux<Topic>, Flux<Topic>> createTopic(ExecutionContext context) {
return topicFluxEntity -> {
return topicFluxEntity.map(topic -> {
context.getLogger().info("creating topic with title " + topic.getTopicTitle());
return questionService.createTopic(topic);
});
};
}

@Bean
public Function<Topic, Topic> getTopicById() {
return topic -> tRepository.findById(topic.getId()).isPresent() ? tRepository.findById(topic.getId()).get()
: Topic.EMPTY_DATABASE;
public Function<Flux<String>, Flux<Topic>> getTopicById(ExecutionContext context) {
return topicFluxEntity -> {
return topicFluxEntity.map(topicId -> {
context.getLogger().info("retrieving topic with id " + topicId);
return questionService.getTopicById(topicId);
});
};
}

@Bean
public Consumer<Topic> updateTopic() {

return new Consumer<Topic>() {

@Override
public void accept(Topic topic) {
Topic oldTopic = tRepository.findById(topic.getId()).isPresent()
? tRepository.findById(topic.getId()).get()
: null;
Topic newTopic = new Topic(topic.getId(),
topic.getTopicTitle() != null ? topic.getTopicTitle() : oldTopic.getTopicTitle(),
topic.getName() != null ? topic.getName() : oldTopic.getName(),
topic.getQuestions() != null ? topic.getQuestions() : oldTopic.getQuestions());
tRepository.save(newTopic);
}

public Function<Flux<Topic>, Flux<Topic>> updateTopic(ExecutionContext context) {
return topicFluxEntity -> {
return topicFluxEntity.map(topic -> {
context.getLogger().info("retrieving topic with id " + topic);
return questionService.updateTopicById(topic);
});
};
}

@Bean
public Consumer<Topic> deleteTopic() {
return topic -> tRepository.deleteById(topic.getId());
public Consumer<Topic> deleteTopicById(ExecutionContext context) {
return deletionRequest -> questionService.deleteTopicById(deletionRequest.getId());
}

@Bean
public Function<Topic, List<Question>> getQuestions() {

return new Function<Topic, List<Question>>() {
@Override
public List<Question> apply(Topic t) {
List<Question> x = new ArrayList<>();
repository.findAllById(tRepository.findById(t.getId()).get().getQuestions()).forEach(x::add);
return x;
}
public Function<Flux<Topic>, Flux<List<Question>>> getQuestions(ExecutionContext context) {

return topicFluxEntity -> {
return topicFluxEntity.map(topic -> {
context.getLogger().info("retrieving questions of topic " + topic.getTopicTitle());
return questionService.getAllQuestionsInTopic(topic.getId());
});
};
}

}
}
6 changes: 3 additions & 3 deletions src/main/java/io/mathdojo/QuestionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

public class QuestionHandler extends AzureSpringBootRequestHandler<Question, Question> {

@FunctionName("getQuestion")
@FunctionName("getQuestionsByTitleAndDifficulty")
public Question executeGet(@HttpTrigger(name = "request", methods = {
HttpMethod.GET }, authLevel = AuthorizationLevel.ANONYMOUS, route = "question") HttpRequestMessage<Optional<Question>> request,
HttpMethod.GET }, authLevel = AuthorizationLevel.ANONYMOUS, route = "questions") HttpRequestMessage<Optional<Question>> request,
ExecutionContext context) {
context.getLogger().info("Retrieving question");

Expand Down Expand Up @@ -61,7 +61,7 @@ public Question executeUpdate(
return handleRequest(request.getBody().get(), context);
}

@FunctionName("deleteQuestion")
@FunctionName("deleteQuestionById")
public Question executeDelete(@HttpTrigger(name = "request", methods = {
HttpMethod.DELETE }, authLevel = AuthorizationLevel.ANONYMOUS, route = "questions/{questionId}") HttpRequestMessage<Optional<Question>> request,
ExecutionContext context, @BindingName("questionId") String questionId) {
Expand Down
Loading