diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 9885f84..03c9937 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,61 @@ package com.example.nto.controller; +import com.example.nto.service.BookingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.Map; /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("/api/{code}") public class BookingController { + + @Autowired + private BookingService bookingService; + + @GetMapping("/booking") + public ResponseEntity getAvailablePlaces(@PathVariable String code) { + try { + if (!bookingService.employeeExists(code)) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + + Map availablePlaces = bookingService.getAvailablePlaces(); + return ResponseEntity.ok(availablePlaces); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } + + @PostMapping("/book") + public ResponseEntity bookPlace(@PathVariable String code, @RequestBody Map request) { + try { + if (!bookingService.employeeExists(code)) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + + String date = (String) request.get("date"); + Integer placeId = (Integer) request.get("placeId"); + + if (date == null || placeId == null) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + + boolean success = bookingService.createBooking(code, date, placeId); + if (!success) { + return ResponseEntity.status(HttpStatus.CONFLICT).build(); + } + + return ResponseEntity.status(HttpStatus.CREATED).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } } diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 47658f9..ea23a53 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,10 +1,46 @@ package com.example.nto.controller; +import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ + +@RestController +@RequestMapping("/api/{code}") public class EmployeeController { + + @Autowired + private EmployeeService employeeService; + + @GetMapping("/auth") + public ResponseEntity auth(@PathVariable String code) { + try { + boolean exists = employeeService.existsByCode(code); + if (!exists) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + return ResponseEntity.ok().build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } + + @GetMapping("/info") + public ResponseEntity getInfo(@PathVariable String code) { + try { + return employeeService.getEmployeeInfo(code) + .map(ResponseEntity::ok) + .orElse(ResponseEntity.status(HttpStatus.UNAUTHORIZED).build()); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 31ec148..aa185b4 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,5 +1,7 @@ package com.example.nto.service; +import java.util.Map; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -7,4 +9,7 @@ package com.example.nto.service; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface BookingService { + boolean employeeExists(String code); + Map getAvailablePlaces(); + boolean createBooking(String code, String date, Integer placeId); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index cccd209..b240899 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -1,5 +1,8 @@ package com.example.nto.service; +import java.util.Map; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -7,4 +10,6 @@ package com.example.nto.service; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface EmployeeService { + boolean existsByCode(String code); + Optional> getEmployeeInfo(String code); } diff --git a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java index d24b244..9ad3f20 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -1,6 +1,22 @@ package com.example.nto.service.impl; import com.example.nto.service.BookingService; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; +import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.EmployeeRepository; +import com.example.nto.repository.PlaceRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +24,90 @@ import com.example.nto.service.BookingService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service public class BookingServiceImpl implements BookingService { + + @Autowired + private BookingRepository bookingRepository; + + @Autowired + private EmployeeRepository employeeRepository; + + @Autowired + private PlaceRepository placeRepository; + + @Override + public boolean employeeExists(String code) { + return employeeRepository.findByCode(code).isPresent(); + } + + @Override + public Map getAvailablePlaces() { + Map result = new HashMap<>(); + LocalDate today = LocalDate.now(); + + List allPlaces = placeRepository.findAll(); + + for (int i = 0; i < 4; i++) { + LocalDate date = today.plusDays(i); + String dateStr = date.toString(); + + List bookingsOnDate = bookingRepository.findByDate(date); + + List bookedPlaceIds = new ArrayList<>(); + for (Booking booking : bookingsOnDate) { + bookedPlaceIds.add(booking.getPlace().getId()); + } + + List> availablePlaces = new ArrayList<>(); + for (Place place : allPlaces) { + if (!bookedPlaceIds.contains(place.getId())) { + Map placeInfo = new HashMap<>(); + placeInfo.put("id", place.getId()); + placeInfo.put("place", place.getPlace()); + availablePlaces.add(placeInfo); + } + } + + result.put(dateStr, availablePlaces); + } + + return result; + } + + @Override + @Transactional + public boolean createBooking(String code, String date, Integer placeId) { + try { + Optional employeeOpt = employeeRepository.findByCode(code); + if (employeeOpt.isEmpty()) { + return false; + } + + Optional placeOpt = placeRepository.findById(placeId.longValue()); + if (placeOpt.isEmpty()) { + return false; + } + + LocalDate bookingDate = LocalDate.parse(date); + Place place = placeOpt.get(); + Employee employee = employeeOpt.get(); + + Optional existingBooking = bookingRepository.findByDateAndPlaceId(bookingDate, place.getId()); + if (existingBooking.isPresent()) { + return false; + } + + Booking booking = new Booking(); + booking.setDate(bookingDate); + booking.setPlace(place); + booking.setEmployee(employee); + + bookingRepository.save(booking); + return true; + + } catch (Exception e) { + return false; + } + } } diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java index f8125e5..2307946 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,6 +1,15 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +17,44 @@ import com.example.nto.service.EmployeeService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service public class EmployeeServiceImpl implements EmployeeService { + + @Autowired + private EmployeeRepository employeeRepository; + + @Override + public boolean existsByCode(String code) { + return employeeRepository.findByCode(code).isPresent(); + } + + @Override + public Optional> getEmployeeInfo(String code) { + Optional employeeOpt = employeeRepository.findByCode(code); + + if (employeeOpt.isEmpty()) { + return Optional.empty(); + } + + Employee employee = employeeOpt.get(); + Map info = new HashMap<>(); + info.put("name", employee.getName()); + info.put("photoUrl", employee.getPhotoUrl()); + + Map> bookingsMap = new HashMap<>(); + + // Используем bookingList вместо bookings + if (employee.getBookingList() != null) { + for (Booking booking : employee.getBookingList()) { + Map bookingInfo = new HashMap<>(); + bookingInfo.put("id", booking.getId()); + bookingInfo.put("place", booking.getPlace()); + + bookingsMap.put(booking.getDate().toString(), bookingInfo); + } + } + + info.put("booking", bookingsMap); + return Optional.of(info); + } }