diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2db2736 --- /dev/null +++ b/pom.xml @@ -0,0 +1,60 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.4.4 + + + com.ironhack + springBootApp + 0.0.1-SNAPSHOT + springBootApp + Demo project for Spring Boot + + + + + + + + + + + + + + + 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/springBootApp/SpringBootAppApplication.java b/src/main/java/com/ironhack/springBootApp/SpringBootAppApplication.java new file mode 100644 index 0000000..a329af4 --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/SpringBootAppApplication.java @@ -0,0 +1,13 @@ +package com.ironhack.springBootApp; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootAppApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootAppApplication.class, args); + } + +} diff --git a/src/main/java/com/ironhack/springBootApp/controller/GreetingController.java b/src/main/java/com/ironhack/springBootApp/controller/GreetingController.java new file mode 100644 index 0000000..7be0d45 --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/controller/GreetingController.java @@ -0,0 +1,34 @@ +package com.ironhack.springBootApp.controller; + +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GreetingController { + + @RequestMapping("/hello") + public String hello() { + return "Hello World!"; + } + + // Endpoint: /hello/{name} + @RequestMapping("/hello/{name}") + public String helloName(@PathVariable String name) { + return "Hello " + name + "!"; + } + + // Endpoint: /add/{num1}/{num2} + @RequestMapping("/add/{num1}/{num2}") + public String add(@PathVariable int num1, @PathVariable int num2) { + int sum = num1 + num2; + return "Sum: " + sum; + } + + // Endpoint: /multiply/{num1}/{num2} + @RequestMapping("/multiply/{num1}/{num2}") + public String multiply(@PathVariable int num1, @PathVariable int num2) { + int product = num1 * num2; + return "Product: " + product; + } +} diff --git a/src/main/java/com/ironhack/springBootApp/controller/TimeController.java b/src/main/java/com/ironhack/springBootApp/controller/TimeController.java new file mode 100644 index 0000000..f457bbc --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/controller/TimeController.java @@ -0,0 +1,42 @@ +package com.ironhack.springBootApp.controller; + +import com.ironhack.springBootApp.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 String getTime() { + return "Current Time: " + timeService.getCurrentTime().toString(); + } + + @GetMapping("/date") + public String getDate() { + return "Current Date: " + timeService.getCurrentDate().toString(); + } + + @GetMapping("/day") + public String getDayOfWeek() { + return "Today is: " + timeService.getCurrentDayOfWeek().toString(); + } + + @GetMapping("/all") + public Map getAllTimeInfo() { + Map timeInfo = new HashMap<>(); + timeInfo.put("time", timeService.getCurrentTime().toString()); + timeInfo.put("date", timeService.getCurrentDate().toString()); + timeInfo.put("dayOfWeek", timeService.getCurrentDayOfWeek().toString()); + return timeInfo; + } +} diff --git a/src/main/java/com/ironhack/springBootApp/controller/WeatherController.java b/src/main/java/com/ironhack/springBootApp/controller/WeatherController.java new file mode 100644 index 0000000..e533b79 --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/controller/WeatherController.java @@ -0,0 +1,44 @@ +package com.ironhack.springBootApp.controller; + +import com.ironhack.springBootApp.service.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("weather") +public class WeatherController { + private final WeatherService weatherService; + + // Constructor injection + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/temperature") + public int getCurrentTemperature() { + return weatherService.getCurrentTemperature(); + } + + @GetMapping("/condition") + public String getWeatherCondition() { + return weatherService.getWeatherCondition(); + } + + @GetMapping("/wind") + public int getWindSpeed() { + return weatherService.getWindSpeed(); + } + + @GetMapping("/all") + public Map getAllWeatherInfo() { + Map weatherInfo = new HashMap<>(); + weatherInfo.put("temperature", weatherService.getCurrentTemperature()); + weatherInfo.put("condition", weatherService.getWeatherCondition()); + weatherInfo.put("windSpeed", weatherService.getWindSpeed()); + return weatherInfo; +} +} diff --git a/src/main/java/com/ironhack/springBootApp/service/TimeService.java b/src/main/java/com/ironhack/springBootApp/service/TimeService.java new file mode 100644 index 0000000..b32a786 --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/service/TimeService.java @@ -0,0 +1,24 @@ +package com.ironhack.springBootApp.service; + +import org.springframework.stereotype.Service; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalTime; + +@Service +public class TimeService { + public LocalTime getCurrentTime() { + return LocalTime.now(); + } + + // Get current date + public LocalDate getCurrentDate() { + return LocalDate.now(); + } + + // Get current day of the week + public DayOfWeek getCurrentDayOfWeek() { + return LocalDate.now().getDayOfWeek(); + } +} diff --git a/src/main/java/com/ironhack/springBootApp/service/WeatherService.java b/src/main/java/com/ironhack/springBootApp/service/WeatherService.java new file mode 100644 index 0000000..598e71f --- /dev/null +++ b/src/main/java/com/ironhack/springBootApp/service/WeatherService.java @@ -0,0 +1,28 @@ +package com.ironhack.springBootApp.service; + +import org.springframework.stereotype.Service; + +import java.util.Random; + +@Service +public class WeatherService { + + private final Random random = new Random(); + + // Method to get current temperature (random between -10 and 40) + public int getCurrentTemperature() { + return random.nextInt(51) - 10; // Generates a number between -10 and 40 + } + + // Method to get weather condition (random from list) + public String getWeatherCondition() { + String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"}; + int index = random.nextInt(conditions.length); + return conditions[index]; + } + + // Method to get wind speed (random between 0 and 100) + public int getWindSpeed() { + return random.nextInt(101); // Generates a number between 0 and 100 + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..6db5d48 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=springBootApp diff --git a/src/test/java/com/ironhack/springBootApp/SpringBootAppApplicationTests.java b/src/test/java/com/ironhack/springBootApp/SpringBootAppApplicationTests.java new file mode 100644 index 0000000..d33e561 --- /dev/null +++ b/src/test/java/com/ironhack/springBootApp/SpringBootAppApplicationTests.java @@ -0,0 +1,13 @@ +package com.ironhack.springBootApp; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class SpringBootAppApplicationTests { + + @Test + void contextLoads() { + } + +}