Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 36812d3

Browse files
committedAug 7, 2023
fix(server): add supplier module
1 parent 55385b0 commit 36812d3

File tree

7 files changed

+116
-164
lines changed

7 files changed

+116
-164
lines changed
 

‎resources/activities.json

Lines changed: 0 additions & 128 deletions
This file was deleted.

‎resources/suppliers.json

Lines changed: 0 additions & 34 deletions
This file was deleted.

‎server/settings.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = "activity"
1+
rootProject.name = "main.java.com.getyourguide"

‎server/src/main/java/com/getyourguide/activity/GetYourGuideApplication.java renamed to ‎server/src/main/java/com/getyourguide/GetYourGuideApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.getyourguide.activity;
1+
package com.getyourguide;
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.getyourguide.supplier.controller;
2+
3+
import com.getyourguide.supplier.model.Supplier;
4+
import com.getyourguide.supplier.service.SupplierService;
5+
import java.io.IOException;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
12+
import org.springframework.web.bind.annotation.*;
13+
14+
@Controller
15+
@CrossOrigin(origins = "*", allowedHeaders = "*")
16+
public class SupplierController {
17+
18+
@Autowired
19+
private SupplierService supplierService;
20+
21+
@GetMapping("/suppliers")
22+
public ResponseEntity<List<Supplier>> suppliers() {
23+
try {
24+
List<Supplier> suppliers = supplierService.getAllSuppliers();
25+
return ResponseEntity.ok(suppliers);
26+
} catch (IOException e) {
27+
throw new RuntimeException(e);
28+
}
29+
}
30+
31+
@GetMapping("/suppliers/{id}")
32+
public ResponseEntity<Supplier> getSupplierById(@PathVariable long id) {
33+
try {
34+
Optional<Supplier> matchedSupplier = supplierService.getSupplierById(id);
35+
36+
// Return the controller if found, or a 404 response if not found
37+
if (matchedSupplier.isPresent()) {
38+
return ResponseEntity.ok(matchedSupplier.get());
39+
} else {
40+
return ResponseEntity.notFound().build();
41+
}
42+
} catch (IOException e) {
43+
throw new RuntimeException(e);
44+
}
45+
}
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.getyourguide.supplier.model;
2+
3+
import lombok.*;
4+
5+
@Data
6+
@Getter
7+
@Setter
8+
@AllArgsConstructor
9+
@NoArgsConstructor
10+
public class Supplier {
11+
12+
private Long id;
13+
private String name;
14+
private String address;
15+
private String zip;
16+
private String city;
17+
private String country;
18+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.getyourguide.supplier.service;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.getyourguide.supplier.model.Supplier;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import java.util.stream.Collectors;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.core.io.Resource;
13+
import org.springframework.core.io.ResourceLoader;
14+
import org.springframework.stereotype.Service;
15+
16+
@Service
17+
public class SupplierService {
18+
19+
private final ResourceLoader resourceLoader;
20+
21+
@Autowired
22+
public SupplierService(ResourceLoader resourceLoader) {
23+
this.resourceLoader = resourceLoader;
24+
}
25+
26+
public List<Supplier> getAllSuppliers() throws IOException {
27+
List<Supplier> suppliers = readSuppliersFromJsonFile();
28+
return suppliers;
29+
}
30+
31+
public Optional<Supplier> getSupplierById(long id) throws IOException {
32+
List<Supplier> suppliers = readSuppliersFromJsonFile();
33+
return suppliers
34+
.stream()
35+
.filter(supplier -> supplier.getId() == id)
36+
.findFirst();
37+
}
38+
39+
private List<Supplier> readSuppliersFromJsonFile() throws IOException {
40+
ObjectMapper objectMapper = new ObjectMapper();
41+
Resource resource = resourceLoader.getResource(
42+
"classpath:static/suppliers.json"
43+
);
44+
InputStream inputStream = resource.getInputStream();
45+
return objectMapper.readValue(
46+
inputStream,
47+
new TypeReference<List<Supplier>>() {}
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)
Please sign in to comment.