Skip to content
Permalink
Browse files

Added FeedController and FeedCategoryController

  • Loading branch information...
borabilgin committed Jan 26, 2019
1 parent f7749ca commit 5cd8c09c5b9c93992fafb63bb17b542da90ca5ca
@@ -0,0 +1,25 @@
package com.demo.imagebrowser.controller;

import com.demo.imagebrowser.domain.FeedCategory;
import com.demo.imagebrowser.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/secure/category")
public class FeedCategoryController {

@Autowired
private FeedService feedService;

@RequestMapping(method = RequestMethod.PUT)
public ResponseEntity<FeedCategory> addCategory(@RequestParam() String categoryName) {
FeedCategory category = feedService.addCategory(categoryName);
return new ResponseEntity<>(category, HttpStatus.OK);
}
}
@@ -0,0 +1,25 @@
package com.demo.imagebrowser.controller;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/feed")
public class FeedController {

@Autowired
private FeedService feedService;

@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public List<Feed> getFeeds() {
return feedService.getAll();
}
}
@@ -2,12 +2,13 @@

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;

import java.util.List;

public interface FeedService {
List<Feed> findByCategoryName(String categoryName);
void deleteCategoryByName(String categoryName);
FeedCategory addCategory(String categoryName);
Feed addFeed(Feed feed);
List<Feed> getAll();
Boolean categoryExists(String categoryName);
}
@@ -6,10 +6,14 @@
import com.demo.imagebrowser.repository.FeedRepository;
import com.demo.imagebrowser.service.FeedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Service
public class FeedServiceImpl implements FeedService {

@Autowired
@@ -33,16 +37,30 @@ public void deleteCategoryByName(String categoryName) {
feedCategoryRepository.deleteByName(categoryName);
}

@Override
public Boolean categoryExists(String categoryName) {
return feedCategoryRepository.findByName(categoryName) != null;
}

@Override
public FeedCategory addCategory(String categoryName) {
FeedCategory category = new FeedCategory();
category.setName(categoryName);
feedCategoryRepository.save(category);
return category;
if (!categoryExists(categoryName)) {
FeedCategory category = new FeedCategory();
category.setName(categoryName);
feedCategoryRepository.save(category);
return category;
} else {
return null;
}
}

@Override
public Feed addFeed(Feed feed) {
return feedRepository.save(feed);
}

@Override
public List<Feed> getAll() {
return StreamSupport.stream(feedRepository.findAll().spliterator(), false).collect(Collectors.toList());
}
}

0 comments on commit 5cd8c09

Please sign in to comment.
You can’t perform that action at this time.