From a4dc3842ee6cdf1435f636e3a6dd3c91d579320b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Oliva?= Date: Thu, 25 May 2023 13:24:43 -0600 Subject: Added HTTP responses and text has max length. HTTP responses to each action. Raising an error when "text" has surpassed the 120 character limit. --- src/main/java/com/encora/Main.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java index ed210d2..e9a0b24 100644 --- a/src/main/java/com/encora/Main.java +++ b/src/main/java/com/encora/Main.java @@ -2,6 +2,7 @@ package com.encora; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import java.util.Date; @@ -25,10 +26,15 @@ public class Main { } @GetMapping("/todos") + @ResponseStatus(value=HttpStatus.OK) public List getToDos() { return toDosRepository.findAll(); } + @ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Text is longer than 120 characters.") // 404 + public static class longerThanMaxException extends RuntimeException { + // ... + } record NewToDo( String text, Date dueDate, @@ -37,7 +43,11 @@ public class Main { } @PostMapping + @ResponseStatus(value=HttpStatus.OK) public void addToDo(@RequestBody NewToDo toDo) { + if (toDo.text().length() > 120) { + throw new longerThanMaxException(); + } ToDos todo = new ToDos(); todo.setText(toDo.text()); todo.setDueDate(toDo.dueDate()); -- cgit v1.2.3