Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;

@Entity(name = "cd_category")
@Data
Expand All @@ -17,4 +14,5 @@ public class Category {
@EqualsAndHashCode.Include
private int id;
private String name;
private int total;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package ru.checkdev.desc.repository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import ru.checkdev.desc.domain.Category;

public interface CategoryRepository extends CrudRepository<Category, Integer> {

Iterable<Category> findAllByOrderByTotalDesc();

@Transactional(isolation = Isolation.REPEATABLE_READ)
@Modifying
@Query("UPDATE cd_category c SET total = total + 1 WHERE c.id = :id")
void updateStatistic(int id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ public void update(Category category) {

public List<Category> getAll() {
var list = new ArrayList<Category>();
categoryRepository.findAll().forEach(list::add);
categoryRepository.findAllByOrderByTotalDesc().forEach(list::add);
return list;
}

public void updateStatistic(int id) {
categoryRepository.updateStatistic(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ public ResponseEntity<Void> delete(@PathVariable int categoryId) {
categoryService.delete(categoryId);
return ResponseEntity.ok().build();
}

@PutMapping("/statistic")
public ResponseEntity<Void> updateStatistic(@RequestBody int categoryId) {
categoryService.updateStatistic(categoryId);
return ResponseEntity.ok().build();
}
}
7 changes: 7 additions & 0 deletions services/desc/src/main/resources/db/changelog/02-update.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
<column name="name" value="Java Base"/>
</insert>
</changeSet>

<changeSet id="cat_update" author="Daniil62">
<addColumn tableName="cd_category">
<column name="total" type="integer" defaultValue="0"/>
</addColumn>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import ru.job4j.site.dto.CategoryDTO;
import ru.job4j.site.service.AuthService;
import ru.job4j.site.service.CategoriesService;
Expand Down Expand Up @@ -38,4 +35,43 @@ public String createCategory(@ModelAttribute CategoryDTO category, HttpServletRe
categoriesService.create(token, category);
return "redirect:/categories/";
}

@GetMapping("/editForm/{id}/{name}")
public String editForm(Model model,
@PathVariable("id") int id,
@PathVariable("name") String name,
HttpServletRequest req) throws JsonProcessingException {
model.addAttribute("category", new CategoryDTO(id, name));
setManage(model, req);
return "editCategoryForm";
}

@PostMapping("/update")
public String updateCategory(Model model,
@ModelAttribute CategoryDTO category,
HttpServletRequest req) throws JsonProcessingException {
var token = (String) req.getSession().getAttribute("token");
categoriesService.update(token, category);
setManage(model, req);
return "redirect:/categories/";
}

@GetMapping("/statistic/{id}")
public String onCategoryClick(Model model,
@PathVariable("id") int id,
HttpServletRequest req) throws JsonProcessingException {
var token = (String) req.getSession().getAttribute("token");
categoriesService.updateStatistic(token, id);

return "empty";
}

private void setManage(Model model, HttpServletRequest req) throws JsonProcessingException {
var token = (String) req.getSession().getAttribute("token");
var userInfo = authService.userInfo(token);
model.addAttribute("userInfo",userInfo);
var canManage = userInfo.getRoles().stream()
.anyMatch(role -> role.getValue().equals("ROLE_ADMIN"));
model.addAttribute("canManage", canManage);
}
}
15 changes: 15 additions & 0 deletions services/site/src/main/java/ru/job4j/site/domain/Category.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ru.job4j.site.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Category {

private int id;
private String name;
private int total;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package ru.job4j.site.dto;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CategoryDTO {
private int id;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import ru.job4j.site.domain.Category;
import ru.job4j.site.dto.CategoryDTO;

import java.util.List;

@Service
public class CategoriesService {
public List<CategoryDTO> getAll(String token) throws JsonProcessingException {
public List<Category> getAll(String token) throws JsonProcessingException {
var text = new RestAuthCall("http://localhost:9902/categories/").get(token);
var mapper = new ObjectMapper();
return mapper.readValue(text, new TypeReference<>(){});
Expand All @@ -25,4 +25,18 @@ public CategoryDTO create(String token, CategoryDTO category) throws JsonProcess
);
return mapper.readValue(out, CategoryDTO.class);
}

public void update(String token, CategoryDTO category) throws JsonProcessingException {
var mapper = new ObjectMapper();
new RestAuthCall("http://localhost:9902/category/").put(
token,
mapper.writeValueAsString(category)
);
}

public void updateStatistic(String token, int categoryId) throws JsonProcessingException {
var mapper = new ObjectMapper();
new RestAuthCall("http://localhost:9902/category/statistic").put(
token, mapper.writeValueAsString(categoryId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public String post(String token, String json) {
url, new HttpEntity<>(json, headers), String.class
).getBody();
}

public void put(String token, String json) {
var restTemplate = new RestTemplate();
var headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer " + token);
restTemplate.put(
url, new HttpEntity<>(json, headers), String.class
);
}
}
18 changes: 15 additions & 3 deletions services/site/src/main/resources/templates/categories.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
<body>
<div th:insert="fragments/header :: header"></div>
<div class="container">
<div class="row pt-2" th:if="${canManage}">
<div class="col-md-12 pull-right">
<div class="row pt-2">
<div th:if="${canManage}" class="col-md-12 pull-right">
<a th:href="@{/category/createForm}" class="btn btn-secondary active" role="button" aria-pressed="true">
Создать направление
</a>
Expand All @@ -35,11 +35,23 @@
<thead>
<tr>
<th scope="col">Направления</th>
<th scope="col">Просмотры</th>
</tr>
</thead>
<tbody>
<tr th:each="category: ${categories}">
<td th:text="${category.name}" />
<td><a th:href="@{|/category/statistic/${category.id}|}" th:text="${category.name}"></a></td>
<td th:text="${category.total}"></td>
<td th:if="${canManage}">
<p>
<a th:href="@{|/category/editForm/${category.id}/${category.name}|}">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
fill="currentColor" class="bi bi-pencil" viewBox="0 0 16 16">
<path d="M12.146.146a.5.5 0 0 1 .708 0l3 3a.5.5 0 0 1 0 .708l-10 10a.5.5 0 0 1-.168.11l-5 2a.5.5 0 0 1-.65-.65l2-5a.5.5 0 0 1 .11-.168l10-10zM11.207 2.5 13.5 4.793 14.793 3.5 12.5 1.207 11.207 2.5zm1.586 3L10.5 3.207 4 9.707V10h.5a.5.5 0 0 1 .5.5v.5h.5a.5.5 0 0 1 .5.5v.5h.293l6.5-6.5zm-9.761 5.175-.106.106-1.528 3.821 3.821-1.528.106-.106A.5.5 0 0 1 5 12.5V12h-.5a.5.5 0 0 1-.5-.5V11h-.5a.5.5 0 0 1-.468-.325z"/>
</svg>
</a>
</p>
</td>
</tr>
</tbody>
</table>
Expand Down
50 changes: 50 additions & 0 deletions services/site/src/main/resources/templates/editCategoryForm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="../static/img/favorite.png">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!--<meta name="viewport" content="width=device-width, initial-scale=1">-->
<meta name="description" content="Обучение и трудоустройство Java-программистов от Junior до Senior">
<meta name="author" content="Job4j.ru">
<title>Check Dev</title>

<!-- Required meta tags -->
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700,400i,900|Open+Sans:300,400,600,700,800&amp;subset=cyrillic"
rel="stylesheet">
<!-- Bootstrap 5.1.3 CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/css/main.min.css}" rel="stylesheet">
<script th:src="@{/js/bootstrap.min.js}"></script>
<script th:src="@{/js/code.jquery.com_jquery-3.7.0.min.js}"></script>
</head>
<body>
<div th:insert="fragments/header :: header"></div>
<div class="container">
<div class="row pt-2" th:if="${canManage}">
<div class="col-sm">
</div>
<div class="col-sm">
<form th:action="@{/category/update/}" th:object="${category}" method="post">
<div class="form-group">
<input type="hidden" name="id" th:field="*{id}"/>
<label for="name">Название направления</label>
<input type="text" class="form-control" id="name"
name="name"
th:field="*{name}"
aria-describedby="emailHelp" placeholder="Название">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Сохранить</button>
</div>
</form>
</div>
<div class="col-sm">
</div>
</div>
</div>
<div th:insert="fragments/footer :: footer"></div>
</body>
</html>
4 changes: 2 additions & 2 deletions services/site/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700,400i,900|Open+Sans:300,400,600,700,800&amp;subset=cyrillic"
rel="stylesheet">
<!-- Bootstrap 3.3.7 CSS -->
<!-- Bootstrap 5.1.3 CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/css/main.min.css}" rel="stylesheet">
<script th:src="@{/js/bootstrap.min.js}"></script>
Expand Down Expand Up @@ -84,7 +84,7 @@ <h2 class="title">
name="password" placeholder="Password">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Войти</button>
<button type="submit" class="btn btn-primary">Войти</button>
</form>
<div id="register-form" class="reg">
<a th:href="@{https://t.me/checkdev_bot?start=true}">Регистрация через телеграм бота</a>
Expand Down
2 changes: 1 addition & 1 deletion services/site/src/main/resources/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700,400i,900|Open+Sans:300,400,600,700,800&amp;subset=cyrillic"
rel="stylesheet">
<!-- Bootstrap 3.3.7 CSS -->
<!-- Bootstrap 5.1.3 CSS -->
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
<link th:href="@{/css/main.min.css}" rel="stylesheet">
<script th:src="@{/js/bootstrap.min.js}"></script>
Expand Down