Skip to content

Commit

Permalink
[3979] Add Project related REST APIs in Sirius Web
Browse files Browse the repository at this point in the history
Bug: #3979
Signed-off-by: Axel RICHARD <[email protected]>
  • Loading branch information
AxelRICHARD committed Sep 20, 2024
1 parent b4ff0a7 commit 61ae89c
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 1 deletion.
9 changes: 8 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ The new option ALWAYS allows the separator to be displayed in every case.
=== New Features

- https://github.com/eclipse-sirius/sirius-web/issues/3763[#3763] [diagram] Make it possible to display semantic candidates in the selection dialog using a tree

- https://github.com/eclipse-sirius/sirius-web/issues/3979[#3979] [core] Add Project related REST APIs.
The new endpoints are:
** getProjects (`GET /api/rest/projects`): Get all projects.
** getProjectById (`GET /api/rest/projects/{projectId}`): Get project with the given id (projectId).
** createProject (`POST /projects`): Create a new project with the given name and
description (optional).
** deleteProject (`POST /api/rest/projects/{projectId}`): Delete the project with the given id (projectId).
** updateProject (`PUT /projects/{projectId}`): Update the project with the given id (projectId).

=== Improvements

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.dto;

import java.util.List;
import java.util.UUID;

/**
* Interface for the REST Record DTO.
*
* @author arichard
*/
public interface IRestRecord {

UUID id();

String resourceIdentifier();

List<String> alias();

String humanIdentifier();

String decription();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.project.controllers;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.eclipse.sirius.components.core.api.ErrorPayload;
import org.eclipse.sirius.web.application.project.data.versioning.dto.RestBranch;
import org.eclipse.sirius.web.application.project.dto.CreateProjectInput;
import org.eclipse.sirius.web.application.project.dto.CreateProjectSuccessPayload;
import org.eclipse.sirius.web.application.project.dto.DeleteProjectInput;
import org.eclipse.sirius.web.application.project.dto.RenameProjectInput;
import org.eclipse.sirius.web.application.project.dto.RenameProjectSuccessPayload;
import org.eclipse.sirius.web.application.project.dto.RestProject;
import org.eclipse.sirius.web.application.project.services.api.IProjectApplicationService;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* REST Controller for the Project Endpoint.
*
* @author arichard
*/
@RestController
@RequestMapping("/api/rest/projects")
public class ProjectRestController {

private final IProjectApplicationService projectApplicationService;

public ProjectRestController(IProjectApplicationService projectApplicationService) {
this.projectApplicationService = Objects.requireNonNull(projectApplicationService);
}

@GetMapping
public ResponseEntity<List<RestProject>> getProjects() {
var restProjects = this.projectApplicationService.findAll(PageRequest.of(0, 20))
.map(project -> new RestProject(project.id(), null, List.of(), null, null, project.name(), List.of()))
.toList();

return new ResponseEntity<>(restProjects, HttpStatus.OK);
}

@GetMapping(path = "/{projectId}")
public ResponseEntity<RestProject> getProjectById(@PathVariable UUID projectId) {
var restProject = this.projectApplicationService.findById(projectId)
.map(project -> new RestProject(project.id(), null, List.of(), null, null, project.name(), List.of()));

if (restProject.isPresent()) {
return new ResponseEntity<>(restProject.get(), HttpStatus.OK);
}

return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}

@PostMapping
public ResponseEntity<RestProject> createProject(@RequestParam String name, @RequestParam Optional<String> description) {
var createProjectInput = new CreateProjectInput(UUID.randomUUID(), name, List.of());
var newProjectPayload = this.projectApplicationService.createProject(createProjectInput);

if (newProjectPayload instanceof CreateProjectSuccessPayload createProjectSuccessPayload) {
var projectDTO = createProjectSuccessPayload.project();
var restProject = new RestProject(projectDTO.id(), null, List.of(), null, null, projectDTO.name(), List.of());
return new ResponseEntity<>(restProject, HttpStatus.CREATED);
}
// The specification does not handle other HttpStatus than HttpStatus.CREATED for this endpoint
return null;
}

@PutMapping(path = "/{projectId}")
public ResponseEntity<RestProject> updateProject(@PathVariable UUID projectId, @RequestParam Optional<String> name, @RequestParam Optional<String> description, @RequestParam Optional<RestBranch> branch) {
if (name.isPresent()) {
var renameProjectInput = new RenameProjectInput(UUID.randomUUID(), projectId, name.get());
var renamedProjectPayload = this.projectApplicationService.renameProject(renameProjectInput);
if (renamedProjectPayload instanceof RenameProjectSuccessPayload) {
var restProject = new RestProject(projectId, null, List.of(), null, null, name.get(), List.of());
return new ResponseEntity<>(restProject, HttpStatus.OK);
}
}

return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
}

@DeleteMapping(path = "/{projectId}")
public ResponseEntity<RestProject> deleteProject(@PathVariable UUID projectId) {
var restProject = this.projectApplicationService.findById(projectId)
.map(project -> new RestProject(project.id(), null, List.of(), null, null, project.name(), List.of()))
.orElse(null);

var deleteProjectInput = new DeleteProjectInput(UUID.randomUUID(), projectId);
var deleteProjectPayload = this.projectApplicationService.deleteProject(deleteProjectInput);

if (deleteProjectPayload instanceof ErrorPayload) {
return new ResponseEntity<>(null, HttpStatus.NO_CONTENT);
}

return new ResponseEntity<>(restProject, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.project.data.versioning.dto;

import java.text.DateFormat;

import org.eclipse.sirius.web.application.dto.IRestRecord;

/**
* Interface for the REST CommitReference DTO.
*
* @author arichard
*/
public interface IRestCommitReference extends IRestRecord {

DateFormat created();

String name();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.project.data.versioning.dto;

/**
* REST Branch DTO.
*
* @author arichard
*/
public record RestBranch() {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.project.dto;

import java.util.List;
import java.util.UUID;

import org.eclipse.sirius.web.application.dto.IRestRecord;
import org.eclipse.sirius.web.application.query.dto.RestQuery;

/**
* REST Project DTO.
*
* @author arichard
*/
public record RestProject(
UUID id,
String resourceIdentifier,
List<String> alias,
String humanIdentifier,
String decription,
String name,
List<RestQuery> queries) implements IRestRecord {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.query.dto;

import java.util.List;
import java.util.UUID;

import org.eclipse.sirius.web.application.dto.IRestRecord;

/**
* REST Query DTO.
*
* @author arichard
*/
public record RestQuery(
UUID id,
String resourceIdentifier,
List<String> alias,
String humanIdentifier,
String decription) implements IRestRecord {
}
5 changes: 5 additions & 0 deletions packages/sirius-web/backend/sirius-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tngtech.archunit</groupId>
<artifactId>archunit-junit5</artifactId>
Expand Down
Loading

0 comments on commit 61ae89c

Please sign in to comment.