package com.getyourlocation.app.gyl_server.web.controller;

import com.getyourlocation.app.gyl_server.util.LogUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/api")
public class APIController {

    private static final String DIR_UPLOAD = "upload_files";
    private static final Logger LOG = LoggerFactory.getLogger(APIController.class);

    @RequestMapping(path = "/sum", method = RequestMethod.GET)
    public LinkedHashMap<String, Object> sum(
        @RequestParam(value = "x", defaultValue = "0") int x,
        @RequestParam(value = "y", defaultValue = "0") int y,
        HttpServletRequest request, HttpServletResponse response) {
        LogUtil.logReq(LOG, request);
        LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("ans", x + y);
        return result;
    }

    @RequestMapping(path = "/product", method = RequestMethod.POST)
    public LinkedHashMap<String, Object> product(
        @RequestParam(value = "x", defaultValue = "0") int x,
        @RequestParam(value = "y", defaultValue = "0") int y,
        HttpServletRequest request, HttpServletResponse response) {
        LogUtil.logReq(LOG, request);
        LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
        result.put("ans", x * y);
        return result;
    }

    @RequestMapping(path = "/upload", method = RequestMethod.POST)
    public List<String> upload(
        @RequestParam(value = "file", required = true) MultipartFile[] files,
        @RequestParam(value = "ext", required = false) String ext,
        HttpServletRequest request, HttpServletResponse response) {
        LogUtil.logReq(LOG, request);
        List<String> result = new ArrayList<String>();
        for (MultipartFile file : files) {
            if (file.getOriginalFilename().equals("")) {
                continue;
            }
            String contentType = file.getContentType();
            String extension = contentType.substring(contentType.indexOf("/") + 1);
            if (extension.equals("octet-stream")) {
                extension = ext;
            }
            String fileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension;
            String path = request.getSession().getServletContext().getRealPath(DIR_UPLOAD);
            File targetFile = new File(path, fileName);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            try {
                file.transferTo(targetFile);  // Save to disk
            } catch (Exception e) {
                e.printStackTrace();
            }
            result.add(request.getContextPath() + "/" + DIR_UPLOAD + "/" + fileName);
        }
        return result;
    }
}

@@ -3376,11 +3376,414 @@
 t;%0A    %7D
+%0A%0A    @RequestMapping(path = %22/localization%22, method = RequestMethod.POST)%0A    public LinkedHashMap%3CString, Object%3E localization(%0A        HttpServletRequest request, HttpServletResponse response) %7B%0A        LogUtil.logReq(LOG, request);%0A        LinkedHashMap%3CString, Object%3E result = new LinkedHashMap%3CString, Object%3E();%0A        result.put(%22ans%22, %22hello%22);%0A        // TODO %E5%AE%9A%E4%BD%8D%0A        return result;%0A    %7D
 %0A%7D%0A

Add route /api/localization