/*
 * Spring-boot Examples
 *
 * Copyright (c) 2021. Elex. All Rights Reserved.
 * https://www.elex-project.com/
 */

package kr.pe.elex.examples;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@RestController
public class MyController {
	@Autowired
	private PersonRepository repository;

	@GetMapping({"/"})
	public String index() {
		return "home";
	}
	@GetMapping({"/list"})
	public List<PersonDto> list() {
		return repository.findAll().stream()
				.map(PersonDto::of)
				.collect(Collectors.toList());
	}

	@GetMapping(value = {"/add/{name}"})
	public PersonDto addPerson(@PathVariable String name) {

		Person person = repository.save(Person.builder().name(name).build());
		return PersonDto.of(person);
	}

}
