package com.codecrafters.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author Fabian Dietenberger
 */
@RestController()
@RequestMapping("todos")
public class TodoItemRestController {

    private final TodoItemRepository repository;

    @Autowired
    public TodoItemRestController(final TodoItemRepository repository) {
        this.repository = repository;
    }

    @RequestMapping(method = RequestMethod.GET)
    public List<TodoItem> getTodoItems(@RequestParam(value = "text", required = false) String containingText) {
        return repository.findByTextContainingIgnoreCase(Optional.ofNullable(containingText).orElse(""));
    }

    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<String> addTodoItem(@RequestBody final TodoItem item) {
        repository.save(item);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<String> removeTodoItem(@RequestBody final TodoItem item) {
        repository.delete(item);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}

@@ -233,16 +233,42 @@
 ion.*;%0A%0A
+import java.time.Instant;%0A
 import j
@@ -282,16 +282,16 @@
 l.List;%0A
-
 import j
@@ -673,16 +673,31 @@
  public 
+ResponseEntity%3C
 List%3CTod
@@ -702,16 +702,17 @@
 odoItem%3E
+%3E
  getTodo
@@ -798,22 +798,44 @@
         
-return
+final List%3CTodoItem%3E items =
  reposit
@@ -918,16 +918,213 @@
 e(%22%22));%0A
+        return ResponseEntity.ok()%0A                .lastModified(Instant.now().toEpochMilli()) // if we dont return this timestamp the browser could cache the request%0A                .body(items);%0A
     %7D%0A%0A 

Return last-modified in the /todos response.