Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
@JsonSerialize(using = SettingOptionSerializer.class)
@JsonDeserialize(using = SettingOptionDeserializer.class)
public enum SettingOption {
LOGO_URL("The logo url", Category.THEME.name(), Type.FILE.name());
LOGO_URL("The logo url", Category.THEME, Type.FILE);

private final String description;
private final String category;
private final String type;
private final Category category;
private final Type type;

SettingOption(String description, String category, String type) {
SettingOption(String description, Category category, Type type) {
this.description = description;
this.category = category;
this.type = type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public void serialize(
generator.writeFieldName("description");
generator.writeString(settingOption.getDescription());
generator.writeFieldName("category");
generator.writeString(settingOption.getCategory());
generator.writeString(settingOption.getCategory().name());
generator.writeFieldName("type");
generator.writeString(settingOption.getType().name());
generator.writeEndObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.objectcomputing.checkins.services.permissions.Permission;
import com.objectcomputing.checkins.services.permissions.RequiredPermission;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.http.HttpRequest;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.HttpStatus;
import io.micronaut.http.MediaType;
Expand All @@ -16,6 +14,7 @@
import io.micronaut.validation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

import java.net.URI;
import java.util.List;
Expand All @@ -38,49 +37,68 @@ public SettingsController(SettingsServices settingsServices) {
}

/**
* Find setting by its name, or if blank find all settings.
* Find all settings that are currently configured
*
* @param name {@link String} name of the setting
* @return {@link <List<SettingResponseDTO>>} Returned setting
*/
@ExecuteOn(TaskExecutors.BLOCKING)
@Get("/{?name}")
@Get
@RequiredPermission(Permission.CAN_VIEW_SETTINGS)
public List<SettingsResponseDTO> findByName(@Nullable String name) {
return settingsServices.findByName(name);
public List<SettingsResponseDTO> findAllSettings() {
return settingsServices.findAllSettings().stream()
.map(this::fromEntity).toList();
}

/**
* Find setting by its name
*
* @param name {@link String} name of the setting
* @return {@link <SettingResponseDTO>} Returned setting
*/
@ExecuteOn(TaskExecutors.BLOCKING)
@Get("/{name}")
@RequiredPermission(Permission.CAN_VIEW_SETTINGS)
public SettingsResponseDTO findByName(@PathVariable @NotNull String name) {
return fromEntity(settingsServices.findByName(name));
}

/**
* Find all available setting options that can be used to configure a new setting.
* Note: there can only be one setting per unique name
* @return {@link <SettingOption>} Returned setting options
*/
@Get("/options")
@RequiredPermission(Permission.CAN_VIEW_SETTINGS)
public List<SettingOption> getOptions() {
return SettingOption.getOptions();
}

/**
* Create and save a new setting.
*
* @param settingDTO, {@link SettingsCreateDTO}
* Note: there can only be one setting unique name
* @param settingDTO, {@link SettingsDTO}
* @return {@link HttpResponse<SettingsResponseDTO>}
*/
@ExecuteOn(TaskExecutors.BLOCKING)
@Post
@RequiredPermission(Permission.CAN_ADMINISTER_SETTINGS)
public HttpResponse<SettingsResponseDTO> save(@Body @Valid SettingsCreateDTO settingDTO) {
public HttpResponse<SettingsResponseDTO> save(@Body @Valid SettingsDTO settingDTO) {
Setting savedSetting = settingsServices.save(fromDTO(settingDTO));
URI location = UriBuilder.of(PATH).path(savedSetting.getId().toString()).build();
return HttpResponse.created(fromEntity(savedSetting), location);
}

/**
* Update the setting.
* Update only the value field of a setting found by its name.
*
* @param settingDTO, {@link SettingsUpdateDTO}
* @param settingsDTO, {@link SettingsDTO}
* @return {@link <SettingsReponseDTO>}
*/
@Put
@ExecuteOn(TaskExecutors.BLOCKING)
@RequiredPermission(Permission.CAN_ADMINISTER_SETTINGS)
public HttpResponse<SettingsResponseDTO> update(@Body @Valid SettingsUpdateDTO settingDTO) {
Setting savedSetting = settingsServices.update(fromUpdateDTO(settingDTO));
public HttpResponse<SettingsResponseDTO> update(@Body @Valid SettingsDTO settingsDTO) {
Setting savedSetting = settingsServices.update(settingsDTO.getName(), settingsDTO.getValue());
SettingsResponseDTO settingsResponseDTO = fromEntity(savedSetting);
URI location = UriBuilder.of(PATH).path(savedSetting.getId().toString()).build();
return HttpResponse.ok(settingsResponseDTO).headers(headers ->
Expand All @@ -100,18 +118,18 @@ public HttpStatus delete(UUID id) {
return settingsServices.delete(id) ? HttpStatus.OK : HttpStatus.UNPROCESSABLE_ENTITY;
}

private Setting fromDTO(SettingsCreateDTO settingsCreateDTO) {
return new Setting(settingsCreateDTO.getName(), settingsCreateDTO.getValue());
}

private Setting fromUpdateDTO(SettingsUpdateDTO settingsUpdateDTO) {
return new Setting(settingsUpdateDTO.getId(), settingsUpdateDTO.getName(), settingsUpdateDTO.getValue());
private Setting fromDTO(SettingsDTO settingsDTO) {
return new Setting(settingsDTO.getName(), settingsDTO.getValue());
}

private SettingsResponseDTO fromEntity(Setting entity) {
SettingOption option = SettingOption.fromName(entity.getName());
SettingsResponseDTO dto = new SettingsResponseDTO();
dto.setId(entity.getId());
dto.setName(entity.getName());
dto.setDescription(option.getDescription());
dto.setCategory(option.getCategory());
dto.setType(option.getType());
dto.setValue(entity.getValue());
return dto;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@
import io.micronaut.core.annotation.Introspected;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
@Introspected
public class SettingsCreateDTO {
public class SettingsDTO {

@NotNull
@NotBlank
@Schema(description = "name of the setting")
private String name;

@NotNull
@NotBlank
@Schema(description = "value of the setting")
private String value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.micronaut.data.repository.CrudRepository;

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

@JdbcRepository(dialect = Dialect.POSTGRES)
Expand All @@ -18,5 +19,5 @@ public interface SettingsRepository extends CrudRepository<Setting, UUID> {
@NonNull
List<Setting> findAll();

@NonNull List<Setting> findByName(@NonNull String name);
Optional<Setting> findByName(@NonNull String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ public class SettingsResponseDTO {
@Schema(description = "id of the setting")
private UUID id;

@NotNull
@NotBlank
@Schema(description = "name of the setting")
private String name;


@NotBlank
@Schema(description = "description for the setting")
private String description;

@NotNull
@Schema(description = "category of the setting")
private SettingOption.Category category;

@NotNull
@Schema(description = "type of the setting")
private SettingOption.Type type;

@NotBlank
@Schema(description = "value of the setting")
private String value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.objectcomputing.checkins.services.settings;

import io.micrometer.context.Nullable;
import jakarta.validation.constraints.NotNull;

import java.util.List;
import java.util.UUID;
Expand All @@ -9,9 +9,11 @@ public interface SettingsServices {

Setting save(Setting setting);

Setting update(Setting setting);
Setting update(String name, String value);

List<SettingsResponseDTO> findByName(@Nullable String name);
Setting findByName(@NotNull String name);

List<Setting> findAllSettings();

Boolean delete(UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import com.objectcomputing.checkins.exceptions.BadArgException;
import com.objectcomputing.checkins.exceptions.NotFoundException;
import io.micronaut.core.annotation.Nullable;
import jakarta.inject.Singleton;
import jakarta.validation.constraints.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Singleton
public class SettingsServicesImpl implements SettingsServices {
Expand All @@ -24,34 +21,25 @@ public Setting save(Setting setting) {
if (setting.getId() != null) {
throw new BadArgException("Setting ID is autogenerated by the server upon creation, and should not be provided.");
}
validateSettingOption(setting);
validateSettingOption(setting.getName());
return settingsRepository.save(setting);
}

public Setting update(Setting setting) {
validateSettingOption(setting);
if ((settingsRepository.existsByIdAndName(setting.getId(), setting.getName()))) {
return settingsRepository.update(setting);
} else {
throw new BadArgException(String.format("Setting %s does not exist, cannot update", setting.getId()));
}
public Setting update(String name, String value) {
validateSettingOption(name);
Setting setting = settingsRepository.findByName(name)
.orElseThrow(() -> new NotFoundException("Setting with name " + name + " not found."));
setting.setValue(value);
return settingsRepository.update(setting);
}

public List<SettingsResponseDTO> findByName(@Nullable String name) {
List<Setting> searchResult = name == null ? settingsRepository.findAll() : settingsRepository.findByName(name);
return settingToSettingResponseDTO(searchResult);
public Setting findByName(@NotNull String name) {
return settingsRepository.findByName(name)
.orElseThrow(() -> new NotFoundException("Setting with name " + name + " not found."));
}

public List<SettingsResponseDTO> settingToSettingResponseDTO(List<Setting> settings) {
List<SettingsResponseDTO> settingResponseDTOs = new ArrayList<>();
for(Setting setting: settings) {
SettingsResponseDTO dto = new SettingsResponseDTO();
dto.setId(setting.getId());
dto.setName(setting.getName());
dto.setValue(setting.getValue());
settingResponseDTOs.add(dto);
}
return settingResponseDTOs;
public List<Setting> findAllSettings() {
return settingsRepository.findAll();
}

public Boolean delete(@NotNull UUID id) {
Expand All @@ -62,8 +50,8 @@ public Boolean delete(@NotNull UUID id) {
return true;
}

private void validateSettingOption(Setting setting) {
if(!SettingOption.isValidOption(setting.getName())){
private void validateSettingOption(String name) {
if(!SettingOption.isValidOption(name)){
throw new BadArgException("Provided setting name is invalid.");
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE settings ADD CONSTRAINT unique_name UNIQUE (name);
Loading