From f0ce8a615bb558f977970c10295e18e1b20bb7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Bu=C3=9Fmann?= Date: Thu, 2 Jun 2022 19:08:10 +0200 Subject: [PATCH] changed base path of REST api and updated frontend api quering --- ...sitoryConfigurator.java => RepositoryConfig.java} | 12 +++++++----- .../main/whattocook/controller/ItemController.java | 3 --- frontend/src/Api.js | 6 +++++- frontend/src/components/ItemModel.vue | 6 +++--- 4 files changed, 15 insertions(+), 12 deletions(-) rename backend/src/main/whattocook/configs/{RestRepositoryConfigurator.java => RepositoryConfig.java} (59%) diff --git a/backend/src/main/whattocook/configs/RestRepositoryConfigurator.java b/backend/src/main/whattocook/configs/RepositoryConfig.java similarity index 59% rename from backend/src/main/whattocook/configs/RestRepositoryConfigurator.java rename to backend/src/main/whattocook/configs/RepositoryConfig.java index d83568a..0d879c6 100644 --- a/backend/src/main/whattocook/configs/RestRepositoryConfigurator.java +++ b/backend/src/main/whattocook/configs/RepositoryConfig.java @@ -1,17 +1,19 @@ package whattocook.configs; +import org.springframework.context.annotation.Configuration; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer; -import org.springframework.stereotype.Component; import org.springframework.web.servlet.config.annotation.CorsRegistry; -@Component -public class RestRepositoryConfigurator implements RepositoryRestConfigurer { +import whattocook.models.Item; +@Configuration +class RepositoryConfig implements RepositoryRestConfigurer { @Override public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config, CorsRegistry cors) { - config.disableDefaultExposure(); - RepositoryRestConfigurer.super.configureRepositoryRestConfiguration(config, cors); + config.exposeIdsFor(Item.class); + config.setBasePath("/api/v1"); } + } diff --git a/backend/src/main/whattocook/controller/ItemController.java b/backend/src/main/whattocook/controller/ItemController.java index 815f9fb..a106d62 100644 --- a/backend/src/main/whattocook/controller/ItemController.java +++ b/backend/src/main/whattocook/controller/ItemController.java @@ -5,14 +5,11 @@ import lombok.extern.slf4j.Slf4j; import whattocook.models.Item; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import whattocook.models.Unit; import whattocook.services.ItemService; import java.util.List; - @Slf4j @RestController() -@RequestMapping("/api/v1") public class ItemController { @Autowired diff --git a/frontend/src/Api.js b/frontend/src/Api.js index d248e81..12f8a57 100644 --- a/frontend/src/Api.js +++ b/frontend/src/Api.js @@ -10,7 +10,11 @@ const instance = axios.create({ export default { createNew: (name, quantity, unit) => instance.post("/api/v1/items", {name: name, quantity : quantity, unit : unit}), - getAll: () => instance.get('/api/v1/items'), + getAll: () => instance.get('/api/v1/items', { + transformResponse: [function (data) { + return data? JSON.parse(data)._embedded.items : data; + }] + }), removeForId: (id) => instance.delete('/api/v1/items/'+ id) } \ No newline at end of file diff --git a/frontend/src/components/ItemModel.vue b/frontend/src/components/ItemModel.vue index a531b6f..cc7b81a 100644 --- a/frontend/src/components/ItemModel.vue +++ b/frontend/src/components/ItemModel.vue @@ -84,14 +84,14 @@ api.createNew(components[0], parseInt(components[1].replace ( /[^\d.]/g, '' )), - components[1].replace(/[0-9]/g, '')) - .then( (response) => { + components[1].replace(/[0-9]/g, '') === 'ml' ? 'MILLILETERS' : "GRAMMS" + ).then( (response) => { this.$log.debug("New item created:", response); this.items.push({ id: response.data.id, name: components[0], quantity: parseInt(components[1].replace ( /[^\d.]/g, '' )), - unit: components[1].replace(/[0-9]/g, '') + unit: components[1].replace(/[0-9]/g, '') === 'MILLILETERS' ? 'ml' : 'g' }) }).catch((error) => { this.$log.debug(error);