Задания Backend нто
Some checks failed
Android Test / validate-and-test (pull_request) Has been cancelled

This commit is contained in:
2025-11-29 15:31:17 +03:00
parent a6954c2013
commit d5564b63eb
15 changed files with 385 additions and 119 deletions

View File

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

View File

@@ -1,10 +1,133 @@
package com.example.nto.controller; package com.example.nto.controller;
/** import com.example.nto.entity.Booking;
* TODO: ДОРАБОТАТЬ в рамках задания import com.example.nto.entity.Employee;
* ================================= 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.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;
@RestController
@RequestMapping("/api/{code}")
public class BookingController { public class BookingController {
private final EmployeeService employeeService;
private final BookingService bookingService;
private final PlaceService placeService;
public BookingController(EmployeeService employeeService, BookingService bookingService, PlaceService placeService) {
this.employeeService = employeeService;
this.bookingService = bookingService;
this.placeService = placeService;
}
@GetMapping("/auth")
public ResponseEntity<Void> auth(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
if (employeeOpt.isPresent()) {
return ResponseEntity.ok().build();
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
}
}
@GetMapping("/info")
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
Employee employee = employeeOpt.get();
Map<String, Object> response = new HashMap<>();
response.put("name", employee.getName());
response.put("photoUrl", employee.getPhotoUrl());
Map<String, List<Map<String, Object>>> bookingsMap = new HashMap<>();
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
for (Booking b : bookingList) {
String dateKey = b.getDate().toString();
Map<String, Object> bookingData = new HashMap<>();
bookingData.put("id", b.getId());
bookingData.put("place", b.getPlace().getPlace());
bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
}
response.put("booking", bookingsMap);
return ResponseEntity.ok(response);
}
@GetMapping("/booking")
public ResponseEntity<Map<String, List<Map<String, Object>>>> availableBooking(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();
LocalDate today = LocalDate.now();
List<Place> allPlaces = placeService.getAllPlaces();
for (int i = 0; i < 4; i++) {
LocalDate date = today.plusDays(i);
List<Booking> bookings = bookingService.getBookingsByDate(date);
Set<Long> bookedPlaceIds = bookings.stream()
.map(b -> b.getPlace().getId())
.collect(Collectors.toSet());
List<Map<String, Object>> available = allPlaces.stream()
.filter(p -> !bookedPlaceIds.contains(p.getId()))
.map(p -> {
Map<String, Object> m = new HashMap<>();
m.put("id", p.getId());
m.put("place", p.getPlace());
return m;
})
.collect(Collectors.toList());
result.put(date.toString(), available);
}
return ResponseEntity.ok(result);
}
@PostMapping("/book")
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
Employee employee = employeeOpt.get();
try {
String dateStr = body.get("date");
String placeIdStr = body.get("placeID");
if (dateStr == null || placeIdStr == null) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
LocalDate date = LocalDate.parse(dateStr);
Long placeId = Long.parseLong(placeIdStr);
Place place = placeService.getPlaceById(placeId);
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
bookingService.createBooking(employee, place, date);
return ResponseEntity.status(HttpStatus.CREATED).build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}
} }

View File

@@ -1,10 +1,27 @@
package com.example.nto.controller; package com.example.nto.controller;
/** import com.example.nto.entity.Employee;
* TODO: ДОРАБОТАТЬ в рамках задания import com.example.nto.service.EmployeeService;
* ================================= import org.springframework.http.HttpStatus;
* МОЖНО: Добавлять методы, аннотации, зависимости import org.springframework.http.ResponseEntity;
* НЕЛЬЗЯ: Изменять название класса и пакета import org.springframework.web.bind.annotation.*;
*/
import java.util.Optional;
@RestController
@RequestMapping("/api/employee")
public class EmployeeController { public class EmployeeController {
private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService;
}
@GetMapping("/{code}")
public ResponseEntity<Employee> getEmployeeByCode(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code);
return employeeOpt.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}
} }

View File

@@ -1,35 +1,43 @@
package com.example.nto.entity; package com.example.nto.entity;
import jakarta.persistence.FetchType; import jakarta.persistence.*;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate; import java.time.LocalDate;
@Entity
/** @Table(name = "booking")
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Booking { public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private long id;
private LocalDate date; private LocalDate date;
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "place_id") @JoinColumn(name = "place_id")
private Place place; private Place place;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "employee_id")
private Employee employee; private Employee employee;
public Booking() {}
public Booking(Employee employee, Place place, LocalDate date) {
this.employee = employee;
this.place = place;
this.date = date;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public LocalDate getDate() { return date; }
public void setDate(LocalDate date) { this.date = date; }
public Place getPlace() { return place; }
public void setPlace(Place place) { this.place = place; }
public Employee getEmployee() { return employee; }
public void setEmployee(Employee employee) { this.employee = employee; }
} }

View File

@@ -1,34 +1,47 @@
package com.example.nto.entity; package com.example.nto.entity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import java.util.ArrayList;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List; import java.util.List;
@Entity
/** @Table(name = "employee")
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Employee { public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private long id;
private String name; private String name;
@Column(unique = true)
private String code; private String code;
private String photoUrl; private String photoUrl;
@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 = new ArrayList<>();
public Employee() {}
public Employee(String name, String code, String photoUrl) {
this.name = name;
this.code = code;
this.photoUrl = photoUrl;
}
public long getId() { return id; }
public void setId(long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getPhotoUrl() { return photoUrl; }
public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }
public List<Booking> getBookingList() { return bookingList; }
public void setBookingList(List<Booking> bookingList) { this.bookingList = bookingList; }
} }

View File

@@ -1,29 +1,46 @@
package com.example.nto.entity; package com.example.nto.entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.*;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
/** @Table(name = "place")
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Place { public class Place {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private long id; private Long id;
@Column(name = "place_name", nullable = false, unique = true)
private String place; private String place;
public Place() {
}
public Place(String place) {
this.place = place;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
@Override
public String toString() {
return "Place{" +
"id=" + id +
", place='" + place + '\'' +
'}';
}
} }

View File

@@ -1,10 +1,16 @@
package com.example.nto.repository; package com.example.nto.repository;
/** import com.example.nto.entity.Booking;
* TODO: ДОРАБОТАТЬ в рамках задания import com.example.nto.entity.Employee;
* ================================= import com.example.nto.entity.Place;
* МОЖНО: Добавлять методы, аннотации, зависимости import org.springframework.data.jpa.repository.JpaRepository;
* НЕЛЬЗЯ: Изменять название класса и пакета
*/ import java.time.LocalDate;
public interface BookingRepository { import java.util.List;
import java.util.Optional;
public interface BookingRepository extends JpaRepository<Booking, Long> {
List<Booking> findByEmployee(Employee employee);
List<Booking> findByDate(LocalDate date);
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
} }

View File

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

View File

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

View File

@@ -1,10 +1,16 @@
package com.example.nto.service; package com.example.nto.service;
/** import com.example.nto.entity.Booking;
* TODO: ДОРАБОТАТЬ в рамках задания import com.example.nto.entity.Employee;
* ================================= import com.example.nto.entity.Place;
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета import java.time.LocalDate;
*/ import java.util.List;
import java.util.Optional;
public interface BookingService { public interface BookingService {
List<Booking> getBookingsByEmployee(Employee employee);
List<Booking> getBookingsByDate(LocalDate date);
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
Booking createBooking(Employee employee, Place place, LocalDate date);
} }

View File

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

View File

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

View File

@@ -1,12 +1,46 @@
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.entity.Place;
import com.example.nto.repository.BookingRepository;
import com.example.nto.service.BookingService; import com.example.nto.service.BookingService;
import org.springframework.stereotype.Service;
/** import java.time.LocalDate;
* TODO: ДОРАБОТАТЬ в рамках задания import java.util.List;
* ================================= import java.util.Optional;
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета @Service
*/
public class BookingServiceImpl implements BookingService { public class BookingServiceImpl implements BookingService {
private final BookingRepository bookingRepository;
public BookingServiceImpl(BookingRepository bookingRepository) {
this.bookingRepository = bookingRepository;
}
@Override
public List<Booking> getBookingsByEmployee(Employee employee) {
return bookingRepository.findByEmployee(employee);
}
@Override
public List<Booking> getBookingsByDate(LocalDate date) {
return bookingRepository.findByDate(date);
}
@Override
public Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place) {
return bookingRepository.findByDateAndPlace(date, place);
}
@Override
public Booking createBooking(Employee employee, Place place, LocalDate date) {
Booking booking = new Booking();
booking.setEmployee(employee);
booking.setPlace(place);
booking.setDate(date);
return bookingRepository.save(booking);
}
} }

View File

@@ -1,12 +1,22 @@
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.Optional;
/** @Service
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public class EmployeeServiceImpl implements EmployeeService { public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
this.employeeRepository = employeeRepository;
}
@Override
public Optional<Employee> findByCode(String code) {
return employeeRepository.findByCode(code);
}
} }

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 org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PlaceServiceImpl implements PlaceService {
private final PlaceRepository placeRepository;
public PlaceServiceImpl(PlaceRepository placeRepository) {
this.placeRepository = placeRepository;
}
@Override
public List<Place> getAllPlaces() {
return placeRepository.findAll();
}
@Override
public Place getPlaceById(Long id) {
return placeRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Place not found"));
}
}