#3 -- fix routing

This commit is contained in:
Anton Romanov 2022-03-11 09:10:48 +04:00
parent 2ac1b3f3c1
commit 76ab0ddf11
12 changed files with 79 additions and 24 deletions

Binary file not shown.

View File

@ -45,7 +45,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
log.debug("Security enabled"); log.debug("Security enabled");
http.authorizeRequests() http.authorizeRequests()
.antMatchers("/").permitAll() .antMatchers("/").permitAll()
.antMatchers("/login", "/index", "/news/*", "/h2-console/*", "/h2-console").permitAll() .antMatchers("/login", "/index", "/news/**", "/h2-console/*", "/h2-console").permitAll()
.antMatchers("/swagger-ui.html").hasAuthority(UserRoleConstants.ADMIN) .antMatchers("/swagger-ui.html").hasAuthority(UserRoleConstants.ADMIN)
.anyRequest().authenticated() .anyRequest().authenticated()
.and() .and()

View File

@ -22,7 +22,7 @@ public class IndexController {
@GetMapping("/") @GetMapping("/")
public String index(Model model) { public String index(Model model) {
model.addAttribute("news", newsService.getAll()); model.addAttribute("news", newsService.getLast());
return "index"; return "index";
} }
} }

View File

@ -13,12 +13,14 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.model.News; import ru.ulstu.model.News;
import ru.ulstu.service.NewsService; import ru.ulstu.service.NewsService;
import javax.validation.Valid; import javax.validation.Valid;
@Controller @Controller
@RequestMapping("news")
public class NewsController { public class NewsController {
private final NewsService newsService; private final NewsService newsService;
@ -26,6 +28,12 @@ public class NewsController {
this.newsService = newsService; this.newsService = newsService;
} }
@GetMapping("/news")
public String index(Model model) {
model.addAttribute("news", newsService.getAll());
return "news";
}
@GetMapping("/editNews/{newsId}") @GetMapping("/editNews/{newsId}")
public String editNews(@PathVariable(value = "newsId") Integer id, Model model) { public String editNews(@PathVariable(value = "newsId") Integer id, Model model) {
model.addAttribute("news", (id != null && id != 0) ? newsService.getById(id) : new News()); model.addAttribute("news", (id != null && id != 0) ? newsService.getById(id) : new News());
@ -35,7 +43,7 @@ public class NewsController {
@GetMapping("/news/{newsId}") @GetMapping("/news/{newsId}")
public String viewNews(@PathVariable(value = "newsId") Integer id, Model model) { public String viewNews(@PathVariable(value = "newsId") Integer id, Model model) {
model.addAttribute("news", id != null ? newsService.getById(id) : new News()); model.addAttribute("news", id != null ? newsService.getById(id) : new News());
return "news"; return "viewNews";
} }
@PostMapping("saveNews") @PostMapping("saveNews")
@ -45,12 +53,12 @@ public class NewsController {
} }
newsService.save(news); newsService.save(news);
return "redirect:/"; return "redirect:/news/news";
} }
@GetMapping("deleteNews/{newsId}") @GetMapping("deleteNews/{newsId}")
public String delete(@PathVariable(value = "newsId") Integer id) { public String delete(@PathVariable(value = "newsId") Integer id) {
newsService.delete(id); newsService.delete(id);
return "redirect:/"; return "redirect:/news/news";
} }
} }

View File

@ -3,5 +3,8 @@ package ru.ulstu.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.model.News; import ru.ulstu.model.News;
import java.util.List;
public interface NewsRepository extends JpaRepository<News, Integer> { public interface NewsRepository extends JpaRepository<News, Integer> {
List<News> findFirst3ByOrderByDateDesc();
} }

View File

@ -46,4 +46,8 @@ public class NewsService {
public void delete(Integer id) { public void delete(Integer id) {
newsRepository.deleteById(id); newsRepository.deleteById(id);
} }
public List<News> getLast() {
return newsRepository.findFirst3ByOrderByDateDesc();
}
} }

View File

@ -29,6 +29,6 @@
color: black; color: black;
} }
.link-dark, .link-dark:visited, .link-dark:focus { .link-dark, .link-dark:visited, .link-dark:focus, .link-dark:any-link {
color: black; color: black;
} }

View File

@ -33,7 +33,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav mr-auto">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/news">Новости</a> <a class="nav-link" href="/news/news">Новости</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/news">Заседания</a> <a class="nav-link" href="/news">Заседания</a>

View File

@ -10,7 +10,7 @@
layout:decorate="~{default}"> layout:decorate="~{default}">
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<h3>Редактирование новости:</h3> <h3>Редактирование новости:</h3>
<form action="#" th:action="@{/saveNews}" th:object="${news}" method="post"> <form action="#" th:action="@{/news/saveNews}" th:object="${news}" method="post">
<input type="hidden" th:field="*{id}"> <input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{date}"> <input type="hidden" th:field="*{date}">
<input type="hidden" th:field="*{version}"> <input type="hidden" th:field="*{version}">

View File

@ -17,13 +17,13 @@
<div class="col-md-8"> <div class="col-md-8">
<div class="row"> <div class="row">
<div class="col-md-10"> <div class="col-md-10">
<a th:href="@{'/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a> <a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
</div> </div>
<div sec:authorize="hasRole('ROLE_ADMIN')" class="col-md-2" style="text-align: right"> <div sec:authorize="hasRole('ROLE_ADMIN')" class="col-md-2" style="text-align: right">
<a th:href="@{'/editNews/' + ${n.id}}" class="link-dark"> <a th:href="@{'/news/editNews/' + ${n.id}}" class="link-dark">
<i class="fa fa-pencil" aria-hidden="true"></i> <i class="fa fa-pencil" aria-hidden="true"></i>
</a> </a>
<a th:href="@{'/deleteNews/' + ${n.id}}" class="link-dark" <a th:href="@{'/news/deleteNews/' + ${n.id}}" class="link-dark"
onclick="return confirm('Удалить новость?')"> onclick="return confirm('Удалить новость?')">
<i class="fa fa-trash" aria-hidden="true"></i> <i class="fa fa-trash" aria-hidden="true"></i>
</a> </a>

View File

@ -5,21 +5,36 @@
--> -->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd"> <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html <html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
layout:decorate="~{default}"> layout:decorate="~{default}">
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<div class="row"> <div th:each="n : ${news}" class="news">
<div class="col-md-4"> <div class="row">
<img class="news-image" src="/img/logo.svg"/> <div class="col-md-4">
</div> <img class="news-image" src="/img/logo.svg"/>
<div class="col-md-8"> </div>
<h5 th:text="${news.title}"/> <div class="col-md-8">
<div th:text="${news.text}" class="news-item"></div> <div class="row">
<div class="col-md-10">
<a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
</div>
<div sec:authorize="hasRole('ROLE_ADMIN')" class="col-md-2" style="text-align: right">
<a th:href="@{'/news/editNews/' + ${n.id}}" class="link-dark">
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
<a th:href="@{'/news/deleteNews/' + ${n.id}}" class="link-dark"
onclick="return confirm('Удалить новость?')">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>
</div>
</div>
<div th:text="${n.preview}" class="news-item"></div>
</div>
</div> </div>
<div th:text="${'Опубликовано: ' + #calendars.format(n.date, 'dd.MM.yyyy HH:mm')}"
class="news-date"></div>
<hr/>
</div> </div>
<div th:text="${'Опубликовано: ' + #calendars.format(news.date, 'dd.MM.yyyy HH:mm')}"
class="news-date"></div>
<a href="javascript:history.back()" class="btn btn-outline-dark" style="text-align: right">Назад</a>
</div> </div>
</html> </html>

View File

@ -0,0 +1,25 @@
<!--
~ Copyright (C) 2021 Anton Romanov - All Rights Reserved
~ You may use, distribute and modify this code, please write to: romanov73@gmail.com.
~
-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<div class="row">
<div class="col-md-4">
<img class="news-image" src="/img/logo.svg"/>
</div>
<div class="col-md-8">
<h5 th:text="${news.title}"/>
<div th:text="${news.text}" class="news-item"></div>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(news.date, 'dd.MM.yyyy HH:mm')}"
class="news-date"></div>
<a href="javascript:history.back()" class="btn btn-outline-dark" style="text-align: right">Назад</a>
</div>
</html>