Skip to content

Commit

Permalink
Merge pull request #145 from scc-digitalhub/function-templates
Browse files Browse the repository at this point in the history
API for templates
  • Loading branch information
matteo-s authored Dec 6, 2024
2 parents 60c7cc9 + a7aed22 commit c3429aa
Show file tree
Hide file tree
Showing 8 changed files with 501 additions and 2 deletions.
23 changes: 22 additions & 1 deletion application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@
<groupId>it.smartcommunitylabdhub</groupId>
<artifactId>dh-runtime-base</artifactId>
<version>${revision}</version>
</dependency>
</dependency>
<dependency>
<groupId>it.smartcommunitylabdhub</groupId>
<artifactId>dh-framework-k8s</artifactId>
Expand Down Expand Up @@ -313,6 +313,27 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>templates package</id>
<goals>
<goal>copy-resources</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}/templates</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/../templates</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package it.smartcommunitylabdhub.core.controllers.v1.base;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import it.smartcommunitylabdhub.commons.models.template.Template;
import it.smartcommunitylabdhub.core.ApplicationKeys;
import it.smartcommunitylabdhub.core.annotations.ApiVersion;
import it.smartcommunitylabdhub.core.models.queries.filters.abstracts.TemplateFilter;
import it.smartcommunitylabdhub.core.models.queries.services.SearchableTemplateService;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@ApiVersion("v1")
@RequestMapping("/templates")
//TODO evaluate permissions for project via lookup in dto
@PreAuthorize("hasAuthority('ROLE_USER')")
@Validated
@Tag(name = "Template base API", description = "Endpoints related to entity templates management")
public class TemplateController {

@Autowired
SearchableTemplateService templateService;

@Operation(summary = "List templates", description = "Return a list of all templates")
@GetMapping(path = "", produces = "application/json; charset=UTF-8")
public Page<Template> listTemplates(
@ParameterObject @Valid @Nullable TemplateFilter filter,
@ParameterObject @PageableDefault(page = 0, size = ApplicationKeys.DEFAULT_PAGE_SIZE) @SortDefault.SortDefaults(
{ @SortDefault(sort = "id", direction = Direction.ASC) }
) Pageable pageable
) {
if (filter == null) filter = new TemplateFilter();
return templateService.searchTemplates(pageable, filter);
}

@Operation(summary = "List entity's templates", description = "Return a list of all entity's templates")
@GetMapping(path = "/{entity}", produces = "application/json; charset=UTF-8")
public Page<Template> getTemplates(
@PathVariable @NotNull String entity,
@ParameterObject @Valid @Nullable TemplateFilter filter,
@ParameterObject @PageableDefault(page = 0, size = ApplicationKeys.DEFAULT_PAGE_SIZE) @SortDefault.SortDefaults(
{ @SortDefault(sort = "id", direction = Direction.ASC) }
) Pageable pageable
) {
if (filter == null) filter = new TemplateFilter();
return templateService.searchTemplates(pageable, entity, filter);
}

@Operation(summary = "Get specific template", description = "Return a specific template")
@GetMapping(path = "/{entity}/{id}", produces = "application/json; charset=UTF-8")
public Template getOne(@PathVariable @NotNull String entity, @PathVariable @NotNull String id) {
return templateService.getTemplate(entity, id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package it.smartcommunitylabdhub.core.models.queries.filters.abstracts;

import io.swagger.v3.oas.annotations.media.Schema;
import it.smartcommunitylabdhub.commons.Keys;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Valid
public class TemplateFilter {

@Nullable
protected String q;

@Nullable
@Pattern(regexp = Keys.SLUG_PATTERN)
@Schema(example = "my-function-1", defaultValue = "", description = "Name identifier")
protected String name;

@Nullable
@Pattern(regexp = Keys.SLUG_PATTERN)
@Schema(example = "type", defaultValue = "", description = "Type identifier")
protected String type;

@Nullable
@Pattern(regexp = Keys.SLUG_PATTERN)
@Schema(example = "function", defaultValue = "", description = "Kind identifier")
protected String kind;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.smartcommunitylabdhub.core.models.queries.services;

import it.smartcommunitylabdhub.commons.exceptions.SystemException;
import it.smartcommunitylabdhub.commons.models.template.Template;
import it.smartcommunitylabdhub.core.models.queries.filters.abstracts.TemplateFilter;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/*
* Searchable service for managing function
*/
public interface SearchableTemplateService {
Page<Template> searchTemplates(Pageable pageable, @Valid TemplateFilter filter) throws SystemException;

Page<Template> searchTemplates(Pageable pageable, @NotNull String type, @Valid TemplateFilter filter)
throws SystemException;

Template getTemplate(@NotNull String type, @NotNull String id) throws SystemException;
}
Loading

0 comments on commit c3429aa

Please sign in to comment.