-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from scc-digitalhub/function-templates
API for templates
- Loading branch information
Showing
8 changed files
with
501 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...n/src/main/java/it/smartcommunitylabdhub/core/controllers/v1/base/TemplateController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...n/java/it/smartcommunitylabdhub/core/models/queries/filters/abstracts/TemplateFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
21 changes: 21 additions & 0 deletions
21
...java/it/smartcommunitylabdhub/core/models/queries/services/SearchableTemplateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.