feat: topics, recipe messages
This commit is contained in:
parent
ee16265cb8
commit
8e24e47813
7 changed files with 160 additions and 7 deletions
|
|
@ -2,10 +2,15 @@ package server.api;
|
|||
|
||||
import commons.Recipe;
|
||||
|
||||
import commons.ws.Topics;
|
||||
import commons.ws.messages.CreateRecipeMessage;
|
||||
import commons.ws.messages.DeleteRecipeMessage;
|
||||
import commons.ws.messages.UpdateRecipeMessage;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
|
|
@ -25,9 +30,11 @@ import java.util.Optional;
|
|||
@RequestMapping("/api")
|
||||
public class RecipeController {
|
||||
private final RecipeRepository recipeRepository; // JPA repository used in this controller
|
||||
private final SimpMessagingTemplate messagingTemplate;
|
||||
|
||||
public RecipeController(RecipeRepository recipeRepository) {
|
||||
public RecipeController(RecipeRepository recipeRepository, SimpMessagingTemplate messagingTemplate) {
|
||||
this.recipeRepository = recipeRepository;
|
||||
this.messagingTemplate = messagingTemplate;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -61,6 +68,7 @@ public class RecipeController {
|
|||
PageRequest.of(0, limit.get())
|
||||
).toList());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(recipeRepository.findAll());
|
||||
}
|
||||
|
||||
|
|
@ -76,9 +84,10 @@ public class RecipeController {
|
|||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
// TODO: Send WS update to all subscribers with the updated recipe
|
||||
Recipe saved = recipeRepository.save(recipe);
|
||||
messagingTemplate.convertAndSend(Topics.RECIPES, new UpdateRecipeMessage(saved));
|
||||
|
||||
return ResponseEntity.ok(recipeRepository.save(recipe));
|
||||
return ResponseEntity.ok(saved);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -104,9 +113,10 @@ public class RecipeController {
|
|||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
|
||||
// TODO: Send WS update to all subscribers with the new recipe
|
||||
Recipe saved = recipeRepository.save(recipe);
|
||||
messagingTemplate.convertAndSend(Topics.RECIPES, new CreateRecipeMessage(saved));
|
||||
|
||||
return ResponseEntity.ok(recipeRepository.save(recipe));
|
||||
return ResponseEntity.ok(saved);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -124,7 +134,8 @@ public class RecipeController {
|
|||
}
|
||||
recipeRepository.deleteById(id);
|
||||
|
||||
// TODO: Send WS update to propagate deletion
|
||||
messagingTemplate.convertAndSend(Topics.RECIPES, new DeleteRecipeMessage(id));
|
||||
|
||||
return ResponseEntity.ok(true);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import org.junit.jupiter.api.TestInfo;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import server.database.RecipeRepository;
|
||||
|
||||
|
|
@ -30,6 +31,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||
// This config uses an in-memory database
|
||||
@ActiveProfiles("mock-data-test")
|
||||
public class RecipeControllerTest {
|
||||
|
||||
@Autowired
|
||||
private SimpMessagingTemplate template;
|
||||
|
||||
private RecipeController controller;
|
||||
private List<Recipe> recipes;
|
||||
private final RecipeRepository recipeRepository;
|
||||
|
|
@ -48,7 +53,7 @@ public class RecipeControllerTest {
|
|||
.range(0, NUM_RECIPES)
|
||||
.mapToObj(x -> new Recipe(null, "Recipe " + x, List.of(), List.of()))
|
||||
.toList();
|
||||
controller = new RecipeController(recipeRepository);
|
||||
controller = new RecipeController(recipeRepository, template);
|
||||
Set<String> tags = info.getTags();
|
||||
List<Long> ids = new ArrayList<>();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue