Skip to content

Commit c3559e1

Browse files
committed
Lab 4 final
1 parent df6f0cf commit c3559e1

File tree

8 files changed

+210
-0
lines changed

8 files changed

+210
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ro.unibuc.lab04.main;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class App {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(App.class);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package ro.unibuc.lab04.main;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.bind.annotation.*;
6+
import ro.unibuc.lab04.main.dto.DishDto;
7+
import ro.unibuc.lab04.main.service.DishService;
8+
9+
import java.util.List;
10+
11+
@RestController
12+
@RequestMapping("/dishes")
13+
public class DishController {
14+
15+
private final DishService dishService;
16+
17+
public DishController(DishService dishService) {
18+
this.dishService = dishService;
19+
}
20+
21+
@GetMapping
22+
public List<DishDto> getDishes() {
23+
return dishService.getAll();
24+
}
25+
26+
@PostMapping
27+
@ResponseStatus(HttpStatus.CREATED)
28+
public DishDto addDish(@RequestBody DishDto dto) {
29+
return dishService.addDish(dto);
30+
}
31+
32+
@GetMapping("/{dishId}")
33+
public ResponseEntity<DishDto> getById(@PathVariable("dishId") int dishId) {
34+
return ResponseEntity.of(dishService.getById(dishId));
35+
}
36+
37+
@DeleteMapping("/{dishId}")
38+
public void deleteById(@PathVariable("dishId") int dishId) {
39+
dishService.deleteById(dishId);
40+
}
41+
42+
@PutMapping("/{dishId}")
43+
public ResponseEntity<DishDto> updateDish(@RequestBody DishDto body, @PathVariable("dishId") int dishId) {
44+
return ResponseEntity.of(dishService.update(new DishDto(dishId, body.name(), body.price())));
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ro.unibuc.lab04.main.dto;
2+
3+
public record DishDto(Integer id, String name, int price) {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package ro.unibuc.lab04.main.model;
2+
3+
public record Dish(Integer id, String name, int price) {
4+
5+
public Dish(String name, int price) {
6+
this(null, name, price);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ro.unibuc.lab04.main.repo;
2+
3+
import ro.unibuc.lab04.main.model.Dish;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public interface DishRepository {
9+
10+
Dish add(Dish dish);
11+
12+
List<Dish> findAll();
13+
14+
Dish removeById(int id);
15+
16+
Dish update(Dish dish);
17+
18+
Optional<Dish> findById(int id);
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package ro.unibuc.lab04.main.repo;
2+
3+
import org.springframework.stereotype.Repository;
4+
import ro.unibuc.lab04.main.model.Dish;
5+
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Optional;
10+
11+
@Repository
12+
public class InMemoryDishRepository implements DishRepository {
13+
14+
private final Map<Integer, Dish> dishById = new HashMap<>();
15+
private int idCounter = 0;
16+
17+
@Override
18+
public Dish add(Dish dish) {
19+
final var storedDish = new Dish(++idCounter, dish.name(), dish.price());
20+
dishById.put(storedDish.id(), storedDish);
21+
return storedDish;
22+
}
23+
24+
@Override
25+
public List<Dish> findAll() {
26+
return dishById.values().stream().toList();
27+
}
28+
29+
@Override
30+
public Dish removeById(int id) {
31+
return dishById.remove(id);
32+
}
33+
34+
@Override
35+
public Dish update(Dish dish) {
36+
return dishById.computeIfPresent(dish.id(), (k, v) -> dish);
37+
}
38+
39+
@Override
40+
public Optional<Dish> findById(int id) {
41+
return Optional.ofNullable(dishById.get(id));
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package ro.unibuc.lab04.main.service;
2+
3+
import ro.unibuc.lab04.main.dto.DishDto;
4+
5+
import java.util.List;
6+
import java.util.Optional;
7+
8+
public interface DishService {
9+
10+
DishDto addDish(DishDto dish);
11+
12+
List<DishDto> getAll();
13+
14+
Optional<DishDto> getById(int dishId);
15+
16+
Optional<DishDto> update(DishDto dish);
17+
18+
void deleteById(int dishId);
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package ro.unibuc.lab04.main.service;
2+
3+
import org.springframework.stereotype.Service;
4+
import ro.unibuc.lab04.main.dto.DishDto;
5+
import ro.unibuc.lab04.main.model.Dish;
6+
import ro.unibuc.lab04.main.repo.DishRepository;
7+
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
@Service
12+
public class SimpleDishService implements DishService {
13+
14+
private final DishRepository dishRepo;
15+
16+
public SimpleDishService(DishRepository dishRepo) {
17+
this.dishRepo = dishRepo;
18+
}
19+
20+
@Override
21+
public DishDto addDish(DishDto dish) {
22+
final var savedDish = dishRepo.add(new Dish(dish.name(), dish.price()));
23+
return mapToDto(savedDish);
24+
}
25+
26+
@Override
27+
public List<DishDto> getAll() {
28+
return dishRepo.findAll().stream()
29+
.map(SimpleDishService::mapToDto)
30+
.toList();
31+
}
32+
33+
@Override
34+
public Optional<DishDto> getById(int dishId) {
35+
return dishRepo.findById(dishId).map(SimpleDishService::mapToDto);
36+
}
37+
38+
@Override
39+
public Optional<DishDto> update(DishDto dish) {
40+
return Optional.ofNullable(dishRepo.update(mapToModel(dish))).map(SimpleDishService::mapToDto);
41+
}
42+
43+
@Override
44+
public void deleteById(int dishId) {
45+
dishRepo.removeById(dishId);
46+
}
47+
48+
private static DishDto mapToDto(Dish dish) {
49+
return new DishDto(dish.id(), dish.name(), dish.price());
50+
}
51+
52+
private static Dish mapToModel(DishDto dto) {
53+
return new Dish(dto.id(), dto.name(), dto.price());
54+
}
55+
}

0 commit comments

Comments
 (0)