thirdLabWork
This commit is contained in:
parent
d60fbbe32f
commit
3823e88121
@ -14,6 +14,9 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
|
20
front/db.json
Normal file
20
front/db.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"ReportSotr": [
|
||||
{
|
||||
"id": 1,
|
||||
"CompanyName": "json-server",
|
||||
"Date": "typicode",
|
||||
""
|
||||
}
|
||||
],
|
||||
"comments": [
|
||||
{
|
||||
"id": 1,
|
||||
"body": "some comment",
|
||||
"postId": 1
|
||||
}
|
||||
],
|
||||
"profile": {
|
||||
"name": "typicode"
|
||||
}
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package com.example.demo.speaker.controller;
|
||||
|
||||
import com.example.demo.speaker.service.MethodService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MethodController {
|
||||
private final MethodService methodService;
|
||||
|
||||
public MethodController(MethodService speakerService) {
|
||||
this.methodService = speakerService;
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public String Sum(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return methodService.Sum(first,second,type);
|
||||
}
|
||||
|
||||
@GetMapping("/minus")
|
||||
public String Ras(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return methodService.Ras(first,second,type);
|
||||
}
|
||||
|
||||
@GetMapping("/multi")
|
||||
public String Pros(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return methodService.Pros(first,second,type);
|
||||
}
|
||||
|
||||
@GetMapping("/div")
|
||||
public String Del(@RequestParam(value = "first", defaultValue = "1") Object first,
|
||||
@RequestParam(value = "second", defaultValue = "1") Object second,
|
||||
@RequestParam(value = "type", defaultValue = "int") String type) {
|
||||
return methodService.Del(first,second,type);
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.example.demo.speaker.controller;
|
||||
|
||||
import com.example.demo.speaker.model.ReportSort;
|
||||
import com.example.demo.speaker.service.ReportSotrService;
|
||||
import com.example.demo.speaker.service.TypeSortService;
|
||||
import com.example.demo.speaker.service.WorkerService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/reports")
|
||||
public class ReportSotrController {
|
||||
private final ReportSotrService reportService;
|
||||
|
||||
|
||||
public ReportSotrController(ReportSotrService reportService) {
|
||||
this.reportService = reportService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ReportSort getReport(@PathVariable Long id) {
|
||||
return reportService.findReport(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<ReportSort> getReports() {
|
||||
return reportService.findAllReports();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public ReportSort createReport(@RequestParam("CompanyName") String CompanyName,
|
||||
@RequestParam("createDate") Date createDate,
|
||||
@RequestParam("IdType") Long IdType,
|
||||
@RequestParam("Text") String Text) {
|
||||
return reportService.addReport(CompanyName, createDate, IdType, Text);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public ReportSort updateStudent(@PathVariable Long id,
|
||||
@RequestParam("approveDate") Date approveDate,
|
||||
@RequestParam("isActive") Boolean isActive,
|
||||
@RequestParam("Worker") Long IdWorker) {
|
||||
return reportService.updateReport(id, approveDate, isActive, IdWorker);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ReportSort deleteReport(@PathVariable Long id) {
|
||||
return reportService.deleteReport(id);
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
public String ListOfReport() {
|
||||
var List = reportService.findAllReports();
|
||||
TypeSortService typeService = new TypeSortService();
|
||||
WorkerService workerService = new WorkerService();
|
||||
String result = "";
|
||||
for (var i : List) {
|
||||
var Type = typeService.findType(i.GetType());
|
||||
var Worker = workerService.findWorker(i.GetWorker());
|
||||
result += i.toString() + "Type: " + Type.toString() + "Worker: "+Worker.toString() + "\n\r";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.example.demo.speaker.controller;
|
||||
|
||||
import com.example.demo.speaker.model.TypeSotr;
|
||||
import com.example.demo.speaker.service.TypeSortService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/types")
|
||||
public class TypeSotrController {
|
||||
private final TypeSortService TypeService;
|
||||
|
||||
|
||||
public TypeSotrController(TypeSortService typeService) {
|
||||
this.TypeService = typeService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public TypeSotr getType(@PathVariable Long id) {
|
||||
return TypeService.findType(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<TypeSotr> getStudents() {
|
||||
return TypeService.findAllTypes();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public TypeSotr createStudent(@RequestParam("Name") String name) {
|
||||
return TypeService.addType(name);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public TypeSotr deleteStudent(@PathVariable Long id) {
|
||||
return TypeService.deleteType(id);
|
||||
}
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package com.example.demo.speaker.controller;
|
||||
|
||||
import com.example.demo.speaker.model.Worker;
|
||||
import com.example.demo.speaker.service.WorkerService;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
@RestController
|
||||
@RequestMapping("/workers")
|
||||
public class WorkerController {
|
||||
private final WorkerService workerService;
|
||||
|
||||
|
||||
public WorkerController(WorkerService workerService) {
|
||||
this.workerService = workerService;
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Worker getWorker(@PathVariable Long id) {
|
||||
return workerService.findWorker(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Worker> getWorkers() {
|
||||
return workerService.findAllWorkers();
|
||||
}
|
||||
|
||||
@PostMapping("/")
|
||||
public Worker createStudent(@RequestParam("Name") String Name) {
|
||||
return workerService.addWorker(Name);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}")
|
||||
public Worker updateWorker(@PathVariable Long id,
|
||||
@RequestParam("Name") String Name) {
|
||||
return workerService.updateReportWorker(id, Name);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public Worker deleteWorker(@PathVariable Long id) {
|
||||
return workerService.deleteWorker(id);
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
public interface IMethod<T> {
|
||||
T Sum(T first, T second);
|
||||
|
||||
T Multiply(T first, Integer second);
|
||||
|
||||
T Minus(T first, Integer second);
|
||||
|
||||
T Div(T first, T second);
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@Component(value="boolean")
|
||||
public class MethodBoolean implements IMethod<Boolean>{
|
||||
@Override
|
||||
public Boolean Sum(Boolean first, Boolean second) {
|
||||
return first || second;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean Multiply(Boolean first, Integer second) {
|
||||
return first && second%2==0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean Minus(Boolean first, Integer second) {
|
||||
return (!first && second%2==0) || (first && !(second%2==0));
|
||||
}
|
||||
@Override
|
||||
public Boolean Div(Boolean first, Boolean second) {
|
||||
if (!second) return false;
|
||||
else if(!first) return false;
|
||||
else return true;
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="int")
|
||||
public class MethodInt implements IMethod<Integer>{
|
||||
public Integer Sum(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) + Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Multiply(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) * Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Minus(Integer first, Integer second) {
|
||||
return Integer.parseInt(first.toString()) - Integer.parseInt(second.toString());
|
||||
}
|
||||
|
||||
public Integer Div(Integer first, Integer second) {
|
||||
int num = Integer.parseInt(second.toString());
|
||||
if (num == 0){
|
||||
return null;
|
||||
}else{
|
||||
return Integer.parseInt(first.toString()) / num;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.example.demo.speaker.domain;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component(value="string")
|
||||
public class MethodString implements IMethod<String>{
|
||||
@Override
|
||||
public String Sum(String first, String second) {
|
||||
return first.concat(second);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Multiply(String first, Integer second) {
|
||||
if (second != 0){
|
||||
String temp = "";
|
||||
for (int i = 0; i < second; i++){
|
||||
temp = temp.concat(first);
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
else{
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Minus(String first, Integer second) {
|
||||
String temp = first;
|
||||
if(temp.length() >= second){
|
||||
return temp.substring(0, first.length() - second);
|
||||
}else{
|
||||
return first;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Div(String first, String second) {
|
||||
if (first.contains(second)){
|
||||
return "true";
|
||||
}else{
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
}
|
107
src/main/java/com/example/demo/speaker/model/ReportSort.java
Normal file
107
src/main/java/com/example/demo/speaker/model/ReportSort.java
Normal file
@ -0,0 +1,107 @@
|
||||
package com.example.demo.speaker.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Objects;
|
||||
|
||||
@Entity
|
||||
public class ReportSort {
|
||||
@jakarta.persistence.Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
|
||||
public ReportSort(String name, Date createDate, Long IdType, String text) {
|
||||
this.NameCompany = name;
|
||||
this.CreateDate = createDate;
|
||||
this.IdType = IdType;
|
||||
this.Comment = text;
|
||||
}
|
||||
@Column()
|
||||
private String NameCompany;
|
||||
private String Comment;
|
||||
private Date CreateDate;
|
||||
private Date ApproveDate;
|
||||
private Long IdType;
|
||||
private Boolean IsActive;
|
||||
private Long IdSotr;
|
||||
|
||||
public String GetName()
|
||||
{
|
||||
return this.NameCompany;
|
||||
}
|
||||
|
||||
public Date GetCreateDate()
|
||||
{
|
||||
return this.CreateDate;
|
||||
}
|
||||
|
||||
public Date GetApproveDate()
|
||||
{
|
||||
return this.ApproveDate;
|
||||
}
|
||||
public Long GetType()
|
||||
{
|
||||
return this.IdType;
|
||||
}
|
||||
|
||||
public Long GetWorker()
|
||||
{
|
||||
return this.IdSotr;
|
||||
}
|
||||
public void SetWorker(Long id)
|
||||
{
|
||||
this.IdSotr = id;
|
||||
}
|
||||
public Boolean GetActive()
|
||||
{
|
||||
return this.IsActive;
|
||||
}
|
||||
public ReportSort() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ReportSort report = (ReportSort) o;
|
||||
return Objects.equals(id, report.id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Report{" +
|
||||
"id=" + id +
|
||||
", NameCompany='" + NameCompany + '\'' +
|
||||
", IsActive='" + IsActive + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public void SetApproveDate(Date date)
|
||||
{
|
||||
this.ApproveDate = date;
|
||||
}
|
||||
|
||||
public void SetUser(Long id)
|
||||
{
|
||||
this.IdSotr = id;
|
||||
}
|
||||
|
||||
public void SetActive(Boolean active)
|
||||
{
|
||||
this.IsActive = active;
|
||||
}
|
||||
}
|
39
src/main/java/com/example/demo/speaker/model/TypeSotr.java
Normal file
39
src/main/java/com/example/demo/speaker/model/TypeSotr.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.example.demo.speaker.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class TypeSotr {
|
||||
@Column()
|
||||
private String Type;
|
||||
@jakarta.persistence.Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
public String GetType()
|
||||
{
|
||||
return this.Type;
|
||||
}
|
||||
|
||||
public TypeSotr() {
|
||||
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public TypeSotr(String TypeName)
|
||||
{
|
||||
this.Type = TypeName;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Type{" +
|
||||
"id=" + id +
|
||||
", Type='" + Type + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
47
src/main/java/com/example/demo/speaker/model/Worker.java
Normal file
47
src/main/java/com/example/demo/speaker/model/Worker.java
Normal file
@ -0,0 +1,47 @@
|
||||
package com.example.demo.speaker.model;
|
||||
|
||||
import jakarta.persistence.Column;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
|
||||
@Entity
|
||||
public class Worker {
|
||||
public Worker()
|
||||
{
|
||||
|
||||
}
|
||||
public Worker(String FIO)
|
||||
{
|
||||
this.FIO = FIO;
|
||||
}
|
||||
@jakarta.persistence.Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private Long id;
|
||||
@Column()
|
||||
private String FIO;
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
public String GetFio()
|
||||
{
|
||||
return this.FIO;
|
||||
}
|
||||
public void SetFio(String fio)
|
||||
{
|
||||
this.FIO = fio;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Worker{" +
|
||||
"id=" + id +
|
||||
", Type='" + FIO + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
@ -1,101 +0,0 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.domain.IMethod;
|
||||
import com.example.demo.speaker.domain.MethodBoolean;
|
||||
import com.example.demo.speaker.domain.MethodString;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MethodService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public MethodService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public String Sum(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString){
|
||||
if(first instanceof String && second instanceof String)
|
||||
{
|
||||
return String.format("%s", operation.Sum(first,second));
|
||||
}
|
||||
return "Uncorrect type";
|
||||
}else if (operation instanceof MethodBoolean){
|
||||
if(first instanceof Boolean && second instanceof Boolean)
|
||||
{
|
||||
return operation.Sum(Boolean.parseBoolean(first.toString()),Boolean.parseBoolean(second.toString())).toString();
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}
|
||||
else{
|
||||
if(first instanceof Integer && second instanceof Integer)
|
||||
{
|
||||
return String.format("%s", operation.Sum(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}
|
||||
}
|
||||
|
||||
public String Ras(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString){
|
||||
if(first instanceof String && second instanceof Integer)
|
||||
{
|
||||
return String.format("%s", operation.Minus(first,Integer.parseInt(second.toString())));
|
||||
}
|
||||
return "Uncorrect type";
|
||||
}else if (operation instanceof MethodBoolean){
|
||||
if(first instanceof Boolean && second instanceof Integer)
|
||||
{
|
||||
return operation.Minus(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}else{
|
||||
if(first instanceof Integer && second instanceof Integer)
|
||||
{
|
||||
return String.format("%s", operation.Minus(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}
|
||||
}
|
||||
|
||||
public String Pros(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString){
|
||||
if(first instanceof String && second instanceof Integer)
|
||||
{
|
||||
return String.format("%s", operation.Multiply(first,Integer.parseInt(second.toString())));
|
||||
}
|
||||
return "Uncorrect type";
|
||||
}else if (operation instanceof MethodBoolean){
|
||||
if(first instanceof Boolean && second instanceof Integer)
|
||||
{
|
||||
return operation.Multiply(Boolean.parseBoolean(first.toString()), Integer.parseInt(second.toString())).toString();
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}else{
|
||||
if(first instanceof Integer && second instanceof Integer)
|
||||
{
|
||||
return String.format("%s", operation.Multiply(Integer.parseInt(first.toString()),Integer.parseInt(second.toString())));
|
||||
}
|
||||
else return "Uncorrect type";
|
||||
}
|
||||
}
|
||||
|
||||
public String Del(Object first, Object second, String type) {
|
||||
final IMethod operation = (IMethod) applicationContext.getBean(type);
|
||||
if (operation instanceof MethodString){
|
||||
return String.format("%s", operation.Div(first,second));
|
||||
}else if (operation instanceof MethodBoolean){
|
||||
if(first.toString()!="true" && second.toString() != "false")
|
||||
{
|
||||
throw new ClassCastException("Uncorrect type");
|
||||
}
|
||||
return String.format("%s", operation.Div(Boolean.parseBoolean(first.toString()), Boolean.parseBoolean(second.toString())).toString());
|
||||
}else {
|
||||
return String.format("%s", operation.Div(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.model.ReportSort;
|
||||
import com.example.demo.speaker.model.Worker;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import javax.swing.text.html.HTMLDocument;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@Service
|
||||
public class ReportSotrService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public ReportSort addReport(String name, Date createDate, Long IdType, String text) {
|
||||
if (!StringUtils.hasText(name) || ! StringUtils.hasText(createDate.toString())
|
||||
|| ! StringUtils.hasText(IdType.toString())) {
|
||||
throw new IllegalArgumentException("Some field of report is null or empty");
|
||||
}
|
||||
final ReportSort report = new ReportSort(name, createDate, IdType, text);
|
||||
em.persist(report);
|
||||
return report;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public ReportSort findReport(Long id) {
|
||||
final ReportSort report = em.find(ReportSort.class, id);
|
||||
if (report == null) {
|
||||
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<ReportSort> findAllReports() {
|
||||
return em.createQuery("select s from ReportSort s", ReportSort.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ReportSort updateReport(Long id, Date approveDate, Boolean isActive, Long IdWorker) {
|
||||
if (!StringUtils.hasText(approveDate.toString()) || !StringUtils.hasText(isActive.toString())) {
|
||||
throw new IllegalArgumentException("Some field is null or empty");
|
||||
}
|
||||
final ReportSort currentReport = findReport(id);
|
||||
currentReport.SetApproveDate(approveDate);
|
||||
currentReport.SetActive(isActive);
|
||||
currentReport.SetUser(IdWorker);
|
||||
return em.merge(currentReport);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public ReportSort deleteReport(Long id) {
|
||||
final ReportSort currentReport = findReport(id);
|
||||
em.remove(currentReport);
|
||||
return currentReport;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllReports() {
|
||||
em.createQuery("delete from ReportSort").executeUpdate();
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.model.ReportSort;
|
||||
import com.example.demo.speaker.model.TypeSotr;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@Service
|
||||
public class TypeSortService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public TypeSotr addType(String name) {
|
||||
if (!StringUtils.hasText(name)) {
|
||||
throw new IllegalArgumentException("Name of Type is null or empty");
|
||||
}
|
||||
final TypeSotr type = new TypeSotr(name);
|
||||
em.persist(type);
|
||||
return type;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public TypeSotr findType(Long id) {
|
||||
final TypeSotr type = em.find(TypeSotr.class, id);
|
||||
if (type == null) {
|
||||
throw new EntityNotFoundException(String.format("Type with id [%s] is not found", id));
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<TypeSotr> findAllTypes() {
|
||||
return em.createQuery("select s from TypeSotr s", TypeSotr.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public TypeSotr deleteType(Long id) {
|
||||
final TypeSotr currentType = findType(id);
|
||||
em.remove(currentType);
|
||||
return currentType;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllTypes() {
|
||||
em.createQuery("delete from TypeSotr").executeUpdate();
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package com.example.demo.speaker.service;
|
||||
|
||||
import com.example.demo.speaker.model.ReportSort;
|
||||
import com.example.demo.speaker.model.Worker;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@Service
|
||||
public class WorkerService {
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Transactional
|
||||
public Worker addWorker(String name) {
|
||||
if (!StringUtils.hasText(name) ) {
|
||||
throw new IllegalArgumentException("Name of worker is null or empty");
|
||||
}
|
||||
final Worker worker = new Worker(name);
|
||||
em.persist(worker);
|
||||
return worker;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Worker findWorker(Long id) {
|
||||
final Worker worker = em.find(Worker.class, id);
|
||||
if (worker == null) {
|
||||
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
|
||||
}
|
||||
return worker;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Worker> findAllWorkers() {
|
||||
return em.createQuery("select s from Worker s", Worker.class)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Worker updateReportWorker(Long id, String Name) {
|
||||
if (!StringUtils.hasText(Name.toString())) {
|
||||
throw new IllegalArgumentException("Name is null or empty");
|
||||
}
|
||||
final Worker currentWorker = findWorker(id);
|
||||
currentWorker.SetFio(Name);
|
||||
return em.merge(currentWorker);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Worker deleteWorker(Long id) {
|
||||
final Worker currentWorker = findWorker(id);
|
||||
em.remove(currentWorker);
|
||||
return currentWorker;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void deleteAllWorkers() {
|
||||
em.createQuery("delete from Worker").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,6 +1,5 @@
|
||||
package com.example.demo;
|
||||
|
||||
import com.example.demo.speaker.service.MethodService;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -9,106 +8,6 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
|
||||
@SpringBootTest
|
||||
class DemoApplicationTests {
|
||||
@Autowired
|
||||
private MethodService methodService;
|
||||
@Test
|
||||
void testIntType() {
|
||||
String res = methodService.Sum(2, "2", "int");
|
||||
Assertions.assertEquals("Uncorrect type", res);
|
||||
}
|
||||
@Test
|
||||
void testStringType() {
|
||||
String res = methodService.Sum(2, 2, "string");
|
||||
Assertions.assertEquals("Uncorrect type", res);
|
||||
}
|
||||
@Test
|
||||
void testBooleanType() {
|
||||
String res = methodService.Sum("3", "2", "boolean");
|
||||
Assertions.assertEquals("Uncorrect type", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntSum() {
|
||||
Integer res = Integer.valueOf(methodService.Sum(2, 2, "int"));
|
||||
Assertions.assertEquals(4, res);
|
||||
}
|
||||
@Test
|
||||
void testStringSum() {
|
||||
String res = methodService.Sum("hello, ", "2", "string");
|
||||
Assertions.assertEquals("hello, 2", res);
|
||||
}
|
||||
@Test
|
||||
void testBooleanSum() {
|
||||
String res = methodService.Sum(true, true, "boolean");
|
||||
Assertions.assertEquals("true", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntDiff() {
|
||||
Integer res = Integer.valueOf(methodService.Ras(4, 2, "int"));
|
||||
Assertions.assertEquals(2, res);
|
||||
}
|
||||
@Test
|
||||
void testStringDiff() {
|
||||
String res = methodService.Ras("hello2", 2, "string");
|
||||
Assertions.assertEquals("hell", res);
|
||||
}
|
||||
@Test
|
||||
void testBooleanDiff() {
|
||||
String res = methodService.Ras(true, 2, "boolean");
|
||||
Assertions.assertEquals("false", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntMultiple() {
|
||||
Integer res = Integer.valueOf(methodService.Pros(2, 3, "int"));
|
||||
Assertions.assertEquals(6, res);
|
||||
}
|
||||
@Test
|
||||
void testStringMultiple() {
|
||||
String res = methodService.Pros("test", 2, "string");
|
||||
Assertions.assertEquals("testtest", res);
|
||||
}
|
||||
@Test
|
||||
void testBooleanMultiple() {
|
||||
String res = methodService.Pros(true, 2, "boolean");
|
||||
Assertions.assertEquals("true", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntDivide() {
|
||||
Integer res = Integer.valueOf(methodService.Del(10, 2, "int"));
|
||||
Assertions.assertEquals(5, res);
|
||||
}
|
||||
@Test
|
||||
void testStringDivide() {
|
||||
String res = methodService.Del("test", "2", "string");
|
||||
Assertions.assertEquals("false", res);
|
||||
}
|
||||
@Test
|
||||
void testBooleanDivide() {
|
||||
String res = methodService.Del(true, 2, "boolean");
|
||||
Assertions.assertEquals("false", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorErrorWired() {
|
||||
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> methodService.Sum(1, 2, "date"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorTypeString() {
|
||||
Assertions.assertThrows(ClassCastException.class, () -> methodService.Del(1, 2, "string"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorTypeBoolean() {
|
||||
Assertions.assertThrows(ClassCastException.class, () -> methodService.Del("jjj15", 3, "boolean"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testErrorTypeInt() {
|
||||
Assertions.assertThrows(NumberFormatException.class, () -> methodService.Del(true, 2, "int"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
74
src/test/java/com/example/demo/JPATests.java
Normal file
74
src/test/java/com/example/demo/JPATests.java
Normal file
@ -0,0 +1,74 @@
|
||||
package com.example.demo;
|
||||
|
||||
|
||||
import com.example.demo.speaker.model.ReportSort;
|
||||
import com.example.demo.speaker.service.ReportSotrService;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
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.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class JPATests {
|
||||
private static final Logger log = LoggerFactory.getLogger(JPATests.class);
|
||||
private static final SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.ENGLISH);
|
||||
|
||||
@Autowired
|
||||
private ReportSotrService reportService;
|
||||
|
||||
@Test
|
||||
void testReportCreate() throws ParseException {
|
||||
|
||||
reportService.deleteAllReports();
|
||||
final ReportSort report = reportService.addReport("Mars", formatter.parse("2018-05-05"),
|
||||
0L, "Хотим сотрудничать");
|
||||
log.info(report.toString());
|
||||
Assertions.assertNotNull(report.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReportRead() throws ParseException {
|
||||
reportService.deleteAllReports();
|
||||
final ReportSort report = reportService.addReport("Mars", formatter.parse("2018-05-05"),
|
||||
0L, "Хотим сотрудничать");
|
||||
log.info(report.toString());
|
||||
final ReportSort findStudent = reportService.findReport(report.getId());
|
||||
log.info(findStudent.toString());
|
||||
Assertions.assertEquals(report, findStudent);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReportsReadNotFound() {
|
||||
reportService.deleteAllReports();
|
||||
Assertions.assertThrows(EntityNotFoundException.class, () -> reportService.findReport(-1L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testReportsReadAll() throws ParseException {
|
||||
reportService.deleteAllReports();
|
||||
reportService.addReport("Mars", formatter.parse("2018-05-05"),
|
||||
0L, "Хотим сотрудничать");
|
||||
reportService.addReport("AIST", formatter.parse("2019-05-05"),
|
||||
1L, "Хотим сотрудничать");
|
||||
final List<ReportSort> reports = reportService.findAllReports();
|
||||
log.info(reports.toString());
|
||||
Assertions.assertEquals(reports.size(), 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStudentReadAllEmpty() {
|
||||
reportService.deleteAllReports();
|
||||
final List<ReportSort> reports = reportService.findAllReports();
|
||||
log.info(reports.toString());
|
||||
Assertions.assertEquals(reports.size(), 0);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user