Skip to content

Commit

Permalink
Endpoints re-organization + files overwriting (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-ennahdi authored Nov 7, 2024
1 parent 2c9cd09 commit 17afa34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.github.mohamedennahdi.objectmorph.app.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
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 com.github.mohamedennahdi.objectmorph.app.service.ObjectmorphService;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
public class ObjectmorphController {

Expand All @@ -25,16 +23,6 @@ public ObjectmorphController(ObjectmorphService objectmorphService) {
}

@CrossOrigin(origins = "*")
@RequestMapping( value = "/hello", method = RequestMethod.GET )
public ResponseEntity<String> hello() {
return new ResponseEntity<>( "Hello World", HttpStatus.OK);
}

@RequestMapping( value = "/test", method = RequestMethod.GET )
public ResponseEntity<String> test(List<String> files) {
return new ResponseEntity<>( "Hello World", HttpStatus.OK);
}

@GetMapping( value = "/html" )
public ResponseEntity<String> html(@Parameter(name = "sourceCode", description = "Source Code", example = "class SourceCode { int attribute1; int attribute2; SourceCode(){} public int getAttribute1() { return attribute1;}}")
@RequestParam("sourceCode") String[] sourceCode) {
Expand All @@ -43,8 +31,7 @@ public ResponseEntity<String> test(List<String> files) {
html = objectmorphService.generateHTML(sourceCode);
return new ResponseEntity<>(html, HttpStatus.OK);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("Error in ObjectmorphController", e);
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.github.mohamedennahdi.objectmorph.app.service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Service;

import com.github.mohamedennahdi.objectmorph.app.logic.ObjectmorphComponent;
Expand Down Expand Up @@ -39,8 +38,12 @@ public String generateHTML(String[] sourceCodes) throws IOException, URISyntaxEx
if (matcher.find()) {
String fileName = matcher.group() + ".java";
log.info("Filename: " + fileName);
File file = Files.createFile(Paths.get(path + File.separator + fileName)).toFile();
FileUtils.writeStringToFile(file, sourceCode, StandardCharsets.UTF_8);
File file = new File(path + File.separator + fileName);
try (Writer fileWriter = new FileWriter(file, false)) {
fileWriter.write(sourceCode);
} catch (Exception e) {
throw e;
}
files.add(file);
}
}
Expand All @@ -49,6 +52,7 @@ public String generateHTML(String[] sourceCodes) throws IOException, URISyntaxEx
} catch (Exception e) {
throw e;
} finally {
log.info("Files removal.");
for (File file : files) {
Files.delete(file.toPath());
}
Expand Down

0 comments on commit 17afa34

Please sign in to comment.