Commit e796f19f authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'appsettings_update' into 'main'

AppSettings update

See merge request grin-global/grin-global-server!385
parents e78e7408 c000044a
......@@ -122,10 +122,10 @@ public class AppController extends ApiBaseController {
return l10n;
}
@GetMapping(value = "/setting/{name}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "getSetting", description = "Get setting by category and name", summary = "Get")
public String getSetting(@RequestParam(value="categoryTag", required = false) final String categoryTag, @PathVariable(value="name") final String name) {
return settingsService.getSetting(categoryTag, name).getValue();
@GetMapping(value = "/setting", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "getSettings", description = "Get settings by category and name", summary = "Get")
public List<AppSetting> getSettings(@RequestParam(value="categoryTag", required = false) final String categoryTag, @RequestParam(value="name") final String name) {
return settingsService.getSettings(categoryTag, name);
}
public static class LookupMap<K1, K2, V> extends TreeMap<K1, Map<K2, V>> {
......
......@@ -59,4 +59,13 @@ public interface AppSettingsService extends CRUDService<AppSetting> {
*/
AppSetting getSetting(String categoryTag, String name);
/**
* Gets the list of {@link AppSetting} by category and name
*
* @param categoryTag
* @param name
* @return the list of {@link AppSetting}
*/
List<AppSetting> getSettings(String categoryTag, String name);
}
......@@ -18,6 +18,7 @@ package org.gringlobal.service.impl;
import java.util.List;
import java.util.Optional;
import org.apache.commons.compress.utils.Lists;
import org.gringlobal.api.exception.NotFoundElement;
import org.apache.commons.lang3.StringUtils;
import org.gringlobal.model.AppResource;
......@@ -128,6 +129,16 @@ public class AppSettingsServiceImpl extends CRUDServiceImpl<AppSetting, AppSetti
return appSetting;
}
@Override
public List<AppSetting> getSettings(String categoryTag, String name) {
var qAs = QAppSetting.appSetting;
var appSettings = Lists.newArrayList(appSettingRepository.findAll(qAs.name.eq(name).and(categoryTag == null ? qAs.categoryTag.isNull() : qAs.categoryTag.eq(categoryTag))).iterator());
if (appSettings.isEmpty()) {
throw new NotFoundElement("No settings with name " + name + " for categoryTag=" + categoryTag);
}
return appSettings;
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
......
......@@ -147,32 +147,35 @@ public class AppControllerTest extends AbstractApiV1Test {
/*@formatter:off*/
mockMvc
.perform(get(AppController.API_URL + "/setting/{name}", "Setting name 1")
.perform(get(AppController.API_URL + "/setting")
.queryParam("categoryTag", "Category Tag")
.queryParam("name", "Setting name 1")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isString())
.andExpect(jsonPath("$", is("Category setting value 1")))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].value", is("Category setting value 1")))
;
/*@formatter:on*/
/*@formatter:off*/
mockMvc
.perform(get(AppController.API_URL + "/setting/{name}", "Setting name 2")
.perform(get(AppController.API_URL + "/setting")
.queryParam("name", "Setting name 2")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isString())
.andExpect(jsonPath("$", is("Setting value 2")))
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$[0].value", is("Setting value 2")))
;
/*@formatter:on*/
/*@formatter:off*/
mockMvc
.perform(get(AppController.API_URL + "/setting/{name}", "Setting name X")
.perform(get(AppController.API_URL + "/setting")
.queryParam("name", "Setting name X")
.contentType(MediaType.APPLICATION_JSON))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isNotFound())
......
......@@ -183,4 +183,38 @@ public class AppSettingsServiceTest extends AbstractServicesTest {
assertThat(boolVal, not(nullValue()));
assertThat(boolVal.isEmpty(), is(true));
}
@Test
public void getSettingsTest() {
var list = appSettingsService.settingList();
assertThat(list, not(nullValue()));
assertThat(list, hasSize(0));
var appSetting = appSettingsService.create(createSetting(CATEGORY_TAG, "S1", "Value 1"));
assertThat(appSetting.getCategoryTag(), is(CATEGORY_TAG));
assertThat(appSetting.getName(), is("S1"));
assertThat(appSetting.getValue(), is("Value 1"));
var appSetting2 = appSettingsService.create(createSetting(CATEGORY_TAG, "S1", "Value 2"));
assertThat(appSetting2.getCategoryTag(), is(CATEGORY_TAG));
assertThat(appSetting2.getName(), is("S1"));
assertThat(appSetting2.getValue(), is("Value 2"));
list = appSettingsService.settingList();
assertThat(list, hasSize(2));
var loaded = appSettingsService.getSettings(CATEGORY_TAG, "S1");
assertThat(loaded, not(nullValue()));
assertThat(loaded, hasSize(2));
assertThat(loaded.get(0).getCategoryTag(), is(appSetting.getCategoryTag()));
assertThat(loaded.get(0).getSortOrder(), is(appSetting.getSortOrder()));
assertThat(loaded.get(0).getName(), is(appSetting.getName()));
assertThat(loaded.get(0).getValue(), is(oneOf(appSetting.getValue(), appSetting2.getValue())));
assertThat(loaded.get(1).getCategoryTag(), is(appSetting.getCategoryTag()));
assertThat(loaded.get(1).getSortOrder(), is(appSetting.getSortOrder()));
assertThat(loaded.get(1).getName(), is(appSetting.getName()));
assertThat(loaded.get(1).getValue(), is(oneOf(appSetting.getValue(), appSetting2.getValue())));
list = appSettingsService.settingList();
assertThat(list, hasSize(2));
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment