ini
This commit is contained in:
parent
6f791755e0
commit
51ecb6c593
106
.idea/workspace.xml
generated
Normal file
106
.idea/workspace.xml
generated
Normal file
@ -0,0 +1,106 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="AutoImportSettings">
|
||||
<option name="autoReloadType" value="SELECTIVE" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="bugfix and write last endpoint">
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/App.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/App.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/CodeRepository.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/CodeRepository.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/EmployeeRepository.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/EmployeeRepository.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||
</component>
|
||||
<component name="Git.Settings">
|
||||
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||
</component>
|
||||
<component name="ProjectColorInfo">{
|
||||
"associatedIndex": 6
|
||||
}</component>
|
||||
<component name="ProjectId" id="2pzTQTjFGjDiu9p581cgyZYbH35" />
|
||||
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
|
||||
<component name="ProjectViewState">
|
||||
<option name="hideEmptyMiddlePackages" value="true" />
|
||||
<option name="showLibraryContents" value="true" />
|
||||
</component>
|
||||
<component name="PropertiesComponent"><![CDATA[{
|
||||
"keyToString": {
|
||||
"Application.App.executor": "Run",
|
||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||
"RunOnceActivity.git.unshallow": "true",
|
||||
"git-widget-placeholder": "main",
|
||||
"kotlin-language-version-configured": "true",
|
||||
"settings.editor.selected.configurable": "MavenSettings"
|
||||
}
|
||||
}]]></component>
|
||||
<component name="RunManager">
|
||||
<configuration name="App" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
|
||||
<option name="MAIN_CLASS_NAME" value="com.example.nto.App" />
|
||||
<module name="NTO-2024" />
|
||||
<extension name="coverage">
|
||||
<pattern>
|
||||
<option name="PATTERN" value="com.example.nto.*" />
|
||||
<option name="ENABLED" value="true" />
|
||||
</pattern>
|
||||
</extension>
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration default="true" type="JetRunConfigurationType">
|
||||
<module name="NTO-2024-Backend" />
|
||||
<method v="2">
|
||||
<option name="Make" enabled="true" />
|
||||
</method>
|
||||
</configuration>
|
||||
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
|
||||
<module name="NTO-2024-Backend" />
|
||||
<option name="filePath" />
|
||||
<method v="2" />
|
||||
</configuration>
|
||||
<recent_temporary>
|
||||
<list>
|
||||
<item itemvalue="Application.App" />
|
||||
</list>
|
||||
</recent_temporary>
|
||||
</component>
|
||||
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||
<component name="TaskManager">
|
||||
<task active="true" id="Default" summary="Default task">
|
||||
<changelist id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="" />
|
||||
<created>1733767887785</created>
|
||||
<option name="number" value="Default" />
|
||||
<option name="presentableId" value="Default" />
|
||||
<updated>1733767887785</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00001" summary="core :pig:">
|
||||
<option name="closed" value="true" />
|
||||
<created>1733776041055</created>
|
||||
<option name="number" value="00001" />
|
||||
<option name="presentableId" value="LOCAL-00001" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1733776041055</updated>
|
||||
</task>
|
||||
<task id="LOCAL-00002" summary="bugfix and write last endpoint">
|
||||
<option name="closed" value="true" />
|
||||
<created>1733863638197</created>
|
||||
<option name="number" value="00002" />
|
||||
<option name="presentableId" value="LOCAL-00002" />
|
||||
<option name="project" value="LOCAL" />
|
||||
<updated>1733863638197</updated>
|
||||
</task>
|
||||
<option name="localTasksCounter" value="3" />
|
||||
<servers />
|
||||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<MESSAGE value="core :pig:" />
|
||||
<MESSAGE value="bugfix and write last endpoint" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value="bugfix and write last endpoint" />
|
||||
</component>
|
||||
</project>
|
57
pom.xml
Normal file
57
pom.xml
Normal file
@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>NTO-2024</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.5.5</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-ui</artifactId>
|
||||
<version>1.7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
13
src/main/java/com/example/nto/App.java
Normal file
13
src/main/java/com/example/nto/App.java
Normal file
@ -0,0 +1,13 @@
|
||||
package com.example.nto;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@EntityScan(basePackages = "com.example.nto.entity")
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.repository.CodeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class EmployeeController {
|
||||
private final EmployeeService employeeService;
|
||||
private final CodeRepository codeRepository;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService, CodeRepository codeRepository) {
|
||||
this.employeeService = employeeService;
|
||||
this.codeRepository = codeRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Эндпоинт для проверки присутствия логина в емплоеееее таблице
|
||||
*
|
||||
* @param login Искомый логин
|
||||
* @return статус код, смотри документацию к тз
|
||||
*/
|
||||
@GetMapping("/api/{login}/auth")
|
||||
public ResponseEntity<String> authAttempt(@PathVariable String login) {
|
||||
try {
|
||||
if (employeeService.checkEmployeeExists(login)) {
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).build(); // Логин найден ДВЕСТИ
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); // Логин не найден 401 неавторизован
|
||||
|
||||
} catch (
|
||||
Exception e) // Я понял статус "что-то пошло не так", как то, что произошла какая-то ошибка. Вообще по идее должен быть статус 500, но допустим
|
||||
{
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Получить информацию по емплоеееее
|
||||
*
|
||||
* @param login логин пользователя
|
||||
* @return емплоеееееее иначе 401 бан бан бан бан, а если ошибка, то 400 БАААААН
|
||||
*/
|
||||
@GetMapping("/api/{login}/info")
|
||||
public ResponseEntity<Employee> getEmployeeInfo(@PathVariable String login) {
|
||||
try {
|
||||
Optional<Employee> employee = employeeService.getEmployeeInfo(login);
|
||||
return employee.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.status(HttpStatus.UNAUTHORIZED).build());
|
||||
} catch (
|
||||
Exception e) // Я понял статус "что-то пошло не так", как то, что произошла какая-то ошибка. Вообще по идее должен быть статус 500, но допустим
|
||||
{
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверка на дверь открыть не открыть
|
||||
* @param login логин входящего
|
||||
* @param value код двери
|
||||
* @return статус код отвта
|
||||
*/
|
||||
@PatchMapping("api/{login}/open")
|
||||
public ResponseEntity<String> openDoorAttempt(@PathVariable String login, @RequestBody Long value) {
|
||||
try {
|
||||
if (!employeeService.checkEmployeeExists(login)) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
|
||||
if (!codeRepository.existsByValue(value)) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
employeeService.updateLastVisit(login);
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).build();
|
||||
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
39
src/main/java/com/example/nto/entity/Code.java
Normal file
39
src/main/java/com/example/nto/entity/Code.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Code {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private long value;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public long getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(long value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
77
src/main/java/com/example/nto/entity/Employee.java
Normal file
77
src/main/java/com/example/nto/entity/Employee.java
Normal file
@ -0,0 +1,77 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Employee {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String login;
|
||||
private String name;
|
||||
private String photo;
|
||||
private String position;
|
||||
private LocalDateTime lastVisit;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
public void setLogin(String login) {
|
||||
this.login = login;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPhoto() {
|
||||
return photo;
|
||||
}
|
||||
|
||||
public void setPhoto(String photo) {
|
||||
this.photo = photo;
|
||||
}
|
||||
|
||||
public String getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
public void setPosition(String position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
public LocalDateTime getLastVisit() {
|
||||
return lastVisit;
|
||||
}
|
||||
|
||||
public void setLastVisit(LocalDateTime lastVisit) {
|
||||
this.lastVisit = lastVisit;
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.example.nto.repository;
|
||||
|
||||
import com.example.nto.entity.Code;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface CodeRepository extends JpaRepository<Code, Long> {
|
||||
boolean existsByValue(Long value);
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.example.nto.repository;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
boolean existsByLogin(String login);
|
||||
Optional<Employee> getByLogin(String login);
|
||||
Optional<Employee> findByLogin(String login);
|
||||
}
|
12
src/main/java/com/example/nto/service/EmployeeService.java
Normal file
12
src/main/java/com/example/nto/service/EmployeeService.java
Normal file
@ -0,0 +1,12 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeService {
|
||||
boolean checkEmployeeExists(String login);
|
||||
Optional<Employee> getEmployeeInfo(String login);
|
||||
boolean doorIsOpen(String login, long code);
|
||||
void updateLastVisit(String login);
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.entity.Code;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.repository.CodeRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final CodeRepository codeRepository;
|
||||
|
||||
@Autowired
|
||||
public EmployeeServiceImpl(EmployeeRepository employeeRepository, CodeRepository codeRepository){
|
||||
this.employeeRepository = employeeRepository;
|
||||
this.codeRepository = codeRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Проверить существование логина в базе данных
|
||||
* @param login логин убзера
|
||||
* @return Существует ли запись в бд
|
||||
*/
|
||||
@Override
|
||||
public boolean checkEmployeeExists(String login) {
|
||||
return employeeRepository.existsByLogin(login);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить информацию о юбзере по логину
|
||||
* @param login логин юбзера
|
||||
* @return всю инфу по юзеру
|
||||
*/
|
||||
@Override
|
||||
public Optional<Employee> getEmployeeInfo(String login) {
|
||||
return employeeRepository.getByLogin(login);
|
||||
}
|
||||
|
||||
/**
|
||||
* Попытка открыть дверь
|
||||
* @param login Логин юзера
|
||||
* @return true если дверь открыта, иначе false
|
||||
*/
|
||||
@Override
|
||||
public boolean doorIsOpen(String login, long code) {
|
||||
if (employeeRepository.existsByLogin(login)) {
|
||||
Code codeObject = new Code();
|
||||
codeObject.setValue(code);
|
||||
codeRepository.save(codeObject);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Эта темка обновит время последнего визита до текущего момента
|
||||
* @param login логин пользователя
|
||||
*/
|
||||
@Override
|
||||
public void updateLastVisit(String login) {
|
||||
Employee employee = employeeRepository.getByLogin(login).get();
|
||||
employee.setLastVisit(LocalDateTime.now());
|
||||
}
|
||||
}
|
28
src/main/resources/application.yml
Normal file
28
src/main/resources/application.yml
Normal file
@ -0,0 +1,28 @@
|
||||
spring:
|
||||
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb
|
||||
|
||||
h2:
|
||||
console:
|
||||
#enabled: false
|
||||
enabled: true
|
||||
|
||||
jpa:
|
||||
#generate-ddl: false
|
||||
generate-ddl: true
|
||||
|
||||
hibernate:
|
||||
#ddl-auto: none
|
||||
ddl-auto: create-drop
|
||||
|
||||
# Показываем запросы
|
||||
show-sql: true
|
||||
|
||||
# Своевременный запуск data.sql
|
||||
defer-datasource-initialization: true
|
||||
|
||||
spring-doc:
|
||||
swagger-ui:
|
||||
path: /swagger-ui.html
|
||||
operationsSorter: method
|
14
src/main/resources/data.sql
Normal file
14
src/main/resources/data.sql
Normal file
@ -0,0 +1,14 @@
|
||||
INSERT INTO employee (id, login, name, photo, position, last_visit)
|
||||
VALUES
|
||||
(1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
|
||||
(2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
|
||||
(3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
|
||||
(4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36');
|
||||
|
||||
INSERT INTO code (value)
|
||||
VALUES
|
||||
(1234567890123456789),
|
||||
(9223372036854775807),
|
||||
(1122334455667788990),
|
||||
(998877665544332211),
|
||||
(5566778899001122334);
|
Loading…
x
Reference in New Issue
Block a user