controllers and services

This commit is contained in:
2025-12-11 20:46:54 +03:00
parent 5865cb5a28
commit 6e83eb0cbb
6 changed files with 246 additions and 0 deletions

View File

@@ -1,10 +1,61 @@
package com.example.nto.controller; 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: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
@RestController
@RequestMapping("/api/{code}")
public class BookingController { 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<String, Object> availablePlaces = bookingService.getAvailablePlaces();
return ResponseEntity.ok(availablePlaces);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
@PostMapping("/book")
public ResponseEntity<Void> bookPlace(@PathVariable String code, @RequestBody Map<String, Object> 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();
}
}
} }

View File

@@ -1,10 +1,46 @@
package com.example.nto.controller; 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: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
@RestController
@RequestMapping("/api/{code}")
public class EmployeeController { public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/auth")
public ResponseEntity<Void> 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();
}
}
} }

View File

@@ -1,5 +1,7 @@
package com.example.nto.service; package com.example.nto.service;
import java.util.Map;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
@@ -7,4 +9,7 @@ package com.example.nto.service;
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
public interface BookingService { public interface BookingService {
boolean employeeExists(String code);
Map<String, Object> getAvailablePlaces();
boolean createBooking(String code, String date, Integer placeId);
} }

View File

@@ -1,5 +1,8 @@
package com.example.nto.service; package com.example.nto.service;
import java.util.Map;
import java.util.Optional;
/** /**
* TODO: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
* ================================= * =================================
@@ -7,4 +10,6 @@ package com.example.nto.service;
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
public interface EmployeeService { public interface EmployeeService {
boolean existsByCode(String code);
Optional<Map<String, Object>> getEmployeeInfo(String code);
} }

View File

@@ -1,6 +1,22 @@
package com.example.nto.service.impl; package com.example.nto.service.impl;
import com.example.nto.service.BookingService; 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: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +24,90 @@ import com.example.nto.service.BookingService;
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
@Service
public class BookingServiceImpl implements BookingService { 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<String, Object> getAvailablePlaces() {
Map<String, Object> result = new HashMap<>();
LocalDate today = LocalDate.now();
List<Place> allPlaces = placeRepository.findAll();
for (int i = 0; i < 4; i++) {
LocalDate date = today.plusDays(i);
String dateStr = date.toString();
List<Booking> bookingsOnDate = bookingRepository.findByDate(date);
List<Long> bookedPlaceIds = new ArrayList<>();
for (Booking booking : bookingsOnDate) {
bookedPlaceIds.add(booking.getPlace().getId());
}
List<Map<String, Object>> availablePlaces = new ArrayList<>();
for (Place place : allPlaces) {
if (!bookedPlaceIds.contains(place.getId())) {
Map<String, Object> 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<Employee> employeeOpt = employeeRepository.findByCode(code);
if (employeeOpt.isEmpty()) {
return false;
}
Optional<Place> placeOpt = placeRepository.findById(placeId.longValue());
if (placeOpt.isEmpty()) {
return false;
}
LocalDate bookingDate = LocalDate.parse(date);
Place place = placeOpt.get();
Employee employee = employeeOpt.get();
Optional<Booking> 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;
}
}
} }

View File

@@ -1,6 +1,15 @@
package com.example.nto.service.impl; 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 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: ДОРАБОТАТЬ в рамках задания * TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +17,44 @@ import com.example.nto.service.EmployeeService;
* МОЖНО: Добавлять методы, аннотации, зависимости * МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета * НЕЛЬЗЯ: Изменять название класса и пакета
*/ */
@Service
public class EmployeeServiceImpl implements EmployeeService { public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
public boolean existsByCode(String code) {
return employeeRepository.findByCode(code).isPresent();
}
@Override
public Optional<Map<String, Object>> getEmployeeInfo(String code) {
Optional<Employee> employeeOpt = employeeRepository.findByCode(code);
if (employeeOpt.isEmpty()) {
return Optional.empty();
}
Employee employee = employeeOpt.get();
Map<String, Object> info = new HashMap<>();
info.put("name", employee.getName());
info.put("photoUrl", employee.getPhotoUrl());
Map<String, Map<String, Object>> bookingsMap = new HashMap<>();
// Используем bookingList вместо bookings
if (employee.getBookingList() != null) {
for (Booking booking : employee.getBookingList()) {
Map<String, Object> 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);
}
} }