add api/<CODE>/auth and api/<CODE>/info

This commit is contained in:
2025-11-28 00:32:33 +03:00
parent a6954c2013
commit af8267a22a
16 changed files with 246 additions and 11 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,33 @@
package com.example.nto.controller;
import com.example.nto.dto.EmployeeInfoWithBookingDto;
import com.example.nto.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class EmployeeController {
private final EmployeeService employeeService;
@GetMapping("/{code}/auth")
void auth(@PathVariable("code") String code) {
employeeService.auth(code);
}
@GetMapping("/{code}/info")
EmployeeInfoWithBookingDto info(@PathVariable("code") String code) {
return employeeService.info(code);
}
}

View File

@@ -0,0 +1,14 @@
package com.example.nto.dto;
import com.example.nto.entity.Place;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDate;
@Data
@Builder
public class BookingRecordDto {
private long id;
private String place;
}

View File

@@ -0,0 +1,18 @@
package com.example.nto.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class BookingWithDateDto {
private LocalDate date;
private long id;
private String place;
}

View File

@@ -0,0 +1,15 @@
package com.example.nto.dto;
import lombok.Builder;
import lombok.Data;
import java.time.LocalDate;
import java.util.Map;
@Data
@Builder
public class EmployeeInfoWithBookingDto {
private String name;
private String photoUrl;
Map<LocalDate, BookingRecordDto> booking;
}

View File

@@ -0,0 +1,11 @@
package com.example.nto.dto;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class EmployeeNameAndPhotoDto {
private String name;
private String photoUrl;
}

View File

@@ -0,0 +1,24 @@
package com.example.nto.dto.mapper;
import com.example.nto.dto.BookingRecordDto;
import com.example.nto.dto.BookingWithDateDto;
import lombok.experimental.UtilityClass;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
@UtilityClass
public class ListBookingWithDateDtoMapper {
public static Map<LocalDate, BookingRecordDto> toMapLocalDateBookingRecordDto(List<BookingWithDateDto> d) {
Map<LocalDate, BookingRecordDto> res = new TreeMap<>();
d.forEach(bookingWithDate -> {
res.put(bookingWithDate.getDate(), BookingRecordDto.builder()
.id(bookingWithDate.getId())
.place(bookingWithDate.getPlace())
.build());
});
return res;
}
}

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;
@@ -21,15 +19,25 @@ import java.time.LocalDate;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "booking")
public class Booking {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "date")
private LocalDate date;
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
@JoinColumn(name = "place_id")
private Place place;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(
name = "employee_id",
referencedColumnName = "id"
)
private Employee employee;
}

View File

@@ -19,14 +19,21 @@ import java.util.List;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "employee")
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "name")
private String name;
@Column(name = "code")
private String code;
@Column(name = "photo_url")
private String photoUrl;
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)

View File

@@ -1,8 +1,6 @@
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;
@@ -19,11 +17,14 @@ import lombok.NoArgsConstructor;
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "place")
public class Place {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private long id;
@Column(name = "place_name")
private String place;
}

View File

@@ -0,0 +1,7 @@
package com.example.nto.exception;
public class EmployeeNotFoundByCodeException extends RuntimeException {
public EmployeeNotFoundByCodeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,38 @@
package com.example.nto.exception;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(EmployeeNotFoundByCodeException.class)
ResponseEntity<String> codeNotFoundExceptionHandler(EmployeeNotFoundByCodeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
}
/*@ExceptionHandler(CodeNotFoundException.class)
ResponseEntity<String> codeNotFoundExceptionHandler(CodeNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(EmployeeNotFoundException.class)
ResponseEntity<String> employeeNotFoundExceptionHandler(EmployeeNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(EmployeeDataNotFoundException.class)
ResponseEntity<String> employeeDataNotFoundExceptionHandler(EmployeeDataNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(EmployeeIsBlockedException.class)
ResponseEntity<String> employeeIsBlockedExceptionHandler(EmployeeIsBlockedException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.LOCKED);
}
@ExceptionHandler(SelfChangeException.class)
ResponseEntity<String> selfChangeExceptionHandler(SelfChangeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_ACCEPTABLE);
}*/
}

View File

@@ -1,10 +1,22 @@
package com.example.nto.repository;
import com.example.nto.dto.BookingWithDateDto;
import com.example.nto.entity.Booking;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public interface BookingRepository {
@Repository
public interface BookingRepository extends JpaRepository<Booking, Long> {
@Query("select new com.example.nto.dto.BookingWithDateDto(b.date, b.id, b.place.place) from Booking b where b.employee.code = :code order by b.date")
List<BookingWithDateDto> getBookingsByCode(@Param("code") String code);
}

View File

@@ -1,10 +1,23 @@
package com.example.nto.repository;
import com.example.nto.dto.EmployeeNameAndPhotoDto;
import com.example.nto.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public interface EmployeeRepository {
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
@Query("select count(e) > 0 from Employee e where e.code = :code")
boolean isExist(@Param("code") String code);
@Query("select e.name, e.photoUrl from Employee e where e.code = :code")
EmployeeNameAndPhotoDto getNameAndPhoto(@Param("code") String code);
}

View File

@@ -1,5 +1,7 @@
package com.example.nto.service;
import com.example.nto.dto.EmployeeInfoWithBookingDto;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
* =================================
@@ -7,4 +9,6 @@ package com.example.nto.service;
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
public interface EmployeeService {
void auth(String code);
EmployeeInfoWithBookingDto info(String code);
}

View File

@@ -1,6 +1,15 @@
package com.example.nto.service.impl;
import com.example.nto.dto.EmployeeInfoWithBookingDto;
import com.example.nto.dto.EmployeeNameAndPhotoDto;
import com.example.nto.exception.EmployeeNotFoundByCodeException;
import com.example.nto.repository.BookingRepository;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.EmployeeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import static com.example.nto.dto.mapper.ListBookingWithDateDtoMapper.toMapLocalDateBookingRecordDto;
/**
* TODO: ДОРАБОТАТЬ в рамках задания
@@ -8,5 +17,31 @@ import com.example.nto.service.EmployeeService;
* МОЖНО: Добавлять методы, аннотации, зависимости
* НЕЛЬЗЯ: Изменять название класса и пакета
*/
@Service
@RequiredArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
private final EmployeeRepository employeeRepository;
private final BookingRepository bookingRepository;
@Override
public void auth(String code) {
if (!employeeRepository.isExist(code)) {
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
}
}
@Override
public EmployeeInfoWithBookingDto info(String code) {
if (!employeeRepository.isExist(code)) {
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
}
EmployeeNameAndPhotoDto nameAndPhoto = employeeRepository.getNameAndPhoto(code);
return EmployeeInfoWithBookingDto.builder()
.name(nameAndPhoto.getName())
.photoUrl(nameAndPhoto.getPhotoUrl())
.booking(toMapLocalDateBookingRecordDto(bookingRepository.getBookingsByCode(code)))
.build();
}
}