add controllers

This commit is contained in:
DozorovaA.A 2023-05-09 18:39:37 +04:00
parent 50b50ebe0d
commit 69fef2a6c7
9 changed files with 119 additions and 281 deletions

View File

@ -1,43 +1,49 @@
package com.example.demo.speaker.controller; 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.AppointmentService;
import com.example.demo.speaker.service.RequestForCooperationService; import com.example.demo.speaker.service.RequestForCooperationService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
//import javax.validation.Valid;
import java.util.*; import java.util.*;
@RestController @RestController
@RequestMapping("/types") @RequestMapping("/appointment")
public class AppointmentController { public class AppointmentController {
private final AppointmentService appointmentService; private final AppointmentService appointmentService;
private final RequestForCooperationService requestForCooperationService;
public AppointmentController(AppointmentService appointmentService) {
public AppointmentController(AppointmentService appointmentService, RequestForCooperationService requestForCooperationService) {
this.appointmentService = appointmentService; this.appointmentService = appointmentService;
this.requestForCooperationService = requestForCooperationService; }
@PostMapping
public AppointmentDTO addAppointment(@RequestBody AppointmentDTO appointmentDTO) {
return new AppointmentDTO(appointmentService.addAppointment(appointmentDTO));
} }
@PutMapping("/{id}")
@GetMapping("/{id}") public AppointmentDTO updateAppointment(@PathVariable Long id,@RequestBody AppointmentDTO appointmentDTO) {
public Appointment getPost(@PathVariable Long id) { return new AppointmentDTO(appointmentService.updateAppointment(id,appointmentDTO));
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}") @DeleteMapping("/{id}")
public Appointment deletePost(@PathVariable Long id) { public AppointmentDTO removeAppointment(@PathVariable Long id) {
return appointmentService.deletePost(id); return new AppointmentDTO(appointmentService.deleteAppointment(id));
} }
@DeleteMapping
public void removeAllAppointments() {
appointmentService.deleteAllAppointments();
}
@GetMapping("/{id}")
public AppointmentDTO findAppointment(@PathVariable Long id) {
return new AppointmentDTO(appointmentService.findAppointment(id));
}
public List<AppointmentDTO> findAllAppointments() {
return appointmentService.findAllAppointments()
.stream()
.map(AppointmentDTO::new)
.toList();
}
} }

View File

@ -14,47 +14,43 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/companies") @RequestMapping("/company")
public class CompanyController { public class CompanyController {
private final EmployeeService employeeService;
private final RequestForCooperationService requestForCooperationService;
private final AppointmentService appointmentService;
private final CompanyService companyService; private final CompanyService companyService;
public CompanyController(CompanyService _companyService) {
this.companyService = _companyService;
}
@PostMapping
public CompanyDTO addAppointment(@RequestBody CompanyDTO companyDTO) {
return new CompanyDTO(companyService.addCompany(companyDTO));
}
public CompanyController(CompanyService companyService, EmployeeService employeeService, RequestForCooperationService requestForCooperationService, AppointmentService appointmentService) { @PutMapping("/{id}")
this.employeeService = employeeService; public CompanyDTO updateAppointment(@PathVariable Long id,@RequestBody CompanyDTO companyDTO) {
this.requestForCooperationService = requestForCooperationService; return new CompanyDTO(companyService.updateCompany(id,companyDTO));
this.appointmentService = appointmentService; }
this.companyService = companyService;
@DeleteMapping("/{id}")
public CompanyDTO removeCompany(@PathVariable Long id) {
return new CompanyDTO(companyService.deleteCompany(id));
}
@DeleteMapping
public void removeAllCompanies() {
companyService.deleteAllCompanies();
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Company getCompany(@PathVariable Long id) { public CompanyDTO findCompany(@PathVariable Long id) {
return companyService.findCompany(id); return new CompanyDTO(companyService.findCompany(id));
} }
@GetMapping("/") public List<CompanyDTO> findAllCompanies() {
public List<Company> getCompanies() { return companyService.findAllCompanies()
return companyService.findAllCompanies(); .stream()
} .map(CompanyDTO::new)
.toList();
@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);
}
@GetMapping("/list={id}")
public List<RequestForCooperation> getRequestsOfCompany(@PathVariable Long id)
{
return companyService.findCompany(id).getList();
} }
} }

View File

@ -35,41 +35,29 @@ public class EmployeeController {
} }
@PostMapping("/") @PostMapping("/")
public Employee createWorker(@RequestParam("Name") String Name, public EmployeeDTO createWorker(@RequestBody EmployeeDTO emplyeeDTO) {
@RequestParam("postId") Long postId) { return new EmployeeDTO(employeeService.addWorker(emplyeeDTO));
final Appointment appointment = appointmentService.findPost(postId);
return employeeService.addWorker(Name, appointment);
} }
@PatchMapping("/{id}") @PatchMapping("/{id}")
public Employee updateWorker(@PathVariable Long id, public EmployeeDTO updateWorker(@PathVariable Long id,@RequestBody EmployeeDTO employeeDTO) {
@RequestParam("Name") String Name, return new EmployeeDTO(employeeService.updateReportWorker(id, employeeDTO));
@RequestParam("PostId") Long postId) {
final Appointment appointment = appointmentService.findPost(postId);
return employeeService.updateReportWorker(id, Name, appointment);
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public Employee deleteWorker(@PathVariable Long id) { public EmployeeDTO deleteWorker(@PathVariable Long id) {
return employeeService.deleteWorker(id); return new EmployeeDTO(employeeService.deleteWorker(id));
} }
@GetMapping("/id={id}") @GetMapping("/id={id}")
public List<RequestForCooperation> getListOfReports(@RequestParam("id") Long id) { public List<RequestForCooperation> getListOfReports(@PathVariable Long id) {
var list = requestForCooperationService.findAllRequests(); var employee = employeeService.findWorker(id);
List<RequestForCooperation> listOfReports = new ArrayList<>(); return employeeService.findAllRequestsOfEmployee(employee);
for (RequestForCooperation l :list) {
if(l.getWorker().getId() == id)
{
listOfReports.add(l);
}
}
return listOfReports;
} }
@GetMapping("/postId={postId}") @GetMapping("/postId={postId}")
public List<Employee> getListOfWorkers(@RequestParam("postId") Long id) { public List<Employee> getListOfWorkers(@RequestParam("postId") Long id) {
final Appointment appointment = appointmentService.findPost(id); final Appointment appointment = appointmentService.findAppointment(id);
return employeeService.findWorkerByPost(appointment); return employeeService.findWorkerByPost(appointment);
} }

View File

@ -91,7 +91,7 @@ public class RequestForCooperationController {
typeSotr = TypeOfRequestEnum.TRACKING; typeSotr = TypeOfRequestEnum.TRACKING;
break; break;
} }
EmployeeService employeeService = new EmployeeService(employeeRepository, appointmentRepository); EmployeeService employeeService = new EmployeeService(employeeRepository, appointmentRepository, requestForCooperationRepository);
String result = ""; String result = "";
for (var i : List) { for (var i : List) {
var Worker = employeeService.findWorker(i.getWorker().getId()); var Worker = employeeService.findWorker(i.getWorker().getId());

View File

@ -9,6 +9,6 @@ import org.springframework.data.repository.query.Param;
import java.util.List; import java.util.List;
public interface ICompanyRepository extends JpaRepository<Company, Long> { public interface ICompanyRepository extends JpaRepository<Company, Long> {
@Query("SELECT DISTINCT p.requests FROM Company p where :request MEMBER OF p.requests") @Query("SELECT DISTINCT p.requests FROM Company p where :company = p.id")
List<RequestForCooperation> getTypesRequest(@Param("request") RequestForCooperation request); List<RequestForCooperation> getRequestsOfCompany(@Param("company") Long id);
} }

View File

@ -12,4 +12,7 @@ import java.util.List;
public interface IEmployeeRepository extends JpaRepository<Employee, Long> { public interface IEmployeeRepository extends JpaRepository<Employee, Long> {
@Query("SELECT DISTINCT p.fio FROM Employee p where :appointment = p.appointment") @Query("SELECT DISTINCT p.fio FROM Employee p where :appointment = p.appointment")
List<Employee> getEmployeeByAppointment(@Param("appointment") Appointment appointment); List<Employee> getEmployeeByAppointment(@Param("appointment") Appointment appointment);
@Query("SELECT DISTINCT p FROM RequestForCooperation p where :employee = p.sotr")
List<RequestForCooperation> getRequestsOfEmployee(@Param("employee") Employee employee);
} }

View File

@ -1,68 +1,78 @@
package com.example.demo.speaker.service; package com.example.demo.speaker.service;
import com.example.demo.speaker.controller.CompanyDTO;
import com.example.demo.speaker.model.Company; import com.example.demo.speaker.model.Company;
import com.example.demo.speaker.model.RequestForCooperation; import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.model.TypeOfRequestEnum; import com.example.demo.speaker.model.TypeOfRequestEnum;
import jakarta.persistence.EntityManager; import com.example.demo.speaker.repository.ICompanyRepository;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.List; import java.util.List;
import java.util.Optional;
@Service @Service
public class CompanyService { public class CompanyService {
@PersistenceContext private final ICompanyRepository companyRepository;
private EntityManager em;
public CompanyService(ICompanyRepository companyRepository) {
this.companyRepository = companyRepository;
}
@Transactional @Transactional
public Company addCompany(String nameCompany, String legalAdressCompany, String adressCompany, public Company addCompany(CompanyDTO companyDTO) {
String contactEmail) { final Company company = new Company(companyDTO.getNameCompany(),
if (!StringUtils.hasText(nameCompany) && !StringUtils.hasText(nameCompany) && companyDTO.getLegalAdressCompany(),
!StringUtils.hasText(nameCompany) && !StringUtils.hasText(nameCompany)) { companyDTO.getAdressCompany(),
throw new IllegalArgumentException("One of the field is null or empty"); companyDTO.getContactEmail());
} companyRepository.save(company);
final Company company = new Company(nameCompany, legalAdressCompany, adressCompany, contactEmail);
em.persist(company);
return company; return company;
} }
@Transactional
public Company addRequest(CompanyDTO companyDTO, List<RequestForCooperation> requests){
final Company currentCompany = findCompany(companyDTO.getId());
currentCompany.setList((RequestForCooperation) requests);
return currentCompany;
}
@Transactional
public Company updateCompany(Long id, CompanyDTO companyDTO) {
final Company currentCompany = findCompany(id);
currentCompany.setNameCompany(companyDTO.getNameCompany());
currentCompany.setAdressCompany(currentCompany.getAdressCompany());
currentCompany.setLegalAdressCompany(currentCompany.getLegalAdressCompany());
currentCompany.setContactEmail(currentCompany.getContactEmail());
companyRepository.save(currentCompany);
return currentCompany;
}
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Company findCompany(Long id) { public Company findCompany(Long id) {
final Company company = em.find(Company.class, id); final Optional<Company> company = companyRepository.findById(id);
if (company == null) { return company.orElseThrow(()->new CompanyNotFoundException(id));
throw new EntityNotFoundException(String.format("Company with id [%s] is not found", id));
}
return company;
} }
@Transactional(readOnly = true) @Transactional(readOnly = true)
public List<Company> findAllCompanies() { public List<Company> findAllCompanies() {
return em.createQuery("select c from Company c", Company.class) return companyRepository.findAll();
.getResultList();
} }
@Transactional @Transactional
public Company deleteCompany(Long id) { public Company deleteCompany(Long id) {
final Company currentCompany = findCompany(id); final Company currentCompany = findCompany(id);
em.remove(currentCompany); companyRepository.delete(currentCompany);
return currentCompany; return currentCompany;
} }
@Transactional @Transactional
public void deleteAllCompanies() { public void deleteAllCompanies() {
em.createQuery("delete from Company").executeUpdate(); companyRepository.deleteAll();
} }
@Transactional @Transactional
public List<RequestForCooperation> getTypesRequests(Long id, TypeOfRequestEnum type) public List<RequestForCooperation> getTypesRequests(Long id)
{ {
final Company comp = findCompany(id); return companyRepository.getRequestsOfCompany(id);
var list = comp.getList();
List<RequestForCooperation> res = list.stream().
filter(r -> r.getType() == type).toList();
return res;
} }
} }

View File

@ -1,17 +1,15 @@
package com.example.demo.speaker.service; package com.example.demo.speaker.service;
import com.example.demo.speaker.controller.AppointmentDTO;
import com.example.demo.speaker.controller.EmployeeDTO; import com.example.demo.speaker.controller.EmployeeDTO;
import com.example.demo.speaker.model.Appointment; import com.example.demo.speaker.model.Appointment;
import com.example.demo.speaker.model.Employee; import com.example.demo.speaker.model.Employee;
import com.example.demo.speaker.model.RequestForCooperation;
import com.example.demo.speaker.repository.IAppointmentRepository; import com.example.demo.speaker.repository.IAppointmentRepository;
import com.example.demo.speaker.repository.IEmployeeRepository; import com.example.demo.speaker.repository.IEmployeeRepository;
import jakarta.persistence.EntityNotFoundException; import com.example.demo.speaker.repository.IRequestForCooperationRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -63,6 +61,13 @@ private final IAppointmentRepository appointmentRepository;
return list; return list;
} }
@Transactional
public List<RequestForCooperation> findAllRequestsOfEmployee(Employee employee)
{
List<RequestForCooperation> list = employeeRepository.getRequestsOfEmployee(employee);
return list;
}
@Transactional @Transactional
public Employee deleteWorker(Long id) { public Employee deleteWorker(Long id) {
final Employee currentEmployee = findWorker(id); final Employee currentEmployee = findWorker(id);

View File

@ -23,176 +23,6 @@ import java.util.stream.Stream;
@SpringBootTest @SpringBootTest
public class JPATests { 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
RequestForCooperationService requestService;
@Autowired
AppointmentService appointmentService;
@Autowired
EmployeeService employeeService;
@Autowired
CompanyService companyService;
@Test
@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");
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 checkConnectionCompanyAndRequest() throws ParseException {
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
RequestForCooperation report = requestService.addRequest(comp,
formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
report = requestService.addRequest(comp,
formatter.parse("18-Jun-2018"),
type, "Хотим сотрудничать");
var listRequest = comp.getList();
Assertions.assertEquals(2, listRequest.size());
}
@Test
void checkConnectionRequestAndCompany() throws ParseException {
requestService.deleteAllRequests();
companyService.deleteAllCompanies();
final Company comp = companyService.addCompany("Mars", "Ульяновск, Солнечная 19",
"Солнечная 19", "mars@mail.ru");
final TypeOfRequestEnum type = TypeOfRequestEnum.TRACKING;
RequestForCooperation report = requestService.addRequest(comp,
formatter.parse("05-Jun-2018"),
type, "Хотим сотрудничать");
report = requestService.addRequest(comp,
formatter.parse("18-Jun-2018"),
type, "Хотим сотрудничать");
List<Company> listCompany = requestService.findAllRequests().
stream().map(p -> p.getName()).toList();
Assertions.assertEquals(1, listCompany.size());
}
@Test
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());
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() {
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 {
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(), 3);
}
@Test
void testReportsReadAllEmpty() {
requestService.deleteAllRequests();
final List<RequestForCooperation> reports = requestService.findAllRequests();
log.info(reports.toString());
Assertions.assertEquals(0, reports.size());
}
} }