2 Commits

Author SHA1 Message Date
06dd4eea2a third labWork 2023-05-16 11:30:27 +04:00
60dfe21df8 second labWork 2023-05-01 23:02:20 +04:00
23 changed files with 1274 additions and 29 deletions

View File

@@ -1,40 +1,52 @@
const operateButton = document.getElementById("operate"); const operateButton = document.getElementById("operate");
const operateStringButton = document.getElementById("operateString");
const val1 = document.getElementById("val1"); const val1 = document.getElementById("val1");
const val2 = document.getElementById("val2"); const val2 = document.getElementById("val2");
const select = document.getElementById("operation"); const select = document.getElementById("operation");
const finish = document.getElementById("finish"); const finish = document.getElementById("finish");
const operateFButton = document.getElementById("operateF");
const operateFStringButton = document.getElementById("operateFString");
const float1 = document.getElementById("float1");
const float2 = document.getElementById("float2");
const selectF = document.getElementById("operationF");
const finishF = document.getElementById("finishF");
operateButton.onclick=function(){ operateButton.onclick=function(){
operate(); operate("number");
}; };
function operate(){ operateStringButton.onclick=function(){
operate("string");
}
operateFButton.onclick=function(){
operate("numberF");
};
function operate(param){
switch(parseInt(select.value)){ switch(parseInt(select.value)){
case 1: case 1:
doOperation("calc1") doOperation("calc1", param)
break; break;
case 2: case 2:
doOperation("calc2") doOperation("calc2", param)
break; break;
case 3: case 3:
doOperation("calc3") doOperation("calc3", param)
break; break;
case 4: case 4:
doOperation("calc4") doOperation("calc4", param)
break; break;
}; };
} }
function checkNumber(res){ function checkNumber(res){
if(res.indexOf(".") != -1){ return res;
return parseInt(res);
}
else{
return res;
}
} }
async function doOperation(address) { function doOperation(address, type){
const response = await fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}`) console.log("Тип переменных: " + type)
let res = await response.text() fetch(`http://localhost:8080/${address}?val1=${val1.value}&val2=${val2.value}&type=${type}`)
finish.innerHTML = checkNumber(res); .then(response => response.text())
.then(res=>finish.innerHTML=checkNumber(res));
} }

View File

@@ -14,29 +14,33 @@
<form class=" d-flex flex-column align-items-center text-center"> <form class=" d-flex flex-column align-items-center text-center">
<div class=""> <div class="">
Введите число Введите число
<input id="val1" class="form-control" type="number" value="0" /> <input id="val1" class="form-control" type="numberF" value="0" />
</div> </div>
<div> <div>
Выберите действие Выберите действие
<select class="form-select" id="operation"> <select class="form-select" id="operation">
<option value="1">умножить</option> <option value="1">умножить</option>
<option value="2">возвести в степень</option> <option value="2">сложить</option>
<option value="3">факториал</option> <option value="3">вычесть</option>
<option value="4">число Фибоначчи</option> <option value="4">делить</option>
</select> </select>
</div> </div>
<div> <div>
Введите второе число Введите второе число
<input id="val2" class="form-control" type="number" value="0" /> <input id="val2" class="form-control" type="numberF" value="0" />
</div> </div>
<div> <div>
<button type="button" class="btn btn-primary" id="operate">Вычислить</button> <button type="button" class="btn btn-primary" id="operate">Вычислить</button>
</div> </div>
<div>
<button type="button" class="btn btn-primary" id="operateF">Вычислить вещественное</button>
</div>
<div>
<button type="button" class="btn btn-primary" id="operateString">Строки</button>
</div>
<div> <div>
Результат: Результат:
<p id="finish"></p> <p id="finish"></p>
</div> </div>
</form> <script src="LogForMainPage.js"></script>
<script src="../../../../demo/demo/MainPage/LogForMainPage.js"></script>
</body> </body>

View File

@@ -14,6 +14,13 @@ repositories {
dependencies { dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'com.h2database:h2:2.1.210'
implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.5'
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
} }

View File

@@ -0,0 +1,56 @@
package com.example.demo.Books.Controller;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Models.Book;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/book")
public class BookController {
private final BookService bookService;
public BookController(BookService bookService) {
this.bookService = bookService;
}
@GetMapping("/test")
public String test() {
return "Test request";
}
@GetMapping("/{id}")
public Book getBook(@PathVariable Long id) {
return bookService.findBook(id);
}
@GetMapping("/")
public List<Book> getBooks() {
return bookService.findAllBooks();
}
@PostMapping("/")
public Book createBook(@RequestParam String name) {
return bookService.addBook(name);
}
@PatchMapping("/{id}")
public Book updateBook(@PathVariable Long id,
@RequestParam("firstName") String name) {
return bookService.updateBook(id, name);
}
@PatchMapping("/add_genre/{id}")
public Book addGenre(@PathVariable Long id, @RequestParam Long genre_id) {
return bookService.addGenre(id, genre_id);
}
@DeleteMapping("/{id}")
public Book deleteBook(@PathVariable Long id) {
return bookService.deleteBook(id);
}
}

View File

@@ -0,0 +1,43 @@
package com.example.demo.Books.Controller;
import com.example.demo.Books.Service.GenreService;
import com.example.demo.Books.Models.Genre;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/genre")
public class GenreController {
private final GenreService genreService;
public GenreController(GenreService genreService) {
this.genreService = genreService;
}
@GetMapping("/{id}")
public Genre getGenre(@PathVariable Long id) {
return genreService.findGenre(id);
}
@GetMapping("/")
public List<Genre> getGenres() {
return genreService.findAllGenres();
}
@PostMapping("/")
public Genre createGenre(@RequestParam("name") String name) {
return genreService.addGenre(name);
}
@PatchMapping("/{id}")
public Genre updateGenre(@PathVariable Long id,
@RequestParam("name") String name) {
return genreService.updateGenre(id, name);
}
@DeleteMapping("/{id}")
public Genre deleteGenre(@PathVariable Long id) {
return genreService.deleteGenre(id);
}
}

View File

@@ -0,0 +1,61 @@
package com.example.demo.Books.Models;
import jakarta.persistence.Entity;
import jakarta.persistence.*;
import java.util.List;
@Entity
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
private String surname;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "authors")
private List<Book> books;
public Author() {}
public Author(String name, String surname) {
this.name = name;
this.surname = surname;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public void setName(String name) {
this.name = name;
}
public List<Book> getBooks() {
return books;
}
@Override
public String toString() {
String res = "\nBooks{" +
"id: " + id + "," +
"name: " + name + "," +
"surname: " + name + "," +
"books:" + (books == null ? "[]" : books.toString()) + "}"
;
return res;
}
}

View File

@@ -0,0 +1,73 @@
package com.example.demo.Books.Models;
import jakarta.persistence.*;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import java.util.List;
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name="books_genres",
joinColumns = @JoinColumn(name="book_id"),
inverseJoinColumns = @JoinColumn(name="genre_id")
)
private List<Genre> genres;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name="authors_books",
joinColumns = @JoinColumn(name="book_id"),
inverseJoinColumns = @JoinColumn(name="authors_id")
)
private List<Author> authors;
public Book() {}
public Book(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addGenre(Genre g) {
genres.add(g);
}
public void addAuthor(Author author) {
authors.add(author);
}
public List<Author> getAuthors() {
return authors;
}
public List<Genre> getGenres() {
return genres;
}
@Override
public String toString() {
String res = "\nBook{" +
"id: " + id + "," +
"name: " + name + "," +
"genres:" + (genres == null ? "[]" : genres.toString()) + "}"
;
return res;
}
}

View File

@@ -0,0 +1,36 @@
package com.example.demo.Books.Models;
import jakarta.persistence.*;
@Entity
public class Genre {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Genre() {}
public Genre(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Genre{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}

View File

@@ -0,0 +1,76 @@
package com.example.demo.Books.Service;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.example.demo.Books.Models.Author;
import jakarta.persistence.*;
@Service
public class AuthorService {
@PersistenceContext
private EntityManager em;
@Transactional
public Author addAuthor(String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
Author author = new Author(name, surname);
em.persist(author);
return em.find(Author.class, author.getId());
}
@Transactional
public Author addAuthor(String name, String surname, String path) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
Author author = new Author(name, surname);
em.persist(author);
return em.find(Author.class, author.getId());
}
@Transactional(readOnly = true)
public Author findAuthor(Long id) {
final Author author = em.find(Author.class, id);
if (author == null) {
throw new EntityNotFoundException(String.format("Author with id [%s] is not found", id));
}
return author;
}
@Transactional(readOnly = true)
public List<Author> findAllAuthors() {
return em.createQuery("select f from Author f", Author.class)
.getResultList();
}
@Transactional
public Author updateAuthor(Long id, String name, String surname) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Author name is null or empty");
}
final Author currentAuthor = findAuthor(id);
if(name != null) currentAuthor.setName(name);
if(surname != null) currentAuthor.setSurname(surname);
return em.merge(currentAuthor);
}
@Transactional
public Author deleteAuthor(Long id) {
final Author currentAuthor = findAuthor(id);
em.remove(currentAuthor);
return currentAuthor;
}
@Transactional
public void deleteAllAuthors() {
em.createQuery("delete from Author").executeUpdate();
}
}

View File

@@ -0,0 +1,122 @@
package com.example.demo.Books.Service;
import jakarta.persistence.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import java.util.List;
@Service
public class BookService {
@PersistenceContext
private EntityManager em;
@Transactional
public Book addBook(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Book name is null or empty");
}
Book book = new Book(name);
em.persist(book);
return em.find(Book.class, book.getId());
}
@Transactional
public Book addGenre(Long bookId, Long genreId) {
final Book book = em.find(Book.class, bookId);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId));
}
final Genre genre = em.find(Genre.class, genreId);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", genreId));
}
book.addGenre(genre);
return em.merge(book);
}
@Transactional
public Book addAuthor(Long bookId, Long authorId) {
final Book book = em.find(Book.class, bookId);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", bookId));
}
final Author author = em.find(Author.class, authorId);
if (author == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", authorId));
}
book.addAuthor(author);
return em.merge(book);
}
@Transactional(readOnly = true)
public Book findBook(Long id) {
final Book book = em.find(Book.class, id);
if (book == null) {
throw new EntityNotFoundException(String.format("Book with id [%s] is not found", id));
}
return book;
}
@Transactional(readOnly = true)
public List<Book> findBookByGenre(Long id, Long genreId) {
Query query = em.createQuery("select f from Book f " +
"join f.genres g " +
"where g.id = :genreId", Book.class)
.setParameter("genreId", genreId);
List<Book> result = query.getResultList();
return result;
}
@Transactional(readOnly = true)
public List<Book> findBookByAuthor(Long id, String name) {
Query query = em.createQuery("select f from Book f " +
"join f.authors a " +
"where a.name like :authorName", Book.class)
.setParameter("authorName", '%' + name + '%');
List<Book> result = query.getResultList();
return result;
}
@Transactional(readOnly = true)
public List<Book> findAllBooks() {
return em.createQuery("select f from Book f", Book.class)
.getResultList();
}
@Transactional
public Book updateBook(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Book name is null or empty");
}
final Book currentBook = findBook(id);
currentBook.setName(name);
return em.merge(currentBook);
}
@Transactional
public Book deleteBook(Long id) {
final Book currentBook = findBook(id);
em.remove(currentBook);
return currentBook;
}
@Transactional
public void deleteAllBooks() {
em.createQuery("delete from Book").executeUpdate();
}
}

View File

@@ -0,0 +1,65 @@
package com.example.demo.Books.Service;
import com.example.demo.Books.Models.Genre;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import jakarta.persistence.*;
import java.util.List;
@Service
public class GenreService {
@PersistenceContext
private EntityManager em;
@Transactional
public Genre addGenre(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Genre name is null or empty");
}
Genre genre = new Genre(name);
em.persist(genre);
return em.find(Genre.class, genre.getId());
}
@Transactional(readOnly = true)
public Genre findGenre(Long id) {
final Genre genre = em.find(Genre.class, id);
if (genre == null) {
throw new EntityNotFoundException(String.format("Genre with id [%s] is not found", id));
}
return genre;
}
@Transactional(readOnly = true)
public List<Genre> findAllGenres() {
return em.createQuery("select g from Genre g", Genre.class)
.getResultList();
}
@Transactional
public Genre updateGenre(Long id, String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Genre name is null or empty");
}
final Genre currentGenre = findGenre(id);
currentGenre.setName(name);
return em.merge(currentGenre);
}
@Transactional
public Genre deleteGenre(Long id) {
final Genre currentGenre = findGenre(id);
em.remove(currentGenre);
return currentGenre;
}
@Transactional
public void deleteAllGenres() {
em.createQuery("delete from Genre").executeUpdate();
}
}

View File

@@ -0,0 +1,40 @@
package com.example.demo.Calculator.Controller;
import com.example.demo.Calculator.Service.CalculatorService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculatorController {
private final CalculatorService calculatorService;
public CalculatorController(CalculatorService calculatorService){
this.calculatorService=calculatorService;
}
@GetMapping("/calc1")
public Object calc1(@RequestParam(value = "val1", defaultValue = "null") Object val1,
@RequestParam(value = "val2", defaultValue = "null") Object val2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calculatorService.calc1(val1, val2, type);
}
@GetMapping("/calc2")
public Object calc2(@RequestParam(value = "val1", defaultValue = "null") Object val1,
@RequestParam(value = "val2", defaultValue = "null") Object val2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calculatorService.calc2(val1, val2, type);
}
@GetMapping("/calc3")
public Object calc3(@RequestParam(value = "val1", defaultValue = "null") Object val1,
@RequestParam(value = "val2", defaultValue = "null") Object val2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calculatorService.calc3(val1, val2, type);
}
@GetMapping("/calc4")
public Object calc4(@RequestParam(value = "val1", defaultValue = "null") Object val1,
@RequestParam(value = "val2", defaultValue = "null") Object val2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calculatorService.calc4(val1, val2, type);
}
}

View File

@@ -0,0 +1,57 @@
package com.example.demo.Calculator.Domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.stream.Stream;
@Component(value="numberF")
public class CalculatorDoubleArray implements ICalculator<String[]>, InitializingBean, DisposableBean {
private final Logger log= LoggerFactory.getLogger(CalculatorDoubleArray.class);
@Override
public void afterPropertiesSet(){
log.info("calling CalculatorDoubleArray.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("calling CalculatorDoubleArray.destroy()");
}
@Override
public String[] calc1(String[] val1, String[] val2) {
String[] resArr = Stream.concat(Arrays.stream(val1),
Arrays.stream(val2))
.toArray(String[]::new);;
return resArr;
}
@Override
public String[] calc2(String[] val1, String[] val2) {
String[] resArr = Stream.concat(Arrays.stream(val1),
Arrays.stream(val2))
.toArray(String[]::new);;
return resArr;
}
@Override
public String[] calc3(String[] val1, String[] val2) {
String[] resArr = Stream.concat(Arrays.stream(val1),
Arrays.stream(val2))
.toArray(String[]::new);;
return resArr;
}
@Override
public String[] calc4(String[] val1, String[] val2) {
String[] resArr = Stream.concat(Arrays.stream(val1),
Arrays.stream(val2))
.toArray(String[]::new);;
return resArr;
}
}

View File

@@ -0,0 +1,42 @@
package com.example.demo.Calculator.Domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value="number")
public class CalculatorNumber implements ICalculator<Integer>, InitializingBean, DisposableBean{
private final Logger log=LoggerFactory.getLogger(CalculatorNumber.class);
@Override
public void afterPropertiesSet(){
log.info("calling CalculatorNumber.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("calling CalculatorNumber.destroy()");
}
@Override
public Integer calc1(Integer val1, Integer val2) {
return val1 * val2;
}
@Override
public Integer calc2(Integer val1, Integer val2) {
return val1 + val2;
}
@Override
public Integer calc3(Integer val1, Integer val2) {
return val1 - val2;
}
@Override
public Integer calc4(Integer val1, Integer val2) {
return val1 / val2;
}
}

View File

@@ -0,0 +1,46 @@
package com.example.demo.Calculator.Domain;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Component(value="string")
public class CalculatorString implements ICalculator<String>, InitializingBean, DisposableBean {
private final Logger log= LoggerFactory.getLogger(CalculatorString.class);
@Override
public void afterPropertiesSet() {
log.info("calling CalculatorString.afterPropertiesSet()");
}
@Override
public void destroy() {
log.info("CalculatorString.destroy()");
}
@Override
public String calc1(String val1, String val2) {
String temp = val1;
for (int i = 0; i < Integer.parseInt(val2) - 1; i++) {
temp = calc2(temp, val1);
}
return temp;
}
@Override
public String calc2(String val1, String val2) {
return val1.concat(val2);
}
@Override
public String calc3(String val1, String val2) {
return val1.replaceFirst(val2, "");
}
@Override
public String calc4(String val1, String val2) {
return val1.replaceAll(val2, "");
}
}

View File

@@ -0,0 +1,11 @@
package com.example.demo.Calculator.Domain;
public interface ICalculator<T> {
T calc1(T val1, T val2);
T calc2(T val1, T val2);
T calc3(T val1, T val2);
T calc4(T val1, T val2);
}

View File

@@ -0,0 +1,81 @@
package com.example.demo.Calculator.Service;
import com.example.demo.Calculator.Domain.ICalculator;
import com.example.demo.Calculator.Domain.CalculatorNumber;
import com.example.demo.Calculator.Domain.CalculatorDoubleArray;
import com.example.demo.Calculator.Domain.CalculatorString;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.io.*;
import java.util.*;
import java.util.Arrays;
@Service
public class CalculatorService {
private final ApplicationContext applicationContext;
public CalculatorService(ApplicationContext applicationContext){
this.applicationContext=applicationContext;
}
public Object calc2(Object val1, Object val2, String type){
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
if (type.equals("string")) {
return temp.calc2(val1.toString(), val2.toString());
}
else if (type.equals("number")) {
return temp.calc2(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
}
else if (type.equals("numberF")) {
return temp.calc2(convToArr(val1) , convToArr(val2));
}
else return null;
}
public Object calc1(Object val1, Object val2, String type){
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
if (type.equals("string")) {
return temp.calc1(val1.toString(), val2.toString());
}
else if (type.equals("number")) {
return temp.calc1(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
}
else if (type.equals("numberF")) {
return temp.calc1(convToArr(val1) , convToArr(val2));
}
else return null;
}
public Object calc3(Object val1, Object val2, String type){
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
if (type.equals("string")) {
return temp.calc3(val1.toString(), val2.toString());
}
else if (type.equals("number")) {
return temp.calc3(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
}
else if (type.equals("numberF")) {
return temp.calc3(convToArr(val1) , convToArr(val2));
}
else return null;
}
public Object calc4(Object val1, Object val2, String type){
final ICalculator temp=(ICalculator) applicationContext.getBean(type);
if (type.equals("string")) {
return temp.calc4(val1.toString(), val2.toString());
}
else if (type.equals("number")) {
return temp.calc4(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
}
else if (type.equals("numberF")) {
return temp.calc4(convToArr(val1) , convToArr(val2));
}
else return null;
}
public String[] convToArr(Object arr){
return Arrays.stream(arr.toString().split("[\\(\\)\\,\\[\\] \\!\\?\\:\\;]")).filter(e -> e.trim().length()>0).toArray(String[]::new);
}
}

View File

@@ -15,7 +15,7 @@ public class DemoApplication {
} }
@GetMapping("/hello") /*@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name); return String.format("Hello %s!", name);
} }
@@ -60,5 +60,5 @@ public class DemoApplication {
} else { } else {
return calc4(val1 - 1) + calc4(val1 - 2); return calc4(val1 - 1) + calc4(val1 - 2);
} }
} }*/
} }

View File

@@ -1 +1,11 @@
spring.main.banner-mode=off
#server.port=8080
spring.datasource.url=jdbc:h2:file:./data
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false

View File

@@ -1,13 +1,109 @@
package com.example.demo; package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.Calculator.Service.CalculatorService;
@SpringBootTest @SpringBootTest
class DemoApplicationTests { class DemoApplicationTests {
@Autowired
CalculatorService calculatorService;
@Test @Test
void contextLoads() { void testCalculatorNumberCalc2() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc2(val1, val2, "number");
Assertions.assertEquals("110", result);
} }
@Test
void testCalculatorNumberCalc3() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc3(val1, val2, "number");
Assertions.assertEquals("90", result);
}
@Test
void testCalculatorNumberCalc1() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc1(val1, val2, "number");
Assertions.assertEquals("1000", result);
}
@Test
void testCalculatorNumberCalc4() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc4(val1, val2, "number");
Assertions.assertEquals("10", result);
}
@Test
void testCalculatorStringCalc2() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc2(val1.toString(), val2.toString(), "string");
Assertions.assertEquals("10010", result);
}
@Test
void testCalculatorStringCalc3() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc3(val1.toString(), val2.toString(), "string");
Assertions.assertEquals("0", result);
}
@Test
void testCalculatorStringCalc1() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc1(val1.toString(), val2.toString(), "string");
Assertions.assertEquals("100100100100100100100100100100", result);
}
@Test
void testCalculatorStringCalc4() {
Object val1 = 100;
Object val2 = 10;
final Object result = calculatorService.calc4(val1.toString(), val2.toString(), "string");
Assertions.assertEquals("0", result);
}
@Test
void testCalculatorFloatNumberCalc2() {
Object val1 = 100.10;
Object val2 = 10.10;
final Object result = calculatorService.calc2(val1, val2, "numberF");
Assertions.assertEquals("110.20", result);
}
@Test
void testCalculatorFloatNumberCalc3() {
Object val1 = 100.10;
Object val2 = 10.10;
final Object result = calculatorService.calc3(val1, val2, "numberF");
Assertions.assertEquals("90", result);
}
@Test
void testCalculatorFloatNumberCalc1() {
Object val1 = 100.10;
Object val2 = 10.10;
final Object result = calculatorService.calc1(val1, val2, "numberF");
Assertions.assertEquals("1011.01", result);
}
@Test
void testCalculatorFloatNumberCalc4() {
Object val1 = 10.5;
Object val2 = 2;
final Object result = calculatorService.calc4(val1, val2, "numberF");
Assertions.assertEquals("5.25", result);
}
} }

View File

@@ -0,0 +1,92 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.AuthorService;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaAuthorsTest {
private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class);
@Autowired
BookService bookService;
@Autowired
AuthorService authorService;
@Test
void testBookCreate() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
Assertions.assertNotNull(author.getId());
}
@Test
void testBookReadNotFound() {
authorService.deleteAllAuthors();
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(-1L));
}
@Test
void findBook() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
final Author findAuthor = authorService.findAuthor(author.getId());
log.info(findAuthor.toString());
Assertions.assertEquals(author.toString(), findAuthor.toString());
}
@Test
void testReadAll() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
final List<Author> authors = authorService.findAllAuthors();
Assertions.assertEquals(authors.size(), 1);
}
@Test
void updateAuthor() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
authorService.updateAuthor(author.getId(), "Alexei", null);
final Author findAuthor = authorService.findAuthor(author.getId());
log.info(findAuthor.toString());
Assertions.assertEquals(findAuthor.getName() + " " + findAuthor.getSurname(), "Alexei Ivanov");
}
@Test
void deleteAuthor() {
authorService.deleteAllAuthors();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
log.info(author.toString());
authorService.deleteAuthor(author.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> authorService.findAuthor(author.getId()));
}
@Test
void addBook() {
authorService.deleteAllAuthors();
bookService.findAllBooks();
final Author author = authorService.addAuthor("Ivan", "Ivanov");
final Book book = bookService.addBook("book");
bookService.addAuthor(book.getId(), author.getId());
final Author findAuthor = authorService.findAuthor(author.getId());
Assertions.assertEquals(findAuthor.getBooks().get(0).getId(), book.getId());
}
}

View File

@@ -0,0 +1,139 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.Books.Models.Author;
import com.example.demo.Books.Models.Book;
import com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.AuthorService;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaBooksTest {
private static final Logger log = LoggerFactory.getLogger(JpaBooksTest.class);
@Autowired
BookService bookService;
@Autowired
GenreService genreService;
@Autowired
AuthorService authorService;
@Test
void testBookCreate() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
Assertions.assertNotNull(book.getId());
}
@Test
void testBookReadNotFound() {
bookService.deleteAllBooks();
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(-1L));
}
@Test
void findBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
final Book findBook = bookService.findBook(book.getId());
log.info(findBook.toString());
Assertions.assertEquals(book.toString(), findBook.toString());
}
@Test
void updateBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
bookService.updateBook(book.getId(), "book 2");
final Book findBook = bookService.findBook(book.getId());
log.info(findBook.toString());
Assertions.assertEquals(findBook.getName(), "book 2");
}
@Test
void testReadAll() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
final List<Book> books = bookService.findAllBooks();
Assertions.assertEquals(books.size(), 1);
}
@Test
void deleteBook() {
bookService.deleteAllBooks();
final Book book = bookService.addBook("book");
log.info(book.toString());
bookService.deleteBook(book.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(book.getId()));
}
@Test
void findBooksByGenres() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
bookService.addBook("book 2");
final Genre genre = genreService.addGenre("Comedy");
bookService.addGenre(book.getId(), genre.getId());
final List<Book> books = bookService.findBookByGenre(book.getId(), genre.getId());
Assertions.assertEquals(books.size(), 1);
}
@Test
void findBooksByActor() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Book book2 = bookService.addBook("book 2");
bookService.addBook("book 3");
final Author author = authorService.addAuthor("Ivan", "Ivanov");
final Author author2 = authorService.addAuthor("Alexei", "Ivanov");
bookService.addAuthor(book.getId(), author.getId());
bookService.addAuthor(book2.getId(), author2.getId());
final List<Book> books = bookService.findBookByAuthor(book.getId(), "Alexei");
Assertions.assertEquals(books.size(), 2);
}
@Test
void addGenre() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Genre genre = genreService.addGenre("Comedy");
bookService.addGenre(book.getId(), genre.getId());
final Book findBook = bookService.findBook(book.getId());
Assertions.assertEquals(findBook.getGenres().get(0).toString(), genre.toString());
}
@Test
void addAuthor() {
bookService.deleteAllBooks();
genreService.deleteAllGenres();
final Book book = bookService.addBook("book");
final Author author = authorService.addAuthor("Ivan", "Ivanov");
bookService.addAuthor(book.getId(), author.getId());
final Book findBook = bookService.findBook(book.getId());
Assertions.assertEquals(findBook.getAuthors().get(0).getId(), author.getId());
}
}

View File

@@ -0,0 +1,76 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.example.demo.Books.Models.Genre;
import com.example.demo.Books.Service.BookService;
import com.example.demo.Books.Service.GenreService;
import jakarta.persistence.*;
import java.util.List;
@SpringBootTest
public class JpaGenresTest {
private static final Logger log = LoggerFactory.getLogger(JpaGenresTest.class);
@Autowired
BookService bookService;
@Autowired
GenreService genreService;
@Test
void testGenreCreate() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
Assertions.assertNotNull(genre.getId());
}
@Test
void testGenreReadNotFound() {
genreService.deleteAllGenres();
Assertions.assertThrows(EntityNotFoundException.class, () -> genreService.findGenre(-1L));
}
@Test
void findGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(genre.toString(), findGenre.toString());
}
@Test
void testReadAll() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
final List<Genre> genres = genreService.findAllGenres();
Assertions.assertEquals(genres.size(), 1);
}
@Test
void updateGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
genreService.updateGenre(genre.getId(), "Roman");
final Genre findGenre = genreService.findGenre(genre.getId());
log.info(findGenre.toString());
Assertions.assertEquals(findGenre.getName(), "Roman");
}
@Test
void deleteGenre() {
genreService.deleteAllGenres();
final Genre genre = genreService.addGenre("Comedy");
log.info(genre.toString());
genreService.deleteGenre(genre.getId());
Assertions.assertThrows(EntityNotFoundException.class, () -> bookService.findBook(genre.getId()));
}
}