-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add isCompleted request param that is not required to /todos request
- Loading branch information
Showing
2 changed files
with
70 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 63 additions & 48 deletions
111
src/main/java/com/example/demo/service/ToDoService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |