add entity company

This commit is contained in:
DozorovaA.A 2023-04-02 17:32:05 +04:00
parent 3823e88121
commit 8fca73e0a4
27 changed files with 962 additions and 540 deletions

View File

@ -15,8 +15,7 @@ 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'
implementation 'com.h2database:h2:2.1.210'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

BIN
data.mv.db Normal file

Binary file not shown.

View File

@ -5,7 +5,7 @@ import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
class webConfiguration implements WebMvcConfigurer {
class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**").allowedMethods("*");

View File

@ -0,0 +1,43 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.service.AppointmentService;
import com.example.demo.speaker.service.RequestForCooperationService;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping("/types")
public class AppointmentController {
private final AppointmentService appointmentService;
private final RequestForCooperationService requestForCooperationService;
public AppointmentController(AppointmentService appointmentService, RequestForCooperationService requestForCooperationService) {
this.appointmentService = appointmentService;
this.requestForCooperationService = requestForCooperationService;
}
@GetMapping("/{id}")
public Appointment getPost(@PathVariable Long id) {
return appointmentService.findPost(id);
}
@GetMapping("/")
public List<Appointment> getAllPosts() {
return appointmentService.findAllPosts();
}
@PostMapping("/")
public Appointment createPost(@RequestParam("Name") String name) {
return appointmentService.addPost(name);
}
@DeleteMapping("/{id}")
public Appointment deletePost(@PathVariable Long id) {
return appointmentService.deletePost(id);
}
}

View File

@ -0,0 +1,56 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.Company;
import com.example.demo.speaker.model.Employee;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.service.AppointmentService;
import com.example.demo.speaker.service.CompanyService;
import com.example.demo.speaker.service.EmployeeService;
import com.example.demo.speaker.service.RequestForCooperationService;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/companies")
public class CompanyController {
private final EmployeeService employeeService;
private final RequestForCooperationService requestForCooperationService;
private final AppointmentService appointmentService;
private final CompanyService companyService;
public CompanyController(CompanyService companyService, EmployeeService employeeService, RequestForCooperationService requestForCooperationService, AppointmentService appointmentService) {
this.employeeService = employeeService;
this.requestForCooperationService = requestForCooperationService;
this.appointmentService = appointmentService;
this.companyService = companyService;
}
@GetMapping("/{id}")
public Company getCompany(@PathVariable Long id) {
return companyService.findCompany(id);
}
@GetMapping("/")
public List<Company> getCompanies() {
return companyService.findAllCompanies();
}
@PostMapping("/create")
public Company createCompany(@RequestParam("Name") String name,
@RequestParam("legalAdressCompany") String legalAdressCompany,
@RequestParam("adressCompany") String adressCompany,
@RequestParam("contactEmail") String contactEmail) {
return companyService.addCompany(name, legalAdressCompany, adressCompany, contactEmail);
}
@DeleteMapping("/{id}")
public Company deleteCompany(@PathVariable Long id) {
return companyService.deleteCompany(id);
}
}

View File

@ -0,0 +1,76 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.Employee;
import com.example.demo.speaker.service.AppointmentService;
import com.example.demo.speaker.service.RequestForCooperationService;
import com.example.demo.speaker.service.EmployeeService;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/workers")
public class EmployeeController {
private final EmployeeService employeeService;
private final RequestForCooperationService requestForCooperationService;
private final AppointmentService appointmentService;
public EmployeeController(EmployeeService employeeService, RequestForCooperationService requestForCooperationService, AppointmentService appointmentService) {
this.employeeService = employeeService;
this.requestForCooperationService = requestForCooperationService;
this.appointmentService = appointmentService;
}
@GetMapping("/{id}")
public Employee getWorker(@PathVariable Long id) {
return employeeService.findWorker(id);
}
@GetMapping("/")
public List<Employee> getWorkers() {
return employeeService.findAllWorkers();
}
@PostMapping("/")
public Employee createWorker(@RequestParam("Name") String Name,
@RequestParam("postId") Long postId) {
final Appointment appointment = appointmentService.findPost(postId);
return employeeService.addWorker(Name, appointment);
}
@PatchMapping("/{id}")
public Employee updateWorker(@PathVariable Long id,
@RequestParam("Name") String Name,
@RequestParam("PostId") Long postId) {
final Appointment appointment = appointmentService.findPost(postId);
return employeeService.updateReportWorker(id, Name, appointment);
}
@DeleteMapping("/{id}")
public Employee deleteWorker(@PathVariable Long id) {
return employeeService.deleteWorker(id);
}
@GetMapping("/id={id}")
public List<RequestForCooperation> getListOfReports(@RequestParam("id") Long id) {
var list = requestForCooperationService.findAllRequests();
List<RequestForCooperation> listOfReports = new ArrayList<>();
for (RequestForCooperation l :list) {
if(l.getWorker().getId() == id)
{
listOfReports.add(l);
}
}
return listOfReports;
}
@GetMapping("/postId={postId}")
public List<Employee> getListOfWorkers(@RequestParam("postId") Long id) {
final Appointment appointment = appointmentService.findPost(id);
return employeeService.findWorkerByPost(appointment);
}
}

View File

@ -1,66 +0,0 @@
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;
}
}

View File

@ -0,0 +1,102 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.TypeOfRequestEnum;
import com.example.demo.speaker.model.Employee;
import com.example.demo.speaker.service.AppointmentService;
import com.example.demo.speaker.service.CompanyService;
import com.example.demo.speaker.service.RequestForCooperationService;
import com.example.demo.speaker.service.EmployeeService;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/reports")
public class RequestForCooperationController {
private final RequestForCooperationService requestForCooperationService;
private final EmployeeService employeeService;
private final AppointmentService appointmentService;
private final CompanyService companyService;
public RequestForCooperationController(RequestForCooperationService requestForCooperationService, EmployeeService employeeService,
AppointmentService appointmentService, CompanyService companyService) {
this.requestForCooperationService = requestForCooperationService;
this.employeeService = employeeService;
this.appointmentService = appointmentService;
this.companyService = companyService;
}
@GetMapping("/{id}")
public RequestForCooperation getRequest(@PathVariable Long id) {
return requestForCooperationService.findRequest(id);
}
@GetMapping("/")
public List<RequestForCooperation> getRequests() {
return requestForCooperationService.findAllRequests();
}
@PostMapping("/")
public RequestForCooperation createRequest(@RequestParam("companyId") Long companyId,
@RequestParam("createDate") Date createDate,
@RequestParam("Type") String type,
@RequestParam("Text") String Text) {
final TypeOfRequestEnum typeSotr;
switch (type)
{
case "TRACKING":
typeSotr = TypeOfRequestEnum.TRACKING;
break;
case "DEVELOP":
typeSotr = TypeOfRequestEnum.DEVELOP;
break;
default:
typeSotr = TypeOfRequestEnum.TRACKING;
break;
}
return requestForCooperationService.addRequest(companyService.findCompany(companyId), createDate, typeSotr, Text);
}
@PatchMapping("/{id}")
public RequestForCooperation updateRequest(@PathVariable Long id,
@RequestParam("approveDate") Date approveDate,
@RequestParam("isActive") Boolean isActive,
@RequestParam("Worker") Long idWorker) {
final Employee employee = employeeService.findWorker(idWorker);
return requestForCooperationService.updateRequest(id, approveDate, isActive, employee);
}
@DeleteMapping("/{id}")
public RequestForCooperation deleteRequest(@PathVariable Long id) {
return requestForCooperationService.deleteRequest(id);
}
@PostMapping("/list")
public String ListOfRequest(@RequestParam("Type") String type) {
var List = requestForCooperationService.findAllRequests();
final TypeOfRequestEnum typeSotr;
switch (type)
{
case "TRACKING":
typeSotr = TypeOfRequestEnum.TRACKING;
break;
case "DEVELOP":
typeSotr = TypeOfRequestEnum.DEVELOP;
break;
default:
typeSotr = TypeOfRequestEnum.TRACKING;
break;
}
EmployeeService employeeService = new EmployeeService();
String result = "";
for (var i : List) {
var Worker = employeeService.findWorker(i.getWorker().getId());
result += i.toString() + "Type: " + typeSotr.toString() + "Worker: "+Worker.toString() + "\n\r";
}
return result;
}
}

View File

@ -1,37 +0,0 @@
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);
}
}

View File

@ -1,43 +0,0 @@
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);
}
}

View File

@ -0,0 +1,53 @@
package com.example.demo.speaker.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Appointment {
@Column(nullable = false)
private String namePost;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public String getNamePost()
{
return this.namePost;
}
public Appointment() { }
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public Appointment(String namePost)
{
this.namePost = namePost;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Appointment appointment = (Appointment) o;
return Objects.equals(id, appointment.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Post{" +
"id=" + id +
",Name='" + namePost + '\'' +
'}';
}
}

View File

@ -0,0 +1,87 @@
package com.example.demo.speaker.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Company {
@Column(nullable = false)
private String nameCompany;
@Column(nullable = false)
private String legalAdressCompany;
@Column(nullable = false)
private String adressCompany;
@Column(nullable = false)
private String contactEmail;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
public Company(){}
public Company(String nameCompany, String legalAdressCompany, String adressCompany,
String contactEmail)
{
this.nameCompany = nameCompany;
this.adressCompany = adressCompany;
this.legalAdressCompany = legalAdressCompany;
this.contactEmail = contactEmail;
}
public Long getId()
{
return this.id;
}
public String getNameCompany()
{
return this.nameCompany;
}
public String getLegalAdressCompany()
{
return this.legalAdressCompany;
}
public String getAdressCompany()
{
return this.adressCompany;
}
public String getContactEmail()
{
return this.contactEmail;
}
public void setNameCompany(String name)
{
this.nameCompany = name;
}
public void setLegalAdressCompany(String adressCompany)
{
this.legalAdressCompany = adressCompany;
}
public void setAdressCompany(String adressCompany)
{
this.adressCompany = adressCompany;
}
public void setContactEmail(String contactEmail)
{
this.contactEmail = contactEmail;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Company company = (Company) o;
return Objects.equals(id, company.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Company{" +
"id=" + id +
",Name='" + nameCompany + '\'' +
'}';
}
}

View File

@ -0,0 +1,70 @@
package com.example.demo.speaker.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false)
private String fio;
@OneToOne(fetch = FetchType.LAZY)
private Appointment appointment;
public Employee() {}
public Employee(String fio, Appointment appointment) {
this.fio = fio;
this.appointment = appointment;
}
public void setId(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public String getFio()
{
return this.fio;
}
public Appointment getPost()
{
return this.appointment;
}
public void setFio(String fio)
{
this.fio = fio;
}
public void setPost(Appointment appointment)
{
this.appointment = appointment;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(id, employee.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return "Worker{" +
"id=" + id +
", Name='" + fio + '\'' +
", Post='" + appointment.getNamePost() + '\'' +
'}';
}
}

View File

@ -1,107 +0,0 @@
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;
}
}

View File

@ -0,0 +1,117 @@
package com.example.demo.speaker.model;
import jakarta.persistence.*;
import java.util.Date;
import java.util.Objects;
@Entity
public class RequestForCooperation {
@jakarta.persistence.Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.LAZY)
private Company nameCompany;
@Column(nullable = false)
private String comment;
@Column(nullable = false)
private Date createDate;
private Date approveDate;
private Boolean isActive;
@OneToOne(fetch = FetchType.LAZY)
private Employee sotr;
@Enumerated(EnumType.STRING)
private TypeOfRequestEnum typeSotr;
public RequestForCooperation() {
}
public RequestForCooperation(Company name, Date createDate, TypeOfRequestEnum type, String text) {
this.nameCompany = name;
this.createDate = createDate;
this.comment = text;
this.typeSotr = type;
}
public Company getName()
{
return this.nameCompany;
}
public TypeOfRequestEnum getType()
{
return this.typeSotr;
}
public Date getCreateDate()
{
return this.createDate;
}
public Date getApproveDate()
{
return this.approveDate;
}
public Employee getWorker()
{
return this.sotr;
}
public void setWorker(Employee sotr)
{
this.sotr = sotr;
}
public Boolean getActive()
{
return this.isActive;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RequestForCooperation report = (RequestForCooperation) 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.toString() + '\'' +
", Type='" + typeSotr + '\'' +
", 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(Employee sotr)
{
this.sotr = sotr;
}
public void setActive(Boolean active)
{
this.isActive = active;
}
public void setType(TypeOfRequestEnum type)
{
this.typeSotr = type;
}
}

View File

@ -0,0 +1,6 @@
package com.example.demo.speaker.model;
public enum TypeOfRequestEnum {
TRACKING,
DEVELOP
}

View File

@ -1,39 +0,0 @@
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 + '\'' +
'}';
}
}

View File

@ -1,47 +0,0 @@
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 + '\'' +
'}';
}
}

View File

@ -1,7 +1,6 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.model.ReportSort;
import com.example.demo.speaker.model.TypeSotr;
import com.example.demo.speaker.model.Appointment;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
@ -9,47 +8,46 @@ 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 {
public class AppointmentService {
@PersistenceContext
private EntityManager em;
@Transactional
public TypeSotr addType(String name) {
public Appointment addPost(String name) {
if (!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Name of Type is null or empty");
}
final TypeSotr type = new TypeSotr(name);
final Appointment type = new Appointment(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) {
public Appointment findPost(Long id) {
final Appointment appointment = em.find(Appointment.class, id);
if (appointment == null) {
throw new EntityNotFoundException(String.format("Type with id [%s] is not found", id));
}
return type;
return appointment;
}
@Transactional(readOnly = true)
public List<TypeSotr> findAllTypes() {
return em.createQuery("select s from TypeSotr s", TypeSotr.class)
public List<Appointment> findAllPosts() {
return em.createQuery("select s from Appointment s", Appointment.class)
.getResultList();
}
@Transactional
public TypeSotr deleteType(Long id) {
final TypeSotr currentType = findType(id);
em.remove(currentType);
return currentType;
public Appointment deletePost(Long id) {
final Appointment currentAppointment = findPost(id);
em.remove(currentAppointment);
return currentAppointment;
}
@Transactional
public void deleteAllTypes() {
em.createQuery("delete from TypeSotr").executeUpdate();
public void deleteAllvs() {
em.createQuery("delete from Appointment").executeUpdate();
}
}

View File

@ -0,0 +1,56 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.model.Company;
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.List;
@Service
public class CompanyService {
@PersistenceContext
private EntityManager em;
@Transactional
public Company addCompany(String nameCompany, String legalAdressCompany, String adressCompany,
String contactEmail) {
if (!StringUtils.hasText(nameCompany) && !StringUtils.hasText(nameCompany) &&
!StringUtils.hasText(nameCompany) && !StringUtils.hasText(nameCompany)) {
throw new IllegalArgumentException("One of the field is null or empty");
}
final Company company = new Company(nameCompany, legalAdressCompany, adressCompany, contactEmail);
em.persist(company);
return company;
}
@Transactional(readOnly = true)
public Company findCompany(Long id) {
final Company company = em.find(Company.class, id);
if (company == null) {
throw new EntityNotFoundException(String.format("Company with id [%s] is not found", id));
}
return company;
}
@Transactional(readOnly = true)
public List<Company> findAllCompanies() {
return em.createQuery("select c from Company c", Company.class)
.getResultList();
}
@Transactional
public Company deleteCompany(Long id) {
final Company currentCompany = findCompany(id);
em.remove(currentCompany);
return currentCompany;
}
@Transactional
public void deleteAllCompanies() {
em.createQuery("delete from Company").executeUpdate();
}
}

View File

@ -0,0 +1,83 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.Employee;
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.ArrayList;
import java.util.List;
@Service
public class EmployeeService {
@PersistenceContext
private EntityManager em;
@Transactional
public Employee addWorker(String name, Appointment appointment) {
if (!StringUtils.hasText(name) || appointment == null) {
throw new IllegalArgumentException("Name of worker is null or empty");
}
final Employee employee = new Employee(name, appointment);
em.persist(employee);
return employee;
}
@Transactional(readOnly = true)
public Employee findWorker(Long id) {
final Employee employee = em.find(Employee.class, id);
if (employee == null) {
throw new EntityNotFoundException(String.format("Report with id [%s] is not found", id));
}
return employee;
}
@Transactional(readOnly = true)
public List<Employee> findAllWorkers() {
return em.createQuery("select s from Employee s", Employee.class)
.getResultList();
}
@Transactional
public Employee updateReportWorker(Long id, String Name, Appointment appointment) {
if (!StringUtils.hasText(Name.toString()) || appointment == null) {
throw new IllegalArgumentException("Name is null or empty");
}
final Employee currentEmployee = findWorker(id);
currentEmployee.setFio(Name);
currentEmployee.setPost(appointment);
return em.merge(currentEmployee);
}
@Transactional
public List<Employee> findWorkerByPost(Appointment appointment) {
if (appointment == null) {
throw new IllegalArgumentException("Post is null or empty");
}
final List<Employee> currentEmployees = findAllWorkers();
List<Employee> list = new ArrayList<Employee>();
for (var l: currentEmployees) {
if (l.getPost().getNamePost() == appointment.getNamePost())
{
list.add(l);
}
}
return list;
}
@Transactional
public Employee deleteWorker(Long id) {
final Employee currentEmployee = findWorker(id);
em.remove(currentEmployee);
return currentEmployee;
}
@Transactional
public void deleteAllWorkers() {
em.createQuery("delete from Employee").executeUpdate();
}
}

View File

@ -1,69 +0,0 @@
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();
}
}

View File

@ -0,0 +1,70 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.model.Company;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.TypeOfRequestEnum;
import com.example.demo.speaker.model.Employee;
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 RequestForCooperationService {
@PersistenceContext
private EntityManager em;
@Transactional
public RequestForCooperation addRequest(Company name, Date createDate, TypeOfRequestEnum type, String text) {
if (name == null || ! StringUtils.hasText(createDate.toString())
|| type == null) {
throw new IllegalArgumentException("Some field of report is null or empty");
}
final RequestForCooperation report = new RequestForCooperation(name, createDate, type, text);
em.persist(report);
return report;
}
@Transactional(readOnly = true)
public RequestForCooperation findRequest(Long id) {
final RequestForCooperation report = em.find(RequestForCooperation.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<RequestForCooperation> findAllRequests() {
return em.createQuery("select s from RequestForCooperation s", RequestForCooperation.class)
.getResultList();
}
@Transactional
public RequestForCooperation updateRequest(Long id, Date approveDate, Boolean isActive, Employee employee) {
if (!StringUtils.hasText(approveDate.toString()) || !StringUtils.hasText(isActive.toString())) {
throw new IllegalArgumentException("Some field is null or empty");
}
final RequestForCooperation currentReport = findRequest(id);
currentReport.setApproveDate(approveDate);
currentReport.setActive(isActive);
currentReport.setUser(employee);
return em.merge(currentReport);
}
@Transactional
public RequestForCooperation deleteRequest(Long id) {
final RequestForCooperation currentReport = findRequest(id);
em.remove(currentReport);
return currentReport;
}
@Transactional
public void deleteAllRequests() {
em.createQuery("delete from RequestForCooperation").executeUpdate();
}
}

View File

@ -1,65 +0,0 @@
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();
}
}

View File

@ -1,13 +0,0 @@
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
@SpringBootTest
class DemoApplicationTests {
}

View File

@ -1,74 +1,160 @@
package com.example.demo;
import com.example.demo.speaker.model.ReportSort;
import com.example.demo.speaker.service.ReportSotrService;
import com.example.demo.speaker.model.*;
import com.example.demo.speaker.service.CompanyService;
import com.example.demo.speaker.service.RequestForCooperationService;
import com.example.demo.speaker.service.AppointmentService;
import com.example.demo.speaker.service.EmployeeService;
import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional;
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 org.springframework.boot.test.context.SpringBootTest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Locale;
@SpringBootTest
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;
RequestForCooperationService requestService;
@Autowired
AppointmentService appointmentService;
@Autowired
EmployeeService employeeService;
@Autowired
CompanyService companyService;
@Test
void testReportCreate() throws ParseException {
@Transactional
void testCompaniesReadAll() {
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final Company comp1 = companyService.addCompany("Mars", "Ульяновск, Чердаклинский район",
"Чердаклинский район", "mars@mail.ru");
final Company comp2 = companyService.addCompany("AIST", "Ульяновск, Кузоватовская 20",
"Кузоватовская 20", "iast@mail.ru");
reportService.deleteAllReports();
final ReportSort report = reportService.addReport("Mars", formatter.parse("2018-05-05"),
0L, "Хотим сотрудничать");
log.info(report.toString());
Assertions.assertNotNull(report.getId());
final List<Company> companies = companyService.findAllCompanies();
log.info(companies.toString());
Assertions.assertEquals(companies.size(), 3);
}
@Test
void testReadCompany()
{
companyService.deleteAllCompanies();
Company addCompany = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
Company comp = companyService.findCompany(addCompany.getId());
log.info(addCompany.toString());
Assertions.assertEquals(comp, addCompany);
}
@Test
void testReportRead() throws ParseException {
reportService.deleteAllReports();
final ReportSort report = reportService.addReport("Mars", formatter.parse("2018-05-05"),
0L, "Хотим сотрудничать");
void testReportCreate() throws ParseException {
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final RequestForCooperation report = requestService.addRequest(comp,
formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
log.info(report.toString());
final ReportSort findStudent = reportService.findReport(report.getId());
log.info(findStudent.toString());
Assertions.assertEquals(report, findStudent);
Assertions.assertNotNull(report.getId());
}
@Test
void testAddWorker() throws ParseException {
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final RequestForCooperation report = requestService.addRequest(comp,
formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
final Appointment appointment = appointmentService.addPost("Manager");
final Employee employee = employeeService.addWorker("NameWorker", appointment);
report.setWorker(employee);
log.info(report.toString());
Assertions.assertNotNull(report.getWorker());
}
@Test
@Transactional
void testReportRead() throws ParseException {
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final RequestForCooperation report = requestService.addRequest(comp, formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
log.info(report.toString());
final RequestForCooperation findApplication = requestService.findRequest(report.getId());
log.info(findApplication.toString());
Assertions.assertEquals(report, findApplication);
}
@Test
void testReportsReadNotFound() {
reportService.deleteAllReports();
Assertions.assertThrows(EntityNotFoundException.class, () -> reportService.findReport(-1L));
requestService.deleteAllRequests();
Assertions.assertThrows(EntityNotFoundException.class, () -> requestService.findRequest(-1L));
}
@Test
void testWorkerAllByPost() throws ParseException {
employeeService.deleteAllWorkers();
appointmentService.deleteAllvs();
companyService.deleteAllCompanies();
final Appointment appointment = appointmentService.addPost("Manager");
final Employee employee = employeeService.addWorker("NAme1", appointment);
final Employee employee1 = employeeService.addWorker("Name2", appointment);
final List<Employee> employees = employeeService.findWorkerByPost(appointment);
log.info(employees.toString());
Assertions.assertEquals(2, employees.size());
}
@Test
@Transactional
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();
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
final TypeOfRequestEnum typeNew = TypeOfRequestEnum.DEVELOP;
Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
Company comp1 = companyService.addCompany("Mars", "Ульяновск, Чердаклинский район",
"Чердаклинский район", "mars@mail.ru");
Company comp2 = companyService.addCompany("AIST", "Ульяновск, Кузоватовская 20",
"Кузоватовская 20", "iast@mail.ru");
RequestForCooperation report = requestService.addRequest(comp, formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
report = requestService.addRequest(comp1, formatter.parse("05-Jun-2019"),
typeNew, "Хотим сотрудничать");
report = requestService.addRequest(comp2, formatter.parse("05-Jun-2019"),
typeNew, "Хотим сотрудничать");
final List<RequestForCooperation> reports = requestService.findAllRequests();
log.info(reports.toString());
Assertions.assertEquals(reports.size(), 2);
Assertions.assertEquals(reports.size(), 3);
}
@Test
void testStudentReadAllEmpty() {
reportService.deleteAllReports();
final List<ReportSort> reports = reportService.findAllReports();
void testReportsReadAllEmpty() {
requestService.deleteAllRequests();
final List<RequestForCooperation> reports = requestService.findAllRequests();
log.info(reports.toString());
Assertions.assertEquals(reports.size(), 0);
Assertions.assertEquals(0, reports.size());
}
}

View File

@ -0,0 +1,6 @@
spring.datasource.url=jdbc:h2:mem:testdb
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=create-drop