Skip to content

Commit

Permalink
Refactor cleanup function and add error handling in build_and_run_app…
Browse files Browse the repository at this point in the history
….sh and build_and_run_app_in_container.sh scripts. Update CSS styles in new_form.html and edit_form.html templates. Add search functionality in AppController.java.
  • Loading branch information
tsviz committed Jan 25, 2024
1 parent feec3d9 commit f204218
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 160 deletions.
7 changes: 4 additions & 3 deletions build_and_run_app.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# !/bin/bash
cleanup() {
docker stop redis_container > /dev/null 2>&1
docker rm redis_container > /dev/null 2>&1
rm *db > /dev/null 2>&1
docker stop redis_container > /dev/null 2>&1 || true
docker rm redis_container > /dev/null 2>&1 || true
rm *db > /dev/null 2>&1 || true
}

# Trap the EXIT signal to perform cleanup
trap cleanup EXIT

set -e # Exit immediately if a command exits with a non-zero status.
mvn clean package -Dmaven.test.skip=true
docker run -d -p 6379:6379 --name redis_container redis
java -jar target/salesmanager-*-SNAPSHOT.jar --spring.redis.host=localhost --spring.redis.port=6379 --spring.redis.mode=standalone --server.port=8086 --spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
17 changes: 9 additions & 8 deletions build_and_run_app_in_container.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,24 @@
cleanup() {
echo "Performing cleanup..."
# Stop and remove the app container if it exists
docker stop app-test
docker rm app-test
docker stop app-test || true
docker rm app-test || true
# Stop and remove the postgresql container if it exists
docker stop postgres_container
docker rm postgres_container
docker stop postgres_container || true
docker rm postgres_container || true
# Remove image if exists
docker rmi myapp-img:v1
docker rmi myapp-img:v1 || true
# Remove volume if exists
docker volume rm postgresql-data
docker volume rm postgresql-data || true
# Stop and remove the redis container if it exists
docker stop redis_container
docker rm redis_container
docker stop redis_container || true
docker rm redis_container || true
}

# Trap the EXIT signal to perform cleanup
trap cleanup EXIT

set -e # Exit immediately if a command exits with a non-zero status.
# Getting local ip address from ifconfig
LOCAL_IP=$(ifconfig | grep 'inet ' | grep -Fv 127.0.0.1 | awk '{print $2}')
# Run a postgresql container with a volume to persist data
Expand Down
46 changes: 25 additions & 21 deletions src/main/java/net/codejava/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,32 @@ public String viewHomePage(Model model , Principal principal, @RequestParam(defa
// }

@RequestMapping("/new")
public String showNewForm(Model model) {
public ModelAndView showNewForm() {
ModelAndView mav = new ModelAndView("new_form");
Sale sale = new Sale();
model.addAttribute("sale", sale);

return "new_form";
mav.addObject("sale", sale);
mav.addObject("enableSearchFeature", enableSearchFeature);
return mav;
}

@RequestMapping("/edit/{id}")
public ModelAndView showEditForm(@PathVariable(name = "id") int id) {
ModelAndView mav = new ModelAndView("edit_form");
Sale sale = dao.get(id);
mav.addObject("sale", sale);
mav.addObject("enableSearchFeature", enableSearchFeature);
return mav;
}

@RequestMapping("/search")
public String search(@ModelAttribute("q") String query, Model model) {
List<Sale> listSale = dao.search(query);
model.addAttribute("listSale", listSale);

boolean enableSearchFeature = true; // or get this value from somewhere else
model.addAttribute("enableSearchFeature", enableSearchFeature);

return "search";
}

@RequestMapping(value = "/save", method = RequestMethod.POST)
Expand Down Expand Up @@ -117,15 +138,6 @@ public String loginPost(HttpServletRequest request, Model model) {
return "redirect:/";
}

@RequestMapping("/edit/{id}")
public ModelAndView showEditForm(@PathVariable(name = "id") int id) {
ModelAndView mav = new ModelAndView("edit_form");
Sale sale = dao.get(id);
mav.addObject("sale", sale);

return mav;
}

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(@ModelAttribute("sale") Sale sale) {
dao.update(sale);
Expand All @@ -145,14 +157,6 @@ public String clearRecord(@PathVariable(name = "id") int id) {
return "redirect:/";
}

// method getmapping for search functionality
@RequestMapping("/search")
public String search(@ModelAttribute("q") String query, Model model) {
List<Sale> listSale = dao.search(query);
model.addAttribute("listSale", listSale);
return "search";
}

@RequestMapping("/export")
public String exportCSV(@ModelAttribute("q") String query, Model model) {
try {
Expand Down
94 changes: 94 additions & 0 deletions src/main/resources/static/js/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
var h1Color, thBgColor, aColor, trBgColor;
var toggleButton = document.getElementById('toggle');

if (window.enableSearchFeature) {
h1Color = '#2196F3';
thBgColor = '#2196F3';
aColor = '#2196F3';
trBgColor = '#c2e0fb';
} else {
h1Color = '#4CAF50';
thBgColor = '#4CAF50';
aColor = '#4CAF50';
trBgColor = '#fbfde3';
}

document.write('<style>');
document.write(`
table {
border-collapse: collapse;
width: 100%;
color: #444; /* Add color to the text in the table */
}
h1 {
color: ${h1Color};
text-shadow: 2px 2px #ccc;
text-align: center;
margin-top: 50px;
}
th {
background-color: ${thBgColor};
color: #fff;
padding: 10px;
}
td {
padding: 10px;
}
a {
text-decoration: none;
color: ${aColor};
padding: 5px 10px;
border-radius: 5px;
border: 1px solid ${aColor};
box-shadow: 3px 3px 5px rgba(0, 0, 0, 0.3); // Add a box shadow
transition: all 0.3s ease;
}
a:hover {
color: #fff;
background-color: ${aColor};
box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.3); // Reduce the box shadow on hover
}
tr:nth-child(even) {
background-color: ${trBgColor};
}
@keyframes rowFadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
tr {
animation: rowFadeIn 1s ease;
}
th, td {
border: 1px solid #ddd; // Add a border
padding: 10px;
}
table {
border-collapse: collapse; // Collapse the borders
width: 100%; // Make the table full width
}
tr:hover {
box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.2); // Increase the spread radius and opacity
transition: box-shadow 0.3s ease;
}
.main-table {
width: 100%; // Set the width of the main table to take up the full width
max-height: 6em; // Set a maximum height for the main table to match the height of 5 rows
overflow-y: auto; // Add a scrollbar if the content exceeds the maximum height
order: 1; // Make the main table appear first
}
.sales-table {
width: 40%; // Set the width of the sales table
max-height: 6em; // Set a maximum height for the sales table
overflow-y: auto; // Add a scrollbar if the content exceeds the maximum height
margin-top: 20px; // Add some margin to separate the sales table from the main table
order: 2; // Make the sales table appear second
}
.hide {
display: none;
}
`);
document.write('</style>');
38 changes: 12 additions & 26 deletions src/main/resources/templates/edit_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,21 @@
body {
background-color: #f2f2f2;
font-family: Arial, sans-serif;
}
h1 {
color: #4CAF50; /* Changed from #2196F3 to #4CAF50 for green */
text-shadow: 2px 2px #ccc;
text-align: center;
margin-top: 50px;
padding: 0 15px;
max-width: 1200px;
margin: auto;
}
table {
border-collapse: collapse;
width: 100%;
margin-top: 50px;
box-shadow: 2px 2px 5px #ccc;
box-shadow: 0px 0px 20px rgba(0,0,0,0.15);
border-radius: 10px;
overflow: hidden;
}
th, td {
text-align: left;
padding: 8px;
border: 1px solid #ccc;
}
th {
background-color: #4CAF50; /* Changed from #2196F3 to #4CAF50 for green */
color: #fff;
}
tr:nth-child(even) {
background-color: #C8E6C9; /* Changed from #E3F2FD to #C8E6C9 for green */
}
a {
text-decoration: none;
color: #4CAF50; /* Changed from #2196F3 to #4CAF50 for green */
padding: 5px 10px;
border-radius: 5px;
border: 1px solid #4CAF50; /* Changed from #2196F3 to #4CAF50 for green */
}
a:hover {
color: #fff;
background-color: #388E3C; /* Changed from #3a81e5 to #388E3C for green */
}
.actions {
display: flex;
Expand All @@ -53,6 +33,12 @@
color: #f44336;
}
</style>
<script th:inline="javascript">
/*<![CDATA[*/
window.enableSearchFeature = /*[[${enableSearchFeature}]]*/ false;
/*]]>*/
</script>
<script src="/js/styles.js"></script>
</head>
<body>
<h1>Edit Sale</h1>
Expand Down
50 changes: 23 additions & 27 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
max-width: 1200px;
margin: auto;
}
h1 {
color: #4CAF50; /* Changed from #2196F3 to #4CAF50 for green */
text-shadow: 2px 2px #ccc;
text-align: center;
margin-top: 50px;
}
table {
border-collapse: collapse;
width: 100%;
Expand All @@ -29,24 +23,6 @@
text-align: left;
padding: 8px;
}
th {
background-color: #4CAF50;
color: #fff;
}
tr:nth-child(even) {
background-color: #fbfde3;
}
a {
text-decoration: none;
color: #4CAF50;
padding: 5px 10px;
border-radius: 5px;
border: 1px solid #4CAF50;
}
a:hover {
color: #fff;
background-color: #4CAF50;
}
.actions {
display: flex;
justify-content: space-between;
Expand All @@ -58,16 +34,17 @@
</style>
<script th:inline="javascript">
/*<![CDATA[*/
var enableSearchFeature = /*[[${enableSearchFeature}]]*/ false;
window.enableSearchFeature = /*[[${enableSearchFeature}]]*/ 'default';
/*]]>*/
</script>
<script>
window.onload = function() {
if (!enableSearchFeature) {
if (!window.enableSearchFeature) {
document.getElementById('searchFeature').style.display = 'none';
}
};
</script>
<script src="js/styles.js"></script>
</head>
<body>
<div align="center" th:if="${listSale}">
Expand All @@ -93,7 +70,26 @@ <h1>Sales Records</h1>
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
<input type="submit" value="Logout" />
</form>
<table style="width: 35%" border="1" cellpadding="10">
<br />
<div class="sales-table">
<table style="width: 100%" border="1" cellpadding="10">
<thead>
<tr>
<th>Item ID</th>
<th>Total Sales</th>
</tr>
</thead>
<tbody>
<tr th:each="sale : ${listSale}">
<td th:text="${sale.item}">Item ID</td>
<td th:text="${'$' + sale.amount * sale.quantity}">Total Sales</td>
</tr>
</tbody>
</table>
</div>
<div class="main-table">
<br />
<table style="width: 35%" border="1" cellpadding="10" class="main-table">
<thead>
<tr>
<th>ID</th>
Expand Down
Loading

0 comments on commit f204218

Please sign in to comment.