60 lines
1.5 KiB
Java
60 lines
1.5 KiB
Java
package rest;
|
|
|
|
import dev.rsems.feedreader.exception.FeedIdMismatchException;
|
|
import dev.rsems.feedreader.exception.FeedNotFoundException;
|
|
import dev.rsems.feedreader.model.Feed;
|
|
import dev.rsems.feedreader.repo.FeedRepository;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/Feeds")
|
|
public class FeedController {
|
|
|
|
@Autowired
|
|
private FeedRepository feedRepository;
|
|
|
|
@GetMapping
|
|
public Iterable findAll() {
|
|
return feedRepository.findAll();
|
|
}
|
|
|
|
@GetMapping("/title/{feedName}")
|
|
public List<Feed> findByTitle(@PathVariable String feedName) {
|
|
return feedRepository.findByName(feedName);
|
|
}
|
|
|
|
@GetMapping("/{id}")
|
|
public Feed findOne(@PathVariable Long id) {
|
|
return feedRepository.findById(id)
|
|
.orElseThrow(RuntimeException::new);
|
|
}
|
|
|
|
@PostMapping
|
|
@ResponseStatus(HttpStatus.CREATED)
|
|
public Feed create(@RequestBody Feed Feed) {
|
|
return feedRepository.save(Feed);
|
|
}
|
|
|
|
@DeleteMapping("/{id}")
|
|
public void delete(@PathVariable Long id) {
|
|
feedRepository.findById(id)
|
|
.orElseThrow(FeedNotFoundException::new);
|
|
feedRepository.deleteById(id);
|
|
}
|
|
|
|
@PutMapping("/{id}")
|
|
public Feed updateFeed(@RequestBody Feed Feed, @PathVariable Long id) {
|
|
if (!Feed.getId().equals(id)) {
|
|
throw new FeedIdMismatchException();
|
|
}
|
|
feedRepository.findById(id)
|
|
.orElseThrow(FeedNotFoundException::new);
|
|
return feedRepository.save(Feed);
|
|
}
|
|
|
|
}
|