package com.example.demo.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@CrossOrigin(origins = "http://localhost:8101")
@RequestMapping(path="/user")
public class UserController {

    @Autowired // this means to get bean called userRepository which is auto-generated by spring. it is ued to handle data
    private UserRepository userRepository;
        ObjectMapper om = new ObjectMapper();

    @PostMapping(path="/add")
    //public @ResponseBody String addNewUser(@RequestParam String username, @RequestParam String email, @RequestParam String password){ //ResponseBody means the returned string is the response
        //RequestParam means it is a parameter from a GET or POST request
      public @ResponseBody String addNewuser(@RequestBody User user) {
        userRepository.save(user);
        return "saved";
    }

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUser(){
        //this returns a JSON or XML with users
        return userRepository.findAll();
    }


}
