реализация get, но пока ещё с ошибками

This commit is contained in:
lynxwq2
2025-11-26 16:04:30 +03:00
parent 0ea199cd6b
commit 5295d49a44
5 changed files with 80 additions and 5 deletions

View File

@@ -1,11 +1,12 @@
package com.example.nto.controller; package com.example.nto.controller;
import org.springframework.web.bind.annotation.GetMapping; import com.example.nto.entity.Employee;
import org.springframework.web.bind.annotation.PathVariable; import com.example.nto.service.EmployeeService;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.util.List;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
@@ -20,4 +21,17 @@ public class EmployeeController {
public int isAuth(@PathVariable int id){ public int isAuth(@PathVariable int id){
return id; return id;
} }
private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping
@ResponseStatus(code = HttpStatus.OK)
public List<Employee> getAll() {
return employeeService.getAll();
}
} }

View File

@@ -31,4 +31,25 @@ public class Employee {
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Booking> bookingList; private List<Booking> bookingList;
public Employee(long id, String name){
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
} }

View File

@@ -1,10 +1,20 @@
package com.example.nto.repository; package com.example.nto.repository;
import com.example.nto.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
public interface EmployeeRepository {
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Optional<Employee> findById(long id);
} }

View File

@@ -1,5 +1,9 @@
package com.example.nto.service; package com.example.nto.service;
import com.example.nto.entity.Employee;
import java.util.List;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
@@ -7,4 +11,7 @@ package com.example.nto.service;
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
public interface EmployeeService { public interface EmployeeService {
List<Employee> getAll();
Employee getById(long id);
} }

View File

@@ -1,6 +1,11 @@
package com.example.nto.service.impl; package com.example.nto.service.impl;
import com.example.nto.entity.Employee;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.EmployeeService; import com.example.nto.service.EmployeeService;
import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +13,23 @@ import com.example.nto.service.EmployeeService;
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
@Service
public class EmployeeServiceImpl implements EmployeeService { public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Override
public List<Employee> getAll() {
return employeeRepository.findAll();
}
@Override
public Employee getById(long id) {
return employeeRepository.findById(id);
}
} }