This commit is contained in:
andbi 2025-11-19 20:22:22 -06:00
parent aa7c30950c
commit 72716e1c28
41 changed files with 15461 additions and 0 deletions

3
backend/.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
backend/target
.DS_Store

9
backend/README.md Normal file
View file

@ -0,0 +1,9 @@
# Start the Server
```bash
mvn clean spring-boot:run
```
That's it. The server will be using port 8081 which can be changed in `application.properties`
Now go to the `../frontend` folder and follow the `README` instructions

41
backend/pom.xml Normal file
View file

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mycompany.backend</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>25</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View file

@ -0,0 +1,13 @@
package com.mycompany.backend.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Main {
static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}

View file

@ -0,0 +1,17 @@
package com.mycompany.backend.demo.api;
import com.mycompany.backend.demo.data.DataType;
import com.mycompany.backend.demo.data.DataTypeConfiguration;
import org.springframework.lang.Nullable;
public interface DataService {
DataTypeConfiguration getConfiguration();
@Nullable
DataType getDataType(String id);
DataTypeConfiguration updateType(DataType newEntity);
void deleteType(String id);
}

View file

@ -0,0 +1,35 @@
package com.mycompany.backend.demo.config;
import com.mycompany.backend.demo.api.DataService;
import com.mycompany.backend.demo.data.DataServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.logging.Logger;
/**
* These beans will be created on boot time and can be later injected in any service/component.
* @see org.springframework.context.annotation.Configuration
*/
@Configuration
public class AppConfiguration implements WebMvcConfigurer {
private static final Logger LOGGER = Logger.getLogger(AppConfiguration.class.getName());
@Bean
public DataService repository() {
LOGGER.info("Initializing data repository bean...");
return new DataServiceImpl();
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // all endpoints
.allowedOrigins("*") // no restrictions
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*");
LOGGER.info("CORS policies configured for the REST API");
}
}

View file

@ -0,0 +1,61 @@
package com.mycompany.backend.demo.data;
import com.mycompany.backend.demo.api.DataService;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A simple in-memory database
*/
public class DataServiceImpl implements DataService {
private static final List<DataType> INITIAL_DATA = List.of(
new DataType("id1", "Asset", List.of(
new DataType.Property("category", "string")
)),
new DataType("id2", "Employee", List.of(
new DataType.Property("name", "string"),
new DataType.Property("email", "string")
))
);
private final Map<String, DataType> dataTypes = new ConcurrentHashMap<>();
public DataServiceImpl() {
// Initialize with initial data (won't be needed with a database layer)
for (DataType s : INITIAL_DATA) {
dataTypes.put(s.id(), s);
}
}
@Override
public DataTypeConfiguration getConfiguration() {
var sorted = new ArrayList<>(dataTypes.values());
sorted.sort(Comparator.comparing(DataType::name));
return new DataTypeConfiguration(sorted);
}
@Override
public DataType getDataType(String id) {
return dataTypes.get(id);
}
@Override
public DataTypeConfiguration updateType(DataType newEntity) {
dataTypes.put(newEntity.id(), newEntity);
// 1. Do other stuff here, e.g. update the related objects.
// This part must be performed in a single transaction
// 2. Return the updated configuration with all the changes included
return getConfiguration();
}
@Override
public void deleteType(String id) {
dataTypes.remove(id);
}
}

View file

@ -0,0 +1,10 @@
package com.mycompany.backend.demo.data;
import java.util.List;
public record DataType(String id, String name, List<Property > properties) {
record Property(String name, String type) {
}
}

View file

@ -0,0 +1,12 @@
package com.mycompany.backend.demo.data;
import java.io.Serializable;
import java.util.List;
/**
* A dummy global configuration structure, like customer-related ontologies.
* For now, only
* @param dataTypes
*/
public record DataTypeConfiguration(List<DataType> dataTypes) implements Serializable {
}

View file

@ -0,0 +1,4 @@
/**
* Mocks of the data-layer and service classes.
*/
package com.mycompany.backend.demo.data;

View file

@ -0,0 +1,71 @@
package com.mycompany.backend.demo.rest;
import com.mycompany.backend.demo.api.DataService;
import com.mycompany.backend.demo.data.DataType;
import com.mycompany.backend.demo.data.DataTypeConfiguration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.logging.Logger;
@RestController
@RequestMapping("/data")
public class StaticRoutes {
private static final Logger LOGGER = Logger.getLogger(StaticRoutes.class.getName());
final DataService dataService;
/**
* Spring Boot will automatically find the {@link DataService} bean as it is
* configured in the {@link com.mycompany.backend.demo.config.AppConfiguration}
*
* @param dataService bean
*/
public StaticRoutes(DataService dataService) {
this.dataService = dataService;
LOGGER.info("Basic static routes created.");
}
/**
* A simple "get all" endpoint
*/
@GetMapping
public ResponseEntity<DataTypeConfiguration> listDataTypes() {
LOGGER.info("Returning all data types...");
return ResponseEntity.ok(dataService.getConfiguration());
}
/**
* A simple "get by id" endpoint
*/
@GetMapping("/{id}")
public ResponseEntity<DataType> getDataType(@PathVariable("id") String id) {
LOGGER.info("Returning all data types...");
var found = dataService.getDataType(id);
if (found == null) {
return ResponseEntity.notFound().build();
} else {
return ResponseEntity.ok(found);
}
}
/**
* This endpoint updates a single data structure element and returns a new list
* of ontologies (they may be interconnected)
*
* @param modified the updated object
* @return the list
*/
@PostMapping
public ResponseEntity<DataTypeConfiguration> updateConfiguration(@RequestBody DataType modified) {
LOGGER.info("Updating data type and related data: " + modified);
return ResponseEntity.ok(dataService.updateType(modified));
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> delete(@PathVariable("id") String id) {
LOGGER.info("Deleting data type with id: " + id);
dataService.deleteType(id);
return ResponseEntity.noContent().build();
}
}

View file

@ -0,0 +1,3 @@
# This "dev" profile will override the default application.properties
# See the Spring Boot docs on how to start different profiles in IDE on via command line
logging.level.root = trace

View file

@ -0,0 +1,3 @@
spring.application.name=DEMO APP
server.port=8081
server.servlet.context-path=/v1

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hi There!</title>
</head>
<body>
<h3>Hi There!</h3>
<p>
In addition to the REST endpoints, Spring Boot can also serve static resources.
</p>
<p>
For REST-only production deployments, serving static pages is usually disabled.
</p>
</body>
</html>

View file

@ -0,0 +1,13 @@
package com.mycompany.backend.demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MainTests {
@Test
void contextLoads() {
}
}