Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 25 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
50 changes: 50 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-boot-fundamentals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-fundamentals</name>
<description>Spring Boot Fundamentals Lab</description>
<properties>
<java.version>17</java.version>
<spring-boot.version>3.3.3</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
11 changes: 11 additions & 0 deletions src/main/java/com/example/demo/DemoApplication.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/example/demo/controller/GreetingController.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/example/demo/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -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<String, String> getAll() {
Map<String, String> map = new HashMap<>();
map.put("time", timeService.getCurrentTime());
map.put("date", timeService.getCurrentDate());
map.put("day", timeService.getDayOfWeek());
return map;
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/example/demo/controller/WeatherController.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> getAll() {
Map<String, Object> map = new HashMap<>();
map.put("temperature", weatherService.getTemperature());
map.put("condition", weatherService.getCondition());
map.put("windSpeed", weatherService.getWindSpeed());
return map;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/example/demo/service/TimeService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/example/demo/service/WeatherService.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Empty file.