Skip to content
9 changes: 9 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

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

10 changes: 10 additions & 0 deletions .idea/libraries/google_code_gson.xml

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

10 changes: 10 additions & 0 deletions .idea/libraries/lib.xml

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

1 change: 1 addition & 0 deletions java-kanban.iml
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="google.code.gson" level="project" />
</component>
</module>
11 changes: 8 additions & 3 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import exception.IntersectionException;
import manager.*;
import manager.FileBackedTaskManager;

import manager.Managers;

import task.Epic;
import task.Status;
import task.Subtask;
import task.Task;


import java.io.File;

import java.time.Duration;
import java.time.LocalDateTime;

Expand All @@ -14,6 +19,7 @@ public class Main {

public static void main(String[] args) throws IntersectionException {


File file = new File("src/loadFile.csv");
System.out.println(FileBackedTaskManager.loadFromFile(file).getHistory());
FileBackedTaskManager taskManager = new FileBackedTaskManager(Managers.getDefaultHistory());
Expand All @@ -36,8 +42,7 @@ public static void main(String[] args) throws IntersectionException {
taskManager.deleteSubtask(4);
System.out.println(epic1);
System.out.println(taskManager.getHistory());


System.out.println(task1);
System.out.println(taskManager.getPrioritizedTasks());


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

public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
}
24 changes: 24 additions & 0 deletions src/manager/DurationAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package manager;

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(JsonWriter jsonWriter, Duration duration) throws IOException {
if (duration != null) {
jsonWriter.value(duration.toMinutes());
} else {
jsonWriter.nullValue();
}
}

@Override
public Duration read(JsonReader jsonReader) throws IOException {
return Duration.ofMinutes(jsonReader.nextInt());
}
}
3 changes: 2 additions & 1 deletion src/manager/InMemoryTaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ private Status epicStatus(Epic epic) {


@Override
public Task updateTask(Task task) {
public Task updateTask(Task task) throws IntersectionException {
taskIntersection(task);
Integer taskId = task.getId();
if (taskId == null || !tasks.containsKey(taskId)) {
return null;
Expand Down
34 changes: 34 additions & 0 deletions src/manager/LocalDateTimeAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package manager;

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

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


public class LocalDateTimeAdapter extends TypeAdapter<LocalDateTime> {
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm");

@Override
public void write(JsonWriter jsonWriter, LocalDateTime localDateTime) throws IOException {

if (localDateTime != null) {
jsonWriter.value(localDateTime.format(dateTimeFormatter));
} else {
jsonWriter.nullValue();
}
}


@Override
public LocalDateTime read(JsonReader jsonReader) throws IOException {
String string = jsonReader.nextString();
if (string.isBlank()) {
return null;
}
return LocalDateTime.parse(string, dateTimeFormatter);
}
}
4 changes: 4 additions & 0 deletions src/manager/Managers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package manager;


public class Managers {


Expand All @@ -10,4 +11,7 @@ public static TaskManager getDefault() {
public static HistoryManager getDefaultHistory() {
return new InMemoryHistoryManager();
}



}
5 changes: 4 additions & 1 deletion src/manager/TaskManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.HashMap;
import java.util.List;
import java.util.Set;


public interface TaskManager {
Expand All @@ -35,7 +36,7 @@ public interface TaskManager {

Task createEpic(Epic epic);

Task updateTask(Task task);
Task updateTask(Task task) throws IntersectionException;

Subtask updateSubtask(Subtask subtask);

Expand All @@ -54,6 +55,8 @@ public interface TaskManager {

int getNextId();

Set<Task> getPrioritizedTasks();

}


48 changes: 48 additions & 0 deletions src/server/BaseHttpHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package server;

import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import manager.TaskManager;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public abstract class BaseHttpHandler implements HttpHandler {
protected TaskManager taskManager;
protected Gson gson;

public BaseHttpHandler(TaskManager taskManager, Gson gson) {
this.taskManager = taskManager;
this.gson = gson;
}

private void sendResponse(HttpExchange h, int statusCode, String text) throws IOException {
byte[] response = text.getBytes(StandardCharsets.UTF_8);
h.getResponseHeaders().add("Content-Type", "application/json;charset=utf-8");
h.sendResponseHeaders(statusCode, response.length);
h.getResponseBody().write(response);
h.close();
}

protected void sendText(HttpExchange h, String text) throws IOException {
sendResponse(h, 200, text);
}

protected void sendAdd(HttpExchange h, String text) throws IOException {
sendResponse(h, 201, text);
}

public void sendNotFound(HttpExchange h, String text) throws IOException {
sendResponse(h, 404, text);
}


public void sendHasInteractions(HttpExchange h, String text) throws IOException {
sendResponse(h, 406, text);
}

public void sendInternalServerError(HttpExchange httpExchange) throws IOException {
httpExchange.sendResponseHeaders(500, 0);
}
}
114 changes: 114 additions & 0 deletions src/server/EpicHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package server;

import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import exception.NotFoundException;
import manager.TaskManager;
import task.Epic;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

public class EpicHandler extends BaseHttpHandler {
public EpicHandler(TaskManager taskManager, Gson gson) {
super(taskManager, gson);
}

private int parsePathID(String path) {
try {
return Integer.parseInt(path);
} catch (NumberFormatException e) {
return -1;
}
}

@Override
public void handle(HttpExchange exchange) throws IOException {

}

private void handleGet(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
if (Pattern.matches("^/epics$", path)) {
try {
String response = gson.toJson(taskManager.getEpics());
sendText(exchange, response);
} catch (Exception e) {
sendInternalServerError(exchange);
}
}
if (Pattern.matches("^/epics/\\d+$", path)) {
try {
String pathId = path.replaceFirst("/epics/", "");
int id = parsePathID(pathId);
if (id != -1) {
String response = gson.toJson(taskManager.getEpic(id));
sendText(exchange, response);
}
} catch (NotFoundException e) {
sendNotFound(exchange, "Эпик не найден");
} catch (Exception e) {
sendInternalServerError(exchange);
}
}
if (Pattern.matches("^/subtasks$", path)) {
try {
String pathId = path.replaceFirst("/epics/", "").replaceFirst("/subtasks$", "");
int id = parsePathID(pathId);
if (id != -1) {
Epic epic = taskManager.getEpic(id);
String response = gson.toJson(taskManager.getSubtasksFromEpic(epic));
sendText(exchange, response);
}
} catch (NotFoundException e) {
sendNotFound(exchange, "Эпик не найден");
} catch (Exception e) {
sendInternalServerError(exchange);
}
}
}

private void handlePost(HttpExchange exchange) throws IOException {

try {
String epic = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
Epic epic1 = gson.fromJson(epic, Epic.class);
taskManager.createEpic(epic1);
sendAdd(exchange, "Эпик создан");
} catch (Exception e) {
sendInternalServerError(exchange);
}
}

private void handleDelete(HttpExchange exchange) throws IOException {
String path = exchange.getRequestURI().getPath();
try {
if (Pattern.matches("^/epics$", path)) {
try {


taskManager.deleteAllEpic();
sendText(exchange, "Удалены все Эпики");
} catch (Exception e) {
sendInternalServerError(exchange);
}
}
if (Pattern.matches("^/epics/\\d+$", path)) {
String epicId = path.replaceFirst("/epics/", "");
int id = parsePathID(epicId);
if (id != -1) {
taskManager.deleteEpic(id);
sendText(exchange, "Удалён эпик с id " + id);
}
}

} catch (NotFoundException e) {
sendNotFound(exchange, "Эпик не найден");
} catch (Exception e) {
sendInternalServerError(exchange);
}

}

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

import com.google.gson.Gson;
import com.sun.net.httpserver.HttpExchange;
import manager.TaskManager;

import java.io.IOException;

public class HistoryHandler extends BaseHttpHandler {
public HistoryHandler(TaskManager taskManager, Gson gson) {
super(taskManager, gson);
}

@Override
public void handle(HttpExchange exchange) throws IOException {
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equals("GET")) {
try {
String history = gson.toJson(taskManager.getHistory());
sendText(exchange, history);
} catch (Exception e) {
sendInternalServerError(exchange);
}
} else {
sendInternalServerError(exchange);
}
}
}
Loading
Loading