diff --git a/.project b/.project new file mode 100644 index 0000000..468de04 --- /dev/null +++ b/.project @@ -0,0 +1,11 @@ + + + springboot + + + + + + + + diff --git a/configurationapp/.gitignore b/configurationapp/.gitignore new file mode 100644 index 0000000..8bd3a05 --- /dev/null +++ b/configurationapp/.gitignore @@ -0,0 +1,4 @@ +/target/ +/.settings/ +/.classpath +/.project 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..695f43e 100644 --- a/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java +++ b/configurationapp/src/main/java/com/att/dao/configurations/ConfigurationDao.java @@ -1,24 +1,27 @@ package com.att.dao.configurations; import com.att.data.configurations.ConfigValue; -import org.springframework.stereotype.Service; + +import org.springframework.stereotype.Repository; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; -@Service +@Repository public class ConfigurationDao { private class IdProvider { - private int currentId; + // Uses CAS and better than synchronized + private AtomicInteger currentId; public IdProvider() { - currentId = 0; + currentId = new AtomicInteger(0); } public int getNextId() { - return this.currentId++; + return this.currentId.incrementAndGet(); } } @@ -34,15 +37,44 @@ public ConfigurationDao() { } public List getConfigurationsForYearMonth(String yearMonth) { - return new ArrayList<>(); + + List configValues = currentConfigurations.get(yearMonth); + + return (configValues != null ? configValues : new ArrayList<>()); } public void addConfiguration(String yearMonth, ConfigValue value) { + int newId = idProvider.getNextId(); + value.setConfigId(newId); + + // multiple threads can try to access the map at the same time + synchronized(currentConfigurations) + { + if (currentConfigurations.containsKey(yearMonth)) { + currentConfigurations.get(yearMonth).add(value); + } + else { + + List configValues = new ArrayList(); + configValues.add(value); + currentConfigurations.put(yearMonth, configValues); + } + } } public void removeAllConfigurationsForYearMonth(String yearMonth) { + + currentConfigurations.remove(yearMonth); + } + + public void removeSingleConfigurationForYearMonth(String yearMonth, int configId) { + synchronized(currentConfigurations) { + if (currentConfigurations.containsKey(yearMonth)) { + currentConfigurations.get(yearMonth).removeIf(cv -> (cv.getConfigId() == configId)); + } + } } } diff --git a/configurationapp/src/main/java/com/att/service/configurations/ConfigurationService.java b/configurationapp/src/main/java/com/att/service/configurations/ConfigurationService.java new file mode 100644 index 0000000..a9dad89 --- /dev/null +++ b/configurationapp/src/main/java/com/att/service/configurations/ConfigurationService.java @@ -0,0 +1,37 @@ +package com.att.service.configurations; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.att.dao.configurations.ConfigurationDao; +import com.att.data.configurations.ConfigValue; + +@Service +public class ConfigurationService { + + @Autowired + private ConfigurationDao dao; + + public List getConfigurationsForYearMonth(String yearMonth) { + + return dao.getConfigurationsForYearMonth(yearMonth); + } + + public void addConfiguration(String yearMonth, ConfigValue value) { + + dao.addConfiguration(yearMonth, value); + } + + public void removeAllConfigurationsForYearMonth(String yearMonth) { + + dao.removeAllConfigurationsForYearMonth(yearMonth); + } + + public void removeSingleConfigurationForYearMonth(String yearMonth, int configId) { + + dao.removeSingleConfigurationForYearMonth(yearMonth, 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..543d1bc 100644 --- a/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java +++ b/configurationapp/src/main/java/com/att/web/configuarations/ConfigurationController.java @@ -1,45 +1,49 @@ package com.att.web.configuarations; -import com.att.dao.configurations.ConfigurationDao; import com.att.data.configurations.ConfigValue; +import com.att.service.configurations.ConfigurationService; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; import java.util.List; @RestController -@RequestMapping(value="/configuration") +@RequestMapping(value="/configurations") public class ConfigurationController { - private ConfigurationDao dao; - - @Autowired - public ConfigurationController(ConfigurationDao dao) { - this.dao = dao; - } - + @Autowired + private ConfigurationService service; + @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.GET) @ResponseBody public List getConfigurationsForYearMonth( @PathVariable("yearMonthNumber") String yearMonth) { - return new ArrayList<>(); + return service.getConfigurationsForYearMonth(yearMonth); + } + + @RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT}) + public void addConfigurationForYearMonth( + @PathVariable("yearMonthNumber") String yearMonth, + @RequestBody ConfigValue value) { + + service.addConfiguration(yearMonth, value); + } @RequestMapping(value="/{yearMonthNumber}", method=RequestMethod.DELETE) public void deleteConfigurationsForYearMonth(@PathVariable("yearMonthNumber") String yearMonth) { - try { - - } catch (Exception ex) { - - } + + service.removeAllConfigurationsForYearMonth(yearMonth); } - - @RequestMapping(value="/{yearMonthNumber}", method={ RequestMethod.POST, RequestMethod.PUT }) - public void addConfigurationForYearMonth( + + @RequestMapping(value="/{yearMonthNumber}/{configId}", method={ RequestMethod.DELETE}) + public void deleteConfigurationForYearMonth( @PathVariable("yearMonthNumber") String yearMonth, - @RequestBody ConfigValue value) { + @PathVariable("configId") int configId) { + + service.removeSingleConfigurationForYearMonth(yearMonth, configId); } } diff --git a/configurationapp/src/main/resources/static/index.html b/configurationapp/src/main/resources/static/index.html index 7cd14ba..8c66779 100644 --- a/configurationapp/src/main/resources/static/index.html +++ b/configurationapp/src/main/resources/static/index.html @@ -2,10 +2,11 @@ Configuration App - + +

Welcome To The Configuration App

@@ -35,7 +36,9 @@

Welcome To The Configuration App

- + + +