diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..90dee70 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + {} + \ No newline at end of file diff --git a/README.md b/README.md index 416cbf5..33d4cce 100644 --- a/README.md +++ b/README.md @@ -1,57 +1,25 @@ -![logo_ironhack_blue 7](https://user-images.githubusercontent.com/23629340/40541063-a07a0a8a-601a-11e8-91b5-2f13e4e6b441.png) - -# LAB | SpringBoot Fundamentals - -### Instructions - -1. Fork this repo. -2. Clone your fork to your local machine. -3. Solve the challenges. - - -## Deliverables - -- Upon completion, add your solution to git. -- Then commit to git and push to your repo on GitHub. -- Make a pull request and paste the pull request link in the submission field in the Student Portal. - -## Tasks - -1. Create a Spring Boot application using Spring Initializr with the following dependencies: - - Spring Web - - Spring Boot DevTools - -2. Create a `GreetingController` class that has the following endpoints: - - `/hello` - Returns "Hello World!" - - `/hello/{name}` - Returns "Hello {name}!" where {name} is a path variable - - `/add/{num1}/{num2}` - Returns the sum of num1 and num2 - - `/multiply/{num1}/{num2}` - Returns the product of num1 and num2 - -3. Create a `WeatherService` class that has methods to: - - Get current temperature (simulate with random number between -10 and 40) - - Get weather condition (randomly return: "Sunny", "Rainy", "Cloudy", "Windy") - - Get wind speed (simulate with random number between 0 and 100) - -4. Create a `WeatherController` class that uses constructor injection to receive the `WeatherService` and has endpoints to: - - `/weather/temperature` - Returns current temperature - - `/weather/condition` - Returns current weather condition - - `/weather/wind` - Returns current wind speed - - `/weather/all` - Returns all weather information in a single response - -5. Create a `TimeService` class that has methods to: - - Get current time - - Get current date - - Get current day of the week - -6. Create a `TimeController` class that uses constructor injection to receive the `TimeService` and has endpoints to: - - `/time` - Returns current time - - `/date` - Returns current date - - `/day` - Returns current day of the week - - `/all` - Returns all time information in a single response - -**Remember**: -- Use proper package structure -- Use constructor injection instead of @Autowired -- Test all endpoints using your web browser or Postman -- Include appropriate error handling -- Use meaningful variable and method names +# Spring Boot Fundamentals Lab + +## How to run +1. Ensure you have Java 17+ and Maven installed. +2. Unzip this project. +3. In the project root, run: + ```bash + mvn spring-boot:run + ``` + +## Endpoints +- `GET /greeting/hello` +- `GET /greeting/hello/{name}` +- `GET /greeting/add/{num1}/{num2}` +- `GET /greeting/multiply/{num1}/{num2}` + +- `GET /weather/temperature` +- `GET /weather/condition` +- `GET /weather/wind` +- `GET /weather/all` + +- `GET /time/time` +- `GET /time/date` +- `GET /time/day` +- `GET /time/all` diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..1247fa2 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + com.example + spring-boot-fundamentals + 0.0.1-SNAPSHOT + spring-boot-fundamentals + Spring Boot Fundamentals Lab + + 17 + 3.3.3 + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + + 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/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java new file mode 100644 index 0000000..1bc9554 --- /dev/null +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -0,0 +1,11 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DemoApplication { + public static void main(String[] args) { + SpringApplication.run(DemoApplication.class, args); + } +} diff --git a/src/main/java/com/example/demo/controller/GreetingController.java b/src/main/java/com/example/demo/controller/GreetingController.java new file mode 100644 index 0000000..20de58e --- /dev/null +++ b/src/main/java/com/example/demo/controller/GreetingController.java @@ -0,0 +1,31 @@ +package com.example.demo.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/greeting") +public class GreetingController { + + @GetMapping("/hello") + public String hello() { + return "Hello World!"; + } + + @GetMapping("/hello/{name}") + public String helloName(@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/example/demo/controller/TimeController.java b/src/main/java/com/example/demo/controller/TimeController.java new file mode 100644 index 0000000..3182265 --- /dev/null +++ b/src/main/java/com/example/demo/controller/TimeController.java @@ -0,0 +1,43 @@ +package com.example.demo.controller; + +import com.example.demo.service.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.HashMap; +import java.util.Map; + +@RestController +@RequestMapping("/time") +public class TimeController { + + private final TimeService timeService; + + 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 getDayOfWeek() { + return timeService.getDayOfWeek(); + } + + @GetMapping("/all") + public Map getAll() { + Map map = new HashMap<>(); + map.put("time", timeService.getCurrentTime()); + map.put("date", timeService.getCurrentDate()); + map.put("day", timeService.getDayOfWeek()); + return map; + } +} diff --git a/src/main/java/com/example/demo/controller/WeatherController.java b/src/main/java/com/example/demo/controller/WeatherController.java new file mode 100644 index 0000000..a4bcb6e --- /dev/null +++ b/src/main/java/com/example/demo/controller/WeatherController.java @@ -0,0 +1,43 @@ +package com.example.demo.controller; + +import com.example.demo.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; + + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/temperature") + public int getTemperature() { + return weatherService.getTemperature(); + } + + @GetMapping("/condition") + public String getCondition() { + return weatherService.getCondition(); + } + + @GetMapping("/wind") + public int getWindSpeed() { + return weatherService.getWindSpeed(); + } + + @GetMapping("/all") + public Map getAll() { + Map map = new HashMap<>(); + map.put("temperature", weatherService.getTemperature()); + map.put("condition", weatherService.getCondition()); + map.put("windSpeed", weatherService.getWindSpeed()); + return map; + } +} diff --git a/src/main/java/com/example/demo/service/TimeService.java b/src/main/java/com/example/demo/service/TimeService.java new file mode 100644 index 0000000..f70ced3 --- /dev/null +++ b/src/main/java/com/example/demo/service/TimeService.java @@ -0,0 +1,25 @@ +package com.example.demo.service; + +import org.springframework.stereotype.Service; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.DayOfWeek; +import java.time.format.TextStyle; +import java.util.Locale; + +@Service +public class TimeService { + + public String getCurrentTime() { + return LocalDateTime.now().toLocalTime().toString(); + } + + public String getCurrentDate() { + return LocalDate.now().toString(); + } + + public String getDayOfWeek() { + DayOfWeek day = LocalDate.now().getDayOfWeek(); + return day.getDisplayName(TextStyle.FULL, Locale.ENGLISH); + } +} diff --git a/src/main/java/com/example/demo/service/WeatherService.java b/src/main/java/com/example/demo/service/WeatherService.java new file mode 100644 index 0000000..4c64541 --- /dev/null +++ b/src/main/java/com/example/demo/service/WeatherService.java @@ -0,0 +1,24 @@ +package com.example.demo.service; + +import org.springframework.stereotype.Service; +import java.util.Random; + +@Service +public class WeatherService { + private final Random random = new Random(); + + // -10..40 + public int getTemperature() { + return random.nextInt(51) - 10; + } + + public String getCondition() { + String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"}; + return conditions[random.nextInt(conditions.length)]; + } + + // 0..100 km/h (or any unit) + public int getWindSpeed() { + return random.nextInt(101); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e69de29