Checkpoint 3. Create tables
This commit is contained in:
parent
c4e4bcec19
commit
83bc1b8e85
8
pom.xml
8
pom.xml
@ -54,6 +54,14 @@
|
|||||||
<groupId>org.postgresql</groupId>
|
<groupId>org.postgresql</groupId>
|
||||||
<artifactId>postgresql</artifactId>
|
<artifactId>postgresql</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- <dependency>-->
|
||||||
|
<!-- <groupId>org.springframework.boot</groupId>-->
|
||||||
|
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
|
||||||
|
<!-- </dependency>-->
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -1,4 +1,40 @@
|
|||||||
package com.example.nto.config;
|
//package com.example.nto.config;
|
||||||
|
//
|
||||||
public class WebSecurityConfig {
|
//import lombok.RequiredArgsConstructor;
|
||||||
}
|
//import org.springframework.context.annotation.Configuration;
|
||||||
|
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
|
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||||
|
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
//import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
//import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
|
//
|
||||||
|
//@Configuration
|
||||||
|
//@EnableWebSecurity
|
||||||
|
//@RequiredArgsConstructor
|
||||||
|
//public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||||
|
//
|
||||||
|
// private final UserDetailsService userDetailsService;
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// protected void configure(HttpSecurity http) throws Exception {
|
||||||
|
// http
|
||||||
|
// .csrf().disable()
|
||||||
|
// .authorizeRequests()
|
||||||
|
// .antMatchers("/api/employee/registration").hasAuthority("ROLE_ADMIN")
|
||||||
|
// .anyRequest().authenticated()
|
||||||
|
// .and()
|
||||||
|
// .httpBasic()
|
||||||
|
// .and()
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
// auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public PasswordEncoder passwordEncoder(){
|
||||||
|
// return new BCryptPasswordEncoder();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
@ -12,13 +12,19 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api/employee")
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
private EmployeeServiceImpl service;
|
private EmployeeServiceImpl service;
|
||||||
|
|
||||||
|
@GetMapping("/{username}")
|
||||||
|
public ResponseEntity<String> findEmployeeByUsername(@PathVariable String username){
|
||||||
|
Employee volunteer = service.findEmployeeByUsername(username);
|
||||||
|
return ResponseEntity.status(HttpStatus.OK).body("Employee with username "+username+" exists");
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/{login}/auth")
|
@GetMapping("/{login}/auth")
|
||||||
private ResponseEntity<String> getAuth(@PathVariable("login") String login){
|
private ResponseEntity<String> getAuth(@PathVariable("login") String login){
|
||||||
@ -27,7 +33,7 @@ public class EmployeeController {
|
|||||||
return ResponseEntity.status(400).body("что-то пошло не так");
|
return ResponseEntity.status(400).body("что-то пошло не так");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = service.findEmployeeByLogin(login);
|
Employee employee = service.findEmployeeByUsername(login);
|
||||||
if(employee!=null){
|
if(employee!=null){
|
||||||
return ResponseEntity.status(200).body("данный логин существует - можно пользоваться приложением");
|
return ResponseEntity.status(200).body("данный логин существует - можно пользоваться приложением");
|
||||||
|
|
||||||
@ -46,7 +52,7 @@ public class EmployeeController {
|
|||||||
return ResponseEntity.status(400).body("что-то пошло не так");
|
return ResponseEntity.status(400).body("что-то пошло не так");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = service.findEmployeeByLogin(login);
|
Employee employee = service.findEmployeeByUsername(login);
|
||||||
if(employee!=null){
|
if(employee!=null){
|
||||||
return ResponseEntity.ok(employee);
|
return ResponseEntity.ok(employee);
|
||||||
|
|
||||||
@ -67,7 +73,7 @@ public class EmployeeController {
|
|||||||
return ResponseEntity.status(400).body("что-то пошло не так");
|
return ResponseEntity.status(400).body("что-то пошло не так");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = service.findEmployeeByLogin(login);
|
Employee employee = service.findEmployeeByUsername(login);
|
||||||
if(employee!=null){
|
if(employee!=null){
|
||||||
employee.setLastVisit(LocalDateTime.now().toString());
|
employee.setLastVisit(LocalDateTime.now().toString());
|
||||||
service.updateLocalTime(employee);
|
service.updateLocalTime(employee);
|
||||||
@ -79,6 +85,8 @@ public class EmployeeController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@PostMapping("/registration")
|
@PostMapping("/registration")
|
||||||
public ResponseEntity<EmployeeDTO> registerEmployee(@RequestBody RegisterDTO dto) {
|
public ResponseEntity<EmployeeDTO> registerEmployee(@RequestBody RegisterDTO dto) {
|
||||||
return ResponseEntity.status(HttpStatus.OK).body(service.createEmployee(dto));
|
return ResponseEntity.status(HttpStatus.OK).body(service.createEmployee(dto));
|
||||||
|
@ -13,7 +13,7 @@ public class EmployeeDTO {
|
|||||||
|
|
||||||
private String photo;
|
private String photo;
|
||||||
|
|
||||||
private String position;
|
private String jobPos;
|
||||||
|
|
||||||
private String lastVisit;
|
private String lastVisit;
|
||||||
|
|
||||||
|
@ -13,6 +13,6 @@ public class RegisterDTO {
|
|||||||
|
|
||||||
private String photo;
|
private String photo;
|
||||||
|
|
||||||
private String position;
|
private String jobPos;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,20 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
public class Authority {
|
import lombok.Data;
|
||||||
|
//import org.springframework.security.core.GrantedAuthority;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Entity
|
||||||
|
@Table(name = "authorities")
|
||||||
|
public class Authority {//implements GrantedAuthority {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "authority")
|
||||||
|
private String authority;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,9 +4,10 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
//import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import java.time.LocalDateTime;
|
import java.util.Set;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@Builder
|
@Builder
|
||||||
@ -14,13 +15,13 @@ import java.time.LocalDateTime;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="employee")
|
@Table(name="employee")
|
||||||
public class Employee {
|
public class Employee {//implements UserDetails {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "login")
|
@Column(name = "username")
|
||||||
private String login;
|
private String username;
|
||||||
|
|
||||||
@Column(name = "password")
|
@Column(name = "password")
|
||||||
private String password;
|
private String password;
|
||||||
@ -31,9 +32,32 @@ public class Employee {
|
|||||||
@Column(name = "photo")
|
@Column(name = "photo")
|
||||||
private String photo;
|
private String photo;
|
||||||
|
|
||||||
@Column(name="position")
|
@Column(name="job_pos")
|
||||||
private String position;
|
private String jobPos;
|
||||||
|
|
||||||
@Column(name="lastVisit")
|
@Column(name="lastVisit")
|
||||||
private String lastVisit;
|
private String lastVisit;
|
||||||
|
|
||||||
|
// @ManyToMany(fetch = FetchType.EAGER)
|
||||||
|
// private Set<Authority> authorities;
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public boolean isAccountNonExpired() {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean isAccountNonLocked() {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean isCredentialsNonExpired() {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public boolean isEnabled() {
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
public interface AuthorityRepository {
|
import com.example.nto.entity.Employee;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface AuthorityRepository extends JpaRepository<Employee, Long> {
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
public Optional<Employee> findEmployeeByLogin(String login);
|
public Optional<Employee> findEmployeeByUsername(String login);
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,8 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
public void updateLocalTime(Employee employee);
|
public void updateLocalTime(Employee employee);
|
||||||
public Employee findEmployeeByLogin(String login);
|
|
||||||
|
public Employee findEmployeeByUsername(String login);
|
||||||
|
|
||||||
EmployeeDTO createEmployee(RegisterDTO dto);
|
EmployeeDTO createEmployee(RegisterDTO dto);
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import com.example.nto.dto.EmployeeDTO;
|
|||||||
import com.example.nto.dto.RegisterDTO;
|
import com.example.nto.dto.RegisterDTO;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.exceptions.EmployeeAlreadyExistException;
|
import com.example.nto.exceptions.EmployeeAlreadyExistException;
|
||||||
|
import com.example.nto.exceptions.EmployeeNotFoundException;
|
||||||
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.util.EmployeeMapper;
|
import com.example.nto.util.EmployeeMapper;
|
||||||
@ -24,25 +25,25 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Employee findEmployeeByLogin(String login) {
|
public Employee findEmployeeByUsername(String login) {
|
||||||
|
|
||||||
return repository.findEmployeeByLogin(login).get();
|
return repository.findEmployeeByUsername(login).orElseThrow(() -> new EmployeeNotFoundException("Employee with username "+login+" not found"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EmployeeDTO createEmployee(RegisterDTO dto) {
|
public EmployeeDTO createEmployee(RegisterDTO dto) {
|
||||||
|
|
||||||
Optional<Employee> optionalEmployee = repository.findEmployeeByLogin(dto.getUsername());
|
Optional<Employee> optionalEmployee = repository.findEmployeeByUsername(dto.getUsername());
|
||||||
if(optionalEmployee.isPresent()){
|
if(optionalEmployee.isPresent()){
|
||||||
throw new EmployeeAlreadyExistException("Employee with username " + dto.getUsername() +" already exist");
|
throw new EmployeeAlreadyExistException("Employee with username " + dto.getUsername() +" already exist");
|
||||||
}
|
}
|
||||||
|
|
||||||
Employee employee = new Employee();
|
Employee employee = new Employee();
|
||||||
employee.setName(dto.getName());
|
employee.setName(dto.getName());
|
||||||
employee.setLogin(dto.getUsername());
|
employee.setUsername(dto.getUsername());
|
||||||
employee.setPassword(dto.getPassword());
|
employee.setPassword(dto.getPassword());
|
||||||
employee.setPhoto(dto.getPhoto());
|
employee.setPhoto(dto.getPhoto());
|
||||||
employee.setPosition(dto.getPosition());
|
employee.setJobPos(dto.getJobPos());
|
||||||
employee.setLastVisit(LocalDateTime.now().toString());
|
employee.setLastVisit(LocalDateTime.now().toString());
|
||||||
|
|
||||||
return EmployeeMapper.convertToDTO(repository.save(employee));
|
return EmployeeMapper.convertToDTO(repository.save(employee));
|
||||||
|
@ -1,4 +1,20 @@
|
|||||||
package com.example.nto.service.impl;
|
//package com.example.nto.service.impl;
|
||||||
|
//
|
||||||
public class UserDetailsServiceImpl {
|
//import com.example.nto.repository.EmployeeRepository;
|
||||||
}
|
//import lombok.RequiredArgsConstructor;
|
||||||
|
//import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
//import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
|
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
||||||
|
//import org.springframework.stereotype.Service;
|
||||||
|
//
|
||||||
|
//@Service
|
||||||
|
//@RequiredArgsConstructor
|
||||||
|
//public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
//
|
||||||
|
// private final EmployeeRepository employeeRepository;
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException{
|
||||||
|
// return employeeRepository.findEmployeeByUsername(s).get();
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
@ -4,19 +4,16 @@ import com.example.nto.dto.EmployeeDTO;
|
|||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
|
||||||
import java.time.format.DateTimeFormatter;
|
|
||||||
import java.time.format.FormatStyle;
|
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class EmployeeMapper {
|
public class EmployeeMapper {
|
||||||
|
|
||||||
public EmployeeDTO convertToDTO(Employee employee){
|
public EmployeeDTO convertToDTO(Employee employee){
|
||||||
EmployeeDTO dto = new EmployeeDTO();
|
EmployeeDTO dto = new EmployeeDTO();
|
||||||
dto.setId(employee.getId());
|
dto.setId(employee.getId());
|
||||||
dto.setUsername(employee.getLogin());
|
dto.setUsername(employee.getUsername());
|
||||||
dto.setName(employee.getName());
|
dto.setName(employee.getName());
|
||||||
dto.setPhoto(employee.getPhoto());
|
dto.setPhoto(employee.getPhoto());
|
||||||
dto.setPosition(employee.getPosition());
|
dto.setJobPos(employee.getJobPos());
|
||||||
dto.setLastVisit(employee.getLastVisit());
|
dto.setLastVisit(employee.getLastVisit());
|
||||||
|
|
||||||
return dto;
|
return dto;
|
||||||
|
@ -1,11 +1,21 @@
|
|||||||
|
server:
|
||||||
|
port: 8081
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
|
sql:
|
||||||
|
init:
|
||||||
|
mode: always
|
||||||
|
|
||||||
datasource:
|
datasource:
|
||||||
url: jdbc:postgresql://localhost:5432/testdb
|
url: jdbc:postgresql://localhost:5432/testdb
|
||||||
|
data: classpath:data.sql
|
||||||
|
schema: classpath:schema.sql
|
||||||
|
|
||||||
username: "postgres"
|
username: "postgres"
|
||||||
password: "MobileDev"
|
password: "MobileDev"
|
||||||
|
|
||||||
|
initialization-mode: always
|
||||||
|
|
||||||
driver-class-name:
|
driver-class-name:
|
||||||
org.postgresql.Driver
|
org.postgresql.Driver
|
||||||
|
|
||||||
@ -18,7 +28,7 @@ spring:
|
|||||||
|
|
||||||
hibernate:
|
hibernate:
|
||||||
#ddl-auto: none
|
#ddl-auto: none
|
||||||
ddl-auto: create
|
ddl-auto: create-drop
|
||||||
|
|
||||||
# Показываем запросы
|
# Показываем запросы
|
||||||
show-sql: true
|
show-sql: true
|
||||||
|
@ -1,4 +1,9 @@
|
|||||||
INSERT INTO employee (id, login, password, name, photo, position, last_visit)
|
INSERT INTO authorities (id, authority)
|
||||||
|
VALUES
|
||||||
|
(1, 'ROLE_EMPLOYEE'),
|
||||||
|
(2, 'ROLE_ADMIN');
|
||||||
|
|
||||||
|
INSERT INTO employee (id, username, password, name, photo, job_pos, last_visit)
|
||||||
VALUES
|
VALUES
|
||||||
(1, 'pivanov', 'employee', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
(1, 'pivanov', 'employee', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
||||||
(2, 'ipetrov', 'employee', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
(2, 'ipetrov', 'employee', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS authorities(
|
||||||
|
id auto_increment primary key, authority VARCHAR(255));
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS employee(
|
||||||
|
id auto_increment primary key, username VARCHAR(255), password VARCHAR(255), name VARCHAR(255), photo VARCHAR(255), job_pos VARCHAR(255), last_visit VARCHAR(255));
|
Loading…
x
Reference in New Issue
Block a user