feat: Initial commit

This commit is contained in:
2026-01-20 12:59:58 +03:00
commit 786d367b5f
15 changed files with 335 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package ru.example.nto;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

View File

@@ -0,0 +1,39 @@
package ru.example.nto.controller;
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;
import ru.example.nto.service.DepartmentService;
import ru.nto.security.Encoder;
import ru.nto.security.EncoderApi;
@RestController
@RequestMapping("code")
public class CodeController {
private final DepartmentService departmentService;
private final EncoderApi encoderApi = Encoder.create();
public CodeController(DepartmentService departmentService) {
this.departmentService = departmentService;
}
@GetMapping("/task1/{id}")
public int getTask1Code(@PathVariable long id) {
//Ответ: 1721890220 - (id % 100) * 17
return encoderApi.encode(null, id);
}
@GetMapping("/task2/{id}")
public int getTask2Code(@PathVariable long id) {
//Ответ: 951167143 - (id % 100) * 17
return encoderApi.encode(departmentService.getAll(), id);
}
@GetMapping("/task3/{id}")
public int getTask3Code(@PathVariable long id) {
//Ответ: 1650897291 - (id % 100) * 17
return encoderApi.encode(departmentService.getByName("Департамент аналитики"), id);
}
}

View File

@@ -0,0 +1,31 @@
package ru.example.nto.controller;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import ru.example.nto.entity.Department;
import ru.example.nto.service.DepartmentService;
import java.util.List;
@RestController
@RequestMapping("api/v1/department")
public class DepartmentController {
private final DepartmentService departmentService;
public DepartmentController(DepartmentService departmentService) {
this.departmentService = departmentService;
}
@GetMapping
@ResponseStatus(code = HttpStatus.OK)
public List<Department> getAll() {
return departmentService.getAll();
}
@GetMapping("/{name}")
@ResponseStatus(HttpStatus.OK)
public Department getByName(@PathVariable String name) {
return departmentService.getByName(name);
}
}

View File

@@ -0,0 +1,60 @@
package ru.example.nto.entity;
import jakarta.persistence.*;
import java.util.Objects;
@Entity
@Table(name = "department")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "name")
private String name;
public Department() {
}
public Department(long id, String name) {
this.id = id;
this.name = name;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Department{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Department that = (Department) o;
return id == that.id && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
}

View File

@@ -0,0 +1,7 @@
package ru.example.nto.exception;
public class DepartmentNotFoundException extends RuntimeException {
public DepartmentNotFoundException(String msg) {
super(msg);
}
}

View File

@@ -0,0 +1,10 @@
package ru.example.nto.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.example.nto.entity.Department;
import java.util.Optional;
public interface DepartmentRepository extends JpaRepository<Department, Long> {
Optional<Department> findByName(String name);
}

View File

@@ -0,0 +1,11 @@
package ru.example.nto.service;
import ru.example.nto.entity.Department;
import java.util.List;
public interface DepartmentService {
List<Department> getAll();
Department getByName(String name);
}

View File

@@ -0,0 +1,30 @@
package ru.example.nto.service.impl;
import org.springframework.stereotype.Service;
import ru.example.nto.entity.Department;
import ru.example.nto.exception.DepartmentNotFoundException;
import ru.example.nto.repository.DepartmentRepository;
import ru.example.nto.service.DepartmentService;
import java.util.List;
@Service
public class DepartmentServiceImpl implements DepartmentService {
private final DepartmentRepository departmentRepository;
public DepartmentServiceImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
@Override
public List<Department> getAll() {
return departmentRepository.findAll();
}
@Override
public Department getByName(String name) {
return departmentRepository.findByName(name)
.orElseThrow(() -> new DepartmentNotFoundException("Department " + name + " not found!"));
}
}