From e04cef037234d33af02124073baeb0a1e6a58216 Mon Sep 17 00:00:00 2001 From: igorfourier Date: Sat, 16 May 2026 09:42:59 +0200 Subject: [PATCH] first commit --- .gitignore | 31 +++++++++++ pom.xml | 54 +++++++++++++++++++ .../SpringBootFundamentalsApplication.java | 14 +++++ .../controller/GreetingController.java | 31 +++++++++++ .../controller/TimeController.java | 49 +++++++++++++++++ .../controller/WeatherController.java | 53 ++++++++++++++++++ .../service/TimeService.java | 31 +++++++++++ .../service/WeatherService.java | 25 +++++++++ src/main/resources/application.properties | 4 ++ 9 files changed, 292 insertions(+) create mode 100644 .gitignore create mode 100644 pom.xml create mode 100644 src/main/java/com/ironhack/springbootfundamentals/SpringBootFundamentalsApplication.java create mode 100644 src/main/java/com/ironhack/springbootfundamentals/controller/GreetingController.java create mode 100644 src/main/java/com/ironhack/springbootfundamentals/controller/TimeController.java create mode 100644 src/main/java/com/ironhack/springbootfundamentals/controller/WeatherController.java create mode 100644 src/main/java/com/ironhack/springbootfundamentals/service/TimeService.java create mode 100644 src/main/java/com/ironhack/springbootfundamentals/service/WeatherService.java create mode 100644 src/main/resources/application.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a0e52d --- /dev/null +++ b/.gitignore @@ -0,0 +1,31 @@ +# Maven +target/ +*.jar +*.war +*.ear +*.zip +*.tar.gz + +# IDE +.idea/ +.vscode/ +*.iml +*.iws +*.ipr +.eclipse +.classpath +.project +.settings/ + +# OS +.DS_Store +Thumbs.db + +# Dependencies +*.class +*.log + +# Spring Boot +logs/ +*.log.* + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..f9851b7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.0 + + + + com.ironhack + springboot-fundamentals + 0.0.1-SNAPSHOT + SpringBoot Fundamentals + Lab for SpringBoot Fundamentals + + + 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 + + + + + diff --git a/src/main/java/com/ironhack/springbootfundamentals/SpringBootFundamentalsApplication.java b/src/main/java/com/ironhack/springbootfundamentals/SpringBootFundamentalsApplication.java new file mode 100644 index 0000000..71decce --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/SpringBootFundamentalsApplication.java @@ -0,0 +1,14 @@ +package com.ironhack.springbootfundamentals; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootFundamentalsApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootFundamentalsApplication.class, args); + } + +} + diff --git a/src/main/java/com/ironhack/springbootfundamentals/controller/GreetingController.java b/src/main/java/com/ironhack/springbootfundamentals/controller/GreetingController.java new file mode 100644 index 0000000..b16532a --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/controller/GreetingController.java @@ -0,0 +1,31 @@ +package com.ironhack.springbootfundamentals.controller; + +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 { + + @GetMapping("/hello") + public String sayHello() { + return "Hello World!"; + } + + @GetMapping("/hello/{name}") + public String greetByName(@PathVariable String name) { + return "Hello " + name + "!"; + } + + @GetMapping("/add/{num1}/{num2}") + public int add(@PathVariable int num1, @PathVariable int num2) { + return num1 + num2; + } + + @GetMapping("/multiply/{num1}/{num2}") + public int multiply(@PathVariable int num1, @PathVariable int num2) { + return num1 * num2; + } + +} + diff --git a/src/main/java/com/ironhack/springbootfundamentals/controller/TimeController.java b/src/main/java/com/ironhack/springbootfundamentals/controller/TimeController.java new file mode 100644 index 0000000..11c1fb2 --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/controller/TimeController.java @@ -0,0 +1,49 @@ +package com.ironhack.springbootfundamentals.controller; + +import com.ironhack.springbootfundamentals.service.TimeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.HashMap; +import java.util.Map; + +@RestController +public class TimeController { + + private final TimeService timeService; + + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/time") + public Map getTime() { + Map response = new HashMap<>(); + response.put("time", timeService.getCurrentTime()); + return response; + } + + @GetMapping("/date") + public Map getDate() { + Map response = new HashMap<>(); + response.put("date", timeService.getCurrentDate()); + return response; + } + + @GetMapping("/day") + public Map getDay() { + Map response = new HashMap<>(); + response.put("day", timeService.getCurrentDayOfWeek()); + return response; + } + + @GetMapping("/all") + public Map getAllTimeInfo() { + Map response = new HashMap<>(); + response.put("time", timeService.getCurrentTime()); + response.put("date", timeService.getCurrentDate()); + response.put("day", timeService.getCurrentDayOfWeek()); + return response; + } + +} + diff --git a/src/main/java/com/ironhack/springbootfundamentals/controller/WeatherController.java b/src/main/java/com/ironhack/springbootfundamentals/controller/WeatherController.java new file mode 100644 index 0000000..ef81b61 --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/controller/WeatherController.java @@ -0,0 +1,53 @@ +package com.ironhack.springbootfundamentals.controller; + +import com.ironhack.springbootfundamentals.service.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.HashMap; +import java.util.Map; + +@RestController +public class WeatherController { + + private final WeatherService weatherService; + + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/weather/temperature") + public Map getTemperature() { + Map response = new HashMap<>(); + response.put("temperature", weatherService.getCurrentTemperature()); + response.put("unit", "Celsius"); + return response; + } + + @GetMapping("/weather/condition") + public Map getCondition() { + Map response = new HashMap<>(); + response.put("condition", weatherService.getWeatherCondition()); + return response; + } + + @GetMapping("/weather/wind") + public Map getWindSpeed() { + Map response = new HashMap<>(); + response.put("windSpeed", weatherService.getWindSpeed()); + response.put("unit", "km/h"); + return response; + } + + @GetMapping("/weather/all") + public Map getAllWeatherInfo() { + Map response = new HashMap<>(); + response.put("temperature", weatherService.getCurrentTemperature()); + response.put("temperatureUnit", "Celsius"); + response.put("condition", weatherService.getWeatherCondition()); + response.put("windSpeed", weatherService.getWindSpeed()); + response.put("windSpeedUnit", "km/h"); + return response; + } + +} + diff --git a/src/main/java/com/ironhack/springbootfundamentals/service/TimeService.java b/src/main/java/com/ironhack/springbootfundamentals/service/TimeService.java new file mode 100644 index 0000000..288072e --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/service/TimeService.java @@ -0,0 +1,31 @@ +package com.ironhack.springbootfundamentals.service; + +import org.springframework.stereotype.Service; +import java.time.LocalDateTime; +import java.time.LocalDate; +import java.time.DayOfWeek; +import java.time.format.DateTimeFormatter; + +@Service +public class TimeService { + + public String getCurrentTime() { + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss"); + return now.format(formatter); + } + + public String getCurrentDate() { + LocalDate today = LocalDate.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + return today.format(formatter); + } + + public String getCurrentDayOfWeek() { + LocalDate today = LocalDate.now(); + DayOfWeek dayOfWeek = today.getDayOfWeek(); + return dayOfWeek.toString(); + } + +} + diff --git a/src/main/java/com/ironhack/springbootfundamentals/service/WeatherService.java b/src/main/java/com/ironhack/springbootfundamentals/service/WeatherService.java new file mode 100644 index 0000000..ba16e1f --- /dev/null +++ b/src/main/java/com/ironhack/springbootfundamentals/service/WeatherService.java @@ -0,0 +1,25 @@ +package com.ironhack.springbootfundamentals.service; + +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; // Rango entre -10 y 40 + } + + public String getWeatherCondition() { + String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"}; + return conditions[random.nextInt(conditions.length)]; + } + + public int getWindSpeed() { + return random.nextInt(101); // Rango entre 0 y 100 + } + +} + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..f7ae0b2 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.application.name=springboot-fundamentals +server.port=8080 +spring.devtools.restart.enabled=true +