Initial commit

This commit is contained in:
2025-11-04 14:25:40 +03:00
commit 80b7e8e22f
16 changed files with 364 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,35 @@
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) {
return encoderApi.encode(null, id);
}
@GetMapping("/task2/{id}")
public int getTask2Code(@PathVariable long id) {
return encoderApi.encode(departmentService.getAll(), id);
}
@GetMapping("/task3/{id}")
public int getTask3Code(@PathVariable long id) {
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,56 @@
package ru.example.nto.entity;
import java.util.Objects;
//TODO: Задание 2
// Добавьте все необходимые аннотации для класса-сущности Department.
public class Department {
private long id;
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,11 @@
package ru.example.nto.repository;
//TODO: Задание 2
// Опишите интерфейс DepartmentRepository,
// так чтобы он обеспечивал корректную работу со всеми CRUD-операциями сущности Department.
public interface DepartmentRepository {
//TODO: Задание 3
// Добавьте метод для получения из базы данных департамента по его наименованию.
}

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,37 @@
package ru.example.nto.service.impl;
import org.springframework.stereotype.Service;
import ru.example.nto.entity.Department;
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;
//TODO: Задание 2
// Исправьте конструктор так, чтобы он корректно инициализировал поле departmentRepository.
public DepartmentServiceImpl() {
this.departmentRepository = null;
}
@Override
public List<Department> getAll() {
//TODO: Задание 2
// Реализуйте метод, который возвращает всех департаментов, которые есть в базе данных.
return null;
}
@Override
public Department getByName(String name) {
//TODO: Задание 3
// Реализуйте метод, который получает департамент по его наименованию
// и обрабатывает результат слоя repository.
// Если департамента с указанным наименованием в базе данных нет,
// то необходимо выбрасывать DepartmentNotFoundException
return null;
}
}