1-db #2

Merged
romanov73 merged 6 commits from 1-db into master 2022-03-09 17:28:19 +04:00
9 changed files with 75 additions and 5 deletions
Showing only changes of commit b7953c4b71 - Show all commits

Binary file not shown.

View File

@ -32,6 +32,12 @@ public class NewsController {
return "editNews"; return "editNews";
} }
@GetMapping("/news/{newsId}")
public String viewNews(@PathVariable(value = "newsId") Integer id, Model model) {
model.addAttribute("news", id != null ? newsService.getById(id) : new News());
return "news";
}
@PostMapping("saveNews") @PostMapping("saveNews")
public String saveNews(@Valid @ModelAttribute News news, BindingResult result) { public String saveNews(@Valid @ModelAttribute News news, BindingResult result) {
if (result.hasErrors()) { if (result.hasErrors()) {
@ -41,4 +47,10 @@ public class NewsController {
return "redirect:/"; return "redirect:/";
} }
@GetMapping("deleteNews/{newsId}")
public String delete(@PathVariable(value = "newsId") Integer id) {
newsService.delete(id);
return "redirect:/";
}
} }

View File

@ -1,5 +1,7 @@
package ru.ulstu.model; package ru.ulstu.model;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Lob; import javax.persistence.Lob;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
@ -12,6 +14,7 @@ public class News extends BaseEntity {
@NotEmpty(message = "Заголовок не может быть пустым") @NotEmpty(message = "Заголовок не может быть пустым")
private String title; private String title;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private Date date; private Date date;
@Lob @Lob

View File

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

View File

@ -23,4 +23,12 @@
.error { .error {
color: red; color: red;
}
.fa {
color: black;
}
.link-dark, .link-dark:visited, .link-dark:focus {
color: black;
} }

View File

@ -17,7 +17,7 @@
<link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/> <link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-select/1.13.8/css/bootstrap-select.min.css"/> <link rel="stylesheet" href="/webjars/bootstrap-select/1.13.8/css/bootstrap-select.min.css"/>
<link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/> <link rel="stylesheet" href="/webjars/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="css/main.css"/> <link rel="stylesheet" href="/css/main.css"/>
</head> </head>
<body> <body>
<nav class="navbar navbar-expand-md navbar-light bg-white"> <nav class="navbar navbar-expand-md navbar-light bg-white">

View File

@ -11,6 +11,9 @@
<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="@{/saveNews}" th:object="${news}" method="post">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{date}">
<input type="hidden" th:field="*{version}">
<div class="form-group"> <div class="form-group">
<label for="title">Заголовок</label> <label for="title">Заголовок</label>
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости"> <input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Заголовок новости">
@ -19,11 +22,13 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="text">Текст новости</label> <label for="text">Текст новости</label>
<textarea class="form-control" id="text" th:field="*{text}" placeholder="Текст новости"></textarea> <textarea class="form-control" id="text" th:field="*{text}" placeholder="Текст новости"
style="height: 300px"></textarea>
<p th:if="${#fields.hasErrors('text')}" th:class="${#fields.hasErrors('text')}? error"> <p th:if="${#fields.hasErrors('text')}" th:class="${#fields.hasErrors('text')}? error">
Не может быть пустым</p> Не может быть пустым</p>
</div> </div>
<button type="submit" class="btn btn-primary">Сохранить</button> <button type="submit" class="btn btn-outline-dark">Сохранить</button>
<a href="javascript:history.back()" class="btn btn-outline-dark">Отмена</a>
</form> </form>
</div> </div>
</html> </html>

View File

@ -12,10 +12,23 @@
<div th:each="n : ${news}" class="news"> <div th:each="n : ${news}" class="news">
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<img class="news-image" src="img/logo.svg"/> <img class="news-image" src="/img/logo.svg"/>
</div> </div>
<div class="col-md-8"> <div class="col-md-8">
<h5 th:text="${n.title}"/> <div class="row">
<div class="col-md-10">
<a th:href="@{'/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
</div>
<div class="col-md-2" style="text-align: right">
<a th:href="@{'/editNews/' + ${n.id}}" class="link-dark">
<i class="fa fa-pencil" aria-hidden="true"></i>
</a>
<a th:href="@{'/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 th:text="${n.preview}" class="news-item"></div>
</div> </div>
</div> </div>

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>