Commit b38b11bf authored by Zane Safi Mroue's avatar Zane Safi Mroue
Browse files

fixed database

parent b953275c
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -13,4 +13,9 @@
      <module name="group8" target="17" />
    </bytecodeTargetLevel>
  </component>
  <component name="JavacSettings">
    <option name="ADDITIONAL_OPTIONS_OVERRIDE">
      <module name="vslam-objects" options="-parameters" />
    </option>
  </component>
</project>
 No newline at end of file
+19 −1
Original line number Diff line number Diff line
@@ -15,6 +15,24 @@
        <exec.mainClass>top.BackendJava</exec.mainClass>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version> <!-- Try changing the version here -->
                <configuration>
                    <source>17</source>
                    <target>17</target>
                    <compilerArgs>
                        <arg>-parameters</arg>
                    </compilerArgs>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <!-- Spring Boot Starter Web -->
        <dependency>
@@ -88,7 +106,7 @@
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <groupId>org.junit.v4</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
+10 −0
Original line number Diff line number Diff line
@@ -117,6 +117,16 @@ public class MongoDBInteraction {
        objectCollection.deleteOne(Filters.eq("index", index));
    }

    public ObjectSet retrieveLatestObjectSet() {
        // Assuming 'index' is an integer that increments with each new ObjectSet
        Document highestIndexDoc = objectCollection.find().sort(new Document("index", -1)).first();
        if (highestIndexDoc != null) {
            return convertDocumentToObjectSet(highestIndexDoc);
        }
        return null;
    }


    private ObjectSet convertDocumentToObjectSet(Document doc) {
        List<Document> pointSetsDocs = (List<Document>) doc.get("objectSets");
        ObjectSet objectSet = new ObjectSet();
+38 −38
Original line number Diff line number Diff line
@@ -27,9 +27,36 @@ public class BackendJava {
        SpringApplication.run(BackendJava.class, args);
    }

    @RestController
    @RequestMapping("/objectset")
    public static class ObjectSetController {

        private final MongoDBInteraction dbInteraction = new MongoDBInteraction();
        private final Gson gson = new GsonBuilder().create();

        @GetMapping("/{index}")
        public ResponseEntity<String> getObjectSet(@PathVariable int index) {
            try {
                ObjectSet objectSet = dbInteraction.retrieveObjectSet(index);
                if (objectSet != null) {
                    return ResponseEntity.ok(gson.toJson(objectSet));
                } else {
                    return ResponseEntity.notFound().build();
                }
            } catch (NumberFormatException e) {
                return ResponseEntity.badRequest().body("{\"error\":\"Invalid index format\"}");
            } catch (Exception e) {
                return ResponseEntity.internalServerError().body("{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}");
            }
        }
    }

    @Controller
    public static class BackendService {

        private final MongoDBInteraction dbInteraction = new MongoDBInteraction();
        private final Gson gson = new GsonBuilder().create();

        @RequestMapping("/hello")
        @ResponseBody
        public String hello() {
@@ -51,20 +78,17 @@ public class BackendJava {

        @RequestMapping("/getJSON")
        @ResponseBody
        public Map<String, ArrayList<ArrayList<Float>>> tempJson() {
            Map<String, ArrayList<ArrayList<Float>>> temp = new HashMap<>();

            ArrayList<Float> pp = new ArrayList<>();
            pp.add(0.0f);
            pp.add(0.0f);
            pp.add(0.0f);

            ArrayList<ArrayList<Float>> obj = new ArrayList<>();
            obj.add(pp);

            temp.put("obj1", obj);

            return temp;
        public ResponseEntity<String> tempJson() {
            try {
                ObjectSet latestObjectSet = dbInteraction.retrieveLatestObjectSet();
                if (latestObjectSet != null) {
                    return ResponseEntity.ok(gson.toJson(latestObjectSet.objects));
                } else {
                    return ResponseEntity.notFound().build();
                }
            } catch (Exception e) {
                return ResponseEntity.internalServerError().body("{\"error\":\"Error retrieving latest object set: " + e.getMessage() + "\"}");
            }
        }

        @RequestMapping("/style/main.css")
@@ -83,30 +107,6 @@ public class BackendJava {
        }
    }

    @RestController
    @RequestMapping("/objectset")
    public static class ObjectSetController {

        private final MongoDBInteraction dbInteraction = new MongoDBInteraction();
        private final Gson gson = new GsonBuilder().create();

        @GetMapping("/{index}")
        public ResponseEntity<String> getObjectSet(@PathVariable int index) {
            try {
                ObjectSet objectSet = dbInteraction.retrieveObjectSet(index);
                if (objectSet != null) {
                    return ResponseEntity.ok(gson.toJson(ObjectSet.objects));
                } else {
                    return ResponseEntity.notFound().build();
                }
            } catch (NumberFormatException e) {
                return ResponseEntity.badRequest().body("{\"error\":\"Invalid index format\"}");
            } catch (Exception e) {
                return ResponseEntity.internalServerError().body("{\"error\":\"Internal Server Error: " + e.getMessage() + "\"}");
            }
        }
    }

    @Configuration
    @EnableWebMvc
    public static class WebMvcConfig implements WebMvcConfigurer {