From 45ab8b99bf27e3923331d2d640463b65ee7ad818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Thu, 25 May 2023 13:23:16 -0600 Subject: Added a POST endpoint. Submit a JSON formatted body with the values "text", "dueDate" and "priority" to submit a new to do. --- src/main/java/com/encora/Main.java | 19 ++++++++++++++++++- src/main/java/com/encora/ToDosRepository.java | 13 ++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index 2989919..ed210d2 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -2,8 +2,9 @@ package com.encora; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; @SpringBootApplication @@ -27,4 +28,20 @@ public class Main { public List getToDos() { return toDosRepository.findAll(); } + + record NewToDo( + String text, + Date dueDate, + Priority priority + ) { + + } + @PostMapping + public void addToDo(@RequestBody NewToDo toDo) { + ToDos todo = new ToDos(); + todo.setText(toDo.text()); + todo.setDueDate(toDo.dueDate()); + todo.setPriority(toDo.priority()); + toDosRepository.save(todo); + } } diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java index 029185b..c979e11 100644 --- a/src/main/java/com/encora/ToDosRepository.java +++ b/src/main/java/com/encora/ToDosRepository.java @@ -38,6 +38,14 @@ public class ToDosRepository implements JpaRepository { return this.todos; } + // Save new element. + @Override + public S save(S entity) { + entity.setId(++this.lastId); + this.todos.add(entity); + return null; + } + /* * N O T Y E T D E F I N E D . */ @@ -121,11 +129,6 @@ public class ToDosRepository implements JpaRepository { return null; } - @Override - public S save(S entity) { - return null; - } - @Override public List saveAll(Iterable entities) { return null; -- cgit v1.2.3