Skip to content
Open
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
9 changes: 8 additions & 1 deletion configurationapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>jar</packaging>

<name>configurationapp</name>
<description></description>
<description>CRUD app for configurations stored based on month and year</description>

<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -39,6 +39,13 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@Service
public class ConfigurationDao {
private class IdProvider {
private static class IdProvider {
private int currentId;

public IdProvider() {
Expand All @@ -26,23 +26,119 @@ public int getNextId() {
* No DB, so store the configs in a map.
*/
private Map<String, List<ConfigValue>> 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<ConfigValue> 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<ConfigValue> list = currentConfigurations.get(yearMonth);
if (list == null) {
list = new ArrayList<ConfigValue>();
}
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<ConfigValue> 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<String, List<ConfigValue>> 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<ConfigValue> 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<ConfigValue> 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<ConfigValue> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConfigValue> 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<ConfigValue> 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);
}
}
75 changes: 39 additions & 36 deletions configurationapp/src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,49 @@
<html>
<head>
<title>Configuration App</title>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="js/app.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="css/main.css" />
</head>
<body>
<h1>Welcome To The Configuration App</h1>
<div class="content">
<div class="controls">
<select id="timePeriod">
<option value="012018">Jan 2018</option>
<option value="022018">Feb 2018</option>
<option value="032018">Mar 2018</option>
<option value="042018">Apr 2018</option>
<option value="052018">May 2018</option>
<option value="062018">Jun 2018</option>
<option value="072018">Jul 2018</option>
</select>
</div>
<div class="config-table-wrapper">
<table id="configTable">
<thead>
<tr>
<td>Configuration Id</td>
<td>Configuration Name</td>
</tr>
</thead>
<tbody>

</tbody>
</table>
</div>
<div class="controls">
<button id="saveButton">Save Configurations</button>
</div>
<h1>Welcome To The Configuration App</h1>
<div class="content">
<div class="controls">
<select id="timePeriod" onchange="app.getData();">
<option value="012023">Jan 2023</option>
<option value="022023">Feb 2023</option>
<option value="032023">Mar 2023</option>
</select>
</div>
<script>
$(document).ready(function() {
app.init();
})
</script>
<div class="config-table-wrapper">
<table id="configTable">
<thead>
<tr>
<td>Configuration Id</td>
<td>Configuration Name</td>
<td>Remove</td>
</tr>
</thead>
<tbody id="configdata"></tbody>
</table>
</div>
<div class="controls">
<h1>Add New Configurations</h1>
<input type="text" id="configid" name="addedConfigId" placeholder="enter id" />
<input type="text" id="configname" name="addedConfigName" placeholder="enter name" />
<button id="saveButton" onclick="app.insertData()">Save Configurations</button>
</div>
<div class="controls">
<h1>Delete All Configurations</h1>
<button id="deleteButton" onclick="app.deleteData()">Delete Configurations</button>
</div>
</div>
<script>
$(document).ready(function () {
app.init();
});
</script>
</body>
</html>
</html>
Loading