package com.yihu.hos.arbiter.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.arbiter.models.Endpoint;
import com.yihu.hos.arbiter.services.EndpointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @created Airhead 2016/7/27.
 */
@RestController
@RequestMapping("/endpoint")
public class EndpointController {
    @Autowired
    private EndpointService endpointService;

    @RequestMapping(method = RequestMethod.POST)
    public void save(String service) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            Endpoint endpoint = objectMapper.readValue(service, Endpoint.class);
            endpointService.save(endpoint);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get(String serviceName) {
        return endpointService.get(serviceName).toString();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String put(String serviceName, String ClientInfo) {
        return endpointService.get(serviceName).toString();
    }
}
