Skip to content
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@
<version>${slf4j.version}</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/com/epam/eighty/service/CustomerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
public interface CustomerService {

/**
* Retrieves a set of all Customer {@link com.epam.eighty.domain.Customer} entities from the datastore.
* Retrieves a list of all {@link com.epam.eighty.domain.Customer Customer} entities from the datastore.
*
* @return the result set
* @return the result list
*/
List<Customer> getAllCustomers();

List<Customer> getSortedSetOfCustomersByName(String customerName);
/**
* Retrieves a list of {@link com.epam.eighty.domain.Customer Customer} entities whose names match the given {@code customerName}.
*
* @param customerName
* @return a list of customers
*/
List<Customer> getCustomersMatchingName(String customerName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add java doc at least here :)


/**
* Retrieves a list of customer {@link com.epam.eighty.domain.Customer} entities whose questions present in topic with given id .
* Retrieves a list of {@link com.epam.eighty.domain.Customer Customer} entities whose questions present in topic with the given {@code id}.
*
* @param id topic id
* @return a list of customers
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/com/epam/eighty/service/QuestionService.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
package com.epam.eighty.service;

import com.epam.eighty.domain.Question;
import java.util.List;

import org.springframework.data.domain.Pageable;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import com.epam.eighty.domain.Question;

/**
* @author Aliaksandr_Padalka
*/
public interface QuestionService {

Optional<Question> getQuestionById(Long id);
Set<Question> getAllQuestions();
Question getQuestionById(Long id);
List<Question> getAllQuestions();
void addQuestion(Question question, Long topicId);
void updateQuestion(Question question);
void deleteQuestion(Long id);
List<Question> getQuestionsPage(Long topicId, Pageable pageable);
List<Question> getQuestionsByTopicAndTag(Long topicId, String tag);
List<Question> getQuestionsByTopicId(Long topicId, Pageable pageable);
List<Question> getQuestionsByTopicIdAndTag(Long topicId, String tag);
List<Question> getQuestionsByTag(String tag);

/**
Expand All @@ -28,5 +26,5 @@ public interface QuestionService {
* @param customer a name of customer
* @return a list of all customer's questions
*/
List<Question> getQuestionsByCustomer(String customer);
List<Question> getQuestionsByCustomerName(String customer);
}
2 changes: 1 addition & 1 deletion src/main/java/com/epam/eighty/service/TagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public interface TagService {
List<Tag> getTagsByTopicId(Long topicId);
List<Tag> getAllTags();
List<Tag> getTopNFromAllTags(Long limit);
List<Tag> getSortedSetOfTagsByName(String tagName);
List<Tag> getTagsMatchingName(String tagName);
}
47 changes: 44 additions & 3 deletions src/main/java/com/epam/eighty/service/TopicService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
package com.epam.eighty.service;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import com.epam.eighty.domain.Topic;
import com.epam.eighty.exception.TopicNotFoundException;

/**
* @author Aliaksandr_Padalka
*/
public interface TopicService {

/**
* Returns root {@link com.epam.eighty.domain.Topic Topic} entity wrapped in {@link java.util.Optional Optional}.
*
* @return root topic
*/
Optional<Topic> getRoot();
Optional<Topic> getTopicById(Long id);
Set<Topic> getAllTopics();

/**
* Returns {@link com.epam.eighty.domain.Topic Topic} entity with the given {@code id}.
*
* @param id topic id
* @return topic
* @throws TopicNotFoundException when topic with the given {@code id} not found
*/
Topic getTopicById(Long id);

/**
* Returns a list of all {@link com.epam.eighty.domain.Topic Topic} entities from the datastore.
*
* @return the result list
*/
List<Topic> getAllTopics();

/**
* Updates the given {@code topic} in the datastore.
*
* @param topic topic to update
*/
void updateTopic(Topic topic);

/**
* Deletes topic with the given {@code id} in the datastore.
*
* @param id topic id
*/
void deleteTopic(Long id);

/**
* Adds the given {@code topic} to topic with the given {@code id} and save it.
*
* @param topic to add
* @param id topic id to add to
* @return added topic
* @throws TopicNotFoundException when topic with the given {@code id} not found
*/
Topic createTopic(Topic topic, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,27 @@
@Transactional
public class CustomerServiceImpl implements CustomerService {

private static final String ANY_SYMBOL = ".*";

@Autowired
private CustomerRepository customerRepository;

@SuppressWarnings("unchecked")
@Override
public List<Customer> getAllCustomers() {
Collection<Customer> customers = customerRepository.findAll().as(Collection.class);
final Collection<Customer> customers = customerRepository.findAll().as(Collection.class);
return customers.stream().filter(customer -> customer.getCount() != null).collect(Collectors.toList());
}

@Override
public List<Customer> getSortedSetOfCustomersByName(final String customerName) {
return customerRepository.getCustomersMatchingName(ANY_SYMBOL + customerName + ANY_SYMBOL);
public List<Customer> getCustomersMatchingName(final String customerName) {
return customerRepository.getCustomersMatchingName(customerName);
}

@Override
public List<Customer> getCustomersByTopicId(final Long topicId) {
List<Customer> customerList = customerRepository.getCustomersByTopicId(topicId);
final List<Customer> customerList = customerRepository.getCustomersByTopicId(topicId);

customerList
.forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(),
topicId)));
.forEach(customer -> customer.setCountInTopic(customerRepository.getQuestionsNumberInTopicByCustomer(customer.getName(), topicId)));
return customerList;
}

Expand Down
32 changes: 15 additions & 17 deletions src/main/java/com/epam/eighty/service/impl/QuestionServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package com.epam.eighty.service.impl;

import com.epam.eighty.domain.Question;
import com.epam.eighty.domain.Topic;
import com.epam.eighty.exception.TopicNotFoundException;
import com.epam.eighty.repository.QuestionRepository;
import com.epam.eighty.repository.TopicRepository;
import com.epam.eighty.service.QuestionService;

import java.util.List;
import java.util.Optional;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.eighty.domain.Question;
import com.epam.eighty.domain.Topic;
import com.epam.eighty.exception.TopicNotFoundException;
import com.epam.eighty.repository.QuestionRepository;
import com.epam.eighty.repository.TopicRepository;
import com.epam.eighty.service.QuestionService;

/**
* @author Aliaksandr_Padalka
*/
Expand All @@ -33,19 +31,19 @@ public class QuestionServiceImpl implements QuestionService {
private Neo4jOperations template;

@Override
public Optional<Question> getQuestionById(final Long id) {
return questionRepo.findOne(id);
public Question getQuestionById(final Long id) {
return questionRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id));
}

@SuppressWarnings("unchecked")
@Override
public Set<Question> getAllQuestions() {
return questionRepo.findAll().as(Set.class);
public List<Question> getAllQuestions() {
return questionRepo.findAll().as(List.class);
}

@Override
public void addQuestion(final Question question, final Long id) {
Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id));
final Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id));
questionRepo.save(question);
topic.getQuestions().add(question);
topicRepo.save(topic);
Expand All @@ -62,12 +60,12 @@ public void deleteQuestion(final Long id) {
}

@Override
public List<Question> getQuestionsPage(final Long topicId, final Pageable pageable) {
public List<Question> getQuestionsByTopicId(final Long topicId, final Pageable pageable) {
return questionRepo.getQuestionsByTopicId(topicId, pageable).getContent();
}

@Override
public List<Question> getQuestionsByTopicAndTag(final Long topicId, final String tag) {
public List<Question> getQuestionsByTopicIdAndTag(final Long topicId, final String tag) {
return questionRepo.getQuestionsByTopicIdAndTag(topicId, tag);
}

Expand All @@ -77,7 +75,7 @@ public List<Question> getQuestionsByTag(final String tag) {
}

@Override
public List<Question> getQuestionsByCustomer(final String customer) {
public List<Question> getQuestionsByCustomerName(final String customer) {
return questionRepo.getQuestionsByCustomerName(customer);
}

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/epam/eighty/service/impl/TagServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
@Transactional
public class TagServiceImpl implements TagService {

private static final String ANY_SYMBOL = ".*";

@Autowired
private TagRepository tagRepo;

@Override
public List<Tag> getTagsByTopicId(final Long topicId) {
List<Tag> tagsList = tagRepo.getTagsByTopicId(topicId);
tagsList
.forEach(tag -> tag.setCountInTopic(tagRepo.getQuestionsNumberInTopicByTag(tag.getTag(), topicId)));
final List<Tag> tagsList = tagRepo.getTagsByTopicId(topicId);
tagsList.forEach(tag -> tag.setCountInTopic(tagRepo.getQuestionsNumberInTopicByTag(tag.getTag(), topicId)));
return tagsList;
}

Expand All @@ -42,8 +39,8 @@ public List<Tag> getTopNFromAllTags(final Long limit) {
}

@Override
public List<Tag> getSortedSetOfTagsByName(final String tagName) {
return tagRepo.getTagsMatchingName(ANY_SYMBOL + tagName + ANY_SYMBOL);
public List<Tag> getTagsMatchingName(final String tagName) {
return tagRepo.getTagsMatchingName(tagName);
}

}
38 changes: 17 additions & 21 deletions src/main/java/com/epam/eighty/service/impl/TopicServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.epam.eighty.service.impl;

import java.util.Optional;
import java.util.Set;
import com.epam.eighty.domain.Topic;
import com.epam.eighty.exception.TopicNotFoundException;
import com.epam.eighty.repository.TopicRepository;
import com.epam.eighty.service.TopicService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.eighty.domain.Topic;
import com.epam.eighty.repository.TopicRepository;
import com.epam.eighty.service.TopicService;
import java.util.List;
import java.util.Optional;

/**
* @author Aliaksandr_Padalka
Expand All @@ -27,26 +28,23 @@ public class TopicServiceImpl implements TopicService {

@Override
public Optional<Topic> getRoot() {
Optional<Topic> root = topicRepo.findBySchemaPropertyValue("title", "root");
root.ifPresent(someRoot ->
someRoot.getTopics().forEach(template::fetch)
);
final Optional<Topic> root = topicRepo.findBySchemaPropertyValue("title", "root");
root.ifPresent(someRoot -> someRoot.getTopics().forEach(template::fetch));
return root;
}

@Override
public Optional <Topic> getTopicById(final Long id) {
Optional<Topic> topic = topicRepo.findOne(id);
topic.ifPresent(t ->
t.getTopics().forEach(template::fetch)
);
public Topic getTopicById(final Long id) {
final Topic topic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id));
topic.getTopics().forEach(template::fetch);

return topic;
}

@SuppressWarnings("unchecked")
@Override
public Set<Topic> getAllTopics() {
return topicRepo.findAll().as(Set.class);
public List<Topic> getAllTopics() {
return topicRepo.findAll().as(List.class);
}

@Override
Expand All @@ -61,11 +59,9 @@ public void deleteTopic(final Long id) {

@Override
public Topic createTopic(final Topic topic, final Long id) {
Optional<Topic> parentTopic = topicRepo.findOne(id);
parentTopic.ifPresent(t -> {
t.getTopics().add(topic);
topicRepo.save(t);
});
final Topic parentTopic = topicRepo.findOne(id).orElseThrow(() -> new TopicNotFoundException(id));
parentTopic.getTopics().add(topic);
topicRepo.save(parentTopic);

return topic;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class CustomerController {
@ResponseBody
@Cacheable(value = "customer", key = "#customerName")
public List<Customer> getSortedSetOfCustomersByName(@ApiParam(name = "customerName", required = true, value = "sorted set of customers by part of customer name") @PathVariable("customerName") final String customerName) {
return customerService.getSortedSetOfCustomersByName(customerName);
return customerService.getCustomersMatchingName(customerName);
}

@ApiOperation(value = "Find all customers", notes = "Get all customers", httpMethod = "GET", response = Customer.class, produces = "application/json")
Expand Down
Loading