24 Commits
main ... mvc

Author SHA1 Message Date
38823a4479 fix likes filtration 2024-06-13 20:01:02 +04:00
a64bb9104e fix from addFavorite to search redirect 2024-06-13 19:44:06 +04:00
b4a3786b33 fix fansNumber 2024-06-13 17:03:00 +04:00
aa9798b68a it is working 2024-06-12 20:01:02 +04:00
dffe1aeae2 mvc fix pagination 2024-06-12 16:27:08 +04:00
444728c8fc pagination does work 2024-06-10 17:39:34 +04:00
03044bd5a3 does not work 2024-06-10 17:19:24 +04:00
8e14a128ea minor edits 2024-06-09 20:28:51 +04:00
f6e5c71aba mvc add filtration in search page 2024-06-09 15:52:53 +04:00
e2e27510da minor edits 2024-06-09 15:28:59 +04:00
b8c94f0659 minor edits 2024-06-09 13:21:51 +04:00
a4c04b94c0 fix selects in book-edit 2024-06-09 13:19:36 +04:00
184e2d3540 minor edits 2024-06-09 13:11:16 +04:00
a6c50d4fcd mvc fix author deletion problem 2024-06-09 13:02:11 +04:00
aa707d530e revert e199691eeb
revert revert 46918a325f

revert потом мб откатить
2024-06-07 18:33:53 +04:00
e199691eeb revert 46918a325f
revert потом мб откатить
2024-06-07 18:27:41 +04:00
46918a325f потом мб откатить 2024-06-07 18:24:44 +04:00
c8b26c8e24 not work 2024-06-07 18:00:02 +04:00
f97d1984e4 minor edits 2024-06-06 22:06:28 +04:00
92414d74c8 mvc add searh and favorites table 2024-06-06 21:35:41 +04:00
989809469d mvc minor edit 2024-06-05 22:44:40 +04:00
80a6dddd0d mvc fix author and type names problem 2024-06-05 22:10:12 +04:00
c5d0bd083f mvc fix controllers 2024-06-05 21:32:19 +04:00
55712b5c6e mvc add code from example (does not work) 2024-06-02 17:34:43 +04:00
61 changed files with 1887 additions and 904 deletions

2
.gitignore vendored
View File

@@ -1,2 +1,4 @@
SpringApp/data.mv.db
SpringApp/library/data.mv.db
SpringApp/library/data.trace.db
SpringApp/data.trace.db

View File

@@ -34,11 +34,15 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.2.224'
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:3.3.0'
runtimeOnly 'org.webjars.npm:bootstrap:5.3.3'
runtimeOnly 'org.webjars.npm:bootstrap-icons:1.11.3'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'com.auth0:java-jwt:4.4.0'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

View File

@@ -1,61 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="out">Push the button</p>
<input id="name"/>
<br/>
<button onclick="get()">Get</button>
<br/>
<button onclick="getTest()">Get Test</button>
<br/>
<button onclick="post()">Post</button>
<br/>
<button onclick="put()">Put</button>
</body>
<script>
const url = "http://localhost:8080/api";
const out = document.getElementById("out");
const name = document.getElementById("name");
const get = async () => {
const res = await fetch(`${url}?name=${name.value}`);
const text = await res.text();
out.innerText = text;
}
const getTest = async () => {
const res = await fetch(`${url}/test`);
const text = await res.text();
out.innerText = text;
}
const post = async () => {
if (!name.value) {
alert("Name is required");
return;
}
const res = await fetch(url, {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: name.value
});
const text = await res.text();
out.innerText = text;
}
const put = async () => {
if (!name.value) {
alert("Name is required");
return;
}
const res = await fetch(`${url}/${name.value}`, {
method: 'put',
headers: {'Content-Type': 'application/json'},
body: name.value
});
const text = await res.text();
out.innerText = text;
}
</script>
</html>

View File

@@ -54,16 +54,20 @@ public class LibraryApplication implements CommandLineRunner {
log.info("Create default authors values");
final var author1 = authorService.create(new AuthorEntity("author1"));
final var author2 = authorService.create(new AuthorEntity("author2"));
final var author3 = authorService.create(new AuthorEntity("author3"));
log.info("Create default books values");
final var book1 = bookService.create(new BookEntity("book1", type1));
final var book2 = bookService.create(new BookEntity("book2", type1));
final var book3 = bookService.create(new BookEntity("book3", type2));
final var book4 = bookService.create(new BookEntity("book4", type2));
final var book1 = bookService.create(new BookEntity("book1", type1), new ArrayList<>());
final var book2 = bookService.create(new BookEntity("book2", type1), new ArrayList<>());
final var book3 = bookService.create(new BookEntity("book3", type2), new ArrayList<>());
final var book4 = bookService.create(new BookEntity("book4", type2), new ArrayList<>());
final var book5 = bookService.create(new BookEntity("book5", type2), new ArrayList<>());
bookService.addAuthor(author1.getId(), book1.getId());
bookService.addAuthor(author2.getId(), book2.getId());
bookService.addAuthor(author1.getId(), book3.getId());
bookService.addAuthor(author1.getId(), book4.getId());
bookService.addAuthor(author1.getId(), book5.getId());
bookService.addAuthor(author2.getId(), book2.getId());
bookService.addAuthor(author2.getId(), book3.getId());
log.info("Create default users values");

View File

@@ -1,28 +1,28 @@
package com.ip.library.controllers.authors;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ip.library.controllers.users.UserRole;
import com.ip.library.core.configuration.Constants;
import jakarta.validation.Valid;
@RestController
@Secured(value = UserRole.Secured.ADMIN)
@RequestMapping(Constants.API_URL + "/author")
@Controller
@RequestMapping(AuthorController.URL)
public class AuthorController {
public static final String URL = Constants.API_URL + "/author";
private static final String AUTHOR_VIEW = "author";
private static final String AUTHOR_EDIT_VIEW = "author-edit";
private static final String AUTHOR_ATTRIBUTE = "author";
private final AuthorService authorService;
private final ModelMapper modelMapper;
@@ -40,27 +40,66 @@ public class AuthorController {
}
@GetMapping
public List<AuthorDto> getAll() {
return authorService.getAll().stream().map(this::toDto).toList();
public String getAll(Model model) {
model.addAttribute(
"items",
authorService
.getAll()
.stream()
.map(this::toDto)
.toList());
return AUTHOR_VIEW;
}
@GetMapping("/{id}")
public AuthorDto get(@PathVariable(name = "id") Long id) {
return toDto(authorService.get(id));
@GetMapping("/edit/")
public String create(Model model) {
model.addAttribute(AUTHOR_ATTRIBUTE, new AuthorDto());
return AUTHOR_EDIT_VIEW;
}
@PostMapping
public AuthorDto create(@RequestBody @Valid AuthorDto dto) {
return toDto(authorService.create(toEntity(dto)));
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = AUTHOR_ATTRIBUTE) @Valid AuthorDto author,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return AUTHOR_EDIT_VIEW;
}
authorService.create(toEntity(author));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/{id}")
public AuthorDto update(@PathVariable(name = "id") Long id, @RequestBody AuthorDto dto) {
return toDto(authorService.update(id, toEntity(dto)));
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(AUTHOR_ATTRIBUTE, toDto(authorService.get(id)));
return AUTHOR_EDIT_VIEW;
}
@DeleteMapping("/{id}")
public AuthorDto delete(@PathVariable(name = "id") Long id) {
return toDto(authorService.delete(id));
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@ModelAttribute(name = AUTHOR_ATTRIBUTE) @Valid AuthorDto author,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return AUTHOR_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
authorService.update(id, toEntity(author));
return Constants.REDIRECT_VIEW + URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id) {
authorService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@@ -1,18 +1,13 @@
package com.ip.library.controllers.authors;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
import com.ip.library.controllers.books.BookEntity;
import com.ip.library.core.model.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
@Entity
@@ -20,9 +15,6 @@ import jakarta.persistence.Table;
public class AuthorEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 20)
private String name;
@OneToMany(mappedBy = "author", cascade = CascadeType.ALL, orphanRemoval = true)
@OrderBy("id ASC")
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
public AuthorEntity() {
super();
@@ -40,20 +32,11 @@ public class AuthorEntity extends BaseEntity {
this.name = name;
}
public Set<AuthorsBooksEntity> getAuthorsBooks() {
return authorsBooks;
}
public void setBooks(Set<AuthorsBooksEntity> authorsBooks) {
this.authorsBooks = authorsBooks;
}
public boolean addBook(BookEntity book) {
AuthorsBooksEntity entity = new AuthorsBooksEntity(this, book);
boolean result = authorsBooks.add(entity);
if (!book.getAuthorsBooks().contains(entity))
book.getAuthorsBooks().add(entity);
return result;
return true;
}
@Override

View File

@@ -50,7 +50,6 @@ public class AuthorService {
throw new IllegalArgumentException("Updating AuthorEntity is null");
}
final AuthorEntity existsEntity = get(id);
checkNameUniqueness(entity.getName());
existsEntity.setName(entity.getName());
return repository.save(existsEntity);
}

View File

@@ -43,8 +43,6 @@ public class AuthorsBooksEntity {
public void setAuthor(AuthorEntity author) {
this.author = author;
if (!author.getAuthorsBooks().contains(this))
author.getAuthorsBooks().add(this);
}
public AuthorEntity getAuthor() {

View File

@@ -0,0 +1,6 @@
package com.ip.library.controllers.authors_books;
import org.springframework.data.repository.CrudRepository;
public interface AuthorsBooksRepository extends
CrudRepository<AuthorsBooksEntity, AuthorsBooksId> {}

View File

@@ -1,90 +1,170 @@
package com.ip.library.controllers.books;
import java.util.Map;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.ip.library.controllers.authors.AuthorEntity;
import com.ip.library.controllers.authors.AuthorService;
import com.ip.library.controllers.types.TypeService;
import com.ip.library.controllers.users.UserRole;
import com.ip.library.core.api.PageAttributesMapper;
import com.ip.library.core.configuration.Constants;
import jakarta.validation.Valid;
@RestController
@Secured(value = UserRole.Secured.ADMIN)
@RequestMapping(Constants.API_URL + "/book")
@Controller
@RequestMapping(BookController.URL)
public class BookController {
public static final String URL = Constants.API_URL + "/book";
private static final String BOOK_VIEW = "book";
private static final String BOOK_EDIT_VIEW = "book-edit";
private static final String BOOK_ATTRIBUTE = "book";
private static final String PAGE_ATTRIBUTE = "page";
private static final String AUTHOR_ATTRIBUTE = "authorId";
private static final String AUTHORS_ATTRIBUTE = "authors";
private static final String TYPE_ATTRIBUTE = "typeId";
private static final String TYPES_ATTRIBUTE = "types";
private final BookService bookService;
private final TypeService typeService;
private final AuthorService authorService;
private final ModelMapper modelMapper;
public BookController(BookService bookService, TypeService typeService, ModelMapper modelMapper) {
public BookController(
BookService bookService,
TypeService typeService,
AuthorService authorService,
ModelMapper modelMapper) {
this.bookService = bookService;
this.typeService = typeService;
this.authorService = authorService;
this.modelMapper = modelMapper;
}
private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
List<AuthorEntity> authors = entity.getAuthorsBooks().stream().map(x -> x.getAuthor()).toList();
bookDto.setAuthorsId(authors.stream().map(x -> x.getId()).toList());
bookDto.setTypeName(entity.getType().getName());
StringBuilder authorName = new StringBuilder();
for (AuthorEntity authorEntity : authors) {
authorName.append(", ").append(authorEntity.getName());
}
if (authorName.length() > 0) {
bookDto.setAuthorName(authorName.toString().substring(2));
} else {
bookDto.setAuthorName("Неизвестен");
}
return bookDto;
}
private BookEntity toEntity(BookDto dto) {
private BookEntity toBookEntity(BookDto dto) {
final BookEntity entity = modelMapper.map(dto, BookEntity.class);
entity.setType(typeService.get(dto.getTypeId()));
return entity;
}
private List<Long> toAuthorsIdList(BookDto dto) {
return dto.getAuthorsId();
}
@GetMapping
public List<BookDto> getAll(
@RequestParam(name = "typeId", defaultValue = "-1") Long typeId,
@RequestParam(name = "authorId", defaultValue = "-1") Long authorId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return bookService.getAll(typeId, authorId, page, size).stream().map(this::toBookDto).toList();
public String getAll(
@RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId,
@RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
bookService.getAll(typeId, authorId, page, Constants.DEFUALT_PAGE_SIZE), this::toBookDto);
model.addAllAttributes(attributes);
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_VIEW;
}
@GetMapping("/{id}")
public BookDto get(@PathVariable(name = "id") Long id) {
return toBookDto(bookService.get(id));
@GetMapping("/edit/")
public String create(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
model.addAttribute(BOOK_ATTRIBUTE, new BookDto());
model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll());
model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_EDIT_VIEW;
}
@PostMapping
public BookDto create(@RequestBody @Valid BookDto dto) {
return toBookDto(bookService.create(toEntity(dto)));
@PostMapping("/edit/")
public String create(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = BOOK_ATTRIBUTE) @Valid BookDto book,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll());
model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_EDIT_VIEW;
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
bookService.create(toBookEntity(book), toAuthorsIdList(book));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/{id}")
public BookDto update(@PathVariable(name = "id") Long id, @RequestBody BookDto dto) {
return toBookDto(bookService.update(id, toEntity(dto)));
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(BOOK_ATTRIBUTE, toBookDto(bookService.get(id)));
model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll());
model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_EDIT_VIEW;
}
@DeleteMapping("/{id}")
public BookDto delete(@PathVariable(name = "id") Long id) {
return toBookDto(bookService.delete(id));
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = BOOK_ATTRIBUTE) @Valid BookDto book,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll());
model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll());
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
bookService.update(id, toBookEntity(book), toAuthorsIdList(book));
return Constants.REDIRECT_VIEW + URL;
}
@GetMapping("/{bookId}/author/{authorId}")
public boolean addAuthor(
@PathVariable(name = "bookId") Long bookId,
@PathVariable(name = "authorId") Long authorId) {
return bookService.addAuthor(authorId, bookId);
}
@Secured(value = { UserRole.Secured.USER, UserRole.Secured.ADMIN })
@GetMapping("/{bookId}/number")
public int getBookSubscribersNumber(@PathVariable(name = "bookId") Long bookId) {
return bookService.getBookSubscribersNumber(bookId);
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
bookService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@@ -11,6 +11,10 @@ import jakarta.validation.constraints.NotNull;
public class BookDto {
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Long id;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String authorName;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private String typeName;
@NotBlank
private String name;
@NotNull
@@ -18,6 +22,10 @@ public class BookDto {
private Long typeId;
@NotNull
private List<Long> authorsId;
@NotNull
private boolean isChosen = false;
@Min(0)
private int fansNumber = 0;
public Long getId() {
return id;
@@ -27,6 +35,22 @@ public class BookDto {
this.id = id;
}
public String getAuthorName() {
return authorName;
}
public void setAuthorName(String authorName) {
this.authorName = authorName;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getName() {
return name;
}
@@ -47,7 +71,23 @@ public class BookDto {
return authorsId;
}
public void setAuthorId(List<Long> authorsId) {
public void setAuthorsId(List<Long> authorsId) {
this.authorsId = authorsId;
}
public boolean getIsChosen() {
return isChosen;
}
public void setIsChosen(boolean isChosen) {
this.isChosen = isChosen;
}
public int getFansNumber() {
return fansNumber;
}
public void setFansNumber(int fansNumber) {
this.fansNumber = fansNumber;
}
}

View File

@@ -15,10 +15,13 @@ import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import com.ip.library.controllers.authors.AuthorEntity;
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
import com.ip.library.controllers.favorites.FavoriteEntity;
import com.ip.library.controllers.types.TypeEntity;
import com.ip.library.controllers.users.UserEntity;
@Entity
@Table(name = "books")
@@ -29,10 +32,16 @@ public class BookEntity extends BaseEntity {
@JoinColumn(name = "type_id", nullable = false)
@OrderBy("id ASC")
private TypeEntity type;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL,
orphanRemoval = true, fetch = FetchType.EAGER)
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OrderBy("id ASC")
private Set<AuthorsBooksEntity> authorsBooks = new HashSet<>();
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OrderBy("id ASC")
private Set<FavoriteEntity> fans = new HashSet<>();
@Transient
private boolean isChosen = false;
@Column(nullable = false)
private int fansNumber = 0;
public BookEntity() {
super();
@@ -43,12 +52,14 @@ public class BookEntity extends BaseEntity {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
public BookEntity(BookEntity book, boolean isChosen) {
this.isChosen = isChosen;
this.name = book.name;
this.type = book.type;
this.authorsBooks =book.authorsBooks;
this.fans = book.fans;
this.fansNumber = book.fansNumber;
this.id = book.id;
}
public TypeEntity getType() {
@@ -63,18 +74,70 @@ public class BookEntity extends BaseEntity {
return authorsBooks;
}
public void setAuthors(Set<AuthorsBooksEntity> authorsBooks) {
public void setAuthorsBooks(Set<AuthorsBooksEntity> authorsBooks) {
this.authorsBooks = authorsBooks;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getIsChosen() {
return isChosen;
}
public void setIsChosen(boolean isChosen) {
this.isChosen = isChosen;
}
public int getFansNumber() {
return fansNumber;
}
public void setFansNumber(int fansNumber) {
this.fansNumber = fansNumber;
}
public Set<FavoriteEntity> getFans() {
return fans;
}
public void setFans(Set<FavoriteEntity> fans) {
this.fans = fans;
}
public boolean addAuthor(AuthorEntity author) {
AuthorsBooksEntity entity = new AuthorsBooksEntity(author, this);
boolean result = authorsBooks.add(entity);
if (!author.getAuthorsBooks().contains(entity))
author.getAuthorsBooks().add(entity);
return authorsBooks.add(entity);
}
public void removeAuthor(AuthorsBooksEntity authorBookRecord) {
if (authorsBooks.contains(authorBookRecord)) {
authorsBooks.remove(authorBookRecord);
}
}
public boolean addFan(UserEntity user) {
FavoriteEntity entity = new FavoriteEntity(user, this);
boolean result = fans.add(entity);
if (!user.getFavorites().contains(entity)) {
user.getFavorites().add(entity);
}
fansNumber++;
return result;
}
public void removeFan(FavoriteEntity favorite) {
if (fans.contains(favorite)) {
fans.remove(favorite);
}
fansNumber--;
}
@Override
public int hashCode() {
return Objects.hash(id, name, type);

View File

@@ -9,14 +9,33 @@ import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import com.ip.library.controllers.authors.AuthorEntity;
public interface BookRepository extends
CrudRepository<BookEntity, Long>,
PagingAndSortingRepository<BookEntity, Long> {
Optional<BookEntity> findByNameIgnoreCase(String name);
@Query(
"select new com.ip.library.controllers.books.BookEntity(b, " +
"case when f.id.bookId is null then false else true end) " +
"from BookEntity b left join FavoriteEntity f " +
"on b.id = f.id.bookId and f.user.id = ?1 " +
"order by b.id")
Page<BookEntity> findAll(long userId, Pageable pageable);
List<BookEntity> findByTypeId(long typeId);
Page<BookEntity> findByTypeId(long typeId, Pageable pageable);
@Query(
"select new com.ip.library.controllers.books.BookEntity(b, " +
"case when f.id.bookId is null then false else true end) " +
"from BookEntity b left join FavoriteEntity f " +
"on b.id = f.id.bookId and f.user.id = ?2 " +
"where b.type.id = ?1 " +
"order by b.id")
Page<BookEntity> findByTypeId(long typeId, long userId, Pageable pageable);
@Query(
"select ab.book " +
@@ -32,6 +51,16 @@ public interface BookRepository extends
"order by ab.book.id")
Page<BookEntity> findByAuthorId(Long authorId, Pageable pageable);
@Query(
"select new com.ip.library.controllers.books.BookEntity(b, " +
"case when f.id.bookId is null then false else true end) " +
"from BookEntity b " +
"left join FavoriteEntity f on b.id = f.id.bookId and f.user.id = ?2 " +
"left join AuthorsBooksEntity ab on b.id = ab.id.bookId " +
"where ab.id.authorId = ?1 " +
"order by b.id")
Page<BookEntity> findByAuthorId(Long authorId, long userId, Pageable pageable);
@Query(
"select ab.book " +
"from AuthorsBooksEntity ab " +
@@ -47,10 +76,27 @@ public interface BookRepository extends
"ab.book.type.id = ?2 " +
"order by ab.book.id")
Page<BookEntity> findByAuthorIdAndTypeId(Long authorId, Long typeId, Pageable pageable);
@Query(
"select new com.ip.library.controllers.books.BookEntity(b, " +
"case when f.id.bookId is null then false else true end) " +
"from BookEntity b " +
"left join FavoriteEntity f on b.id = f.id.bookId and f.user.id = ?3 " +
"left join AuthorsBooksEntity ab on b.id = ab.id.bookId " +
"where ab.id.authorId = ?1 and b.type.id = ?2 " +
"order by b.id")
Page<BookEntity> findByAuthorIdAndTypeId(Long authorId, Long typeId, long userId, Pageable pageable);
@Query(
"select count(*) as number " +
"from FavoriteEntity f " +
"where f.book.id = ?1")
int getBookSubscribersNumber(long bookId);
@Query(
"select f.author " +
"from AuthorsBooksEntity f " +
"where f.book.id = ?1 " +
"order by f.author.id")
public List<AuthorEntity> getAuthors(Long bookId);
}

View File

@@ -10,20 +10,32 @@ import org.springframework.transaction.annotation.Transactional;
import com.ip.library.controllers.authors.AuthorEntity;
import com.ip.library.controllers.authors.AuthorService;
import com.ip.library.controllers.authors_books.AuthorsBooksEntity;
import com.ip.library.controllers.authors_books.AuthorsBooksId;
import com.ip.library.controllers.authors_books.AuthorsBooksRepository;
import com.ip.library.controllers.users.UserRepository;
import com.ip.library.core.error.NotFoundException;
@Service
public class BookService {
private final BookRepository repository;
private final AuthorsBooksRepository authorsBooksRepository;
private final BookRepository bookRepository;
private final AuthorService authorService;
private final UserRepository userRepository;
public BookService(BookRepository repository, AuthorService authorService) {
this.repository = repository;
public BookService(
BookRepository bookRepository,
AuthorService authorService,
AuthorsBooksRepository authorsBooksRepository,
UserRepository userRepository) {
this.bookRepository = bookRepository;
this.authorService = authorService;
this.authorsBooksRepository = authorsBooksRepository;
this.userRepository = userRepository;
}
private void checkNameUniqueness(String name){
if (repository.findByNameIgnoreCase(name).isPresent()) {
if (bookRepository.findByNameIgnoreCase(name).isPresent()) {
throw new IllegalArgumentException(
String.format("Book with name %s already exists", name)
);
@@ -32,46 +44,70 @@ public class BookService {
@Transactional(readOnly = true)
public List<BookEntity> getAll(){
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
return StreamSupport.stream(bookRepository.findAll().spliterator(), false).toList();
}
@Transactional(readOnly = true)
public Page<BookEntity> getAll(long typeId, long authorId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
if (typeId <= 0L && authorId <= 0L)
return repository.findAll(pageRequest);
return bookRepository.findAll(pageRequest);
if (authorId <= 0L)
return repository.findByTypeId(typeId, pageRequest);
return bookRepository.findByTypeId(typeId, pageRequest);
if (typeId <= 0L)
return repository.findByAuthorId(authorId, pageRequest);
return repository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
return bookRepository.findByAuthorId(authorId, pageRequest);
return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, pageRequest);
}
@Transactional(readOnly = true)
public Page<BookEntity> getAll(long typeId, long authorId, long userId, int page, int size) {
PageRequest pageRequest = PageRequest.of(page, size);
if (typeId <= 0L && authorId <= 0L) {
return bookRepository.findAll(userId, pageRequest);
}
else if (authorId <= 0L){
return bookRepository.findByTypeId(typeId, userId, pageRequest);
}
else if (typeId <= 0L) {
return bookRepository.findByAuthorId(authorId, userId, pageRequest);
}
return bookRepository.findByAuthorIdAndTypeId(authorId, typeId, userId, pageRequest);
}
@Transactional(readOnly = true)
public List<BookEntity> getAll(long typeId, long authorId) {
if (typeId <= 0L && authorId <= 0L)
return StreamSupport.stream(repository.findAll().spliterator(),
return StreamSupport.stream(bookRepository.findAll().spliterator(),
false).toList();
if (authorId <= 0L)
return repository.findByTypeId(typeId);
return bookRepository.findByTypeId(typeId);
if (typeId <= 0L)
return repository.findByAuthorId(authorId);
return repository.findByAuthorIdAndTypeId(authorId, typeId);
return bookRepository.findByAuthorId(authorId);
return bookRepository.findByAuthorIdAndTypeId(authorId, typeId);
}
@Transactional
public List<BookEntity> findByAuthorId(long authorId) {
return bookRepository.findByAuthorId(authorId);
}
@Transactional(readOnly = true)
public BookEntity get(long id) {
return repository.findById(id)
return bookRepository.findById(id)
.orElseThrow(() -> new NotFoundException(BookEntity.class, id));
}
@Transactional
public BookEntity create(BookEntity entity) {
public BookEntity create(BookEntity entity, List<Long> authorsId) {
if (entity == null) {
throw new IllegalArgumentException("Creating BookEntity is null");
}
checkNameUniqueness(entity.getName());
return repository.save(entity);
BookEntity result = bookRepository.save(entity);
addAuthors(
result.getId(),
authorsId);
return get(result.getId());
}
@Transactional
@@ -80,22 +116,39 @@ public class BookService {
throw new IllegalArgumentException("Updating BookEntity is null");
}
final BookEntity existsEntity = get(id);
checkNameUniqueness(entity.getName());
existsEntity.setName(entity.getName());
existsEntity.setType(entity.getType());
return repository.save(existsEntity);
bookRepository.save(existsEntity);
return get(id);
}
@Transactional
public BookEntity update(long id, BookEntity entity, List<Long> authorsId) {
update(id, entity);
updateAuthors(id, authorsId);
return get(id);
}
@Transactional
public BookEntity delete(long id) {
final BookEntity existsEntity = get(id);
repository.delete(existsEntity);
bookRepository.delete(existsEntity);
return existsEntity;
}
@Transactional(readOnly = true)
public int getBookSubscribersNumber(long bookId) {
return repository.getBookSubscribersNumber(bookId);
return bookRepository.getBookSubscribersNumber(bookId);
}
@Transactional(readOnly = true)
public List<BookEntity> getUserFavorities (long userId) {
return userRepository.getUserFavorities(userId);
}
@Transactional(readOnly = true)
public List<AuthorEntity> getAuthors(long bookId) {
return bookRepository.getAuthors(bookId);
}
@Transactional
@@ -106,7 +159,40 @@ public class BookService {
}
@Transactional
public List<BookEntity> findByAuthorId(long authorId) {
return repository.findByAuthorId(authorId);
public void removeAuthor(long authorId, long bookId) {
final AuthorsBooksEntity authorBookRecord = authorsBooksRepository.findById(
new AuthorsBooksId(authorId, bookId))
.orElseThrow(() -> new IllegalArgumentException("Invalid id")
);
final BookEntity book = get(bookId);
authorsBooksRepository.delete(authorBookRecord);
book.removeAuthor(authorBookRecord);
}
@Transactional
public boolean addAuthors(long bookId, List<Long> authorsId) {
final BookEntity book = get(bookId);
for (Long authorId : authorsId) {
final AuthorEntity existsAuthor = authorService.get(authorId);
if (!existsAuthor.addBook(book)) {
return false;
}
}
return true;
}
@Transactional
public void updateAuthors(long bookId, List<Long> authorsId) {
List<AuthorEntity> oldAuthors = getAuthors(bookId);
for (AuthorEntity author : oldAuthors) {
long currentAuthorId = author.getId();
if (!authorsId.contains(currentAuthorId)) {
removeAuthor(currentAuthorId, bookId);
}
else {
authorsId.remove(currentAuthorId);
}
}
addAuthors(bookId, authorsId);
}
}

View File

@@ -0,0 +1,6 @@
package com.ip.library.controllers.favorites;
import org.springframework.data.repository.CrudRepository;
public interface FavoriteRepository extends
CrudRepository<FavoriteEntity, UserBookId> {}

View File

@@ -1,27 +1,26 @@
package com.ip.library.controllers.types;
import java.util.List;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ip.library.controllers.users.UserRole;
import com.ip.library.core.configuration.Constants;
import jakarta.validation.Valid;
@RestController
@Secured(value = UserRole.Secured.ADMIN)
@RequestMapping(Constants.API_URL + "/type")
@Controller
@RequestMapping(TypeController.URL)
public class TypeController {
public static final String URL = Constants.API_URL + "/type";
private static final String TYPE_VIEW = "type";
private static final String TYPE_EDIT_VIEW = "type-edit";
private static final String TYPE_ATTRIBUTE = "type";
private final TypeService typeService;
private final ModelMapper modelMapper;
@@ -39,27 +38,66 @@ public class TypeController {
}
@GetMapping
public List<TypeDto> getAll() {
return typeService.getAll().stream().map(this::toDto).toList();
public String getAll(Model model) {
model.addAttribute(
"items",
typeService
.getAll()
.stream()
.map(this::toDto)
.toList());
return TYPE_VIEW;
}
@GetMapping("/{id}")
public TypeDto get(@PathVariable(name = "id") Long id) {
return toDto(typeService.get(id));
@GetMapping("/edit/")
public String create(Model model) {
model.addAttribute(TYPE_ATTRIBUTE, new TypeDto());
return TYPE_EDIT_VIEW;
}
@PostMapping
public TypeDto create(@RequestBody @Valid TypeDto dto) {
return toDto(typeService.create(toEntity(dto)));
@PostMapping("/edit/")
public String create(
@ModelAttribute(name = TYPE_ATTRIBUTE) @Valid TypeDto type,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return TYPE_EDIT_VIEW;
}
typeService.create(toEntity(type));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/{id}")
public TypeDto update(@PathVariable(name = "id") Long id, @RequestBody TypeDto dto) {
return toDto(typeService.update(id, toEntity(dto)));
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(TYPE_ATTRIBUTE, toDto(typeService.get(id)));
return TYPE_EDIT_VIEW;
}
@DeleteMapping("/{id}")
public TypeDto delete(@PathVariable(name = "id") Long id) {
return toDto(typeService.delete(id));
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@ModelAttribute(name = TYPE_ATTRIBUTE) @Valid TypeDto type,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return TYPE_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
typeService.update(id, toEntity(type));
return Constants.REDIRECT_VIEW + URL;
}
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id) {
typeService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@@ -50,7 +50,6 @@ public class TypeService {
throw new IllegalArgumentException("Updating TypeEntity is null");
}
final TypeEntity existsEntity = get(id);
checkNameUniqueness(entity.getName());
existsEntity.setName(entity.getName());
return repository.save(existsEntity);
}

View File

@@ -1,45 +0,0 @@
package com.ip.library.controllers.users;
import java.util.Objects;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ip.library.core.configuration.Constants;
@RestController
public class LoginController {
private final UserService userService;
public LoginController(UserService userService) {
this.userService = userService;
}
@PostMapping(Constants.LOGIN_URL)
public String login(
@RequestParam(name = "login") String login,
@RequestParam(name = "password") String password) {
return userService.login(login, password);
}
@PostMapping(Constants.SIGNUP_URL)
public boolean signup(
@RequestParam(name = "login") String login,
@RequestParam(name = "password") String password,
@RequestParam(name = "password2") String password2) {
if (!StringUtils.hasText(login)) {
throw new IllegalArgumentException("Invalid login");
}
if (!StringUtils.hasText(password)) {
throw new IllegalArgumentException("Invalid password");
}
if (!Objects.equals(password, password2)) {
throw new IllegalArgumentException("Passwords are not equals");
}
final UserEntity user = new UserEntity(login, password);
userService.create(user);
return true;
}
}

View File

@@ -1,51 +1,131 @@
package com.ip.library.controllers.users;
import java.util.List;
import java.util.Map;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.ip.library.controllers.authors.AuthorEntity;
import com.ip.library.controllers.authors.AuthorService;
import com.ip.library.controllers.books.BookDto;
import com.ip.library.controllers.books.BookEntity;
import com.ip.library.controllers.books.BookService;
import com.ip.library.controllers.types.TypeService;
import com.ip.library.core.api.PageAttributesMapper;
import com.ip.library.core.configuration.Constants;
import com.ip.library.core.security.UserPrincipal;
@RestController
@Secured(value = { UserRole.Secured.USER, UserRole.Secured.ADMIN })
@RequestMapping(Constants.API_URL + "/user/{userId}/book")
@Controller
public class UserBookController {
private final UserService userService;
private final ModelMapper modelMapper;
public UserBookController(
UserService userService,
ModelMapper modelMapper) {
this.userService = userService;
this.modelMapper = modelMapper;
}
private static final String BOOK_SEARCH_VIEW = "book-search";
private static final String USER_FAVORITES_VIEW = "user-favorites";
private static final String PAGE_ATTRIBUTE = "page";
private static final String AUTHOR_ATTRIBUTE = "authorId";
private static final String AUTHORS_ATTRIBUTE = "authors";
private static final String TYPE_ATTRIBUTE = "typeId";
private static final String TYPES_ATTRIBUTE = "types";
private static final String PARAMS_ATTRIBUTE = "searchParams";
private final UserService userService;
private final BookService bookService;
private final TypeService typeService;
private final AuthorService authorService;
private final ModelMapper modelMapper;
public UserBookController(
UserService userService,
BookService bookService,
ModelMapper modelMapper,
TypeService typeService,
AuthorService authorService) {
this.bookService = bookService;
this.userService = userService;
this.modelMapper = modelMapper;
this.typeService = typeService;
this.authorService = authorService;
}
private BookDto toBookDto (BookEntity entity) {
BookDto bookDto = modelMapper.map(entity, BookDto.class);
bookDto.setAuthorId(entity.getAuthorsBooks().stream().map(x -> x.getAuthor().getId()).toList());
List<AuthorEntity> authors = entity.getAuthorsBooks().stream().map(x -> x.getAuthor()).toList();
bookDto.setAuthorsId(authors.stream().map(x -> x.getId()).toList());
bookDto.setTypeName(entity.getType().getName());
StringBuilder authorName = new StringBuilder();
for (AuthorEntity authorEntity : authors) {
authorName.append(", ").append(authorEntity.getName());
}
if (authorName.length() > 0) {
bookDto.setAuthorName(authorName.toString().substring(2));
} else {
bookDto.setAuthorName("Неизвестен");
}
return bookDto;
}
@GetMapping("/{bookId}")
public boolean addFavorite(
@PathVariable(name = "userId") Long userId,
@PathVariable(name = "bookId") Long bookId) {
return userService.addFavorite(userId, bookId);
@GetMapping
public String favorites(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model,
@AuthenticationPrincipal UserPrincipal principal) {
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
userService.getUserFavorities(principal.getId(), page, Constants.DEFUALT_PAGE_SIZE),
this::toBookDto);
model.addAllAttributes(attributes);
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_FAVORITES_VIEW;
}
@PostMapping(Constants.API_URL + "/removeFavorite/{id}")
public String removeFavorite(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes,
@AuthenticationPrincipal UserPrincipal principal) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
userService.removeFavorite(principal.getId(), id);
return Constants.REDIRECT_VIEW + "/";
}
@GetMapping(Constants.API_URL + "/search")
public String search(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId,
@RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId,
Model model,
@AuthenticationPrincipal UserPrincipal principal) {
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
bookService.getAll(typeId, authorId, principal.getId(), page, Constants.DEFUALT_PAGE_SIZE),
this::toBookDto);
model.addAllAttributes(attributes);
model.addAttribute(TYPE_ATTRIBUTE, typeId);
model.addAttribute(AUTHOR_ATTRIBUTE, authorId);
model.addAttribute(TYPES_ATTRIBUTE, typeService.getAll());
model.addAttribute(AUTHORS_ATTRIBUTE, authorService.getAll());
model.addAttribute(PARAMS_ATTRIBUTE, "typeId="+typeId+"&authorId="+authorId);
model.addAttribute(PAGE_ATTRIBUTE, page);
return BOOK_SEARCH_VIEW;
}
@PostMapping(Constants.API_URL + "/addFavorite/{id}")
public String addFavorite(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@RequestParam(name = TYPE_ATTRIBUTE, defaultValue = "-1") Long typeId,
@RequestParam(name = AUTHOR_ATTRIBUTE, defaultValue = "-1") Long authorId,
RedirectAttributes redirectAttributes,
@AuthenticationPrincipal UserPrincipal principal) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
redirectAttributes.addAttribute(TYPE_ATTRIBUTE, typeId);
redirectAttributes.addAttribute(AUTHOR_ATTRIBUTE, authorId);
userService.addFavorite(principal.getId(), id);
return Constants.REDIRECT_VIEW + Constants.API_URL + "/search";
}
@GetMapping("/all-books")
public List<BookDto> getUserFavorites(
@PathVariable(name = "userId") Long userId,
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return userService.getUserFavorities(userId, page, size).stream().map(this::toBookDto).toList();
}
}

View File

@@ -1,27 +1,33 @@
package com.ip.library.controllers.users;
import java.util.Map;
import org.modelmapper.ModelMapper;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.ip.library.core.api.PageDto;
import com.ip.library.core.api.PageDtoMapper;
import com.ip.library.core.api.PageAttributesMapper;
import com.ip.library.core.configuration.Constants;
import jakarta.validation.Valid;
@RestController
@Secured(value = UserRole.Secured.ADMIN)
@RequestMapping(Constants.API_URL + "/user")
@Controller
@RequestMapping(UserController.URL)
public class UserController {
public static final String URL = Constants.API_URL + "/user";
private static final String USER_VIEW = "user";
private static final String USER_EDIT_VIEW = "user-edit";
private static final String PAGE_ATTRIBUTE = "page";
private static final String USER_ATTRIBUTE = "user";
private final UserService userService;
private final ModelMapper modelMapper;
@@ -41,34 +47,81 @@ public class UserController {
}
@GetMapping
public PageDto<UserDto> getAll(
@RequestParam(name = "page", defaultValue = "0") int page,
@RequestParam(name = "size", defaultValue = Constants.DEFAULT_PAGE_SIZE) int size) {
return PageDtoMapper.toDto(userService.getAll(page, size), this::toUserDto);
public String getAll(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
final Map<String, Object> attributes = PageAttributesMapper.toAttributes(
userService.getAll(page, Constants.DEFUALT_PAGE_SIZE), this::toUserDto);
model.addAllAttributes(attributes);
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_VIEW;
}
@GetMapping("/{id}")
public UserDto get(@PathVariable(name = "id") Long id) {
return toUserDto(userService.get(id));
@GetMapping("/edit/")
public String create(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
model.addAttribute(USER_ATTRIBUTE, new UserDto());
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_EDIT_VIEW;
}
@PostMapping
public UserDto create(@RequestBody @Valid UserDto dto) {
return toUserDto(userService.create(toUserEntity(dto)));
@PostMapping("/edit/")
public String create(
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDto user,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_EDIT_VIEW;
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
userService.create(toUserEntity(user));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/{id}")
public UserDto update(@PathVariable(name = "id") Long id, @RequestBody UserDto dto) {
return toUserDto(userService.update(id, toUserEntity(dto)));
@GetMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
Model model) {
if (id <= 0) {
throw new IllegalArgumentException();
}
model.addAttribute(USER_ATTRIBUTE, toUserDto(userService.get(id)));
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_EDIT_VIEW;
}
@DeleteMapping("/{id}")
public UserDto delete(@PathVariable(name = "id") Long id) {
return toUserDto(userService.delete(id));
@PostMapping("/edit/{id}")
public String update(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserDto user,
BindingResult bindingResult,
Model model,
RedirectAttributes redirectAttributes) {
if (bindingResult.hasErrors()) {
model.addAttribute(PAGE_ATTRIBUTE, page);
return USER_EDIT_VIEW;
}
if (id <= 0) {
throw new IllegalArgumentException();
}
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
userService.update(id, toUserEntity(user));
return Constants.REDIRECT_VIEW + URL;
}
@PutMapping("/password/{id}")
public UserDto changePassword(@PathVariable(name = "id") Long id, @RequestBody String newPassword) {
return toUserDto(userService.changePassword(id, newPassword));
@PostMapping("/delete/{id}")
public String delete(
@PathVariable(name = "id") Long id,
@RequestParam(name = PAGE_ATTRIBUTE, defaultValue = "0") int page,
RedirectAttributes redirectAttributes) {
redirectAttributes.addAttribute(PAGE_ATTRIBUTE, page);
userService.delete(id);
return Constants.REDIRECT_VIEW + URL;
}
}

View File

@@ -20,7 +20,7 @@ import jakarta.persistence.Table;
public class UserEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 20)
private String login;
@Column(nullable = false, unique = false)
@Column(nullable = false, length = 60, unique = false)
private String password;
private UserRole role = UserRole.USER;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)
@@ -73,11 +73,13 @@ public class UserEntity extends BaseEntity {
}
public boolean addBook(BookEntity book) {
return favorites.add(new FavoriteEntity(this, book));
}
public boolean removeBook(BookEntity book) {
return favorites.remove(new FavoriteEntity(this, book));
FavoriteEntity entity = new FavoriteEntity(this, book);
boolean result = favorites.add(entity);
if (!book.getFans().contains(entity)) {
book.getFans().add(entity);
book.setFansNumber(book.getFansNumber() + 1);
}
return result;
}
@Override

View File

@@ -16,32 +16,33 @@ import org.springframework.util.StringUtils;
import com.ip.library.controllers.books.BookEntity;
import com.ip.library.controllers.books.BookService;
import com.ip.library.controllers.favorites.FavoriteEntity;
import com.ip.library.controllers.favorites.FavoriteRepository;
import com.ip.library.controllers.favorites.UserBookId;
import com.ip.library.core.configuration.Constants;
import com.ip.library.core.error.NotFoundException;
import com.ip.library.core.jwt.JwtException;
import com.ip.library.core.jwt.JwtProvider;
import com.ip.library.core.security.UserPrincipal;
@Service
public class UserService implements UserDetailsService{
private final UserRepository repository;
private final UserRepository userRepository;
private final FavoriteRepository favoriteRepository;
private final BookService bookService;
private final PasswordEncoder passwordEncoder;
private final JwtProvider jwtProvider;
public UserService(
UserRepository repository,
FavoriteRepository favoriteRepository,
BookService bookService,
PasswordEncoder passwordEncoder,
JwtProvider jwtProvider) {
this.repository = repository;
PasswordEncoder passwordEncoder) {
this.userRepository = repository;
this.favoriteRepository = favoriteRepository;
this.bookService = bookService;
this.jwtProvider = jwtProvider;
this.passwordEncoder = passwordEncoder;
}
private void checkLoginUniqueness(String name){
if (repository.findByLoginIgnoreCase(name).isPresent()) {
if (userRepository.findByLoginIgnoreCase(name).isPresent()) {
throw new IllegalArgumentException(
String.format("Type with name %s already exists", name)
);
@@ -50,23 +51,23 @@ public class UserService implements UserDetailsService{
@Transactional(readOnly = true)
public List<UserEntity> getAll() {
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
return StreamSupport.stream(userRepository.findAll().spliterator(), false).toList();
}
@Transactional(readOnly = true)
public Page<UserEntity> getAll(int page, int size) {
return repository.findAll(PageRequest.of(page, size));
return userRepository.findAll(PageRequest.of(page, size));
}
@Transactional(readOnly = true)
public UserEntity get(long id) {
return repository.findById(id)
return userRepository.findById(id)
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
}
@Transactional(readOnly = true)
public UserEntity getByLogin(String login) {
return repository.findByLoginIgnoreCase(login)
return userRepository.findByLoginIgnoreCase(login)
.orElseThrow(() -> new IllegalArgumentException("Invalid login"));
}
@@ -82,21 +83,20 @@ public class UserService implements UserDetailsService{
passwordEncoder.encode(
StringUtils.hasText(password.strip()) ? password : Constants.DEFAULT_PASSWORD));
entity.setRole(Optional.ofNullable(entity.getRole()).orElse(UserRole.USER));
return repository.save(entity);
return userRepository.save(entity);
}
@Transactional
public UserEntity update(long id, UserEntity entity) {
final UserEntity existsEntity = get(id);
checkLoginUniqueness(entity.getLogin());
existsEntity.setLogin(entity.getLogin());
return repository.save(existsEntity);
return userRepository.save(existsEntity);
}
@Transactional
public UserEntity delete(long id) {
final UserEntity existsEntity = get(id);
repository.delete(existsEntity);
userRepository.delete(existsEntity);
return existsEntity;
}
@@ -104,21 +104,21 @@ public class UserService implements UserDetailsService{
public UserEntity giveAdminRole(long id) {
final UserEntity existsEntity = get(id);
existsEntity.setRole(UserRole.ADMIN);
return repository.save(existsEntity);
return userRepository.save(existsEntity);
}
@Transactional
public UserEntity giveUserRole(long id) {
final UserEntity existsEntity = get(id);
existsEntity.setRole(UserRole.USER);
return repository.save(existsEntity);
return userRepository.save(existsEntity);
}
@Transactional
public UserEntity changePassword(long id, String newPassword) {
final UserEntity existsEntity = get(id);
existsEntity.setPassword(newPassword);
return repository.save(existsEntity);
return userRepository.save(existsEntity);
}
@Transactional
@@ -128,32 +128,24 @@ public class UserService implements UserDetailsService{
return existsUser.addBook(book);
}
@Transactional
public FavoriteEntity removeFavorite(long userId, long bookId) {
final FavoriteEntity favorite = favoriteRepository.findById(new UserBookId(userId, bookId))
.orElseThrow(() -> new IllegalArgumentException("Invalid id"));
BookEntity book = bookService.get(bookId);
book.removeFan(favorite);
favoriteRepository.delete(favorite);
return favorite;
}
@Transactional(readOnly = true)
public List<BookEntity> getUserFavorities (long userId) {
return repository.getUserFavorities(userId);
return userRepository.getUserFavorities(userId);
}
@Transactional(readOnly = true)
public Page<BookEntity> getUserFavorities (long userId, int page, int size) {
return repository.getUserFavorities(userId, PageRequest.of(page, size));
}
@Transactional(readOnly = true)
public UserDetails getByToken(String token) {
if (!jwtProvider.isTokenValid(token))
throw new JwtException("Bad token");
final String userLogin = jwtProvider.getLoginFromToken(token)
.orElseThrow(() -> new JwtException("Token is not contain Login"));
return loadUserByUsername(userLogin);
}
@Transactional(readOnly = true)
public String login(String login, String password) {
final UserEntity existsUser = getByLogin(login);
if (!passwordEncoder.matches(password, existsUser.getPassword()))
throw new IllegalArgumentException("Invalid login");
return jwtProvider.generateToken(existsUser.getLogin());
return userRepository.getUserFavorities(userId, PageRequest.of(page, size));
}
@Override

View File

@@ -0,0 +1,63 @@
package com.ip.library.controllers.users;
import java.util.Objects;
import org.modelmapper.ModelMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
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 com.ip.library.core.configuration.Constants;
import jakarta.validation.Valid;
@Controller
@RequestMapping(UserSignupController.URL)
public class UserSignupController {
public static final String URL = Constants.SIGNUP_URL;
private static final String SIGNUP_VIEW = "signup";
private static final String USER_ATTRIBUTE = "user";
private final UserService userService;
private final ModelMapper modelMapper;
public UserSignupController(
UserService userService,
ModelMapper modelMapper) {
this.userService = userService;
this.modelMapper = modelMapper;
}
private UserEntity toEntity(UserSignupDto dto) {
return modelMapper.map(dto, UserEntity.class);
}
@GetMapping
public String getSignup(Model model) {
model.addAttribute(USER_ATTRIBUTE, new UserSignupDto());
return SIGNUP_VIEW;
}
@PostMapping
public String signup(
@ModelAttribute(name = USER_ATTRIBUTE) @Valid UserSignupDto user,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
return SIGNUP_VIEW;
}
if (!Objects.equals(user.getPassword(), user.getPasswordConfirm())) {
bindingResult.rejectValue("password", "signup:passwords", "Пароли не совпадают.");
model.addAttribute(USER_ATTRIBUTE, user);
return SIGNUP_VIEW;
}
userService.create(toEntity(user));
return Constants.REDIRECT_VIEW + Constants.LOGIN_URL + "?signup";
}
}

View File

@@ -0,0 +1,40 @@
package com.ip.library.controllers.users;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
public class UserSignupDto {
@NotBlank
@Size(min = 3, max = 20)
private String login;
@NotBlank
@Size(min = 3, max = 60)
private String password;
@NotBlank
@Size(min = 3, max = 60)
private String passwordConfirm;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPasswordConfirm() {
return passwordConfirm;
}
public void setPasswordConfirm(String passwordConfirm) {
this.passwordConfirm = passwordConfirm;
}
}

View File

@@ -0,0 +1,18 @@
package com.ip.library.core.api;
import java.util.Map;
import java.util.function.Function;
import org.springframework.data.domain.Page;
public class PageAttributesMapper {
private PageAttributesMapper() {
}
public static <E, D> Map<String, Object> toAttributes(Page<E> page, Function<E, D> mapper) {
return Map.of(
"items", page.getContent().stream().map(mapper::apply).toList(),
"currentPage", page.getNumber(),
"totalPages", page.getTotalPages());
}
}

View File

@@ -1,97 +0,0 @@
package com.ip.library.core.api;
import java.util.ArrayList;
import java.util.List;
public class PageDto<D> {
private List<D> items = new ArrayList<>();
private int itemsCount;
private int currentPage;
private int currentSize;
private int totalPages;
private long totalItems;
private boolean isFirst;
private boolean isLast;
private boolean hasNext;
private boolean hasPrevious;
public List<D> getItems() {
return items;
}
public void setItems(List<D> items) {
this.items = items;
}
public int getItemsCount() {
return itemsCount;
}
public void setItemsCount(int itemsCount) {
this.itemsCount = itemsCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentSize() {
return currentSize;
}
public void setCurrentSize(int currentSize) {
this.currentSize = currentSize;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public long getTotalItems() {
return totalItems;
}
public void setTotalItems(long totalItems) {
this.totalItems = totalItems;
}
public boolean isFirst() {
return isFirst;
}
public void setFirst(boolean isFirst) {
this.isFirst = isFirst;
}
public boolean isLast() {
return isLast;
}
public void setLast(boolean isLast) {
this.isLast = isLast;
}
public boolean isHasNext() {
return hasNext;
}
public void setHasNext(boolean hasNext) {
this.hasNext = hasNext;
}
public boolean isHasPrevious() {
return hasPrevious;
}
public void setHasPrevious(boolean hasPrevious) {
this.hasPrevious = hasPrevious;
}
}

View File

@@ -1,24 +0,0 @@
package com.ip.library.core.api;
import java.util.function.Function;
import org.springframework.data.domain.Page;
public class PageDtoMapper {
private PageDtoMapper() {}
public static <D, E> PageDto<D> toDto(Page<E> page, Function<E, D> mapper) {
final PageDto<D> dto = new PageDto<>();
dto.setItems(page.getContent().stream().map(mapper::apply).toList());
dto.setItemsCount(page.getNumberOfElements());
dto.setCurrentPage(page.getNumber());
dto.setCurrentSize(page.getSize());
dto.setTotalPages(page.getTotalPages());
dto.setTotalItems(page.getTotalElements());
dto.setFirst(page.isFirst());
dto.setLast(page.isLast());
dto.setHasNext(page.hasNext());
dto.setHasPrevious(page.hasPrevious());
return dto;
}
}

View File

@@ -3,11 +3,14 @@ package com.ip.library.core.configuration;
public class Constants {
public static final String SEQUENCE_NAME = "hibernate_sequence";
public static final int DEFUALT_PAGE_SIZE = 2;
public static final String API_URL = "/api/1.0";
public static final String DEFAULT_PAGE_SIZE = "5";
public static final String REDIRECT_VIEW = "redirect:";
public static final String LOGIN_URL = "/login";
public static final String LOGOUT_URL = "/logout";
public static final String SIGNUP_URL = "/signup";
public static final String DEFAULT_PASSWORD = "123456";

View File

@@ -1,13 +1,23 @@
package com.ip.library.core.configuration;
import org.modelmapper.ModelMapper;
import org.modelmapper.PropertyMap;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.ip.library.core.model.BaseEntity;
@Configuration
public class MapperConfiguration {
@Bean
ModelMapper modelMapper() {
return new ModelMapper();
final ModelMapper mapper = new ModelMapper();
mapper.addMappings(new PropertyMap<Object, BaseEntity>() {
@Override
protected void configure() {
skip(destination.getId());
}
});
return mapper;
}
}

View File

@@ -0,0 +1,14 @@
package com.ip.library.core.configuration;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addViewControllers(@NonNull ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}
}

View File

@@ -1,83 +1,53 @@
package com.ip.library.core.error;
import java.io.IOException;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.ModelAndView;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@RestControllerAdvice
public class AdviceController implements AuthenticationEntryPoint {
@ControllerAdvice
public class AdviceController {
private final Logger log = LoggerFactory.getLogger(AdviceController.class);
public static ErrorCauseDto getRootCause(Throwable throwable) {
private static Throwable getRootCause(Throwable throwable) {
Throwable rootCause = throwable;
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
rootCause = rootCause.getCause();
}
final StackTraceElement firstError = rootCause.getStackTrace()[0];
return new ErrorCauseDto(
rootCause.getClass().getName(),
firstError.getMethodName(),
firstError.getFileName(),
firstError.getLineNumber());
return rootCause;
}
private ResponseEntity<ErrorDto> handleException(Throwable throwable, HttpStatusCode httpCode) {
private static Map<String, Object> getAttributes(HttpServletRequest request, Throwable throwable) {
final Throwable rootCause = getRootCause(throwable);
final StackTraceElement firstError = rootCause.getStackTrace()[0];
return Map.of(
"message", rootCause.getMessage(),
"url", request.getRequestURL(),
"exception", rootCause.getClass().getName(),
"file", firstError.getFileName(),
"method", firstError.getMethodName(),
"line", firstError.getLineNumber());
}
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, Throwable throwable) throws Throwable {
if (AnnotationUtils.findAnnotation(throwable.getClass(),
ResponseStatus.class) != null) {
throw throwable;
}
log.error("{}", throwable.getMessage());
throwable.printStackTrace();
final ErrorDto errorDto = new ErrorDto(throwable.getMessage(), AdviceController.getRootCause(throwable));
return new ResponseEntity<>(errorDto, httpCode);
}
@Override
public void commence(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException authException) throws IOException, ServletException {
final ResponseEntity<ErrorDto> body = handleException(authException, HttpStatus.UNAUTHORIZED);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
response.setStatus(body.getStatusCode().value());
response.getWriter().write(new ObjectMapper().writeValueAsString(body.getBody()));
}
@ExceptionHandler(AccessDeniedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN)
public ResponseEntity<ErrorDto> handleAccessDeniedException(Throwable throwable) {
return handleException(throwable, HttpStatus.FORBIDDEN);
}
@ExceptionHandler(NotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ResponseEntity<ErrorDto> handleNotFoundException(Throwable throwable) {
return handleException(throwable, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ResponseEntity<ErrorDto> handleDataIntegrityViolationException(Throwable throwable) {
return handleException(throwable, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ResponseEntity<ErrorDto> handleAnyException(Throwable throwable) {
return handleException(throwable, HttpStatus.INTERNAL_SERVER_ERROR);
final ModelAndView model = new ModelAndView();
model.addAllObjects(getAttributes(request, throwable));
model.setViewName("error");
return model;
}
}

View File

@@ -1,31 +0,0 @@
package com.ip.library.core.error;
class ErrorCauseDto {
private String exception;
private String methodName;
private String fineName;
private int lineNumber;
ErrorCauseDto(String exception, String methodName, String fineName, int lineNumber) {
this.exception = exception;
this.methodName = methodName;
this.fineName = fineName;
this.lineNumber = lineNumber;
}
public String getException() {
return exception;
}
public String getMethodName() {
return methodName;
}
public String getFineName() {
return fineName;
}
public int getLineNumber() {
return lineNumber;
}
}

View File

@@ -1,20 +0,0 @@
package com.ip.library.core.error;
public class ErrorDto {
private String error;
private ErrorCauseDto rootCause;
public ErrorDto(String error, ErrorCauseDto rootCause) {
this.error = error;
this.rootCause = rootCause;
}
public String getError() {
return error;
}
public ErrorCauseDto getRootCause() {
return rootCause;
}
}

View File

@@ -1,11 +0,0 @@
package com.ip.library.core.jwt;
public class JwtException extends RuntimeException {
public JwtException(Throwable throwable) {
super(throwable);
}
public JwtException(String message) {
super(message);
}
}

View File

@@ -1,58 +0,0 @@
package com.ip.library.core.jwt;
import java.io.IOException;
import org.springframework.lang.NonNull;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;
import com.ip.library.controllers.users.UserService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class JwtFilter extends OncePerRequestFilter {
private static final String AUTHORIZATION = "Authorization";
public static final String TOKEN_BEGIN_STR = "Bearer ";
private final UserService userService;
public JwtFilter(UserService userService) {
this.userService = userService;
}
private String getTokenFromRequest(HttpServletRequest request) {
String bearer = request.getHeader(AUTHORIZATION);
if (StringUtils.hasText(bearer) && StringUtils.startsWithIgnoreCase(bearer, TOKEN_BEGIN_STR)) {
return bearer.substring(TOKEN_BEGIN_STR.length());
}
return null;
}
@Override
protected void doFilterInternal(
@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull FilterChain filterChain) throws ServletException, IOException {
final String token = getTokenFromRequest(request);
if (!StringUtils.hasText(token)) {
filterChain.doFilter(request, response);
return;
}
final UserDetails user = userService.getByToken(token);
final UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(
user,
null,
user.getAuthorities());
final SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(auth);
SecurityContextHolder.setContext(context);
filterChain.doFilter(request, response);
}
}

View File

@@ -1,18 +0,0 @@
package com.ip.library.core.jwt;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "jwt", ignoreInvalidFields = true)
public class JwtProperties {
private String key = "";
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}

View File

@@ -1,110 +0,0 @@
package com.ip.library.core.jwt;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Optional;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
@Component
public class JwtProvider {
private static final Logger log = LoggerFactory.getLogger(JwtProvider.class);
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
private static final String ISSUER = "auth0";
private final Algorithm algorithm;
private final JWTVerifier verifier;
public JwtProvider(JwtProperties jwtProperties) {
final String key = jwtProperties.getKey();
if (!StringUtils.hasText(key)) {
log.info("Generate new JWT key");
try {
final MessageDigest salt = MessageDigest.getInstance("SHA-256");
salt.update(UUID.randomUUID().toString().getBytes(StandardCharsets.UTF_8));
final String hex = bytesToHex(salt.digest());
log.info("Use generated JWT key for prod \n{}", hex);
algorithm = Algorithm.HMAC256(hex);
} catch (NoSuchAlgorithmException e) {
throw new JwtException(e);
}
} else {
log.info("Use JWT key from config \n{}", key);
algorithm = Algorithm.HMAC256(key);
}
verifier = JWT.require(algorithm)
.withIssuer(ISSUER)
.build();
}
private static String bytesToHex(byte[] bytes) {
byte[] hexChars = new byte[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
}
return new String(hexChars, StandardCharsets.UTF_8);
}
public String generateToken(String login) {
final Date issueDate = Date.from(LocalDate.now()
.atStartOfDay(ZoneId.systemDefault())
.toInstant());
final Date expireDate = Date.from(LocalDate.now()
.plusDays(15)
.atStartOfDay(ZoneId.systemDefault())
.toInstant());
return JWT.create()
.withIssuer(ISSUER)
.withIssuedAt(issueDate)
.withExpiresAt(expireDate)
.withSubject(login)
.sign(algorithm);
}
private DecodedJWT validateToken(String token) {
try {
return verifier.verify(token);
} catch (JWTVerificationException e) {
throw new JwtException(String.format("Token verification error: %s", e.getMessage()));
}
}
public boolean isTokenValid(String token) {
if (!StringUtils.hasText(token)) {
return false;
}
try {
validateToken(token);
return true;
} catch (JwtException e) {
log.error(e.getMessage());
return false;
}
}
public Optional<String> getLoginFromToken(String token) {
try {
return Optional.ofNullable(validateToken(token).getSubject());
} catch (JwtException e) {
log.error(e.getMessage());
return Optional.empty();
}
}
}

View File

@@ -2,60 +2,55 @@ package com.ip.library.core.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.annotation.web.configurers.HeadersConfigurer.FrameOptionsConfig;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.util.CollectionUtils;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import com.ip.library.controllers.authors.AuthorController;
import com.ip.library.controllers.books.BookController;
import com.ip.library.controllers.types.TypeController;
import com.ip.library.controllers.users.UserController;
import com.ip.library.controllers.users.UserRole;
import com.ip.library.controllers.users.UserService;
import com.ip.library.controllers.users.UserSignupController;
import com.ip.library.core.configuration.Constants;
import com.ip.library.core.error.AdviceController;
import com.ip.library.core.jwt.JwtFilter;
@Configuration
@EnableWebSecurity
@EnableMethodSecurity(securedEnabled = true)
public class SecurityConfiguration {
@Bean
SecurityFilterChain filterChain(
HttpSecurity httpSecurity,
UserService userService,
AdviceController adviceController) throws Exception {
SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
httpSecurity.headers(headers -> headers.frameOptions(FrameOptionsConfig::sameOrigin));
httpSecurity.csrf(AbstractHttpConfigurer::disable);
httpSecurity.cors(Customizer.withDefaults());
httpSecurity.sessionManagement(sessionManagement -> sessionManagement
.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
httpSecurity.authorizeHttpRequests(requests -> requests
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**", "/swagger-ui.html")
.requestMatchers("/css/**", "/webjars/**", "/*.svg")
.permitAll());
httpSecurity.authorizeHttpRequests(requests -> requests
.requestMatchers("/h2-console/**").hasRole(UserRole.ADMIN.name())
.requestMatchers(HttpMethod.POST, Constants.SIGNUP_URL).anonymous()
.requestMatchers(HttpMethod.POST, Constants.LOGIN_URL).anonymous()
.requestMatchers(TypeController.URL).hasRole(UserRole.ADMIN.name())
.requestMatchers(BookController.URL).hasRole(UserRole.ADMIN.name())
.requestMatchers(UserController.URL).hasRole(UserRole.ADMIN.name())
.requestMatchers(AuthorController.URL).hasRole(UserRole.ADMIN.name())
.requestMatchers(UserSignupController.URL).anonymous()
.requestMatchers(Constants.LOGIN_URL).anonymous()
.anyRequest().authenticated());
httpSecurity.exceptionHandling(exceptionHandling -> exceptionHandling
.authenticationEntryPoint(adviceController));
httpSecurity.formLogin(formLogin -> formLogin
.loginPage(Constants.LOGIN_URL));
httpSecurity.addFilterBefore(new JwtFilter(userService), UsernamePasswordAuthenticationFilter.class);
httpSecurity.rememberMe(rememberMe -> rememberMe.key("uniqueAndSecret"));
httpSecurity.logout(logout -> logout
.deleteCookies("JSESSIONID"));
return httpSecurity.build();
}
@@ -72,18 +67,4 @@ public class SecurityConfiguration {
PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
if (!CollectionUtils.isEmpty(config.getAllowedOrigins())
|| !CollectionUtils.isEmpty(config.getAllowedOriginPatterns())) {
source.registerCorsConfiguration(Constants.API_URL + "/**", config);
source.registerCorsConfiguration("/v3/api-docs", config);
source.registerCorsConfiguration("/swagger-ui/**", config);
}
return new CorsFilter(source);
}
}

View File

@@ -1,31 +0,0 @@
package com.ip.library.core.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
@Configuration
@OpenAPIDefinition
public class SwaggerConfiguration {
private static final String SCHEME_NAME = "JWT";
private static final String SCHEME = "bearer";
private SecurityScheme createBearerScheme() {
return new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme(SCHEME);
}
@Bean
OpenAPI customOpenApi() {
return new OpenAPI()
.components(new Components()
.addSecuritySchemes(SCHEME_NAME, createBearerScheme()))
.addSecurityItem(new SecurityRequirement().addList(SCHEME_NAME));
}
}

View File

@@ -0,0 +1,67 @@
html,
body {
height: 100%;
}
h1 {
font-size: 1.5em;
}
h2 {
font-size: 1.25em;
}
h3 {
font-size: 1.1em;
}
td form {
margin: 0;
padding: 0;
margin-top: -.25em;
}
.button-fixed-width {
width: 150px;
}
.button-link {
padding: 0;
}
.invalid-feedback {
display: block;
}
.w-10 {
width: 10% !important;
}
.my-navbar {
background-color: #3c3c3c !important;
}
.my-navbar .link a:hover {
text-decoration: underline;
}
.my-navbar .logo {
width: 26px;
height: 26px;
}
.my-footer {
background-color: #2c2c2c;
height: 32px;
color: rgba(255, 255, 255, 0.5);
}
.cart-image {
width: 3.1rem;
padding: 0.25rem;
border-radius: 0.5rem;
}
.cart-item {
height: auto;
}

View File

@@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-book-half" viewBox="0 0 16 16">
<path d="M8.5 2.687c.654-.689 1.782-.886 3.112-.752 1.234.124 2.503.523 3.388.893v9.923c-.918-.35-2.107-.692-3.287-.81-1.094-.111-2.278-.039-3.213.492V2.687zM8 1.783C7.015.936 5.587.81 4.287.94c-1.514.153-3.042.672-3.994 1.105A.5.5 0 0 0 0 2.5v11a.5.5 0 0 0 .707.455c.882-.4 2.303-.881 3.68-1.02 1.409-.142 2.59.087 3.223.877a.5.5 0 0 0 .78 0c.633-.79 1.814-1.019 3.222-.877 1.378.139 2.8.62 3.681 1.02A.5.5 0 0 0 16 13.5v-11a.5.5 0 0 0-.293-.455c-.952-.433-2.48-.952-3.994-1.105C10.413.809 8.985.936 8 1.783"/>
</svg>

After

Width:  |  Height:  |  Size: 648 B

View File

@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редакторовать автора</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/api/1.0/author/edit/{id}(id=${author.id})}" th:object="${author}" method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Автор</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" href="/api/1.0/author">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Авторы</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Авторы</h2>
<div>
<a href="/api/1.0/author/edit/" class="btn btn-primary">Добавить автора</a>
</div>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Автор</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="author : ${items}">
<th scope="row" th:text="${author.id}"></th>
<td th:text="${author.name}"></td>
<td>
<form th:action="@{/api/1.0/author/edit/{id}(id=${author.id})}" method="get">
<button type="submit" class="btn btn-link button-link">Редактировать</button>
</form>
</td>
<td>
<form th:action="@{/api/1.0/author/delete/{id}(id=${author.id})}" method="post">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">Удалить</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
</th:block>
</main>
</body>
</html>

View File

@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редакторовать книгу</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/api/1.0/book/edit/{id}(id=${book.id},page=${page})}" th:object="${book}"
method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Название книги</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<select class="form-control" id="typeId" name="typeId" th:field="*{typeId}">
<option value="">Выберите жанр</option>
<option th:each="type : ${types}"
th:value="${type.id}"
th:text="${type.name}">
</option>
</select>
</div>
<div class="mb-3">
<label for="name" class="form-label">Авторы:</label>
<select multiple="true" class="form-control" id="authorsId" name="authorsId" th:field="*{authorsId}">
<option th:each="author : ${authors}"
th:value="${author.id}"
th:text="${author.name}">
</option>
</select>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" th:href="@{/api/1.0/book(page=${page})}">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Поиск</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Поиск</h2>
<form method="get">
<div class="mb-3">
<select class="form-control" id="typeId" name="typeId">
<option value="-1">Выберите жанр</option>
<option th:each="type : ${types}"
th:value="${type.id}"
th:text="${type.name}"
th:selected="${type.id==typeId}">
</option>
</select>
</div>
<div class="mb-3">
<select class="form-control" id="authorId" name="authorId">
<option value="-1">Выберите автора</option>
<option th:each="author : ${authors}"
th:value="${author.id}"
th:text="${author.name}"
th:selected="${author.id==authorId}">
</option>
</select>
</div>
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Обновить</button>
</form>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Название</th>
<th scope="col" class="w-auto">Жанр</th>
<th scope="col" class="w-auto">Автор</th>
<th scope="col" class="w-auto">Добавили в избранное</th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${items}">
<th scope="row" th:text="${book.id}"></th>
<td th:text="${book.name}"></td>
<td th:text="${book.typeName}"></td>
<td th:text="${book.authorName}"></td>
<td th:text="${book.fansNumber}"></td>
<td>
<form th:action="@{/api/1.0/addFavorite/{id}(id=${book.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<input type="hidden" th:name="authorId" th:value="${authorId}">
<input type="hidden" th:name="typeId" th:value="${typeId}">
<button type="submit" class="btn btn-link button-link"
th:hidden="${book.isChosen}">В избранное</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
<th:block th:replace="~{ pagination :: pagination-with-params (
url=${'api/1.0/search'},
totalPages=${totalPages},
currentPage=${currentPage},
searchParams=${searchParams}
)}" />
</th:block>
</main>
</body>
</html>

View File

@@ -0,0 +1,60 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Книги</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Книги</h2>
<div>
<a th:href="@{/api/1.0/book/edit/(page=${page})}" class="btn btn-primary">Добавить книгу</a>
</div>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Название</th>
<th scope="col" class="w-auto">Жанр</th>
<th scope="col" class="w-auto">Автор</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${items}">
<th scope="row" th:text="${book.id}"></th>
<td th:text="${book.name}"></td>
<td th:text="${book.typeName}"></td>
<td th:text="${book.authorName}"></td>
<td>
<form th:action="@{/api/1.0/book/edit/{id}(id=${book.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">Редактировать</button>
</form>
</td>
<td>
<form th:action="@{/api/1.0/book/delete/{id}(id=${book.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">Удалить</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
<th:block th:replace="~{ pagination :: pagination (
url=${'api/1.0/book'},
totalPages=${totalPages},
currentPage=${currentPage}) }" />
</th:block>
</main>
</body>
</html>

View File

@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html lang="ru" data-bs-theme="dark" xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity6">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/logo.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">MyLibrary</title>
<script type="text/javascript" src="/webjars/bootstrap/5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/5.3.3/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/webjars/bootstrap-icons/1.11.3/font/bootstrap-icons.min.css" />
<link rel="stylesheet" href="/css/style.css" />
</head>
<body class="h-100 d-flex flex-column">
<nav class="navbar navbar-expand-md my-navbar" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="/">
<i class="bi bi-book-half d-inline-block align-top me-1 logo"></i>
MyLibrary
</a>
<th:block sec:authorize="isAuthenticated()" th:with="userName=${#authentication.name}">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#main-navbar"
aria-controls="main-navbar" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="main-navbar">
<ul class="navbar-nav me-auto link" th:with="activeLink=${#objects.nullSafe(servletPath, '')}">
<a class="nav-link" href="/api/1.0/search"
th:classappend="${activeLink.startsWith('/api/1.0/search') ? 'active' : ''}">
Поиск
</a>
<a class="nav-link" href="/"
th:classappend="${activeLink.startsWith('/api/1.0/search') ? 'active' : ''}">
Избранное
</a>
<th:block sec:authorize="hasRole('ADMIN')">
<a class="nav-link" href="/api/1.0/book"
th:classappend="${activeLink.startsWith('/api/1.0/book') ? 'active' : ''}">
Книги
</a>
<a class="nav-link" href="/api/1.0/user"
th:classappend="${activeLink.startsWith('/api/1.0/user') ? 'active' : ''}">
Пользователи
</a>
<a class="nav-link" href="/api/1.0/author"
th:classappend="${activeLink.startsWith('/api/1.0/author') ? 'active' : ''}">
Авторы
</a>
<a class="nav-link" href="/api/1.0/type"
th:classappend="${activeLink.startsWith('/api/1.0/type') ? 'active' : ''}">
Жанры книг
</a>
<a class="nav-link" href="/h2-console/" target="_blank">Консоль H2</a>
</th:block>
</ul>
<ul class="navbar-nav" th:if="${not #strings.isEmpty(userName)}">
<form th:action="@{/logout}" method="post">
<button type="submit" class="navbar-brand nav-link" onclick="return confirm('Вы уверены?')">
Выход ([[${userName}]])
</button>
</form>
</ul>
</div>
</th:block>
</div>
</nav>
<main class="container-fluid p-2" layout:fragment="content">
</main>
<footer class="my-footer mt-auto d-flex flex-shrink-0 justify-content-center align-items-center">
Автор, [[${#dates.year(#dates.createNow())}]]
</footer>
</body>
</html>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Ошибка</title>
</head>
<body>
<main layout:fragment="content">
<ul class="list-group mb-2">
<th:block th:if="${#strings.isEmpty(message)}">
<li class="list-group-item">
Неизвестная ошибка
</li>
</th:block>
<th:block th:if="${not #strings.isEmpty(message)}">
<li class="list-group-item">
<strong>Ошибка:</strong> [[${message}]]
</li>
</th:block>
<th:block th:if="${not #strings.isEmpty(url)}">
<li class="list-group-item">
<strong>Адрес:</strong> [[${url}]]
</li>
<li class="list-group-item">
<strong>Класс исключения:</strong> [[${exception}]]
</li>
<li class="list-group-item">
[[${method}]] ([[${file}]]:[[${line}]])
</li>
</th:block>
</ul>
<a class="btn btn-primary button-fixed-width" href="/">На главную</a>
</main>
</body>
</html>

View File

@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Вход</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/login}" method="post">
<div th:if="${param.error}" class="alert alert-danger">
Неверный логин или пароль
</div>
<div th:if="${param.logout}" class="alert alert-success">
Выход успешно произведен
</div>
<div th:if="${param.signup}" class="alert alert-success">
Пользователь успешно создан
</div>
<div class="mb-3">
<label for="username" class="form-label">Имя пользователя</label>
<input type="text" id="username" name="username" class="form-control" required minlength="3"
maxlength="20">
</div>
<div class="mb-3">
<label for="password" class="form-label">Пароль</label>
<input type="password" id="password" name="password" class="form-control" required minlength="3"
maxlength="20">
</div>
<div class="form-check mb-3">
<input class="form-check-input" type="checkbox" id="remember-me" name="remember-me" checked>
<label class="form-check-label" for="remember-me">Запомнить меня</label>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Войти</button>
<a class="btn btn-secondary button-fixed-width" href="/signup">Регистрация</a>
</div>
</form>
</main>
</body>
</html>
</html>

View File

@@ -0,0 +1,98 @@
<!DOCTYPE html>
<html lang="ru" xmlns:th="http://www.thymeleaf.org">
<body>
<th:block th:fragment="pagination (url, totalPages, currentPage)">
<nav th:if="${totalPages > 1}" th:with="
maxPage=2,
currentPage=${currentPage + 1}">
<ul class="pagination justify-content-center"
th:with="
seqFrom=${currentPage - maxPage < 1 ? 1 : currentPage - maxPage},
seqTo=${currentPage + maxPage > totalPages ? totalPages : currentPage + maxPage}">
<th:block th:if="${currentPage > maxPage + 1}">
<li class="page-item">
<a class="page-link" aria-label="Previous"
th:href="@{/{url}?page=0(url=${url})}">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li class="page-item disabled">
<span class="page-link" aria-label="Previous">
<span aria-hidden="true">&hellip;</span>
</span>
</li>
</th:block>
<li class="page-item" th:each="page : ${#numbers.sequence(seqFrom, seqTo)}"
th:classappend="${page == currentPage} ? 'active' : ''">
<a class=" page-link"
th:href="@{/{url}?page={page}(url=${url},page=${page - 1})}">
<span th:text="${page}" />
</a>
</li>
<th:block th:if="${currentPage < totalPages - maxPage}">
<li class="page-item disabled">
<span class="page-link" aria-label="Previous">
<span aria-hidden="true">&hellip;</span>
</span>
</li>
<li class="page-item">
<a class="page-link" aria-label="Next"
th:href="@{/{url}?page={page}(url=${url},page=${totalPages - 1})}">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</th:block>
</ul>
</nav>
</th:block>
<th:block th:fragment="pagination-with-params (url, totalPages, currentPage, searchParams)">
<nav th:if="${totalPages > 1}" th:with="
maxPage=2,
currentPage=${currentPage + 1}">
<ul class="pagination justify-content-center"
th:with="
seqFrom=${currentPage - maxPage < 1 ? 1 : currentPage - maxPage},
seqTo=${currentPage + maxPage > totalPages ? totalPages : currentPage + maxPage}">
<th:block th:if="${currentPage > maxPage + 1}">
<li class="page-item">
<a class="page-link" aria-label="Previous"
th:href="@{/{url}?page=0&(url=${url})} + ${searchParams}">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li class="page-item disabled">
<span class="page-link" aria-label="Previous">
<span aria-hidden="true">&hellip;</span>
</span>
</li>
</th:block>
<li class="page-item" th:each="page : ${#numbers.sequence(seqFrom, seqTo)}"
th:classappend="${page == currentPage} ? 'active' : ''">
<a class=" page-link"
th:href="@{/{url}?page={page}&(url=${url},page=${page - 1})} + ${searchParams}">
<span th:text="${page}" />
</a>
</li>
<th:block th:if="${currentPage < totalPages - maxPage}">
<li class="page-item disabled">
<span class="page-link" aria-label="Previous">
<span aria-hidden="true">&hellip;</span>
</span>
</li>
<li class="page-item">
<a class="page-link" aria-label="Next"
th:href="@{/{url}?page={page}&(url=${url},page=${totalPages - 1})} + ${searchParams}">
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</th:block>
</ul>
</nav>
</th:block>
</body>
</html>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Вход</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/signup}" th:object="${user}" method="post">
<div class="mb-3">
<label for="login" class="form-label">Имя пользователя</label>
<input type="text" th:field="*{login}" id="login" class="form-control">
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="password" class="form-label">Пароль</label>
<input type="password" th:field="*{password}" id="password" class="form-control">
<div th:if="${#fields.hasErrors('password')}" th:errors="*{password}" class="invalid-feedback"></div>
</div>
<div class="mb-3">
<label for="passwordConfirm" class="form-label">Пароль (подтверждение)</label>
<input type="password" th:field="*{passwordConfirm}" id="passwordConfirm" class="form-control">
<div th:if="${#fields.hasErrors('passwordConfirm')}" th:errors="*{passwordConfirm}"
class="invalid-feedback"></div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Регистрация</button>
<a class="btn btn-secondary button-fixed-width" href="/">Отмена</a>
</div>
</form>
</main>
</body>
</html>
</html>

View File

@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редакторовать жанр</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/api/1.0/type/edit/{id}(id=${type.id})}" th:object="${type}" method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="name" class="form-label">Жанр книги</label>
<input type="text" th:field="*{name}" id="name" class="form-control">
<div th:if="${#fields.hasErrors('name')}" th:errors="*{name}" class="invalid-feedback"></div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" href="/api/1.0/type">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Жанры книг</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Жанры книг</h2>
<div>
<a href="/api/1.0/type/edit/" class="btn btn-primary">Добавить жанр</a>
</div>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Жанр</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="type : ${items}">
<th scope="row" th:text="${type.id}"></th>
<td th:text="${type.name}"></td>
<td>
<form th:action="@{/api/1.0/type/edit/{id}(id=${type.id})}" method="get">
<button type="submit" class="btn btn-link button-link">Редактировать</button>
</form>
</td>
<td>
<form th:action="@{/api/1.0/type/delete/{id}(id=${type.id})}" method="post">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">Удалить</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
</th:block>
</main>
</body>
</html>

View File

@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Редакторовать пользователя</title>
</head>
<body>
<main layout:fragment="content">
<form action="#" th:action="@{/api/1.0/user/edit/{id}(id=${user.id},page=${page})}" th:object="${user}"
method="post">
<div class="mb-3">
<label for="id" class="form-label">ID</label>
<input type="text" th:value="*{id}" id="id" class="form-control" readonly disabled>
</div>
<div class="mb-3">
<label for="login" class="form-label">Имя пользователя</label>
<input type="text" th:field="*{login}" id="login" class="form-control">
<div th:if="${#fields.hasErrors('login')}" th:errors="*{login}" class="invalid-feedback"></div>
</div>
<div class="mb-3 d-flex flex-row">
<button class="btn btn-primary me-2 button-fixed-width" type="submit">Сохранить</button>
<a class="btn btn-secondary button-fixed-width" th:href="@{/api/1.0/user(page=${page})}">Отмена</a>
</div>
</form>
</main>
</body>
</html>

View File

@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Избранное</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Избранное</h2>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Название</th>
<th scope="col" class="w-auto">Жанр</th>
<th scope="col" class="w-auto">Автор</th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${items}">
<th scope="row" th:text="${book.id}"></th>
<td th:text="${book.name}"></td>
<td th:text="${book.typeName}"></td>
<td th:text="${book.authorName}"></td>
<td>
<form th:action="@{/api/1.0/removeFavorite/{id}(id=${book.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">Удалить</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
<th:block th:replace="~{ pagination :: pagination (
url=${''},
totalPages=${totalPages},
currentPage=${currentPage}) }" />
</th:block>
</main>
</body>
</html>

View File

@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html lang="ru" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
<title>Пользователи</title>
</head>
<body>
<main layout:fragment="content">
<th:block th:switch="${items.size()}">
<h2 th:case="0">Данные отсутствуют</h2>
<th:block th:case="*">
<h2>Пользователи</h2>
<table class="table">
<caption></caption>
<thead>
<tr>
<th scope="col" class="w-10">ID</th>
<th scope="col" class="w-auto">Имя пользователя</th>
<th scope="col" class="w-10"></th>
<th scope="col" class="w-10"></th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${items}">
<th scope="row" th:text="${user.id}"></th>
<td th:text="${user.login}"></td>
<td>
<form th:action="@{/api/1.0/user/edit/{id}(id=${user.id})}" method="get">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link">Редактировать</button>
</form>
</td>
<td>
<form th:action="@{/api/1.0/user/delete/{id}(id=${user.id})}" method="post">
<input type="hidden" th:name="page" th:value="${page}">
<button type="submit" class="btn btn-link button-link"
onclick="return confirm('Вы уверены?')">Удалить</button>
</form>
</td>
</tr>
</tbody>
</table>
</th:block>
<th:block th:replace="~{ pagination :: pagination (
url=${'api/1.0/user'},
totalPages=${totalPages},
currentPage=${currentPage}) }" />
</th:block>
</main>
</body>
</html>

View File

@@ -80,9 +80,5 @@ class AuthorsTests {
IllegalArgumentException.class,
() -> authorService.create(new AuthorEntity(entity.getName()))
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> authorService.update(entity.getId(), new AuthorEntity(entity.getName()))
);
}
}

View File

@@ -1,6 +1,8 @@
package com.ip.library;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.AfterEach;
@@ -46,13 +48,22 @@ class BooksTests {
var type2 = typeService.create(new TypeEntity("type2"));
author1 = authorService.create(new AuthorEntity("author1"));
var author2 = authorService.create(new AuthorEntity("author2"));
book1 = bookService.create(new BookEntity("book1", type1));
book2 = bookService.create(new BookEntity("book2", type1));
book3 = bookService.create(new BookEntity("book3", type2));
bookService.addAuthor(author1.getId(), book1.getId());
bookService.addAuthor(author1.getId(), book3.getId());
bookService.addAuthor(author2.getId(), book1.getId());
bookService.addAuthor(author2.getId(), book2.getId());
book1 = bookService.create(new BookEntity("book1", type1),
Arrays.asList(new Long[] {
author1.getId(),
author2.getId()
}));
book2 = bookService.create(new BookEntity("book2", type1),
Arrays.asList(new Long[] {
author2.getId()
}));
book3 = bookService.create(new BookEntity("book3", type2),
Arrays.asList(new Long[] {
author1.getId()
}));
book1 = bookService.get(book1.getId());
book2 = bookService.get(book2.getId());
book3 = bookService.get(book3.getId());
}
@Test
@@ -60,6 +71,7 @@ class BooksTests {
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertEquals("book1", book1.getName());
Assertions.assertEquals(type1, book1.getType());
Assertions.assertEquals(2, bookService.getAuthors(book1.getId()).size());
Assertions.assertEquals(0, bookService.getBookSubscribersNumber(book1.getId()));
}
@@ -80,12 +92,23 @@ class BooksTests {
Assertions.assertEquals(testType, book1.getType());
}
@Test
void updateAuthorsTest() {
AuthorEntity testAuthor = authorService.create(new AuthorEntity("author"));
final List<Long> testAuthors = Arrays.asList(new Long[] {
testAuthor.getId()
});
book1 = bookService.update(book1.getId(), book1, testAuthors);
Assertions.assertTrue(bookService.getAuthors(book1.getId()).contains(testAuthor));
Assertions.assertEquals(1, bookService.getAuthors(book1.getId()).size());
}
@Test
void deleteTest() {
bookService.delete(book1.getId());
Assertions.assertEquals(2, bookService.getAll().size());
final BookEntity newEntity = bookService.create(
new BookEntity(book1.getName(), book1.getType()));
new BookEntity(book1.getName(), book1.getType()), new ArrayList<>());
Assertions.assertEquals(3, bookService.getAll().size());
Assertions.assertNotEquals(book1.getId(), newEntity.getId());
}
@@ -94,11 +117,13 @@ class BooksTests {
void nullNameTest() {
Assertions.assertThrows(
DataIntegrityViolationException.class,
() -> bookService.create(new BookEntity(null, book1.getType()))
() -> bookService.create(new BookEntity(null, book1.getType()),
new ArrayList<>())
);
Assertions.assertThrows(
DataIntegrityViolationException.class,
() -> bookService.update(book1.getId(), new BookEntity(null, book1.getType()))
() -> bookService.update(book1.getId(), new BookEntity(null, book1.getType()),
new ArrayList<>())
);
}
@@ -106,11 +131,13 @@ class BooksTests {
void nullTypeTest() {
Assertions.assertThrows(
DataIntegrityViolationException.class,
() -> bookService.create(new BookEntity(book1.getName() + "TEST", null))
() -> bookService.create(new BookEntity(book1.getName() + "TEST", null),
new ArrayList<>())
);
Assertions.assertThrows(
DataIntegrityViolationException.class,
() -> bookService.update(book1.getId(), new BookEntity(book1.getName() + "TEST", null))
() -> bookService.update(book1.getId(), new BookEntity(book1.getName() + "TEST", null),
new ArrayList<>())
);
}
@@ -118,11 +145,8 @@ class BooksTests {
void uniqueNameTest() {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> bookService.create(new BookEntity(book1.getName(), book1.getType()))
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> bookService.update(book1.getId(), new BookEntity(book1.getName(), book1.getType()))
() -> bookService.create(new BookEntity(book1.getName(), book1.getType()),
new ArrayList<>())
);
}
@@ -141,4 +165,14 @@ class BooksTests {
Assertions.assertEquals(1, list.size());
Assertions.assertTrue(list.contains(book1));
}
@Test
void removeAuthorTest() {
Assertions.assertTrue(
bookService.getAuthors(book1.getId()).contains(author1));
bookService.removeAuthor(author1.getId(), book1.getId());
book1 = bookService.get(book1.getId());
Assertions.assertTrue(
!bookService.getAuthors(book1.getId()).contains(author1));
}
}

View File

@@ -1,6 +1,7 @@
package com.ip.library;
import java.util.List;
import java.util.ArrayList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
@@ -40,20 +41,31 @@ class FavoritesTests {
void createData() {
removeData();
TypeEntity type = typeService.create(new TypeEntity("type1"));
book1 = bookService.create(new BookEntity("book1", type));
book2 = bookService.create(new BookEntity("book2", type));
book1 = bookService.create(new BookEntity("book1", type), new ArrayList<Long>());
book2 = bookService.create(new BookEntity("book2", type), new ArrayList<Long>());
user1 = userService.create(new UserEntity("user3", "aqw2sed45"));
user2 = userService.create(new UserEntity("user1", "123"));
userService.create(new UserEntity("user2", "456"));
userService.addFavorite(user1.getId(), book2.getId());
userService.addFavorite(user2.getId(), book1.getId());
userService.addFavorite(user2.getId(), book2.getId());
book1 = bookService.get(book1.getId());
book2 = bookService.get(book2.getId());
}
@Test
void getBookSubscribersNumberTest() {
Assertions.assertEquals(1, bookService.getBookSubscribersNumber(book1.getId()));
Assertions.assertEquals(2, bookService.getBookSubscribersNumber(book2.getId()));
void getFansNumberTest() {
Assertions.assertEquals(1, book1.getFansNumber());
Assertions.assertEquals(2, book2.getFansNumber());
}
@Test
void removeFavoriteTest() {
Assertions.assertTrue(userService.getUserFavorities(user2.getId()).contains(book1));
userService.removeFavorite(user2.getId(), book1.getId());
Assertions.assertTrue(!userService.getUserFavorities(user2.getId()).contains(book1));
book1 = bookService.get(book1.getId());
Assertions.assertEquals(0, book1.getFansNumber());
}
@Test

View File

@@ -82,9 +82,5 @@ class TypesTests {
IllegalArgumentException.class,
() -> typeService.create(new TypeEntity(entity.getName()))
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> typeService.update(entity.getId(), new TypeEntity(entity.getName()))
);
}
}

View File

@@ -76,7 +76,7 @@ class UsersTests {
@Test
void changePasswordTest() {
String newPassword = user.getPassword() + "TEST";
String newPassword = "TEST";
user = userService.changePassword(user.getId(), newPassword);
Assertions.assertEquals(newPassword, user.getPassword());
}
@@ -120,9 +120,5 @@ class UsersTests {
IllegalArgumentException.class,
() -> userService.create(new UserEntity(user.getLogin(), user.getPassword()))
);
Assertions.assertThrows(
IllegalArgumentException.class,
() -> userService.update(user.getId(), new UserEntity(user.getLogin()))
);
}
}