From 2788f6a5cc73b21c7001d4a32eedbf99cf8b814a Mon Sep 17 00:00:00 2001 From: LirSA6CW Date: Mon, 1 Jun 2026 14:12:34 +0200 Subject: [PATCH] Lab Java Spring Boot Fundamentals completed --- .gitignore | 6 ++ pom.xml | 50 +++++++++++++++ .../java/com/ironhack/lab/LabApplication.java | 14 +++++ .../lab/controller/GreetingController.java | 49 +++++++++++++++ .../lab/controller/TimeController.java | 59 ++++++++++++++++++ .../lab/controller/WeatherController.java | 61 +++++++++++++++++++ .../com/ironhack/lab/service/TimeService.java | 35 +++++++++++ .../ironhack/lab/service/WeatherService.java | 36 +++++++++++ src/main/resources/application.properties | 2 + 9 files changed, 312 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/ironhack/lab/LabApplication.java create mode 100644 src/main/java/com/ironhack/lab/controller/GreetingController.java create mode 100644 src/main/java/com/ironhack/lab/controller/TimeController.java create mode 100644 src/main/java/com/ironhack/lab/controller/WeatherController.java create mode 100644 src/main/java/com/ironhack/lab/service/TimeService.java create mode 100644 src/main/java/com/ironhack/lab/service/WeatherService.java create mode 100644 src/main/resources/application.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fbf7b1d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +target/ +.idea/ +${project.build.directory}/ + +*.iml +*.log diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..a1d570a --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.5 + + + + com.ironhack + lab-springboot-fundamentals + 0.0.1-SNAPSHOT + lab-springboot-fundamentals + LAB SpringBoot Fundamentals - Ironhack + + + 17 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/src/main/java/com/ironhack/lab/LabApplication.java b/src/main/java/com/ironhack/lab/LabApplication.java new file mode 100644 index 0000000..7045d2c --- /dev/null +++ b/src/main/java/com/ironhack/lab/LabApplication.java @@ -0,0 +1,14 @@ +package com.ironhack.lab; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * LabApplication — Entry point for the Spring Boot application. + */ +@SpringBootApplication +public class LabApplication { + public static void main(String[] args) { + SpringApplication.run(LabApplication.class, args); + } +} \ No newline at end of file diff --git a/src/main/java/com/ironhack/lab/controller/GreetingController.java b/src/main/java/com/ironhack/lab/controller/GreetingController.java new file mode 100644 index 0000000..2e86bec --- /dev/null +++ b/src/main/java/com/ironhack/lab/controller/GreetingController.java @@ -0,0 +1,49 @@ +package com.ironhack.lab.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +/** + * GreetingController — Handles basic greeting and math endpoints. + * Task 2 of the lab. + */ +@RestController +public class GreetingController { + + /** + * GET /hello + * Returns a static greeting message. + */ + @GetMapping("/hello") + public String hello() { + return "Hello World!"; + } + + /** + * GET /hello/{name} + * Returns a personalized greeting using a path variable. + */ + @GetMapping("/hello/{name}") + public String helloName(@PathVariable String name) { + return "Hello " + name + "!"; + } + + /** + * GET /add/{num1}/{num2} + * Returns the sum of two numbers. + */ + @GetMapping("/add/{num1}/{num2}") + public String add(@PathVariable double num1, @PathVariable double num2) { + return num1 + " + " + num2 + " = " + (num1 + num2); + } + + /** + * GET /multiply/{num1}/{num2} + * Returns the product of two numbers. + */ + @GetMapping("/multiply/{num1}/{num2}") + public String multiply(@PathVariable double num1, @PathVariable double num2) { + return num1 + " x " + num2 + " = " + (num1 * num2); + } +} diff --git a/src/main/java/com/ironhack/lab/controller/TimeController.java b/src/main/java/com/ironhack/lab/controller/TimeController.java new file mode 100644 index 0000000..5ab7009 --- /dev/null +++ b/src/main/java/com/ironhack/lab/controller/TimeController.java @@ -0,0 +1,59 @@ +package com.ironhack.lab.controller; + +import com.ironhack.lab.service.TimeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * TimeController — Exposes time endpoints using TimeService. + * Uses constructor injection (no @Autowired). + * Task 6 of the lab. + */ +@RestController +public class TimeController { + + private final TimeService timeService; + + // Constructor injection + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + /** + * GET /time + * Returns the current time. + */ + @GetMapping("/time") + public String getTime() { + return "Current time: " + timeService.getCurrentTime(); + } + + /** + * GET /date + * Returns the current date. + */ + @GetMapping("/date") + public String getDate() { + return "Current date: " + timeService.getCurrentDate(); + } + + /** + * GET /day + * Returns the current day of the week. + */ + @GetMapping("/day") + public String getDay() { + return "Current day: " + timeService.getCurrentDayOfWeek(); + } + + /** + * GET /all + * Returns all time information in a single response. + */ + @GetMapping("/all") + public String getAll() { + return "Time: " + timeService.getCurrentTime() + " | " + + "Date: " + timeService.getCurrentDate() + " | " + + "Day: " + timeService.getCurrentDayOfWeek(); + } +} diff --git a/src/main/java/com/ironhack/lab/controller/WeatherController.java b/src/main/java/com/ironhack/lab/controller/WeatherController.java new file mode 100644 index 0000000..dc267df --- /dev/null +++ b/src/main/java/com/ironhack/lab/controller/WeatherController.java @@ -0,0 +1,61 @@ +package com.ironhack.lab.controller; + +import com.ironhack.lab.service.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * WeatherController — Exposes weather endpoints using WeatherService. + * Uses constructor injection (no @Autowired). + * Task 4 of the lab. + */ +@RestController +@RequestMapping("/weather") +public class WeatherController { + + private final WeatherService weatherService; + + // Constructor injection + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + /** + * GET /weather/temperature + * Returns the current simulated temperature. + */ + @GetMapping("/temperature") + public String getTemperature() { + return "Current temperature: " + weatherService.getCurrentTemperature() + "°C"; + } + + /** + * GET /weather/condition + * Returns the current simulated weather condition. + */ + @GetMapping("/condition") + public String getCondition() { + return "Current condition: " + weatherService.getWeatherCondition(); + } + + /** + * GET /weather/wind + * Returns the current simulated wind speed. + */ + @GetMapping("/wind") + public String getWind() { + return "Current wind speed: " + weatherService.getWindSpeed() + " km/h"; + } + + /** + * GET /weather/all + * Returns all weather information in a single response. + */ + @GetMapping("/all") + public String getAll() { + return "Temperature: " + weatherService.getCurrentTemperature() + "°C | " + + "Condition: " + weatherService.getWeatherCondition() + " | " + + "Wind: " + weatherService.getWindSpeed() + " km/h"; + } +} diff --git a/src/main/java/com/ironhack/lab/service/TimeService.java b/src/main/java/com/ironhack/lab/service/TimeService.java new file mode 100644 index 0000000..28f0375 --- /dev/null +++ b/src/main/java/com/ironhack/lab/service/TimeService.java @@ -0,0 +1,35 @@ +package com.ironhack.lab.service; + +import org.springframework.stereotype.Service; +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.format.DateTimeFormatter; + +/** + * TimeService — Provides current time, date and day of the week. + * Task 5 of the lab. + */ +@Service +public class TimeService { + + /** + * Returns the current time in HH:mm:ss format. + */ + public String getCurrentTime() { + return LocalTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss")); + } + + /** + * Returns the current date in yyyy-MM-dd format. + */ + public String getCurrentDate() { + return LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); + } + + /** + * Returns the current day of the week (e.g. MONDAY). + */ + public String getCurrentDayOfWeek() { + return LocalDate.now().getDayOfWeek().toString(); + } +} diff --git a/src/main/java/com/ironhack/lab/service/WeatherService.java b/src/main/java/com/ironhack/lab/service/WeatherService.java new file mode 100644 index 0000000..bc9ad61 --- /dev/null +++ b/src/main/java/com/ironhack/lab/service/WeatherService.java @@ -0,0 +1,36 @@ +package com.ironhack.lab.service; + +import org.springframework.stereotype.Service; +import java.util.Random; + +/** + * WeatherService — Simulates weather data using random values. + * Task 3 of the lab. + */ +@Service +public class WeatherService { + + private final Random random = new Random(); + + /** + * Returns a simulated temperature between -10 and 40 degrees. + */ + public int getCurrentTemperature() { + return random.nextInt(51) - 10; // range: -10 to 40 + } + + /** + * Returns a randomly selected weather condition. + */ + public String getWeatherCondition() { + String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"}; + return conditions[random.nextInt(conditions.length)]; + } + + /** + * Returns a simulated wind speed between 0 and 100 km/h. + */ + public int getWindSpeed() { + return random.nextInt(101); // range: 0 to 100 + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..0f432fe --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=lab-springboot-fundamentals +server.port=8080