Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/dictionaries/Andrey.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/libraries/gson_2_9_0.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
Сохранение списка задач в файл. Восстановление задач из файла.

## Спринт №8
Добавляем дату и время
Добавляем дату и время

## Спринт №9
Реализуем HTTP API
4 changes: 2 additions & 2 deletions data/tasks.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
id;DateTime;Duration(min);type;name;status;description;epic
0;2024.12.31 12:05;15;TASK;Задача №1;DONE;Описание задачи №1;
1;2024.12.31 23:55;10;TASK;Задача №2;NEW;С новым годом!;
1;2024.12.31 12:05;15;TASK;Задача №1;DONE;Описание задачи №1;
8;2024.12.31 23:55;10;TASK;Задача №...;NEW;С новым годом!;
2;2024.12.31 10:55;110;EPIC;Эпик №1;IN_PROGRESS;-;
6;2024.12.31 14:45;10;EPIC;Эпик №2;NEW;-;
3;2024.12.31 12:30;15;SUBTASK;Подзадача №1;DONE;-;2
Expand Down
1 change: 1 addition & 0 deletions java-kanban.iml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="gson-2.9.0" level="project" />
</component>
</module>
5 changes: 4 additions & 1 deletion src/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import managers.Managers;
import managers.TaskManager;
import tasks.Epic;
import tasks.Subtask;
import tasks.Task;
import managers.FileBackedTaskManager;

import java.time.Duration;
import java.time.LocalDateTime;
Expand All @@ -18,7 +21,7 @@ public static void main(String[] args) {

System.out.println("\nПроверяем статус эпика : " + manager.getEpic(6).toString());

System.out.println("\nПроверяем статус задачи : " + manager.getTask(0).toString());
System.out.println("\nПроверяем статус задачи : " + manager.getTask(1).toString());

System.out.println("\nПроверяем статус подзадачи : " + manager.getSubtask(5).toString());

Expand Down
25 changes: 25 additions & 0 deletions src/adapters/DurationAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.time.Duration;

public class DurationAdapter extends TypeAdapter<Duration> {
@Override
public void write(final JsonWriter jsonWriter, final Duration duration) throws IOException {
if (duration == null) {
jsonWriter.value(0);
return;
}
jsonWriter.value(duration.toMinutes());
}

@Override
public Duration read(final JsonReader jsonReader) throws IOException {
return Duration.ofMinutes(Integer.decode(jsonReader.nextString()));
}

}
11 changes: 11 additions & 0 deletions src/adapters/JsFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package adapters;

public class JsFormatter {
public static final String MESSAGE = "{\n \"message\":\"%s\"\n}";

public static final String ID_MESSAGE = "{\n \"id\":%d,"
+ "\n \"message\":\"%s\"\n}";

private JsFormatter() {
}
}
32 changes: 32 additions & 0 deletions src/adapters/LocalDateTimeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import tasks.Task;

import java.io.IOException;
import java.time.LocalDateTime;


public class LocalDateTimeAdapter extends TypeAdapter<LocalDateTime> {

@Override
public void write(final JsonWriter jsonWriter, final LocalDateTime localDateTime) throws IOException {
if (localDateTime == null) {
jsonWriter.value("null");
return;
}
jsonWriter.value(localDateTime.format(Task.DATE_TIME_FORMATTER));
}

@Override
public LocalDateTime read(final JsonReader jsonReader) throws IOException {
String value = jsonReader.nextString();
if (value.equals("null")) {
return null;
}
return LocalDateTime.parse(value, Task.DATE_TIME_FORMATTER);
}

}
29 changes: 29 additions & 0 deletions src/adapters/TaskStatusAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package adapters;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import tasks.TaskStatus;

import java.io.IOException;

public class TaskStatusAdapter extends TypeAdapter<TaskStatus> {
@Override
public void write(final JsonWriter jsonWriter, final TaskStatus taskStatus) throws IOException {
if (taskStatus == null) {
jsonWriter.value(TaskStatus.NEW.toString());
return;
}
jsonWriter.value(taskStatus.toString());
}

@Override
public TaskStatus read(final JsonReader jsonReader) throws IOException {
TaskStatus taskStatus = TaskStatus.valueOf(jsonReader.nextString());
if (taskStatus == null) {
return TaskStatus.NEW;
}
return taskStatus;
}

}
15 changes: 15 additions & 0 deletions src/exceptions/NotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package exceptions;

public class NotFoundException extends RuntimeException {
private final String taskInfo;

public NotFoundException(final String text, final String taskInfo) {
super(text);
this.taskInfo = taskInfo;
}

public String getDetailMessage() {
return getMessage() + " " + taskInfo;
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package exceptions;

public class TaskCrossTimeException extends RuntimeException {
public class TimeIntersectionException extends RuntimeException {
private final String existsTasks;

public TaskCrossTimeException(final String text, final String existsTasks) {
public TimeIntersectionException(final String text, final String existsTasks) {
super(text);
this.existsTasks = existsTasks;
}
Expand Down
60 changes: 60 additions & 0 deletions src/httpapi/BaseHttpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package httpapi;

import adapters.JsFormatter;
import com.sun.net.httpserver.HttpExchange;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

public class BaseHttpHandler {
protected static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

/**
* Передач ответа на HTTP запрос
*
* @param h- объект http запроса
* @param text - текст ответа
* @param retCode - код завершения обработки запроса
*/
public void sendText(HttpExchange h, String text, int retCode) /*throws IOException*/ {
try {
byte[] resp = text.getBytes(StandardCharsets.UTF_8);
h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8");
h.sendResponseHeaders(retCode, resp.length);
h.getResponseBody().write(resp);
} catch (IOException e) {
System.out.println("Произошла ошибка при передаче ответа на запрос.\n" +
e.getMessage());
}
}

/**
* Чтение параметра http запроса
*
* @param h - объект http запроса
* @return - идентификатор элемента в списке задач
*/
public Optional<Integer> getElementId(HttpExchange h) {
String[] pathParts = h.getRequestURI().getPath().split("/");
if (pathParts.length < 3) {
return Optional.empty();
}
try {
return Optional.of(Integer.parseInt(pathParts[2]));
} catch (NumberFormatException exception) {
return Optional.empty();
}
}

/**
* Обработка Нераспознанного запроса
*/
protected void handleUnknown(HttpExchange exchange, String method) {
sendText(exchange,
String.format(JsFormatter.MESSAGE, "Метод не поддержмвается - " + method),
406);
}

}
Loading