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