Skip to content

Commit

Permalink
VUU25: Implement DTOs
Browse files Browse the repository at this point in the history
  • Loading branch information
cfisher-scottlogic committed Sep 22, 2023
1 parent 2bb9c0a commit 55aa632
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 60 deletions.
17 changes: 4 additions & 13 deletions layout-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,22 @@
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
package org.finos.vuu.layoutserver.controller;

import org.finos.vuu.layoutserver.dto.response.LayoutDTO;
import org.finos.vuu.layoutserver.dto.response.MetadataDTO;
import org.finos.vuu.layoutserver.model.Layout;
import org.finos.vuu.layoutserver.model.Metadata;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController("/layout")
@RestController
@RequestMapping("/layout")
public class LayoutController {

/**
* Gets all layouts if no IDs are specified, otherwise gets the specified layouts
*
* @param ids IDs of the layouts to get
* @return the layouts
*/
@GetMapping
public List<Layout> getLayouts(@RequestBody(required = false) String[] ids) {
return new ArrayList<>();
public static final String LAYOUT_ID = "testLayoutId";

// TODO: Delete dummy data
private Layout createDummyLayout(String id) {
Layout layout = new Layout();
layout.setId(id);
layout.setDefinition("testDefinition");
Metadata metadata = new Metadata();
metadata.setId("testMetadataId");
metadata.setLayout(layout);
metadata.setName("testName");
metadata.setGroup("testGroup");
metadata.setScreenshot("testScreenshot");
metadata.setUser("testUser");
layout.setMetadata(metadata);
return layout;
}

/**
Expand All @@ -28,50 +46,51 @@ public List<Layout> getLayouts(@RequestBody(required = false) String[] ids) {
* @return the layout
*/
@GetMapping("/{id}")
public Layout getLayout(@PathVariable String id) {
return Layout.builder().build();
public LayoutDTO getLayout(@PathVariable String id) {
Layout layout = createDummyLayout(id);
return LayoutDTO.fromEntity(layout);
}

/**
* Gets metadata for all layouts if no IDs are specified, otherwise gets the metadata for specified layouts
* Gets metadata for all layouts
*
* @param ids IDs of the layouts to get metadata for
* @return the metadata
*/
@GetMapping("/metadata")
public List<Metadata> getMetadata(@RequestBody(required = false) String[] ids) {
return new ArrayList<>();
public List<MetadataDTO> getMetadata() {
Layout layout = createDummyLayout(LAYOUT_ID);
return List.of(MetadataDTO.fromEntity(layout.getMetadata()));
}

/**
* Creates a new layout
*
* @return the ID of the new layout`
* @return the ID of the new layout
*/
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public String createLayout() {
return "Hello World";
public String createLayout(@RequestBody LayoutDTO layoutDTO) {
return createDummyLayout(LAYOUT_ID).getId();
}

/**
* Updates the specified layout
*
* @param id ID of the layout to update
* @return the ID of the updated layout
* @param id ID of the layout to update
* @param layoutDTO the new data to overwrite the layout with
*/
@ResponseStatus(HttpStatus.ACCEPTED)
@PutMapping("/{id}")
public String updateLayout(@PathVariable String id) {
return "Hello World";
public void updateLayout(@PathVariable String id, @RequestBody LayoutDTO layoutDTO) {
createDummyLayout(LAYOUT_ID);
}

/**
* Deletes the specified layout
*
* @param id ID of the layout to delete
* @return the ID of the deleted layout
*/
@ResponseStatus(HttpStatus.ACCEPTED)
@DeleteMapping("/{id}")
public String deleteLayout(@PathVariable String id) {
return "Hello World";
}
public void deleteLayout(@PathVariable String id) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.finos.vuu.layoutserver.dto.response;

import lombok.Builder;
import lombok.Data;
import org.finos.vuu.layoutserver.model.Layout;

import java.io.Serializable;

@Data
@Builder
public class LayoutDTO implements Serializable {

private String id;
private String definition;
private MetadataDTO metadata;

public static LayoutDTO fromEntity(Layout layout) {
return LayoutDTO.builder()
.id(layout.getId())
.definition(layout.getDefinition())
.metadata(MetadataDTO.fromEntity(layout.getMetadata()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.finos.vuu.layoutserver.dto.response;

import lombok.Builder;
import lombok.Data;
import org.finos.vuu.layoutserver.model.Metadata;

import java.io.Serializable;
import java.util.Date;

@Data
@Builder
public class MetadataDTO implements Serializable {

private String layoutId;
private String name;
private String group;
private String screenshot;
private String user;
private Date date;

public static MetadataDTO fromEntity(Metadata metadata) {
return MetadataDTO.builder()
.layoutId(metadata.getLayout().getId())
.name(metadata.getName())
.group(metadata.getGroup())
.screenshot(metadata.getScreenshot())
.user(metadata.getUser())
.date(metadata.getDate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package org.finos.vuu.layoutserver.model;

import jakarta.persistence.*;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Entity
@Data
@Builder
@Entity
@NoArgsConstructor
public class Layout {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;

@NonNull
private String definition;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "metadata_id", referencedColumnName = "id")
@NonNull
private Metadata metadata;

protected Layout() {}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package org.finos.vuu.layoutserver.model;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.Builder;
import jakarta.persistence.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

import java.util.Date;

@Entity
@Data
@Builder
@NoArgsConstructor
@Entity
public class Metadata {

@Id
@GeneratedValue
private Long id;
@GeneratedValue(strategy = GenerationType.AUTO)
private String id;

@OneToOne(mappedBy = "metadata")
@NonNull
private Layout layout;

private String name;
private String group;
private String screenshot;
private String user;
private Date date;

protected Metadata() {
}
private Date date = new Date();
}
1 change: 1 addition & 0 deletions layout-server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
server.servlet.contextPath=/api
spring.datasource.url=jdbc:h2:file:/data/layout
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
Expand Down

0 comments on commit 55aa632

Please sign in to comment.