Skip to content

Commit

Permalink
Add isCompleted request param that is not required to /todos request
Browse files Browse the repository at this point in the history
  • Loading branch information
danexpc committed Jul 5, 2021
1 parent f9cc28d commit a5a398a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ public String handleException(Exception ex) {

@GetMapping("/todos")
@Valid
public List<ToDoResponse> getAll() {
public List<ToDoResponse> getAll(@RequestParam(required = false) Boolean isCompleted) {
if (isCompleted != null) {
if (isCompleted) {
return toDoService.getAllCompleted();
}
return toDoService.getAllInProgress();
}
return toDoService.getAll();
}

Expand Down
111 changes: 63 additions & 48 deletions src/main/java/com/example/demo/service/ToDoService.java
Original file line number Diff line number Diff line change
@@ -1,67 +1,82 @@
package com.example.demo.service;

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

import org.springframework.stereotype.Service;

import com.example.demo.dto.ToDoResponse;
import com.example.demo.dto.ToDoSaveRequest;
import com.example.demo.dto.mapper.ToDoEntityToResponseMapper;
import com.example.demo.exception.ToDoNotFoundException;
import com.example.demo.model.ToDoEntity;
import com.example.demo.repository.ToDoRepository;
import org.springframework.stereotype.Service;

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

import static com.example.demo.repository.specification.ToDoSpecifications.isCompleted;
import static com.example.demo.repository.specification.ToDoSpecifications.isInProgress;
import static org.springframework.data.jpa.domain.Specification.where;

@Service
public class ToDoService {

private final ToDoRepository toDoRepository;

public ToDoService(ToDoRepository toDoRepository) {
this.toDoRepository = toDoRepository;
}

public List<ToDoResponse> getAll() {
return toDoRepository.findAll().stream()
.map(ToDoEntityToResponseMapper::map)
.collect(Collectors.toList());
}
private final ToDoRepository toDoRepository;

public ToDoService(ToDoRepository toDoRepository) {
this.toDoRepository = toDoRepository;
}

public List<ToDoResponse> getAll() {
return toDoRepository.findAll().stream()
.map(ToDoEntityToResponseMapper::map)
.collect(Collectors.toList());
}

public List<ToDoResponse> getAllCompleted() {
return toDoRepository.findAll(where(isCompleted())).stream()
.map(ToDoEntityToResponseMapper::map)
.collect(Collectors.toList());
}

public List<ToDoResponse> getAllInProgress() {
return toDoRepository.findAll(where(isInProgress())).stream()
.map(ToDoEntityToResponseMapper::map)
.collect(Collectors.toList());
}

public ToDoResponse upsert(ToDoSaveRequest toDoDTO) throws ToDoNotFoundException {
ToDoEntity todo;
//update if it has id or create if it hasn't
if (toDoDTO.id == null) {
todo = new ToDoEntity(toDoDTO.text);
} else {
todo = toDoRepository.findById(toDoDTO.id).orElseThrow(() -> new ToDoNotFoundException(toDoDTO.id));
todo.setText(toDoDTO.text);
}
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}
public ToDoResponse upsert(ToDoSaveRequest toDoDTO) throws ToDoNotFoundException {
ToDoEntity todo;
//update if it has id or create if it hasn't
if (toDoDTO.id == null) {
todo = new ToDoEntity(toDoDTO.text);
} else {
todo = toDoRepository.findById(toDoDTO.id).orElseThrow(() -> new ToDoNotFoundException(toDoDTO.id));
todo.setText(toDoDTO.text);
}
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}

public ToDoResponse completeToDo(Long id) throws ToDoNotFoundException {
ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id));
todo.completeNow();
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}
public ToDoResponse completeToDo(Long id) throws ToDoNotFoundException {
ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id));
todo.completeNow();
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}

public ToDoResponse cancelToDo(Long id) throws ToDoNotFoundException {
ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id));
todo.cancelNow();
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}
public ToDoResponse cancelToDo(Long id) throws ToDoNotFoundException {
ToDoEntity todo = toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id));
todo.cancelNow();
return ToDoEntityToResponseMapper.map(toDoRepository.save(todo));
}

public ToDoResponse getOne(Long id) throws ToDoNotFoundException {
return ToDoEntityToResponseMapper.map(
toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id))
);
}
public ToDoResponse getOne(Long id) throws ToDoNotFoundException {
return ToDoEntityToResponseMapper.map(
toDoRepository.findById(id).orElseThrow(() -> new ToDoNotFoundException(id))
);
}

public void deleteOne(Long id) {
toDoRepository.deleteById(id);
}
public void deleteOne(Long id) {
toDoRepository.deleteById(id);
}

public void deleteAll() {
toDoRepository.deleteAll();
}
public void deleteAll() {
toDoRepository.deleteAll();
}
}

0 comments on commit a5a398a

Please sign in to comment.