base commit
This commit is contained in:
parent
35f89f9a7f
commit
7b478e035b
@ -20,16 +20,16 @@ public class BookingController {
|
|||||||
|
|
||||||
private final BookingService bookingService;
|
private final BookingService bookingService;
|
||||||
|
|
||||||
@GetMapping("/{code}/booking")
|
@GetMapping("/{code}/{password}/booking")
|
||||||
@ResponseStatus(code = HttpStatus.OK)
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
public Map<LocalDate, List<PlaceDto>> getByDate(@PathVariable String code) {
|
public Map<LocalDate, List<PlaceDto>> getByDate(@PathVariable String code, @PathVariable String password) {
|
||||||
return bookingService.getFreePlace(code);
|
return bookingService.getFreePlace(code, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{code}/book")
|
@PostMapping("/{code}/{password}/book")
|
||||||
@ResponseStatus(code = HttpStatus.CREATED)
|
@ResponseStatus(code = HttpStatus.CREATED)
|
||||||
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto) {
|
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto, @PathVariable String password) {
|
||||||
bookingService.create(code, bookingCreateDto);
|
bookingService.create(code, password, bookingCreateDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,14 +16,14 @@ public class EmployeeController {
|
|||||||
|
|
||||||
@GetMapping("/{code}/info")
|
@GetMapping("/{code}/info")
|
||||||
@ResponseStatus(code = HttpStatus.OK)
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
public EmployeeDto getByCode(@PathVariable String code) {
|
public EmployeeDto getByCode(@PathVariable String login) {
|
||||||
return employeeService.getByCode(code);
|
return employeeService.getByCode(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/login/{username}/{password}")
|
@GetMapping("/login/{username}/{password}")
|
||||||
@ResponseStatus(code = HttpStatus.OK)
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
public void login(@PathVariable String username, @PathVariable String password){
|
public EmployeeDto login(@PathVariable String username, @PathVariable String password){
|
||||||
employeeService.auth(username, password);
|
return employeeService.auth(username, password);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.example.nto.controller.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class EmployeeCreateDto {
|
||||||
|
@NotNull
|
||||||
|
private String username;
|
||||||
|
@NotNull
|
||||||
|
private String password;
|
||||||
|
}
|
||||||
@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
|
|||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@ -19,7 +20,6 @@ public class EmployeeDto {
|
|||||||
private String name;
|
private String name;
|
||||||
private String photoUrl;
|
private String photoUrl;
|
||||||
private Map<LocalDate, PlaceDto> booking;
|
private Map<LocalDate, PlaceDto> booking;
|
||||||
private String password;
|
|
||||||
|
|
||||||
public static EmployeeDto toDto(Employee employee) {
|
public static EmployeeDto toDto(Employee employee) {
|
||||||
Map<LocalDate, PlaceDto> dtoTreeMap = new TreeMap<>();
|
Map<LocalDate, PlaceDto> dtoTreeMap = new TreeMap<>();
|
||||||
@ -27,6 +27,15 @@ public class EmployeeDto {
|
|||||||
dtoTreeMap.put(booking.getDate(), PlaceDto.toDto(booking.getPlace()));
|
dtoTreeMap.put(booking.getDate(), PlaceDto.toDto(booking.getPlace()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return new EmployeeDto(employee.getName(), employee.getPhotoUrl(), dtoTreeMap, employee.getPassword());
|
return new EmployeeDto(employee.getName(), employee.getPhotoUrl(), dtoTreeMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static EmployeeDto toDtoOpt(Optional<Employee> employee) {
|
||||||
|
Map<LocalDate, PlaceDto> dtoTreeMap = new TreeMap<>();
|
||||||
|
for (Booking booking : employee.get().getBookingList()) {
|
||||||
|
dtoTreeMap.put(booking.getDate(), PlaceDto.toDto(booking.getPlace()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new EmployeeDto(employee.get().getName(), employee.get().getPhotoUrl(), dtoTreeMap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import lombok.NoArgsConstructor;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@ -38,4 +39,7 @@ public class Employee {
|
|||||||
|
|
||||||
@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;
|
||||||
|
|
||||||
|
@ManyToMany
|
||||||
|
private Set<Role> roles;
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/main/java/com/example/nto/entity/Role.java
Normal file
21
src/main/java/com/example/nto/entity/Role.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Data
|
||||||
|
public class Role {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "role")
|
||||||
|
private String role;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class InvalidPassword extends RuntimeException {
|
||||||
|
public InvalidPassword(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -9,7 +9,7 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface BookingService {
|
public interface BookingService {
|
||||||
Map<LocalDate, List<PlaceDto>> getFreePlace(String code);
|
Map<LocalDate, List<PlaceDto>> getFreePlace(String code, String password);
|
||||||
|
|
||||||
Booking create(String code, BookingCreateDto bookingCreateDto);
|
Booking create(String code, String password, BookingCreateDto bookingCreateDto);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,13 +1,11 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeCreateDto;
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import com.example.nto.exception.InvalidPassword;
|
import com.example.nto.exception.InvalidPassword;
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
EmployeeDto getByCode(String code);
|
EmployeeDto getByCode(String code);
|
||||||
|
|
||||||
void auth(String username, String password) throws InvalidPassword;
|
EmployeeDto auth(String username, String password) throws InvalidPassword;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,8 +37,8 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public Map<LocalDate, List<PlaceDto>> getFreePlace(String code) {
|
public Map<LocalDate, List<PlaceDto>> getFreePlace(String code, String password) {
|
||||||
// employeeService.auth(code);
|
employeeService.auth(code, password);
|
||||||
|
|
||||||
List<Place> allPlaces = placeRepository.findAll();
|
List<Place> allPlaces = placeRepository.findAll();
|
||||||
|
|
||||||
@ -72,14 +72,15 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public Booking create(String code, BookingCreateDto bookingCreateDto) {
|
public Booking create(String code, String password, BookingCreateDto bookingCreateDto) {
|
||||||
|
employeeService.auth(code, password);
|
||||||
LocalDate date = bookingCreateDto.getDate();
|
LocalDate date = bookingCreateDto.getDate();
|
||||||
LocalDate today = LocalDate.now(ZoneId.systemDefault());
|
LocalDate today = LocalDate.now(ZoneId.systemDefault());
|
||||||
if (date.isBefore(today) || date.isAfter(today.plusDays(daysAhead))) {
|
if (date.isBefore(today) || date.isAfter(today.plusDays(daysAhead))) {
|
||||||
throw new IllegalArgumentException("Date is out of booking window");
|
throw new IllegalArgumentException("Date is out of booking window");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = employeeRepository.findByCode(code)
|
Employee employee = employeeRepository.findByUsername(code)
|
||||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
||||||
|
|
||||||
long placeId = bookingCreateDto.getPlaceId();
|
long placeId = bookingCreateDto.getPlaceId();
|
||||||
|
|||||||
@ -1,18 +1,14 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.controller.dto.EmployeeCreateDto;
|
|
||||||
import com.example.nto.controller.dto.EmployeeDto;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.exception.EmployeeAlreadyExists;
|
|
||||||
import com.example.nto.exception.EmployeeNotFoundException;
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
import com.example.nto.exception.InvalidPassword;
|
import com.example.nto.exception.InvalidPassword;
|
||||||
import com.example.nto.exception.InvalidUsername;
|
import com.example.nto.exception.InvalidUsername;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import com.example.nto.utils.PasswordValidator;
|
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.java.Log;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@ -38,15 +34,16 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public void auth(String username, String password) throws InvalidPassword {
|
public EmployeeDto auth(String username, String password) throws InvalidPassword {
|
||||||
Optional<Employee> employee = employeeRepository.findByUsername(username);
|
Optional<Employee> employee = employeeRepository.findByUsername(username);
|
||||||
if(employee.isEmpty()){
|
if(employee.isEmpty()){
|
||||||
throw new InvalidUsername("Invalid Username");
|
throw new InvalidUsername("Invalid Username");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if(!EmployeeDto.toDto(employee.get()).getPassword().equals(password)){
|
if(!employee.get().getPassword().equals(password)){
|
||||||
throw new InvalidPassword("Wrong password: " + password + " " + EmployeeDto.toDto(employee.get()).getPassword());
|
throw new InvalidPassword("Wrong password: " + password);
|
||||||
}
|
}
|
||||||
|
return EmployeeDto.toDtoOpt(employee);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/main/java/com/example/nto/utils/PasswordValidator.java
Normal file
16
src/main/java/com/example/nto/utils/PasswordValidator.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.example.nto.utils;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class PasswordValidator {
|
||||||
|
public static boolean validate(String password){
|
||||||
|
Pattern p = Pattern.compile("*[%?()*^$#@!/.,><|';:]*");
|
||||||
|
if(password.length() < 8){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(!p.matcher(password).matches()){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user