From a7f7b397ddeac3393628606d254fd0c1fee4c480 Mon Sep 17 00:00:00 2001 From: vn53nec Date: Sat, 11 Mar 2023 23:50:11 -0600 Subject: [PATCH] Added CRUD operations for configurations. --- configurationapp/pom.xml | 9 +- .../dao/configurations/ConfigurationDao.java | 104 +++++++++++++++++- .../att/data/configurations/ConfigValue.java | 32 ++---- .../ConfigurationController.java | 64 ++++++----- .../src/main/resources/static/index.html | 75 +++++++------ .../src/main/resources/static/js/app.js | 70 ++++++++++-- 6 files changed, 253 insertions(+), 101 deletions(-) diff --git a/configurationapp/pom.xml b/configurationapp/pom.xml index 78931d3..5783a40 100644 --- a/configurationapp/pom.xml +++ b/configurationapp/pom.xml @@ -9,7 +9,7 @@ jar configurationapp - + CRUD app for configurations stored based on month and year org.springframework.boot @@ -39,6 +39,13 @@ spring-boot-starter-test test + + + org.projectlombok + lombok + 1.18.26 + provided + diff --git a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java index f889558..ba059ad 100644 --- a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java +++ b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java @@ -10,7 +10,7 @@ @Service public class ConfigurationDao { - private class IdProvider { + private static class IdProvider { private int currentId; public IdProvider() { @@ -26,23 +26,119 @@ public int getNextId() { * No DB, so store the configs in a map. */ private Map> currentConfigurations; - private IdProvider idProvider; + private final IdProvider idProvider; + + private static final String NO_NULLS_MSG = "Null values are not allowed"; + public ConfigurationDao() { idProvider = new IdProvider(); currentConfigurations = new HashMap<>(); + init(); } + /** + * Returns a list of configurations for the selected month + * + */ public List getConfigurationsForYearMonth(String yearMonth) { - return new ArrayList<>(); + if (yearMonth == null) { + throw new IllegalArgumentException(NO_NULLS_MSG); + } + + return currentConfigurations.get(yearMonth); } + /** + * Adds a single configuration, does not check for duplicates. + * + */ public void addConfiguration(String yearMonth, ConfigValue value) { - int newId = idProvider.getNextId(); + if (yearMonth == null || value == null) { + throw new IllegalArgumentException(NO_NULLS_MSG); + } + + if (value.getConfigId() == -1) { + // preserve existing, but assign new if does not exist + value.setConfigId(idProvider.getNextId()); + + } + List list = currentConfigurations.get(yearMonth); + if (list == null) { + list = new ArrayList(); + } + list.add(value); + currentConfigurations.put(yearMonth, list); } + /** + * Removes all the configurations for the selected month + * + */ public void removeAllConfigurationsForYearMonth(String yearMonth) { + if (yearMonth != null) { + currentConfigurations.remove(yearMonth); + } + } + /** + * Removes the specific configurations for the selected month + * + */ + public void removeConfiguration(String yearMonth, int id) { + if (yearMonth != null) { + List list = currentConfigurations.get(yearMonth); + for (ConfigValue c : list) { + if (c.getConfigId() == id) { + list.remove(c); + break; + } + } + } } + + /** + * Removes all the configurations. + */ + public void removeAllConfigurations() { + currentConfigurations = new HashMap<>(); + } + + /** + * Returns a map of all configurations + * + */ + public Map> getCurrentConfigurations() { + // it should be a DB call here, so let's not bother with creating a deep copy + return currentConfigurations; + } + + private void init() { + // should be equivalent to initial database call with a default query + List list1 = new ArrayList<>(); + + list1.add(new ConfigValue("A", idProvider.getNextId())); + list1.add(new ConfigValue("B", idProvider.getNextId())); + list1.add(new ConfigValue("C", idProvider.getNextId())); + list1.add(new ConfigValue("D", idProvider.getNextId())); + currentConfigurations.put("032023", list1); + + List list2 = new ArrayList<>(); + list2.add(new ConfigValue("A", idProvider.getNextId())); + list2.add(new ConfigValue("C", idProvider.getNextId())); + list2.add(new ConfigValue("F", idProvider.getNextId())); + list2.add(new ConfigValue("G", idProvider.getNextId())); + list2.add(new ConfigValue("H", idProvider.getNextId())); + currentConfigurations.put("022023", list2); + + List list3 = new ArrayList<>(); + list3.add(new ConfigValue("A", idProvider.getNextId())); + list3.add(new ConfigValue("B", idProvider.getNextId())); + list3.add(new ConfigValue("C", idProvider.getNextId())); + list3.add(new ConfigValue("D", idProvider.getNextId())); + list3.add(new ConfigValue("E", idProvider.getNextId())); + currentConfigurations.put("012023", list3); + } + } diff --git a/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java b/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java index 55339a3..c3588bc 100644 --- a/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java +++ b/configurationapp/src/main/java/com/att/data/configurations/ConfigValue.java @@ -1,34 +1,16 @@ package com.att.data.configurations; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + /** * Data Model */ +@Data +@AllArgsConstructor +@NoArgsConstructor public class ConfigValue { private String configName; private int configId; - - public ConfigValue(String name, int id) { - this.configId = id; - this.configName = name; - } - - public ConfigValue() { - - } - - public void setConfigName(String name) { - this.configName = name; - } - - public String getConfigName() { - return this.configName; - } - - public void setConfigId(int id) { - this.configId = id; - } - - public int getConfigId() { - return this.configId; - } } diff --git a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java index 995402a..1344575 100644 --- a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java +++ b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java @@ -2,44 +2,58 @@ import com.att.dao.configurations.ConfigurationDao; import com.att.data.configurations.ConfigValue; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; +import java.util.Map; @RestController -@RequestMapping(value="/configuration") +@RequestMapping(value = "/configuration") +@Slf4j public class ConfigurationController { - private ConfigurationDao dao; + private static final String ERROR_OCCURED = "Error occured"; - @Autowired - public ConfigurationController(ConfigurationDao dao) { - this.dao = dao; - } - - @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.GET) - @ResponseBody - public List getConfigurationsForYearMonth( - @PathVariable("yearMonthNumber") String yearMonth) { - - return new ArrayList<>(); - } + private final ConfigurationDao configurationDao; - @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.DELETE) - public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { - try { + @Autowired + public ConfigurationController(ConfigurationDao dao) { + this.configurationDao = dao; + } - } catch (Exception ex) { + @RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.GET) + @ResponseBody + public List getConfigurationsForYearMonth( + @PathVariable("yearMonthNumber") String yearMonth) { + return configurationDao.getConfigurationsForYearMonth(yearMonth); + } - } + @RequestMapping(value = "/{yearMonthNumber}", method = RequestMethod.DELETE) + public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { + try { + configurationDao.removeAllConfigurationsForYearMonth(yearMonth); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); } - - @RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT }) - public void addConfigurationForYearMonth( - @PathVariable("yearMonthNumber") String yearMonth, - @RequestBody ConfigValue value) { - + } + + @RequestMapping(value = "singleConfig/{yearMonthNumber}", method = RequestMethod.DELETE) + public void deleteSingleConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth, @RequestParam("configId") int configId) { + try { + configurationDao.removeConfiguration(yearMonth, configId); + } catch (Exception ex) { + log.error(ERROR_OCCURED, ex); } + } + + @RequestMapping( + value = "/{yearMonthNumber}", + method = {RequestMethod.POST, RequestMethod.PUT}) + public void addConfigurationForYearMonth( + @PathVariable("yearMonthNumber") String yearMonth, @RequestBody ConfigValue value) { + configurationDao.addConfiguration(yearMonth, value); + } } diff --git a/configurationapp/src/main/resources/static/index.html b/configurationapp/src/main/resources/static/index.html index 7cd14ba..37f653b 100644 --- a/configurationapp/src/main/resources/static/index.html +++ b/configurationapp/src/main/resources/static/index.html @@ -2,46 +2,49 @@ Configuration App - + + -

Welcome To The Configuration App

-
-
- -
-
- - - - - - - - - - -
Configuration IdConfiguration Name
-
-
- -
+

Welcome To The Configuration App

+
+
+
- +
+ + + + + + + + + +
Configuration IdConfiguration NameRemove
+
+
+

Add New Configurations

+ + + +
+
+

Delete All Configurations

+ +
+
+ - \ No newline at end of file + diff --git a/configurationapp/src/main/resources/static/js/app.js b/configurationapp/src/main/resources/static/js/app.js index 2d4b945..fd28cbb 100644 --- a/configurationapp/src/main/resources/static/js/app.js +++ b/configurationapp/src/main/resources/static/js/app.js @@ -1,25 +1,75 @@ -(function() { - function App() { +(function () { + function App() {} - } + App.prototype.getData = function () { + $.ajax({ + url: "/configuration/" + $("#timePeriod").val(), + success: function (result) { + $("#configdata").empty(); + var tableContent = ""; + for (var i = 0; i < result.length; i++) { + tableContent += ""; + tableContent += "" + result[i].configId + ""; + tableContent += "" + result[i].configName + ""; + tableContent += "" + '' + ""; + tableContent += ""; + } + $("#configdata").append(tableContent); + }, + }); + }; - App.prototype.getData = function() { - $.ajax('',{ + App.prototype.insertData = function () { + $.ajax({ + headers: { + Accept: "application/json", + "Content-Type": "application/json", + }, + url: "/configuration/" + $("#timePeriod").val(), + dataType: "text", + method: "POST", + data: JSON.stringify({ + configName: $("#configname").val(), + configId: $("#configid").val(), + }), + success: function (data) { + $("#configname").val(""); + $("#configid").val(""); + app.getData(); + }, + }); + }; - }).then(function(data) { + App.prototype.deleteData = function () { + $.ajax({ + url: "/configuration/" + $("#timePeriod").val(), + method: "DELETE", + success: function (data) { + app.getData(); + }, + }); + }; - }) + App.prototype.deleteSpecificData = function (input) { + $.ajax({ + url: "/configuration/singleConfig/" + $("#timePeriod").val() + '?configId=' + input, + method: "DELETE", + success: function (data) { + app.getData(); + }, + }); }; - App.prototype.init = function() { - $('#configTable').DataTable({ + App.prototype.init = function () { + $("#configTable").DataTable({ scrollY: 300, paging: false, sorting: false, searching: false, info: false }); + app.getData(); }; - window.app = new App; + window.app = new App(); })($); \ No newline at end of file