Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. package defaultpackage.topic;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.List;
  9.  
  10. /**
  11. * Created by zales on 02.03.2017.
  12. */
  13. @Service
  14. public class TopicService {
  15.  
  16. @Autowired
  17. private TopicRepository topicRepository;
  18.  
  19. private List<Topic> topics = new ArrayList<>(Arrays.asList(
  20. new Topic("spring", "Spring Framework", "Spring Framework Description"),
  21. new Topic("java", "Core Java", "Core Java Description"),
  22. new Topic("javascript", "JavaScript", "Java Script Description")
  23. ));
  24.  
  25. public List<Topic> getAllTopics(){
  26. List<Topic> topics = new ArrayList<>();
  27. topicRepository.findAll()
  28. .forEach(topics::add);
  29. return topics;
  30. }
  31.  
  32. public Topic getTopic(final String id){
  33. return topics.stream().filter(t -> t.getId().equals(id)).findFirst().get();
  34. }
  35.  
  36.  
  37. public void addTopic(Topic topic) {
  38. topicRepository.save(topic);
  39. }
  40.  
  41. public void updateTopic(String id, Topic topic) {
  42. for(int i = 0; i < topics.size(); i++){
  43. Topic t = topics.get(i);
  44. if (t.getId().equals(id)){
  45. topics.set(i, topic);
  46. return;
  47. }
  48.  
  49. }
  50.  
  51. }
  52.  
  53. public void deleteTopic(String id) {
  54. topics.removeIf(t -> t.getId().equals(id));
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement