package org.odroid.server.controllers;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;
import static org.springframework.web.bind.annotation.RequestMethod.PUT;
import java.io.IOException;
import java.util.List;
import org.odroid.server.controllers.util.ApplicationDataTypeConverter;
import org.odroid.server.entities.Application;
import org.odroid.server.entities.ApplicationDataType;
import org.odroid.server.services.ApplicationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
@RequestMapping(value = "/app")
public class ApplicationController {
private final Logger l = LoggerFactory.getLogger(getClass());
@Autowired
private ApplicationService applicationService;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(ApplicationDataType.class, new ApplicationDataTypeConverter());
}
@RequestMapping(value = "create", method = POST)
@ResponseBody
public Application create(@RequestBody Application a) {
l.debug("creating application: {}", a);
a = applicationService.createApp(a);
return a;
}
@RequestMapping(value = "{hash}", method = GET)
@ResponseBody
public Application applicationForHash(@PathVariable String hash) {
l.debug("getting application for hash: {}", hash);
Application a = applicationService.getApplicationForHash(hash);
l.debug("got application for hash: {}, application: {}", hash, a);
return a;
}
@RequestMapping(method = GET)
@ResponseBody
public List<Application> applications() {
return applicationService.getAllApplications();
}
@RequestMapping(value = "lock/{hash}", method = PUT)
@ResponseBody
public Application lock(@PathVariable String hash) {
return applicationService.lock(hash);
}
@RequestMapping(value = "unlock/{hash}", method = PUT)
@ResponseBody
public Application unlock(@PathVariable String hash) {
return applicationService.unlock(hash);
}
@RequestMapping(value = "data/{hash}/{type}", method = POST)
@ResponseBody
public String addData(@PathVariable String hash, @PathVariable ApplicationDataType type, @RequestParam("file") MultipartFile multipartFile)
throws IOException {
return applicationService.addData(hash, type, multipartFile.getBytes());
}
}