Files
SpringRest/src/main/java/application/controllers/DoorController.java
T
2014-06-12 20:46:47 +02:00

57 lines
1.9 KiB
Java

package application.controllers;
import application.model.Door;
import application.model.DoorMessage;
import application.repositories.DoorRepository;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by lukasgol on 12.06.14.
*/
@Controller
@RequestMapping(value = "/doors")
public class DoorController {
@Autowired
DoorRepository doorRepository;
@RequestMapping
public
@ResponseBody
String listDoorsJson(ModelMap model) throws JSONException {
JSONArray doorArray = new JSONArray();
for (Door door : doorRepository.findAll()) {
JSONObject doorJSON = new JSONObject();
doorJSON.put("doorId", door.getDoorId());
doorJSON.put("message", door.getDoorMessage());
doorArray.put(doorJSON);
}
return doorArray.toString();
}
@RequestMapping("/{doorId}/message")
@ResponseBody
public String getUser(@PathVariable("doorId") Long doorId)throws JSONException{
Door door = doorRepository.findOne(doorId);
DoorMessage message= door.getDoorMessage();
JSONObject loginResponse = new JSONObject();
loginResponse.put("success", true);
loginResponse.put("doorId", door.getDoorId());
JSONObject jsonmessage = new JSONObject();
jsonmessage.put("dateTime",message.getDateTime() );
jsonmessage.put("message",message.getMessage() );
jsonmessage.put("serverMessageId",message.getServerMessageId() );
loginResponse.put("serverMessage", jsonmessage);
return loginResponse.toString();
}
}