Mock basic REST api

This commit is contained in:
Dmitry Isaenko 2024-01-24 20:12:48 +03:00
parent 5a2d4ebe97
commit c9d0e58557
6 changed files with 113 additions and 2 deletions

View file

@ -2,6 +2,8 @@ package ru.redrise.marinesco.library;
import java.util.List; import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany; import jakarta.persistence.ManyToMany;
@ -15,8 +17,9 @@ import lombok.NoArgsConstructor;
public class Author { public class Author {
@Id @Id
private Long id; private Long id;
private String authorName; private String import lombok.extern.slf4j.Slf4j;import lombok.extern.slf4j.Slf4j;;
@JsonIgnore
@ManyToMany(mappedBy = "authors") @ManyToMany(mappedBy = "authors")
private List<Book> books; private List<Book> books;

View file

@ -0,0 +1,32 @@
package ru.redrise.marinesco.library.api;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.redrise.marinesco.data.AuthorRepository;
import ru.redrise.marinesco.library.Author;
@RestController
@RequestMapping(path = "/api/author",
produces = "application/json")
public class AuthorsApiController {
private AuthorRepository authorRepository;
public AuthorsApiController(AuthorRepository authorRepository){
this.authorRepository = authorRepository;
}
@GetMapping
public Iterable<Author> getAuthors(
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "sort", required = false, defaultValue = "authorName") String sortBy){
PageRequest pageRequest = PageRequest.of(
page, 10, Sort.by(sortBy).descending());
return authorRepository.findAll(pageRequest).getContent();
}
}

View file

@ -0,0 +1,38 @@
package ru.redrise.marinesco.library.api;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.redrise.marinesco.data.BookRepository;
import ru.redrise.marinesco.library.Book;
@RestController
@RequestMapping(path = "/api/book",
produces = "application/json")
public class BooksApiController {
private BookRepository bookRepository ;
public BooksApiController(BookRepository bookRepository){
this.bookRepository = bookRepository;
}
@GetMapping(params = "recent")
//@PreAuthorize("hasRole('ADMIN')") // move to security later
public Iterable<Book> recentBooks(){
return getBooks(0, "addedDate");
}
@GetMapping(params = "page")
public Iterable<Book> getBooks(@RequestParam(value = "page", required = true) Integer page,
@RequestParam(value = "sort", required = false, defaultValue = "title") String sortBy){
PageRequest pageRequest = PageRequest.of(
page, 10, Sort.by(sortBy).descending());
return bookRepository.findAll(pageRequest).getContent();
}
}

View file

@ -0,0 +1,33 @@
package ru.redrise.marinesco.library.api;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.redrise.marinesco.data.GenreRepository;
import ru.redrise.marinesco.library.Genre;
@RestController
@RequestMapping(path = "/api/genres", produces = "application/json")
public class GenresApiController {
private GenreRepository genreRepository;
public GenresApiController(GenreRepository genreRepository) {
this.genreRepository = genreRepository;
}
@GetMapping
public Iterable<Genre> getGenres(
@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
@RequestParam(value = "sort", required = false, defaultValue = "genreId") String sortBy) {
PageRequest pageRequest = PageRequest.of(
page, 10, Sort.by(sortBy).descending());
return genreRepository.findAll(pageRequest).getContent();
}
}

View file

@ -53,6 +53,9 @@ public class SecurityConfig {
.requestMatchers(mvc.pattern("/register")).anonymous() .requestMatchers(mvc.pattern("/register")).anonymous()
.requestMatchers(mvc.pattern("/login")).anonymous() .requestMatchers(mvc.pattern("/login")).anonymous()
.requestMatchers(mvc.pattern("/error")).permitAll() .requestMatchers(mvc.pattern("/error")).permitAll()
.requestMatchers(mvc.pattern("/api/**")).permitAll() // TODO: FIX
.requestMatchers(PathRequest.toH2Console()).permitAll() .requestMatchers(PathRequest.toH2Console()).permitAll()
.requestMatchers(mvc.pattern("/")).authenticated() .requestMatchers(mvc.pattern("/")).authenticated()
.requestMatchers(mvc.pattern("/profile/**")).authenticated()//.hasAnyRole("ADMIN", "USER") .requestMatchers(mvc.pattern("/profile/**")).authenticated()//.hasAnyRole("ADMIN", "USER")

View file

@ -6,7 +6,7 @@ spring:
generate-unique-name: false generate-unique-name: false
name: marinesco name: marinesco
# url: jdbc:h2:mem:marinesco # url: jdbc:h2:mem:marinesco
# url: jdbc:h2:file:/tmp/h22 url: jdbc:h2:file:/tmp/h22
username: sa username: sa
password: password:
jpa: jpa:
@ -28,6 +28,8 @@ logging:
level: level:
org: org:
springframework: INFO springframework: INFO
# file:
# path: "/tmp/log"
marinesco: marinesco:
library: library:
filesLocation: "./lib" filesLocation: "./lib"