package com.example.userserver.controller;

import com.example.userserver.entity.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/rest")
public class HelloWorld {

    @GetMapping("/hello")
    public Map<String,Object> hello (HttpSession session){

        User user = new User();
        user.setId(1);
        user.setName("张三");

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        session.setAttribute("user",user);
        if (session.getAttribute("user") != null) {
            resultMap.put("user",user);
        }

        System.out.println("session ID: " + session.getId());

        return resultMap;
    }


    @GetMapping("/h1")
    public Map<String,Object> h1 (HttpSession session){

        User user = new User();
        user.setId(1);
        user.setName("张三");

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        User sessionUser = (User) session.getAttribute("user");
        if (session.getAttribute("user") != null) {
            resultMap.put("data",sessionUser);
        }

        System.out.println("session ID: " + session.getId());

        return resultMap;
    }

}
