package ch.ds1.challengeTask.TodoList.entry;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "api/entry")
public class EntryController {

  private final EntryService entryService;

  @Autowired
  public EntryController(EntryService entryService) {
    this.entryService = entryService;
  }

  @GetMapping
  public List<Entry> getEntries() {
    return entryService.getEntries();
  }

  @GetMapping(path = "{entryId}")
  public Entry getEntry(@PathVariable("entryId") Long entryId) {
    return entryService.getEntry(entryId);
  }

  // TODO: not necessary --> can be removed
  @PostMapping
  public void registerNewEntry(@RequestBody Entry entry) {
    entryService.addNewEntry(entry);

  }

  @DeleteMapping(path = "{entryId}")
  public void deleteEntry(@PathVariable("entryId") Long entryId) {
    entryService.deleteEntry(entryId);
  }

  @PutMapping(path = "{entryId}")
  public Entry updateEntry(
      @PathVariable("entryId") Long entryId,
      @RequestParam(required=false) String title,
      @RequestParam(required=false) boolean done) {
    return entryService.updateEntry(entryId, title, done);
  }


}
