From e4d932a39a0b1a3d0e331e939af5cce763d4ba22 Mon Sep 17 00:00:00 2001 From: cldcr Date: Sat, 2 May 2026 22:08:01 +0200 Subject: [PATCH] LAB4 --- .idea/.gitignore | 10 +++++ .idea/lab-java-springboot-fundamentals.iml | 9 ++++ .idea/misc.xml | 6 +++ .idea/modules.xml | 8 ++++ .../GreetingController.java | 33 ++++++++++++++ .../GreetingController/TimeController.java | 43 +++++++++++++++++++ .../GreetingController/WeatherController.java | 41 ++++++++++++++++++ .../GreetingControllerApplication.java | 13 ++++++ .../TimeService/TimeService.java | 22 ++++++++++ .../WeatherService/WeatherService.java | 22 ++++++++++ .../src/main/resources/application.properties | 1 + .../GreetingControllerApplicationTests.java | 13 ++++++ .idea/vcs.xml | 6 +++ 13 files changed, 227 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/lab-java-springboot-fundamentals.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/src/main/java/com/example/greetingcontroller/GreetingController/GreetingController.java create mode 100644 .idea/src/main/java/com/example/greetingcontroller/GreetingController/TimeController.java create mode 100644 .idea/src/main/java/com/example/greetingcontroller/GreetingController/WeatherController.java create mode 100644 .idea/src/main/java/com/example/greetingcontroller/GreetingControllerApplication.java create mode 100644 .idea/src/main/java/com/example/greetingcontroller/TimeService/TimeService.java create mode 100644 .idea/src/main/java/com/example/greetingcontroller/WeatherService/WeatherService.java create mode 100644 .idea/src/main/resources/application.properties create mode 100644 .idea/src/test/java/com/example/greetingcontroller/GreetingControllerApplicationTests.java create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..30cf57e --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,10 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Ignored default folder with query files +/queries/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/lab-java-springboot-fundamentals.iml b/.idea/lab-java-springboot-fundamentals.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/.idea/lab-java-springboot-fundamentals.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4eb4f51 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..0a6b987 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/src/main/java/com/example/greetingcontroller/GreetingController/GreetingController.java b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/GreetingController.java new file mode 100644 index 0000000..9a3c7ba --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/GreetingController.java @@ -0,0 +1,33 @@ +package com.example.greetingcontroller.GreetingController; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + // Endpoint: /hello + @GetMapping("/hello") + public String hello() { + return "Hello World!"; + } + + // Endpoint: /hello/{name} + @GetMapping("/hello/{name}") + public String helloName(@PathVariable String name) { + return "Hello !"; + } + + // Endpoint: /add/{num1}/{num2} + @GetMapping("/add/{num1}/{num2}") + public int add(@PathVariable int num1, @PathVariable int num2) { + return num1 + num2; + } + + // Endpoint: /multiply/{num1}/{num2} + @GetMapping("/multiply/{num1}/{num2}") + public int multiply(@PathVariable int num1, @PathVariable int num2) { + return num1 * num2; + } +} \ No newline at end of file diff --git a/.idea/src/main/java/com/example/greetingcontroller/GreetingController/TimeController.java b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/TimeController.java new file mode 100644 index 0000000..b464b7b --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/TimeController.java @@ -0,0 +1,43 @@ +package com.example.greetingcontroller.GreetingController; + +import com.example.greetingcontroller.TimeService.TimeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.Map; + +@RestController +@RequestMapping("/time-info") // Opzionale: aggiunge un prefisso per non andare in conflitto con altri controller +public class TimeController { + + private final TimeService timeService; + + // Constructor Injection + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/time") + public String getTime() { + return timeService.getCurrentTime(); + } + + @GetMapping("/date") + public String getDate() { + return timeService.getCurrentDate(); + } + + @GetMapping("/day") + public String getDay() { + return timeService.getDayOfWeek(); + } + + @GetMapping("/all") + public Map getAllTimeInfo() { + return Map.of( + "time", timeService.getCurrentTime(), + "date", timeService.getCurrentDate(), + "dayOfWeek", timeService.getDayOfWeek() + ); + } +} \ No newline at end of file diff --git a/.idea/src/main/java/com/example/greetingcontroller/GreetingController/WeatherController.java b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/WeatherController.java new file mode 100644 index 0000000..57eb7c2 --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/GreetingController/WeatherController.java @@ -0,0 +1,41 @@ +package com.example.greetingcontroller.GreetingController; + +import com.example.greetingcontroller.WeatherService.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.Map; + +@RestController +public class WeatherController { + + private final WeatherService weatherService; + + // Constructor Injection + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/weather/temperature") + public int getTemperature() { + return weatherService.getCurrentTemperature(); + } + + @GetMapping("/weather/condition") + public String getCondition() { + return weatherService.getWeatherCondition(); + } + + @GetMapping("/weather/wind") + public int getWind() { + return weatherService.getWindSpeed(); + } + + @GetMapping("/weather/all") + public Map getAllWeather() { + return Map.of( + "temperature", weatherService.getCurrentTemperature(), + "condition", weatherService.getWeatherCondition(), + "windSpeed", weatherService.getWindSpeed() + ); + } +} \ No newline at end of file diff --git a/.idea/src/main/java/com/example/greetingcontroller/GreetingControllerApplication.java b/.idea/src/main/java/com/example/greetingcontroller/GreetingControllerApplication.java new file mode 100644 index 0000000..9318357 --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/GreetingControllerApplication.java @@ -0,0 +1,13 @@ +package com.example.greetingcontroller; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GreetingControllerApplication { + + public static void main(String[] args) { + SpringApplication.run(GreetingControllerApplication.class, args); + } + +} diff --git a/.idea/src/main/java/com/example/greetingcontroller/TimeService/TimeService.java b/.idea/src/main/java/com/example/greetingcontroller/TimeService/TimeService.java new file mode 100644 index 0000000..517e622 --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/TimeService/TimeService.java @@ -0,0 +1,22 @@ +package com.example.greetingcontroller.TimeService; + +import org.springframework.stereotype.Service; +import java.time.LocalTime; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +@Service +public class TimeService { + + public String getCurrentTime() { + return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")); + } + + public String getCurrentDate() { + return LocalDate.now().toString(); + } + + public String getDayOfWeek() { + return LocalDate.now().getDayOfWeek().name(); + } +} \ No newline at end of file diff --git a/.idea/src/main/java/com/example/greetingcontroller/WeatherService/WeatherService.java b/.idea/src/main/java/com/example/greetingcontroller/WeatherService/WeatherService.java new file mode 100644 index 0000000..2eb1552 --- /dev/null +++ b/.idea/src/main/java/com/example/greetingcontroller/WeatherService/WeatherService.java @@ -0,0 +1,22 @@ +package com.example.greetingcontroller.WeatherService; + +import org.springframework.stereotype.Service; +import java.util.Random; + +@Service +public class WeatherService { + private final Random random = new Random(); + + public int getCurrentTemperature() { + return random.nextInt(51) - 10; // Range: -10 a 40 + } + + public String getWeatherCondition() { + String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"}; + return conditions[random.nextInt(conditions.length)]; + } + + public int getWindSpeed() { + return random.nextInt(101); // Range: 0 a 100 + } +} diff --git a/.idea/src/main/resources/application.properties b/.idea/src/main/resources/application.properties new file mode 100644 index 0000000..4d667fd --- /dev/null +++ b/.idea/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=GreetingController diff --git a/.idea/src/test/java/com/example/greetingcontroller/GreetingControllerApplicationTests.java b/.idea/src/test/java/com/example/greetingcontroller/GreetingControllerApplicationTests.java new file mode 100644 index 0000000..6d3da5d --- /dev/null +++ b/.idea/src/test/java/com/example/greetingcontroller/GreetingControllerApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.greetingcontroller; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class GreetingControllerApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file