Complete backend functionality
Some checks failed
Android Test / validate-and-test (pull_request) Has been cancelled

This commit is contained in:
2025-12-11 20:15:53 +07:00
parent 83b3202ea2
commit cebc033f0f
17 changed files with 327 additions and 14 deletions

View File

@@ -1,12 +1,17 @@
package com.example.nto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

View File

@@ -1,10 +1,79 @@
package com.example.nto.controller;
import com.example.nto.controller.dto.BookingRequest;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Place;
import com.example.nto.service.BookingService;
import com.example.nto.service.EmployeeService;
import com.example.nto.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequestMapping("/api/{code}")
public class BookingController {
@Autowired
private EmployeeService employeeService;
@Autowired
private BookingService bookingService;
@Autowired
private PlaceService placeService;
@Value("${booking.days-ahead}")
private int daysAhead;
@GetMapping("/booking")
public ResponseEntity<Map<LocalDate, List<Place>>> getAvailablePlaces(@PathVariable String code) {
var user = employeeService.getByCode(code);
if (user == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
try {
var today = LocalDate.now();
var allPlaces = placeService.getAll();
var upcomingBookings = bookingService.getAll();
var bookedPlacesByDate = upcomingBookings.stream()
.collect(Collectors.groupingBy(
Booking::getDate,
Collectors.mapping(it -> it.getPlace().getId(), Collectors.toSet())
));
var result = new LinkedHashMap<LocalDate, List<Place>>();
for (int i = 0; i <= daysAhead; i++) {
var date = today.plusDays(i);
var bookedPlaceIds = bookedPlacesByDate.getOrDefault(date, Collections.emptySet());
var availablePlaces = allPlaces.stream()
.filter(it -> !bookedPlaceIds.contains(it.getId()))
.toList();
result.put(date, availablePlaces);
}
return ResponseEntity.ok(result);
} catch (Exception e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); }
}
@PostMapping("/book")
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody BookingRequest bookingRequest) {
var user = employeeService.getByCode(code);
if (user == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
try {
var alreadyBooked = bookingService.getAll().stream()
.anyMatch(it -> it.getDate().isEqual(bookingRequest.getDate()) && it.getPlace().getId() == bookingRequest.getPlaceId());
if (alreadyBooked) return ResponseEntity.status(HttpStatus.CONFLICT).build();
var place = new Place();
place.setId(bookingRequest.getPlaceId());
bookingService.save(bookingRequest.getDate(), place, user);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); }
}
}

View File

@@ -1,10 +1,53 @@
package com.example.nto.controller;
import com.example.nto.controller.dto.EmployeeInfoResponse;
import com.example.nto.entity.Booking;
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.*;
import java.util.stream.Collectors;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequestMapping("/api/{code}")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@GetMapping("/auth")
public ResponseEntity<Object> checkCode(@PathVariable String code) {
var user = employeeService.getByCode(code);
if (user == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
return ResponseEntity.status(HttpStatus.OK).build();
}
@GetMapping("/info")
public ResponseEntity<EmployeeInfoResponse> getInfo(@PathVariable String code) {
var user = employeeService.getByCode(code);
if (user == null) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
var response = new EmployeeInfoResponse();
response.setName(user.getName());
response.setPhotoUrl(user.getPhotoUrl());
var bookingMap = user.getBookingList().stream().collect(Collectors.toMap(
Booking::getDate,
it -> {
var p = new EmployeeInfoResponse.BookedPlace();
p.setId(it.getPlace().getId());
p.setPlace(it.getPlace().getPlace());
return p;
})
);
response.setBooking(bookingMap);
return ResponseEntity.ok(response);
}
}

View File

@@ -0,0 +1,11 @@
package com.example.nto.controller.dto;
import lombok.Data;
import java.time.LocalDate;
@Data
public class BookingRequest {
private LocalDate date;
private long placeId;
}

View File

@@ -0,0 +1,19 @@
package com.example.nto.controller.dto;
import lombok.Data;
import java.time.LocalDate;
import java.util.Map;
@Data
public class EmployeeInfoResponse {
private String name;
private String photoUrl;
private Map<LocalDate, BookedPlace> booking;
@Data
public static class BookedPlace {
private long id;
private String place;
}
}

View File

@@ -1,8 +1,6 @@
package com.example.nto.entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
@@ -19,10 +17,12 @@ import java.time.LocalDate;
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private LocalDate date;
@@ -31,5 +31,10 @@ public class Booking {
@JoinColumn(name = "place_id")
private Place place;
@ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employee;
public Booking() {}
}

View File

@@ -17,10 +17,12 @@ import java.util.List;
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@@ -31,4 +33,7 @@ public class Employee {
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Booking> bookingList;
public Employee() {}
}

View File

@@ -1,12 +1,11 @@
package com.example.nto.entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
@@ -17,13 +16,20 @@ import lombok.NoArgsConstructor;
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Place {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "place_name")
private String place;
@OneToMany(mappedBy = "place", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Booking> bookingList;
public Place() {}
}

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,12 @@
package com.example.nto.service;
import com.example.nto.entity.Booking;
import com.example.nto.entity.Employee;
import com.example.nto.entity.Place;
import java.time.LocalDate;
import java.util.List;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
@@ -7,4 +14,7 @@ package com.example.nto.service;
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public interface BookingService {
Booking getById(Long id);
List<Booking> getAll();
void save(LocalDate date, Place place, Employee employee);
}

View File

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

View File

@@ -0,0 +1,10 @@
package com.example.nto.service;
import com.example.nto.entity.Place;
import java.util.List;
public interface PlaceService {
Place getById(Long id);
List<Place> getAll();
}

View File

@@ -1,6 +1,18 @@
package com.example.nto.service.impl;
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.service.BookingService;
import io.micrometer.common.lang.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +20,29 @@ import com.example.nto.service.BookingService;
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Service
public class BookingServiceImpl implements BookingService {
@Autowired
private BookingRepository bookingRepository;
@Override
@Nullable
public Booking getById(Long id) {
return bookingRepository.findById(id).orElseGet(() -> null);
}
@Override
public List<Booking> getAll() {
return StreamSupport.stream(bookingRepository.findAll().spliterator(), false).collect(Collectors.toList());
}
@Override
public void save(LocalDate date, Place place, Employee employee) {
var booking = Booking.builder()
.date(date)
.place(place)
.employee(employee)
.build();
bookingRepository.save(booking);
}
}

View File

@@ -1,6 +1,16 @@
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 io.micrometer.common.lang.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +18,31 @@ import com.example.nto.service.EmployeeService;
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
private EmployeeRepository employeeRepository;
@Override
@Nullable
public Employee getById(Long id) {
return employeeRepository.findById(id).orElseGet(() -> null);
}
@Override
@Nullable
public Employee getByName(String name) {
return employeeRepository.findByName(name).orElseGet(() -> null);
}
@Override
@Nullable
public Employee getByCode(String code) {
return employeeRepository.findByCode(code).orElseGet(() -> null);
}
@Override
public List<Employee> getAll() {
return StreamSupport.stream(employeeRepository.findAll().spliterator(), false).collect(Collectors.toList());
}
}

View File

@@ -0,0 +1,29 @@
package com.example.nto.service.impl;
import com.example.nto.entity.Place;
import com.example.nto.repository.PlaceRepository;
import com.example.nto.service.PlaceService;
import io.micrometer.common.lang.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
@Service
public class PlaceServiceImpl implements PlaceService {
@Autowired
private PlaceRepository placeRepository;
@Override
@Nullable
public Place getById(Long id) {
return placeRepository.findById(id).orElseGet(() -> null);
}
@Override
public List<Place> getAll() {
return StreamSupport.stream(placeRepository.findAll().spliterator(), false).collect(Collectors.toList());
}
}