LabWork03: Работа готова
This commit is contained in:
parent
809e593a86
commit
a243920d42
@ -15,6 +15,9 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||||
|
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'
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named('test') {
|
tasks.named('test') {
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.configuration;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import ru.ulstu.is.labwork.calculator.domain.Calculator;
|
|
||||||
import ru.ulstu.is.labwork.calculator.domain.CalculatorInt;
|
|
||||||
import ru.ulstu.is.labwork.calculator.domain.CalculatorString;
|
|
||||||
|
|
||||||
@Configuration
|
|
||||||
public class CalculatorConfiguration {
|
|
||||||
@Bean(value = "inttype")
|
|
||||||
public CalculatorInt createIntCalculator(){
|
|
||||||
return new CalculatorInt();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Bean(value = "stringtype")
|
|
||||||
public CalculatorString createStringCalculator(){
|
|
||||||
return new CalculatorString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.controller;
|
|
||||||
|
|
||||||
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.ulstu.is.labwork.calculator.service.CalculatorService;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
//@RequestMapping("/lab2")
|
|
||||||
public class CalculatorController {
|
|
||||||
private final CalculatorService calculatorService;
|
|
||||||
|
|
||||||
public CalculatorController(CalculatorService calculatorService){
|
|
||||||
this.calculatorService = calculatorService;
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/sum")
|
|
||||||
public String sum( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
|
||||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
|
||||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
|
||||||
{
|
|
||||||
return calculatorService.sum(val1, val2, type).toString();
|
|
||||||
}
|
|
||||||
@GetMapping("/sub")
|
|
||||||
public String sub( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
|
||||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
|
||||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
|
||||||
{
|
|
||||||
return calculatorService.sub(val1, val2, type).toString();
|
|
||||||
}
|
|
||||||
@GetMapping("/mul")
|
|
||||||
public String mul( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
|
||||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
|
||||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
|
||||||
{
|
|
||||||
return calculatorService.mul(val1, val2, type).toString();
|
|
||||||
}
|
|
||||||
@GetMapping("/div")
|
|
||||||
public String div( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
|
||||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
|
||||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
|
||||||
{
|
|
||||||
return calculatorService.div(val1, val2, type).toString();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.domain;
|
|
||||||
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
|
|
||||||
public interface Calculator<T> {
|
|
||||||
T sum(T val1, T val2);
|
|
||||||
T sub(T val1, T val2);
|
|
||||||
T mul(T val1, T val2);
|
|
||||||
T div(T val1, T val2);
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.domain;
|
|
||||||
|
|
||||||
public class CalculatorInt implements Calculator<Integer>{
|
|
||||||
@Override
|
|
||||||
public Integer sum(Integer val1, Integer val2) {
|
|
||||||
return val1 + val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer sub(Integer val1, Integer val2) {
|
|
||||||
return val1 - val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer mul(Integer val1, Integer val2) {
|
|
||||||
return val1 * val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Integer div(Integer val1, Integer val2) {
|
|
||||||
return val1 / val2;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.domain;
|
|
||||||
|
|
||||||
public class CalculatorString implements Calculator<String>{
|
|
||||||
@Override
|
|
||||||
public String sum(String val1, String val2) {
|
|
||||||
return val1 + '+' + val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String sub(String val1, String val2) {
|
|
||||||
return val1 + '-' + val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String mul(String val1, String val2) {
|
|
||||||
return val1 + '*' + val2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String div(String val1, String val2) {
|
|
||||||
return val1 + '/' + val2;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,56 +0,0 @@
|
|||||||
package ru.ulstu.is.labwork.calculator.service;
|
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import ru.ulstu.is.labwork.calculator.domain.Calculator;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class CalculatorService {
|
|
||||||
private final ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
public CalculatorService(ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object sum(Object val1, Object val2, String type){
|
|
||||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
|
||||||
if ("inttype".equals(type)){
|
|
||||||
return calculator.sum(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
|
||||||
}
|
|
||||||
else if ("stringtype".equals(type)) {
|
|
||||||
return String.format("%s", calculator.sum(val1.toString(), val2.toString()));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object sub(Object val1, Object val2, String type){
|
|
||||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
|
||||||
if ("inttype".equals(type)){
|
|
||||||
return calculator.sub(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
|
||||||
}
|
|
||||||
else if ("stringtype".equals(type)) {
|
|
||||||
return String.format("%s", calculator.sub(val1.toString(), val2.toString()));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
public Object mul(Object val1, Object val2, String type){
|
|
||||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
|
||||||
if ("inttype".equals(type)){
|
|
||||||
return calculator.mul(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
|
||||||
}
|
|
||||||
else if ("stringtype".equals(type)) {
|
|
||||||
return String.format("%s", calculator.mul(val1.toString(), val2.toString()));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
public Object div(Object val1, Object val2, String type){
|
|
||||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
|
||||||
if ("inttype".equals(type)){
|
|
||||||
return calculator.div(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
|
||||||
}
|
|
||||||
else if ("stringtype".equals(type)) {
|
|
||||||
return String.format("%s", calculator.div(val1.toString(), val2.toString()));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,67 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.AdditionalTask;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Post;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class SearchService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Object> finder(String searchWord){
|
||||||
|
|
||||||
|
List<Post> postResult = em.createQuery("SELECT p FROM Post p WHERE p.content LIKE :word", Post.class)
|
||||||
|
.setParameter("word", searchWord)
|
||||||
|
.getResultList();
|
||||||
|
|
||||||
|
List<Comment> commentResult = em.createQuery("SELECT c FROM Comment c WHERE c.content LIKE :word", Comment.class)
|
||||||
|
.setParameter("word", searchWord)
|
||||||
|
.getResultList();
|
||||||
|
|
||||||
|
List<Object> result = new ArrayList<>();
|
||||||
|
result.addAll(postResult);
|
||||||
|
result.addAll(commentResult);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//
|
||||||
|
// @Autowired
|
||||||
|
// SearchService searchService;
|
||||||
|
//
|
||||||
|
// @Test
|
||||||
|
// void testFinder(){
|
||||||
|
// commentService.deleteAllComment();
|
||||||
|
// postService.deleteAllPosts();
|
||||||
|
// customerService.deleteAllCustomers();
|
||||||
|
//
|
||||||
|
// Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
// Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
// Customer c3 = customerService.addCustomer("third", "3");
|
||||||
|
//
|
||||||
|
// Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||||
|
// Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||||
|
// Post p3 = postService.addPost(c3, "third title", "searchword");
|
||||||
|
//
|
||||||
|
// Comment com1 = commentService.addComment(c1, p2, "What");
|
||||||
|
// Comment com2 = commentService.addComment(c2, p3, "searchword");
|
||||||
|
// Comment com3 = commentService.addComment(c3, p1, "How");
|
||||||
|
//
|
||||||
|
// List<Object> expectedResult = new ArrayList<>();
|
||||||
|
// expectedResult.add((Object)p3);
|
||||||
|
// expectedResult.add((Object)com2);
|
||||||
|
//
|
||||||
|
// List<Object> actualResult = searchService.finder("searchword");
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(expectedResult.size(),actualResult.size());
|
||||||
|
//
|
||||||
|
// Assertions.assertEquals(expectedResult.get(0),actualResult.get(0));
|
||||||
|
// Assertions.assertEquals(expectedResult.get(1),actualResult.get(1));
|
||||||
|
// }
|
66
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Comment.java
Normal file
66
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Comment.java
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.model;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Comment {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="customer_fk")
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name="post_fk")
|
||||||
|
private Post post;
|
||||||
|
|
||||||
|
public Comment() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Comment(Customer customer, Post post, String content) {
|
||||||
|
this.customer = customer;
|
||||||
|
this.post = post;
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post getPost() {
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Comment comment = (Comment) o;
|
||||||
|
|
||||||
|
return Objects.equals(id, comment.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
}
|
71
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Customer.java
Normal file
71
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Customer.java
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.model;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Customer {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
@Column
|
||||||
|
private String username;
|
||||||
|
@Column
|
||||||
|
private String hashedPassword;
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
|
private List<Comment> comments;
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer", cascade = CascadeType.ALL)
|
||||||
|
private List<Post> posts;
|
||||||
|
|
||||||
|
public Customer() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer(String username, String hashedPassword) {
|
||||||
|
this.username = username;
|
||||||
|
this.hashedPassword = hashedPassword;
|
||||||
|
this.comments = new ArrayList<>();
|
||||||
|
this.posts = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHashedPassword() {
|
||||||
|
return hashedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Comment> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Post> getPosts() {
|
||||||
|
return posts;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHashedPassword(String hashedPassword) {
|
||||||
|
this.hashedPassword = hashedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) return true;
|
||||||
|
if (obj == null || getClass() != obj.getClass()) return false;
|
||||||
|
Customer customer = (Customer) obj;
|
||||||
|
return Objects.equals(id, customer.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
78
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Post.java
Normal file
78
src/main/java/ru/ulstu/is/labwork/dbLab3/model/Post.java
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.model;
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Post {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String title;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
private String content;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.EAGER)
|
||||||
|
@JoinColumn(name = "customer_fk")
|
||||||
|
private Customer customer;
|
||||||
|
|
||||||
|
@OneToMany(fetch = FetchType.EAGER, mappedBy = "post", cascade = CascadeType.ALL)
|
||||||
|
private List<Comment> comments;
|
||||||
|
|
||||||
|
public Post() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Post(Customer customer, String title, String content) {
|
||||||
|
this.customer = customer;
|
||||||
|
this.title = title;
|
||||||
|
this.content = content;
|
||||||
|
this.comments = new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Post post = (Post) o;
|
||||||
|
|
||||||
|
return id.equals(post.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return id.hashCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setContent(String content) {
|
||||||
|
this.content = content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Comment> getComments() {
|
||||||
|
return comments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Customer getCustomer() {
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,72 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.service;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Customer;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Post;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CommentService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment findComment(Long id) {
|
||||||
|
final Comment comment = em.find(Comment.class, id);
|
||||||
|
if (comment == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Comment with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Comment> findAllComments() {
|
||||||
|
return em.createQuery("select c from Comment c", Comment.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment addComment(Customer customer, Post post, String content) {
|
||||||
|
if (customer == null || post == null) {
|
||||||
|
throw new IllegalArgumentException("Invalid customer or post");
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Invalid comment's content");
|
||||||
|
}
|
||||||
|
final Comment comment = new Comment(customer, post, content);
|
||||||
|
comment.getCustomer().getComments().add(comment);
|
||||||
|
comment.getPost().getComments().add(comment);
|
||||||
|
em.persist(comment);
|
||||||
|
return comment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment updateComment(Long id, String content) {
|
||||||
|
if (!StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Comment's content is empty");
|
||||||
|
}
|
||||||
|
final Comment comment = findComment(id);
|
||||||
|
comment.setContent(content);
|
||||||
|
return em.merge(comment);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Comment deleteComment(Long id) {
|
||||||
|
final Comment currentComment = findComment(id);
|
||||||
|
currentComment.getPost().getComments().remove(currentComment);
|
||||||
|
currentComment.getCustomer().getComments().remove(currentComment);
|
||||||
|
em.remove(currentComment);
|
||||||
|
return currentComment;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllComment() {
|
||||||
|
em.createQuery("delete from Comment").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.service;
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Customer;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CustomerService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer findCustomer(Long id) {
|
||||||
|
final Customer customer = em.find(Customer.class, id);
|
||||||
|
if (customer == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("User with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Customer> findAllCustomers() {
|
||||||
|
return em.createQuery("select c from Customer c", Customer.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer addCustomer(String username, String password) {
|
||||||
|
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||||
|
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||||
|
}
|
||||||
|
final Customer customer = new Customer(username, password);
|
||||||
|
em.persist(customer);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer updateCustomer(Long id, String username, String password) {
|
||||||
|
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||||
|
throw new IllegalArgumentException("Customer's username or password is empty");
|
||||||
|
}
|
||||||
|
final Customer customer = findCustomer(id);
|
||||||
|
customer.setUsername(username);
|
||||||
|
customer.setHashedPassword(password);
|
||||||
|
return em.merge(customer);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Customer deleteCustomer(Long id) {
|
||||||
|
final Customer currentCustomer = findCustomer(id);
|
||||||
|
em.remove(currentCustomer);
|
||||||
|
return currentCustomer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllCustomers() {
|
||||||
|
em.createQuery("delete from Customer").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
package ru.ulstu.is.labwork.dbLab3.service;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityManager;
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
import jakarta.persistence.PersistenceContext;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Comment;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Customer;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.Post;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PostService {
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post findPost(Long id) {
|
||||||
|
final Post post = em.find(Post.class, id);
|
||||||
|
if (post == null) {
|
||||||
|
throw new EntityNotFoundException(String.format("Post with id [%s] is not found", id));
|
||||||
|
}
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public List<Post> findAllPosts() {
|
||||||
|
return em.createQuery("select p from Post p", Post.class).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post addPost(Customer customer, String title, String content) {
|
||||||
|
if (customer == null) {
|
||||||
|
throw new IllegalArgumentException("Invalid customer");
|
||||||
|
}
|
||||||
|
if (!StringUtils.hasText(title) | !StringUtils.hasText(content)) {
|
||||||
|
throw new IllegalArgumentException("Invalid post's content or title");
|
||||||
|
}
|
||||||
|
final Post post = new Post(customer, title, content);
|
||||||
|
post.getCustomer().getPosts().add(post);
|
||||||
|
em.persist(post);
|
||||||
|
return post;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post updatePost(Long id, String title, String content) {
|
||||||
|
if (!StringUtils.hasText(content) | !StringUtils.hasText(title)) {
|
||||||
|
throw new IllegalArgumentException("Post's content or title is empty");
|
||||||
|
}
|
||||||
|
final Post post = findPost(id);
|
||||||
|
post.setTitle(title);
|
||||||
|
post.setContent(content);
|
||||||
|
return em.merge(post);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public Post deletePost(Long id) {
|
||||||
|
final Post currentPost = findPost(id);
|
||||||
|
currentPost.getCustomer().getPosts().remove(currentPost);
|
||||||
|
em.remove(currentPost);
|
||||||
|
return currentPost;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
public void deleteAllPosts() {
|
||||||
|
em.createQuery("delete from Post").executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
@ -1,69 +1,125 @@
|
|||||||
package ru.ulstu.is.labwork;
|
package ru.ulstu.is.labwork;
|
||||||
|
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.model.*;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.service.CommentService;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.service.CustomerService;
|
||||||
|
import ru.ulstu.is.labwork.dbLab3.service.PostService;
|
||||||
import org.junit.jupiter.api.Assertions;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import ru.ulstu.is.labwork.calculator.service.CalculatorService;
|
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class LabworkApplicationTests {
|
class LabworkApplicationTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
CalculatorService calculatorService;
|
CustomerService customerService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CommentService commentService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
PostService postService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void stringSum() {
|
void testCustomers() {
|
||||||
Assertions.assertEquals("Hello+World!", calculatorService.sum("Hello", "World!", "stringtype"));
|
commentService.deleteAllComment();
|
||||||
Assertions.assertEquals("+Hello, World!", calculatorService.sum("", "Hello, World!", "stringtype"));
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
|
|
||||||
|
Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
Customer c3 = customerService.addCustomer("third", "3");
|
||||||
|
|
||||||
|
Assertions.assertEquals("first", c1.getUsername());
|
||||||
|
Assertions.assertEquals("second", c2.getUsername());
|
||||||
|
Assertions.assertEquals("third", c3.getUsername());
|
||||||
|
|
||||||
|
Assertions.assertEquals(c1, customerService.findCustomer(c1.getId()));
|
||||||
|
|
||||||
|
customerService.deleteCustomer(c2.getId());
|
||||||
|
|
||||||
|
Assertions.assertEquals(2, customerService.findAllCustomers().size());
|
||||||
|
|
||||||
|
Customer c4 = customerService.updateCustomer(c3.getId(), "fourth", "4");
|
||||||
|
|
||||||
|
Assertions.assertNotEquals(c3.getUsername(), c4.getUsername());
|
||||||
|
Assertions.assertNotEquals(c3.getHashedPassword(), c4.getHashedPassword());
|
||||||
|
|
||||||
|
commentService.deleteAllComment();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void stringSub() {
|
void testPost() {
|
||||||
Assertions.assertEquals("Крокодил-Гена", calculatorService.sub("Крокодил", "Гена", "stringtype"));
|
commentService.deleteAllComment();
|
||||||
Assertions.assertEquals("Атомное-сердце", calculatorService.sub("Атомное", "сердце", "stringtype"));
|
postService.deleteAllPosts();
|
||||||
Assertions.assertEquals("Почему-Жак Фреско", calculatorService.sub("Почему", "Жак Фреско", "stringtype"));
|
customerService.deleteAllCustomers();
|
||||||
|
|
||||||
|
Customer c1 = customerService.addCustomer("first", "1");
|
||||||
|
Customer c2 = customerService.addCustomer("second", "2");
|
||||||
|
|
||||||
|
Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||||
|
Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||||
|
|
||||||
|
Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||||
|
|
||||||
|
Assertions.assertEquals(p1.getCustomer(), c1);
|
||||||
|
Assertions.assertEquals(p2.getCustomer(), c2);
|
||||||
|
|
||||||
|
Assertions.assertEquals(c1.getPosts().get(0), p1);
|
||||||
|
Assertions.assertEquals(c2.getPosts().get(0), p2);
|
||||||
|
|
||||||
|
Assertions.assertEquals(p1, postService.findPost(p1.getId()));
|
||||||
|
Assertions.assertEquals(p2, postService.findPost(p2.getId()));
|
||||||
|
|
||||||
|
Post p3 = postService.addPost(c1, "asdf", "asd");
|
||||||
|
postService.deletePost(p1.getId());
|
||||||
|
Assertions.assertEquals(1, customerService.findCustomer(c1.getId()).getPosts().size());
|
||||||
|
|
||||||
|
Post p4 = postService.updatePost(p2.getId(), "third title", "wow");
|
||||||
|
|
||||||
|
Assertions.assertNotEquals(p2.getTitle(), p4.getTitle());
|
||||||
|
Assertions.assertNotEquals(p2.getContent(), p4.getContent());
|
||||||
|
|
||||||
|
commentService.deleteAllComment();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void stringMul() {
|
void testComment() {
|
||||||
Assertions.assertEquals("Это*Звездочка", calculatorService.mul("Это", "Звездочка", "stringtype"));
|
commentService.deleteAllComment();
|
||||||
Assertions.assertEquals("п*чему", calculatorService.mul("п", "чему", "stringtype"));
|
postService.deleteAllPosts();
|
||||||
Assertions.assertEquals("Звездочкой * обозначают умножение", calculatorService.mul("Звездочкой ", " обозначают умножение", "stringtype"));
|
customerService.deleteAllCustomers();
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Customer c1 = customerService.addCustomer("first", "1");
|
||||||
void stringDiv() {
|
Customer c2 = customerService.addCustomer("second", "2");
|
||||||
Assertions.assertEquals("б/п", calculatorService.div("б", "п", "stringtype"));
|
|
||||||
Assertions.assertEquals("/1", calculatorService.div("", "1", "stringtype"));
|
|
||||||
Assertions.assertEquals("-/-", calculatorService.div("-", "-", "stringtype"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Post p1 = postService.addPost(c1, "first title", "nonsense");
|
||||||
void intSum() {
|
Post p2 = postService.addPost(c2, "second title", "ordinal");
|
||||||
Assertions.assertEquals(22, calculatorService.sum(11, 11, "inttype"));
|
|
||||||
Assertions.assertEquals(-10, calculatorService.sum(15, -25, "inttype"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Assertions.assertEquals(2, postService.findAllPosts().size());
|
||||||
void intSub() {
|
|
||||||
Assertions.assertEquals(0, calculatorService.sub(10, 10, "inttype"));
|
|
||||||
Assertions.assertEquals(100, calculatorService.sub(100, 0, "inttype"));
|
|
||||||
Assertions.assertEquals(3, calculatorService.sub(-3, -6, "inttype"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Comment com1 = commentService.addComment(c1, p2, "What");
|
||||||
void intMul() {
|
Comment com2 = commentService.addComment(c2, p1, "How");
|
||||||
Assertions.assertEquals(0, calculatorService.mul(0, 10, "inttype"));
|
|
||||||
Assertions.assertEquals(18, calculatorService.mul(6, 3, "inttype"));
|
|
||||||
Assertions.assertEquals(-4, calculatorService.mul(-1, 4, "inttype"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
Assertions.assertEquals(c1, p2.getComments().get(0).getCustomer());
|
||||||
void intDiv() {
|
Assertions.assertEquals(c2, p1.getComments().get(0).getCustomer());
|
||||||
Assertions.assertEquals(-1, calculatorService.div(-1, 1, "inttype"));
|
|
||||||
Assertions.assertEquals(2, calculatorService.div(100, 50, "inttype"));
|
Comment com3 = commentService.addComment(c1, p1, "Really");
|
||||||
Assertions.assertEquals(8, calculatorService.div(64, 8, "inttype"));
|
|
||||||
|
Assertions.assertEquals(com2, commentService.findComment(p1.getComments().get(0).getId()));
|
||||||
|
|
||||||
|
Comment com4 = commentService.updateComment(com3.getId(), "Not really");
|
||||||
|
|
||||||
|
Assertions.assertNotEquals(com3.getContent(), com4.getContent());
|
||||||
|
Assertions.assertEquals(com3.getCustomer().getId(), com4.getCustomer().getId());
|
||||||
|
|
||||||
|
commentService.deleteAllComment();
|
||||||
|
postService.deleteAllPosts();
|
||||||
|
customerService.deleteAllCustomers();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user