Skip to content

Commit 4c954ed

Browse files
committed
added ability to edit category
1 parent 27357ed commit 4c954ed

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

src/main/java/ru/mrchebik/repository/CategoryRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,8 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
2323

2424
@Query("select category.categoryId, category.name, category.level, category.parentId from ru.mrchebik.model.Category category where category.parentId = :parentId and category.user.userId = :userId")
2525
List<Object[]> findByParentId(@Param("parentId") long parentId, @Param("userId") long userId);
26+
27+
@Modifying
28+
@Query("update ru.mrchebik.model.Category category set category.name = :name where category.categoryId = :categoryId")
29+
void update(@Param("categoryId") long categoryId, @Param("name") String name);
2630
}

src/main/java/ru/mrchebik/service/CategoryService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
public interface CategoryService {
1111
void add(Category category);
12+
void edit(String name, long categoryId);
1213
Category findById(long id);
1314
Category findByParentIdThroughCategoryId(long parentId, long userId);
1415
List<Category> findByParentId(long parentId, long userId);

src/main/java/ru/mrchebik/service/impl/CategoryServiceImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public void add(Category category) {
2626
categoryRepository.saveAndFlush(category);
2727
}
2828

29+
@Override
30+
public void edit(String name, long categoryId) {
31+
categoryRepository.update(categoryId, name);
32+
}
33+
2934
@Override
3035
public Category findById(long id) {
3136
return categoryRepository.findOne(id);

src/main/java/ru/mrchebik/web/CategoryController.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,12 @@ private void removeCategories(Category category, long userId) {
134134
}
135135
}
136136
}
137+
138+
@RequestMapping(value = "/edit", method = GET)
139+
public String removeCategory(@RequestParam String id,
140+
@RequestParam String name) {
141+
categoryService.edit(name, Long.parseLong(id));
142+
143+
return "redirect:/blog/categories/-1";
144+
}
137145
}

src/main/webapp/WEB-INF/views/Category.jsp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
<script type="text/javascript" src="<c:url value="/resources/js/Category.js"/>"></script>
1717
<script type="text/javascript" src="<c:url value="/resources/js/submit.js"/>"></script>
1818
<style>
19-
td:nth-child(3) {
19+
td:nth-child(3), td:nth-child(4) {
2020
width: 3%;
2121
}
22+
23+
#save, .text {
24+
display: none;
25+
}
26+
2227
</style>
2328
</head>
2429
<body>
@@ -74,7 +79,8 @@
7479
<c:forEach items="${categories}" var="category" >
7580
<tr>
7681
<td>${categories.indexOf(category) + 1}</td>
77-
<td onclick="window.location.href='/blog/categories/${category.categoryId}'">${category.name}</td>
82+
<td><span id="cat${category.categoryId}" class="fake-link" onclick="window.location.href='/blog/categories/${category.categoryId}'">${category.name}</span><input id="text${category.categoryId}" class="text" type="text" placeholder="Name" value="${category.name}" oninput="checkText(this)"></td>
83+
<td><span id="edit" class="fake-link" onclick="edit(${category.categoryId})">Edit</span><span id="save" class="fake-link" onclick="saveData(${category.categoryId})">Save</span></td>
7884
<td><span class="fake-link" onclick="window.location.href='/blog/categories/delete?id=${category.categoryId}'">Remove</span></td>
7985
</tr>
8086
</c:forEach>

src/main/webapp/resources/js/Title.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,4 @@ function checkSelects(sel, i, maxLevel) {
6161
nextBox.style.display = 'none';
6262
}
6363
}
64-
}
64+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
/**
22
* Created by mrchebik on 24.01.17.
33
*/
4+
var error = 1;
5+
46
function submitData(id, elementId) {
57
document.getElementById(elementId).value = id;
68
document.getElementById("form2").submit();
9+
}
10+
11+
function saveData(id) {
12+
if (error == 0) {
13+
window.location.href='/blog/categories/edit?id=' + id + '&name=' + document.getElementById('text' + id).value;
14+
} else {
15+
alert('Error, check name of category');
16+
}
17+
}
18+
19+
function edit(i) {
20+
document.getElementById('edit').style.display = 'none';
21+
document.getElementById('save').style.display = 'block';
22+
document.getElementById('cat' + i).style.display = 'none';
23+
document.getElementById('text' + i).style.display = 'block';
24+
}
25+
26+
function checkText(i) {
27+
if (i.value.length < 1 || i.value.length > 50) {
28+
i.style.borderColor = 'red';
29+
error = 1;
30+
} else {
31+
i.style.borderColor = "black";
32+
error = 0;
33+
}
734
}

0 commit comments

Comments
 (0)