<commit_before>package com.apssouza.controllers;

import com.apssouza.services.ToDoService;
import com.apssouza.entities.ToDo;
import com.apssouza.exceptions.DataNotFoundException;
import com.fasterxml.jackson.databind.JsonNode;
import java.net.URI;
import java.util.List;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

/**
 *
 * @author apssouza
 */
@RequestMapping("/todos")
@RestController
public class ToDoController {

    @Autowired
    ToDoService toDoService;

    @GetMapping
    public List<ToDo> all() {
        return this.toDoService.all();
    }

    @PostMapping
    public ResponseEntity<?> save(@Valid ToDo todo) {
        ToDo saved = this.toDoService.save(todo);
        Long id = saved.getId();
        if (id != null) {
            URI location = ServletUriComponentsBuilder
                    .fromCurrentRequest().path("/{id}")
                    .buildAndExpand(id).toUri();
            return ResponseEntity.created(location).build();
        }
        return ResponseEntity.noContent().build();
    }

    @PutMapping("{id}")
    public ResponseEntity<?> update(@PathVariable long id) {
        return toDoService.findById(id)
                .map( todo -> {            
                    todo.setId(id);
                    toDoService.save(todo);
                    return ResponseEntity.ok(todo);
                }).orElseThrow(() -> new DataNotFoundException("Todo not found"));
    }

    @GetMapping("{id}")
    public ResponseEntity<?> find(@PathVariable long id) {
        return toDoService.findById(id)
                .map(todo -> {
                    return ResponseEntity.ok(todo);
                }).orElseThrow(() -> new DataNotFoundException("Todo not found"));
    }

    @DeleteMapping("{id}")
    public ResponseEntity<?> delete(@PathVariable long id) {
        toDoService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PutMapping("{id}/status")
    public ResponseEntity<?> statusUpdate(@PathVariable long id, JsonNode statusUpdate) {
        JsonNode done = statusUpdate.get("done");
        if (!done.asBoolean()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).
                    header("reason", "JSON should contains field done").
                    build();
        }
        ToDo todo = toDoService.updateStatus(id, done.asBoolean());
        return ResponseEntity.ok(todo);
    }

}<commit_msg>added not found test<commit_after>package com.apssouza.controllers;

import com.apssouza.services.ToDoService;
import com.apssouza.entities.ToDo;
import com.apssouza.exceptions.DataNotFoundException;
import com.fasterxml.jackson.databind.JsonNode;
import java.net.URI;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

/**
 *
 * @author apssouza
 */
@RequestMapping("/todos")
@RestController
public class ToDoController {

    @Autowired
    ToDoService toDoService;

    @GetMapping
    public List<ToDo> all() {
        List<ToDo> all = this.toDoService.all();
        return all;
    }

    @PostMapping
    public ResponseEntity<?> save(@Valid ToDo todo) {
        ToDo saved = this.toDoService.save(todo);
        Long id = saved.getId();
        if (id != null) {
            URI location = ServletUriComponentsBuilder
                    .fromCurrentRequest().path("/{id}")
                    .buildAndExpand(id).toUri();
            return ResponseEntity.created(location).build();
        }
        return ResponseEntity.noContent().build();
    }

    @PutMapping("{id}")
    public ResponseEntity<?> update(@PathVariable long id) {
        return toDoService.findById(id)
                .map(todo -> {
                    todo.setId(id);
                    toDoService.save(todo);
                    return ResponseEntity.ok(todo);
                }).orElseThrow(() -> new DataNotFoundException("Todo not found"));
    }

    @GetMapping("{id}")
    public ResponseEntity<?> find(@PathVariable long id) {
        Optional<ToDo> findById = toDoService.findById(id);
        return findById.map(todo -> {
            return ResponseEntity.ok(todo);
        }).orElseThrow(() -> new DataNotFoundException("Todo not found"));
    }

    @DeleteMapping("{id}")
    public ResponseEntity<?> delete(@PathVariable long id) {
        toDoService.delete(id);
        return ResponseEntity.status(HttpStatus.OK).build();
    }

    @PutMapping("{id}/status")
    public ResponseEntity<?> statusUpdate(@PathVariable long id, JsonNode statusUpdate) {
        JsonNode done = statusUpdate.get("done");
        if (!done.asBoolean()) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).
                    header("reason", "JSON should contains field done").
                    build();
        }
        ToDo todo = toDoService.updateStatus(id, done.asBoolean());
        return ResponseEntity.ok(todo);
    }

}
{'fraction_non_alphanumeric': 0.11893203883495146, 'fraction_numerical': 0.0, 'mean_word_length': 2.967265725288832, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6963520', 'n_tokens_mistral': 1696, 'n_tokens_neox': 1675, 'n_words': 309}