Skip to content

Commit

Permalink
Merge pull request #35 from AleksandraKrasteva/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
AleksandraKrasteva authored Jun 22, 2024
2 parents daf5a76 + 5fe05b0 commit 466e792
Show file tree
Hide file tree
Showing 34 changed files with 73 additions and 142 deletions.
6 changes: 1 addition & 5 deletions JourneyService/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}
Expand All @@ -43,7 +41,6 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

test {
finalizedBy jacocoTestReport
}
Expand Down Expand Up @@ -78,5 +75,4 @@ task integrationTests(type: Test) {
}
description 'run unit tests'
finalizedBy jacocoTestReport

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
@Service
public interface JourneyService {
JourneyEntity createJourney(String authorUsername);

List<JourneyEntity> getAllForUser(String authorUsername);

JourneyEntity getJourneyById(long journeyId);

void deleteJourneyById(long journeyId);

void deleteJourneysForAuthor(String authorUsername);
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public List<JourneyEntity> getAllForUser(String authorUsername) {
List<JourneyEntity> journeys = journeyRepository.getJourneyEntitiesByAuthorUsernameIs(authorUsername);
return journeys;
}

@Override
public JourneyEntity getJourneyById(long journeyId) {
Optional<JourneyEntity> journey = journeyRepository.findById(journeyId);
Expand All @@ -73,7 +72,6 @@ public JourneyEntity getJourneyById(long journeyId) {
}
return null;
}

@Override
public void deleteJourneyById(long journeyId) {
Optional<JourneyEntity> journey = journeyRepository.findById(journeyId);
Expand All @@ -85,6 +83,5 @@ public void deleteJourneyById(long journeyId) {
@Override
public void deleteJourneysForAuthor(String authorUsername) {
journeyRepository.deleteAllByAuthorUsernameIs(authorUsername);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,25 @@ public class JourneyController {

@PostMapping("/create-journey")
public ResponseEntity<JourneyEntity> createJourney(@RequestBody String username){
System.out.println(username);
System.out.println(username.substring(1, username.length()-1));
String usernameFormed = username.substring(1, username.length()-1);
JourneyEntity journey = journeyService.createJourney(usernameFormed);
return ResponseEntity.ok().body(journey);
}

@GetMapping("/view-journey/{id}")
public ResponseEntity<JourneyEntity> getJourneyById(@PathVariable(value = "id") long id) {
JourneyEntity journey = journeyService.getJourneyById(id);
return ResponseEntity.ok().body(journey);
}

@GetMapping("/view-journeys-user/{username}")
public ResponseEntity<List<JourneyEntity>> getJourneysForUser(@PathVariable(value = "username") String username) {
List<JourneyEntity> journeys = journeyService.getAllForUser(username);
return ResponseEntity.ok().body(journeys);
}

@DeleteMapping("/delete-journey/{id}")
public ResponseEntity deleteJourneyById(@PathVariable(value = "id") long id) {
journeyService.deleteJourneyById(id);
return ResponseEntity.ok().build();
}

@DeleteMapping("/delete-journeys-user/{username}")
public ResponseEntity deleteJourneysForUser(@PathVariable(value = "username") String username) {
journeyService.deleteJourneysForAuthor(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

@Configuration
public class RabbitMQConfig {

static final String fanoutExchangeName = "delete-post";
static final String postsQueueName = "delete-post-for-journey";
@Bean
Expand All @@ -24,5 +23,4 @@ public Queue postQueue() {
public Binding postsBinding() {
return BindingBuilder.bind(postQueue()).to(deletePostExchange());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@

@Component
public class RabbitMQProducer {

@Autowired

private RabbitTemplate rabbitTemplate;

@Autowired
private FanoutExchange deletePostExchange;

// Routing key is not set, as the exchange is fanout
public void sendDeletePostMessage(long journeyId) {
System.out.println("In rabbitmq ");
System.out.println("In send delete post message");
System.out.println(journeyId);

rabbitTemplate.convertAndSend(
deletePostExchange.getName(), "", journeyId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
@Component
@AllArgsConstructor
public class RabbitMQReceiver {

private final JourneyService journeyService;
@RabbitListener(queues = "delete-journey-for-post")
public void receiveMessage(int journeyId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@AllArgsConstructor
@NoArgsConstructor
public class JourneyEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
@Repository

public interface JourneyRepository extends JpaRepository<JourneyEntity, Long> {

List<JourneyEntity> getJourneyEntitiesByAuthorUsernameIs(String authorUsername);
@Transactional
void deleteAllByIdIs(long journeyId);
@Transactional
void deleteAllByAuthorUsernameIs(String authorUsername);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Testcontainers()
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
Expand All @@ -42,7 +41,6 @@ public class DeletePostAndJourneyIntegration {
@Autowired
private JourneyService journeyService;
public static Network network = Network.newNetwork();

static RabbitMQContainer rabbit = new RabbitMQContainer(DockerImageName
.parse("ghcr.io/aleksandrakrasteva/rabbitmq:dev")
.asCompatibleSubstituteFor("rabbitmq"))
Expand All @@ -54,7 +52,6 @@ public class DeletePostAndJourneyIntegration {
static GenericContainer postService = new GenericContainer((DockerImageName
.parse("ghcr.io/aleksandrakrasteva/post-service:dev")))
.withImagePullPolicy(PullPolicy.alwaysPull());

@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.rabbitmq.host", rabbit::getHost);
Expand All @@ -65,7 +62,6 @@ static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.username", journeyPostgres::getUsername);
registry.add("spring.datasource.password", journeyPostgres::getPassword);
}

@BeforeAll
public static void setPostService(){
postsPostgres.withNetwork(network).withNetworkAliases("postdb").start();
Expand All @@ -87,7 +83,6 @@ public static void setPostService(){
final String logs = postService.getLogs();
System.out.println(logs);
}

@BeforeEach
void insertTestData() {
JourneyEntity journey = journeyService.createJourney("testUser");
Expand All @@ -103,21 +98,15 @@ void insertTestData() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Create the HttpEntity
HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);

// Send the POST request
ResponseEntity<String> response = restTemplate.exchange(
createPostEndpoint,
HttpMethod.POST,
requestEntity,
String.class
);

System.out.println("HERE IS THE RETURN" + response);

}

@Test
@Tag("integration")
void deletePostsForUserRabbitMQListenerTest() {
Expand All @@ -136,19 +125,14 @@ void deletePostsForUserRabbitMQListenerTest() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);

// Create the HttpEntity
HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);

// Send the POST request
ResponseEntity<String> response = restTemplate.exchange(
deletePostEndpoint,
HttpMethod.DELETE,
requestEntity,
String.class);

final String logs = postService.getLogs();
System.out.println(logs);

Optional<JourneyEntity> after = repository.findById(1L);

assertEquals(true, before.isPresent() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
@Service
public interface PostService {
List<Post> getPostsForUser(String userEmail);

List<Post> getAllPosts();
void deletePostById(DeletePostRequest request);
CreatePostResponse createPost(CreatePostRequest request);

void deletePostsForUser(DeletePostsRequest userEmail);

Long updatePostById(UpdatePostRequest request);

void deletePostForJourney(long journeyId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
public interface ReactionService {
void deleteReaction(long reactionId);
long createReaction(CreateReactionRequest request);

void deleteAllReactionsForPost(long postId);
void deleteAllReactionsFromUser(String username);

List<ReactionEntity> getReactionsForUser(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
@Service
@AllArgsConstructor
public class PostServiceImpl implements PostService {

private final PostRepository postRepository;
private final ReactionRepository reactionRepository;
private final RabbitMQProducer producer;
Expand All @@ -27,7 +26,6 @@ public List<Post> getPostsForUser(String username) {
stream().map(this::convertToPost).collect(Collectors.toList());
return posts;
}

private Post convertToPost(PostEntity post) {
List<ReactionEntity> reactionEntities = reactionRepository.getReactionEntitiesByPostIdIs(post.getId());
List<Reaction> reactions = reactionEntities.stream().map(this::convertReactions).collect(Collectors.toList());
Expand All @@ -36,7 +34,6 @@ private Post convertToPost(PostEntity post) {
.text(post.getText()).username(post.getUsername()).reactions(reactions).build();
return postDto;
}

private Reaction convertReactions(ReactionEntity reaction){
return Reaction.builder().postId(reaction.getPostId())
.id(reaction.getId())
Expand All @@ -47,7 +44,6 @@ public List<Post> getAllPosts() {
List<Post> posts = postRepository.findAll().
stream().map(this::convertToPost).collect(Collectors.toList());
return posts;

}
@Override
public void deletePostById(DeletePostRequest postRequest) {
Expand Down Expand Up @@ -80,7 +76,6 @@ public void deletePostsForUser(DeletePostsRequest request) {
postRepository.deletePostEntitiesByUsernameIs(request.getUsername());
reactionRepository.deleteAllByCreatorIs(request.getUsername());
}

@Override
public Long updatePostById(UpdatePostRequest request) {
Optional<PostEntity> post = postRepository.findById(request.getPostId());
Expand All @@ -91,7 +86,6 @@ public Long updatePostById(UpdatePostRequest request) {
}
return null;
}

@Override
public void deletePostForJourney(long journeyId) {
Optional<PostEntity> post = postRepository.getPostEntityByJourneyIdIs(journeyId);
Expand All @@ -100,6 +94,5 @@ public void deletePostForJourney(long journeyId) {
DeletePostRequest request = DeletePostRequest.builder().deleteJourney(false).postId(post.get().getId()).build();
deletePostById(request);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
public class ReactionServiceImpl implements ReactionService {
private final ReactionRepository reactionRepository;
private final PostRepository postRepository;

@Override
public void deleteReaction(long reactionId) {
System.out.println(reactionId);
reactionRepository.deleteById(reactionId);
}
@Override
Expand All @@ -29,19 +27,14 @@ public long createReaction(CreateReactionRequest request) {
ReactionEntity returned = reactionRepository.save(reaction);
return returned.getId();
}

@Override
public void deleteAllReactionsForPost(long postId) {
reactionRepository.deleteAllByPostIdIs(postId);

}

@Override
public void deleteAllReactionsFromUser(String username) {
reactionRepository.deleteAllByCreatorIs(username);

}

@Override
public List<ReactionEntity> getReactionsForUser(String username) {
return reactionRepository.getReactionEntitiesByCreatorIs(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class PostController {

private final PostService postService;

@PostMapping("/create-post")
public ResponseEntity createPost(@RequestBody CreatePostRequest request) {
CreatePostResponse response = postService.createPost(request);
Expand All @@ -36,7 +35,6 @@ public ResponseEntity deletePost(@RequestBody DeletePostRequest postRequest) {
postService.deletePostById(postRequest);
return ResponseEntity.ok().build();
}

@DeleteMapping("/delete-all-posts")
public ResponseEntity deleteAllPostsForUser(@RequestBody DeletePostsRequest postsRequest) {
postService.deletePostsForUser(postsRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public ResponseEntity deleteReaction(@PathVariable(value = "id") long reactionId
reactionService.deleteReaction(reactionId);
return ResponseEntity.ok().build();
}

@DeleteMapping("/delete-reactions/{username}")
public ResponseEntity deleteReactions(@PathVariable(value = "username") String username) {
reactionService.deleteAllReactionsFromUser(username);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public class CreatePostRequest {
private String text;
private String username;
private long journeyId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
@Builder
public class CreatePostResponse {
private long id;
}
}
Loading

0 comments on commit 466e792

Please sign in to comment.