#7 -- link with news, add date

This commit is contained in:
Anton Romanov 2022-03-17 16:18:21 +04:00
parent 79e7483254
commit 812b9c435d
12 changed files with 130 additions and 43 deletions

View File

@ -36,17 +36,20 @@ dependencies {
implementation group: 'nz.net.ultraq.thymeleaf', name: 'thymeleaf-layout-dialect', version: '3.1.0'
implementation group: 'org.thymeleaf.extras', name: 'thymeleaf-extras-springsecurity5'
implementation group: 'com.h2database', name:'h2'
implementation group: 'javax.xml.bind', name:'jaxb-api'
implementation group: 'org.javassist', name:'javassist'
implementation group: 'javax.xml.bind', name: 'jaxb-api'
implementation group: 'org.javassist', name: 'javassist'
implementation group: 'org.eclipse.jetty', name: 'jetty-servlet', version: versionJetty
implementation group: 'org.eclipse.jetty', name: 'jetty-servlet', version: versionJetty
implementation group: 'org.webjars', name: 'jquery', version: '3.6.0'
implementation group: 'org.webjars', name: 'bootstrap', version: '4.6.0'
implementation group: 'org.webjars', name: 'bootstrap', version: '4.3.0'
implementation group: 'org.webjars', name: 'bootstrap-select', version: '1.13.8'
implementation group: 'org.webjars', name: 'bootstrap-datetimepicker', version: '2.4.4'
implementation group: 'org.webjars', name: 'font-awesome', version: '4.7.0'
implementation group: 'org.webjars', name: 'momentjs', version: '2.24.0'
implementation group: 'org.webjars', name: 'bootstrap-glyphicons', version: 'bdd2cbfba0'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

Binary file not shown.

View File

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

View File

@ -2,9 +2,14 @@ package ru.ulstu.meeting;
import org.springframework.format.annotation.DateTimeFormat;
import ru.ulstu.model.BaseEntity;
import ru.ulstu.news.News;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
@ -13,13 +18,20 @@ public class Meeting extends BaseEntity {
@NotEmpty(message = "Заголовок не может быть пустым")
private String title;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
@DateTimeFormat(pattern = "dd.MM.yyyy HH:mm")
private Date date;
@Lob
@NotEmpty(message = "Текст заседания не может быть пустым")
private String text;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "news_id", referencedColumnName = "id")
private News news;
@Transient
private Integer newsId;
public Meeting() {
}
@ -52,4 +64,22 @@ public class Meeting extends BaseEntity {
public String getText() {
return text;
}
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public Integer getNewsId() {
return newsId == null
? (news != null) ? news.getId() : null
: newsId;
}
public void setNewsId(Integer newsId) {
this.newsId = newsId;
}
}

View File

@ -3,8 +3,10 @@ package ru.ulstu.meeting;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.ulstu.news.News;
import ru.ulstu.news.NewsService;
import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;
@Service
@ -18,17 +20,21 @@ public class MeetingService {
this.newsService = newsService;
}
public void create(Meeting meeting) {
meetingRepository.save(meeting);
newsService.create("Очередное заседание семинара", "Заседание семинара состоится " + meeting.getDate());
@Transactional
public Meeting create(Meeting meeting) {
meeting = meetingRepository.save(meeting);
News news = newsService.create("Объявление о заседании семинара", meeting);
meeting.setNews(news);
return save(meeting);
}
public void save(Meeting meeting) {
if (meeting.getId() != null && (meeting.getId() != 0)) {
meetingRepository.save(meeting);
} else {
create(meeting);
public Meeting save(Meeting meeting) {
if (meeting.getNewsId() != null) {
meeting.setNews(newsService.getById(meeting.getNewsId()));
}
return (meeting.getId() != null && (meeting.getId() != 0))
? meetingRepository.save(meeting)
: create(meeting);
}
public Meeting getById(@NotNull Integer id) {
@ -37,7 +43,12 @@ public class MeetingService {
.orElseThrow(() -> new RuntimeException("Запись о заседании не найдена"));
}
@Transactional
public void delete(Integer id) {
Meeting meeting = meetingRepository.getById(id);
if (meeting.getNews() != null) {
newsService.delete(meeting.getNews().getId());
}
meetingRepository.deleteById(id);
}

View File

@ -2,10 +2,12 @@ package ru.ulstu.news;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.meeting.Meeting;
import ru.ulstu.model.BaseEntity;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.OneToOne;
import javax.persistence.Transient;
import javax.validation.constraints.NotEmpty;
import java.util.Date;
@ -29,6 +31,9 @@ public class News extends BaseEntity {
@Transient
private MultipartFile imageFile;
@OneToOne(mappedBy = "news")
private Meeting meeting;
public News() {
}
@ -78,6 +83,14 @@ public class News extends BaseEntity {
this.imageFile = imageFile;
}
public Meeting getMeeting() {
return meeting;
}
public void setMeeting(Meeting meeting) {
this.meeting = meeting;
}
public String getPreview() {
return text != null && text.length() > MAX_NEWS_TEXT_PREVIEW_LENGTH
? text.substring(0, MAX_NEWS_TEXT_PREVIEW_LENGTH) + "..."

View File

@ -5,7 +5,9 @@ import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import ru.ulstu.files.FileSystemStorageService;
import ru.ulstu.files.FileUtil;
import ru.ulstu.meeting.Meeting;
import javax.transaction.Transactional;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Date;
@ -20,12 +22,19 @@ public class NewsService {
}
public void create(String title, String text) {
newsRepository.save(new News(title, new Date(), text));
create(new News(title, new Date(), text));
}
public void create(News news) {
@Transactional
public News create(String title, Meeting meeting) {
News news = new News(title, new Date(), "Новость о заседении семинара");
news.setMeeting(meeting);
return create(news);
}
public News create(News news) {
news.setDate(new Date());
newsRepository.save(news);
return newsRepository.save(news);
}
public void save(News news) throws IOException {

View File

@ -1,9 +1,3 @@
<!--
~ Copyright (C) 2022 Anton Romanov - All Rights Reserved
~ You may use, distribute and modify this code, please write to: romanov73@gmail.com.
~
-->
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"
@ -13,11 +7,17 @@
<title th:text="#{messages.app-name}">app-name</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script type="text/javascript" src="/webjars/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/4.6.0/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/webjars/momentjs/2.24.0/moment.js"></script>
<script type="text/javascript" src="/webjars/momentjs/2.24.0/locale/ru.js"></script>
<script type="text/javascript" src="/webjars/bootstrap/4.3.0/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/webjars/bootstrap-select/1.13.8/js/bootstrap-select.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/4.6.0/css/bootstrap.min.css"/>
<script type="text/javascript"
src="/webjars/bootstrap-datetimepicker/2.4.4/js/bootstrap-datetimepicker.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/4.3.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/font-awesome/4.7.0/css/font-awesome.min.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-glyphicons/bdd2cbfba0/css/bootstrap-glyphicons.css"/>
<link rel="stylesheet" href="/webjars/bootstrap-datetimepicker/2.4.4/css/bootstrap-datetimepicker.css"/>
<link rel="stylesheet" href="/css/main.css"/>
</head>
<body>

View File

@ -1,9 +1,3 @@
<!--
~ 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"
@ -13,6 +7,7 @@
<form action="#" th:action="@{/meetings/saveMeeting}" th:object="${meeting}" method="post">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{version}">
<input type="hidden" th:field="*{newsId}">
<div class="form-group">
<label for="title">Тема заседания</label>
<input type="text" class="form-control" id="title" th:field="*{title}" placeholder="Тема заседания">
@ -26,8 +21,22 @@
<p th:if="${#fields.hasErrors('text')}" th:class="${#fields.hasErrors('text')}? error">
Не может быть пустым</p>
</div>
<div class="form-group">
<label for="date">Дата заседания</label>
<input type="text" id="date" class="form-control" data-target="#date" th:field="*{date}"/>
<p th:if="${#fields.hasErrors('date')}" th:class="${#fields.hasErrors('date')}? error">
Не может быть пустым</p>
</div>
<button type="submit" class="btn btn-outline-dark">Сохранить</button>
<a href="javascript:history.back()" class="btn btn-outline-dark">Отмена</a>
</form>
<script>
$(function () {
$("#date").datetimepicker({
format: 'dd.mm.yyyy hh:ii',
locale: 'ru'
});
});
</script>
</div>
</html>

View File

@ -16,10 +16,19 @@
<div class="col-md-8">
<div class="row">
<div class="col-md-10">
<a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
<a th:if="${n.meeting == null}" th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5
th:text="${n.title}"/></a>
<a th:if="${n.meeting != null}" th:href="@{'/meetings/meetings/' + ${n.meeting.id}}"
class="link-dark"><h5 th:text="${n.title}"/></a>
</div>
</div>
<div th:text="${n.preview}" class="news-item"></div>
<div th:if="${n.meeting == null}" th:text="${n.preview}" class="news-item"></div>
<div th:if="${n.meeting != null}" th:text="${'Тема заседания: ' + n.meeting.title}"
class="news-item"></div>
<div th:if="${n.meeting != null}"
th:text="${'Дата: ' + #calendars.format(n.meeting.date, 'dd.MM.yyyy HH:mm')}"
class="news-item"></div>
<div th:if="${n.meeting != null}" th:text="${n.meeting.text}" class="news-item"></div>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(n.date, 'dd.MM.yyyy HH:mm')}"

View File

@ -1,9 +1,3 @@
<!--
~ 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"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"

View File

@ -17,10 +17,13 @@
<div class="col-md-8">
<div class="row">
<div class="col-md-10">
<a th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5 th:text="${n.title}"/></a>
<a th:if="${n.meeting == null}" th:href="@{'/news/news/' + ${n.id}}" class="link-dark"><h5
th:text="${n.title}"/></a>
<a th:if="${n.meeting != null}" th:href="@{'/meetings/meetings/' + ${n.meeting.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">
<a th:if="${n.meeting == null}" 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"
@ -29,7 +32,13 @@
</a>
</div>
</div>
<div th:text="${n.preview}" class="news-item"></div>
<div th:if="${n.meeting == null}" th:text="${n.preview}" class="news-item"></div>
<div th:if="${n.meeting != null}" th:text="${'Тема заседания: ' + n.meeting.title}"
class="news-item"></div>
<div th:if="${n.meeting != null}"
th:text="${'Дата: ' + #calendars.format(n.meeting.date, 'dd.MM.yyyy HH:mm')}"
class="news-item"></div>
<div th:if="${n.meeting != null}" th:text="${n.meeting.text}" class="news-item"></div>
</div>
</div>
<div th:text="${'Опубликовано: ' + #calendars.format(n.date, 'dd.MM.yyyy HH:mm')}"