Refactored IngredientController to use services.
This commit is contained in:
parent
707f88b70b
commit
04f58c22cc
2 changed files with 27 additions and 39 deletions
|
|
@ -5,8 +5,6 @@ import commons.ws.Topics;
|
|||
import commons.ws.messages.CreateIngredientMessage;
|
||||
import commons.ws.messages.DeleteIngredientMessage;
|
||||
import commons.ws.messages.UpdateIngredientMessage;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
|
|
@ -19,6 +17,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import server.database.IngredientRepository;
|
||||
import server.service.IngredientService;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.List;
|
||||
|
|
@ -42,12 +41,12 @@ import java.util.List;
|
|||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class IngredientController {
|
||||
private final IngredientRepository ingredientRepository;
|
||||
private final IngredientService ingredientService;
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
public IngredientController(IngredientRepository ingredientRepository,
|
||||
public IngredientController(IngredientService ingredientService,
|
||||
SimpMessagingTemplate messagingTemplate) {
|
||||
this.ingredientRepository = ingredientRepository;
|
||||
this.ingredientService = ingredientService;
|
||||
this.messagingTemplate = messagingTemplate;
|
||||
}
|
||||
|
||||
|
|
@ -75,15 +74,9 @@ public class IngredientController {
|
|||
@RequestParam Optional<Integer> page,
|
||||
@RequestParam Optional<Integer> limit
|
||||
) {
|
||||
List<Ingredient> ingredients = limit
|
||||
.map(l -> {
|
||||
return ingredientRepository.findAllByOrderByNameAsc(
|
||||
PageRequest.of(page.orElse(0), l)
|
||||
).toList();
|
||||
})
|
||||
.orElseGet(ingredientRepository::findAllByOrderByNameAsc);
|
||||
|
||||
return ResponseEntity.ok(ingredients);
|
||||
return limit
|
||||
.map(integer -> ResponseEntity.ok(ingredientService.findAll(page.orElse(0), integer)))
|
||||
.orElseGet(() -> ResponseEntity.ok(ingredientService.findAll()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -102,7 +95,7 @@ public class IngredientController {
|
|||
*/
|
||||
@GetMapping("/ingredients/{id}")
|
||||
public ResponseEntity<Ingredient> getIngredientById(@PathVariable Long id) {
|
||||
return ingredientRepository.findById(id)
|
||||
return ingredientService.findById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
|
@ -130,15 +123,13 @@ public class IngredientController {
|
|||
@PathVariable Long id,
|
||||
@RequestBody Ingredient updated
|
||||
) {
|
||||
if (!ingredientRepository.existsById(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
updated.setId(id);
|
||||
Ingredient savedIngredient = ingredientRepository.save(updated);
|
||||
messagingTemplate.convertAndSend(Topics.INGREDIENTS, new CreateIngredientMessage(savedIngredient));
|
||||
|
||||
return ResponseEntity.ok(savedIngredient);
|
||||
return ingredientService.update(id, updated)
|
||||
.map(saved -> {
|
||||
messagingTemplate.convertAndSend(Topics.INGREDIENTS, new CreateIngredientMessage(saved));
|
||||
return ResponseEntity.ok(saved);
|
||||
})
|
||||
.orElseGet(() -> ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -164,17 +155,12 @@ public class IngredientController {
|
|||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
Ingredient example = new Ingredient();
|
||||
example.name = ingredient.name;
|
||||
|
||||
if (ingredientRepository.existsById(ingredient.id) || ingredientRepository.exists(Example.of(example))) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
Ingredient saved = ingredientRepository.save(ingredient);
|
||||
return ingredientService.create(ingredient)
|
||||
.map(saved -> {
|
||||
messagingTemplate.convertAndSend(Topics.INGREDIENTS, new UpdateIngredientMessage(saved));
|
||||
|
||||
return ResponseEntity.ok(saved);
|
||||
})
|
||||
.orElseGet(() -> ResponseEntity.badRequest().build());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -193,13 +179,11 @@ public class IngredientController {
|
|||
*/
|
||||
@DeleteMapping("/ingredients/{id}")
|
||||
public ResponseEntity<Boolean> deleteIngredient(@PathVariable Long id) {
|
||||
if (!ingredientRepository.existsById(id)) {
|
||||
if (!ingredientService.delete(id)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
ingredientRepository.deleteById(id);
|
||||
messagingTemplate.convertAndSend(Topics.INGREDIENTS, new DeleteIngredientMessage(id));
|
||||
|
||||
return ResponseEntity.ok(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|||
import org.springframework.test.context.ActiveProfiles;
|
||||
import server.database.IngredientRepository;
|
||||
import server.WebSocketConfig;
|
||||
import server.service.IngredientService;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
|
@ -20,10 +21,11 @@ import java.util.stream.Stream;
|
|||
|
||||
@DataJpaTest
|
||||
@ActiveProfiles("mock-data-test")
|
||||
@Import(WebSocketConfig.class)
|
||||
@Import({WebSocketConfig.class, IngredientService.class})
|
||||
public class IngredientControllerTest {
|
||||
private final SimpMessagingTemplate template;
|
||||
private final IngredientRepository ingredientRepository;
|
||||
private final IngredientService ingredientService;
|
||||
private IngredientController controller;
|
||||
|
||||
private static final double PROTEIN_BASE = 1.0;
|
||||
|
|
@ -42,8 +44,10 @@ public class IngredientControllerTest {
|
|||
|
||||
@Autowired
|
||||
public IngredientControllerTest(IngredientRepository ingredientRepository,
|
||||
IngredientService ingredientService,
|
||||
SimpMessagingTemplate template) {
|
||||
this.ingredientRepository = ingredientRepository;
|
||||
this.ingredientService = ingredientService;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
|
|
@ -59,7 +63,7 @@ public class IngredientControllerTest {
|
|||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
controller = new IngredientController(ingredientRepository, template);
|
||||
controller = new IngredientController(ingredientService, template);
|
||||
|
||||
this.createInitialIngredients();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue