Skip to content

Commit

Permalink
Feature/attributes page issue #251 (#296)
Browse files Browse the repository at this point in the history
* src/main/java/com/savvato/tribeapp/controllers/AttributesAPIController.java
-modify signature to accept to service
 src/main/java/com/savvato/tribeapp/repositories/ToBeReviewedRepository.java
-add a method to search for phrases by ID
 src/main/java/com/savvato/tribeapp/services/AttributesService.java
-modified the interface signature
 src/main/java/com/savvato/tribeapp/services/AttributesServiceImpl.java
-modified getAttrbrutes to call getTobereviewed to get pending attributes
-wrote function to get pending attributes
 src/main/resources/db/migration/changelog-202410240839.xml
-added test data to test new function
 src/main/resources/db/migration/changelog-master.xml
-added new changelog to implemented test data
 src/test/java/com/savvato/tribeapp/integration/controllers/AttributesAPIIT.java
-commented out broken tests
 src/test/java/com/savvato/tribeapp/unit/services/AttributesServiceImplTest.java
-commented out broken tests

* src/main/java/com/savvato/tribeapp/services/AttributesService.java
-modified the return for getAttributes to return a map of empty instead of a collection of empty
	modified:   src/main/java/com/savvato/tribeapp/services/AttributesServiceImpl.java
-added getPhrasesToBeReviewedByUserId to the implementation
	modified:   src/test/java/com/savvato/tribeapp/integration/controllers/AttributesAPIIT.java
-fixed test for the controller
	modified:   src/test/java/com/savvato/tribeapp/unit/services/AttributesServiceImplTest.java
-fixed test for get attributes
  • Loading branch information
loretdemolas authored Nov 19, 2024
1 parent 1e9754b commit 67cc37e
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
Expand Down Expand Up @@ -54,15 +55,17 @@ public class AttributesAPIController {

@GetAttributesForUser
@GetMapping("/{userId}")
public ResponseEntity<List<AttributeDTO>> getAttributesForUser(
public ResponseEntity<Map<String, List<AttributeDTO>>> getAttributesForUser(
@Parameter(description = "User ID of user", example = "1") @PathVariable Long userId) {

Optional<List<AttributeDTO>> opt = attributesService.getAttributesByUserId(userId);
Optional<Map<String, List<AttributeDTO>>> opt = attributesService.getAttributesByUserId(userId);

if (opt.isPresent()) return ResponseEntity.status(HttpStatus.OK).body(opt.get());
else return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
if (opt.isPresent()) {
return ResponseEntity.status(HttpStatus.OK).body(opt.get());
} else {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}


@PostMapping("/update")
public ResponseEntity<GenericResponseDTO> uPhraseSequences(@RequestBody @Valid PhraseSequenceRequest req) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.savvato.tribeapp.repositories;

import com.savvato.tribeapp.entities.ToBeReviewed;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -19,4 +20,5 @@ public interface ToBeReviewedRepository extends CrudRepository<ToBeReviewed, Lon

Optional<ToBeReviewed> findByAdverbAndVerbAndNounAndPreposition(String adverb, String verb, String noun, String preposition);

Optional<ToBeReviewed> findById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import com.savvato.tribeapp.dto.AttributeDTO;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public interface AttributesService {

Optional<List<AttributeDTO>> getAttributesByUserId(Long id);
Optional<Map<String, List<AttributeDTO>>> getAttributesByUserId(Long id);

void updatePhraseSequences(long userId, PhraseSequenceDataRequest phrase);

boolean loadSequence(PhraseSequenceRequest phrases);

List<AttributeDTO> getPhrasesToBeReviewedByUserId(Long userId);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import com.savvato.tribeapp.dto.AttributeDTO;
import com.savvato.tribeapp.dto.PhraseDTO;
import com.savvato.tribeapp.entities.PhraseSequence;
import com.savvato.tribeapp.entities.ToBeReviewed;
import com.savvato.tribeapp.repositories.PhraseSequenceRepository;
import com.savvato.tribeapp.repositories.ReviewSubmittingUserRepository;
import com.savvato.tribeapp.repositories.ToBeReviewedRepository;
import com.savvato.tribeapp.repositories.UserPhraseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -26,8 +29,14 @@ public class AttributesServiceImpl implements AttributesService {
@Autowired
private PhraseSequenceRepository phraseSequenceRepository;

@Autowired
private ToBeReviewedRepository toBeReviewedRepository;

@Autowired
ReviewSubmittingUserRepository reviewSubmittingUserRepository;

@Override
public Optional<List<AttributeDTO>> getAttributesByUserId(Long userId) {
public Optional<Map<String, List<AttributeDTO>>> getAttributesByUserId(Long userId) {

List<PhraseSequence> phraseSequences = phraseSequenceRepository.findByUserIdOrderByPosition(userId);

Expand All @@ -40,6 +49,10 @@ public Optional<List<AttributeDTO>> getAttributesByUserId(Long userId) {
// Get all user phrases as phraseDTOs
Optional<Map<PhraseDTO, Integer>> optUserPhraseDTOs = phraseService.getPhraseInformationByUserId(userId);

Map<String, List<AttributeDTO>> resultMap = new HashMap<>();
resultMap.put("attributes", Collections.emptyList());
resultMap.put("pendingAttributes", Collections.emptyList());

// If there are phrases, build DTO and add to attributes list
if (optUserPhraseDTOs.isPresent()) {
Map<PhraseDTO, Integer> phraseDTOUserCountMap = optUserPhraseDTOs.get();
Expand All @@ -62,12 +75,40 @@ public Optional<List<AttributeDTO>> getAttributesByUserId(Long userId) {
})
.collect(Collectors.toList());
attributes.sort(Comparator.comparingLong(a -> (a.phrase.id)));
List<AttributeDTO> pendingAttributes = getPhrasesToBeReviewedByUserId(userId);

resultMap.put("attributes", attributes);
resultMap.put("pendingAttributes", pendingAttributes);

return Optional.of(attributes);
return Optional.of(resultMap);
}

// If no phrases found, return an empty list
return Optional.of(Collections.emptyList());
return Optional.of(resultMap);
}

public List<AttributeDTO> getPhrasesToBeReviewedByUserId(Long userId) {
List<Long> toBeReviewedIds = reviewSubmittingUserRepository.findToBeReviewedIdByUserId(userId);
List<ToBeReviewed> toBeReviewedList = new ArrayList<>();
for (Long id : toBeReviewedIds) {
Optional<ToBeReviewed> optionalToBeReviewed = toBeReviewedRepository.findById(id);
optionalToBeReviewed.ifPresent(toBeReviewedList::add);
}

return toBeReviewedList.stream()
.map(toBeReviewed -> AttributeDTO.builder()
.phrase(PhraseDTO.builder()
.id(toBeReviewed.getId())
.verb(toBeReviewed.getVerb())
.adverb(toBeReviewed.getAdverb())
.preposition(toBeReviewed.getPreposition())
.noun(toBeReviewed.getNoun())
.build())
.userCount(0)
.sequence(0)
.build()
)
.collect(Collectors.toList());
}

//create a senario where false is returned
Expand All @@ -79,6 +120,7 @@ public boolean loadSequence(PhraseSequenceRequest sequence){

return true;
}

@Transactional
public void updatePhraseSequences(long userId, PhraseSequenceDataRequest phrase) {

Expand Down
58 changes: 58 additions & 0 deletions src/main/resources/db/migration/changelog-202410240839.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="202410240839" author="Loretdemolas" context="test">
<comment>Insert test data for user 2 into to_be_reviewed and review_decision tables</comment>

<!-- Insert test data into to_be_reviewed table -->
<insert tableName="to_be_reviewed">
<column name="id" valueNumeric="1" />
<column name="has_been_groomed" valueNumeric="0" />
<column name="adverb" value="quickly" />
<column name="verb" value="jumps" />
<column name="noun" value="frog" />
<column name="preposition" value="over" />
</insert>

<insert tableName="to_be_reviewed">
<column name="id" valueNumeric="2" />
<column name="has_been_groomed" valueNumeric="1" />
<column name="adverb" value="silently" />
<column name="verb" value="sneaks" />
<column name="noun" value="cat" />
<column name="preposition" value="under" />
</insert>

<insert tableName="to_be_reviewed">
<column name="id" valueNumeric="3" />
<column name="has_been_groomed" valueNumeric="0" />
<column name="adverb" value="lazily" />
<column name="verb" value="sleeps" />
<column name="noun" value="dog" />
<column name="preposition" value="on" />
</insert>

<!-- Insert test data into review_submitting_user table -->
<insert tableName="review_submitting_user">
<column name="user_id" valueNumeric="2" />
<column name="to_be_reviewed_id" valueNumeric="1" />
</insert>

<insert tableName="review_submitting_user">
<column name="user_id" valueNumeric="2" />
<column name="to_be_reviewed_id" valueNumeric="2" />
</insert>

<insert tableName="review_submitting_user">
<column name="user_id" valueNumeric="2" />
<column name="to_be_reviewed_id" valueNumeric="3" />
</insert>

</changeSet>

</databaseChangeLog>

1 change: 1 addition & 0 deletions src/main/resources/db/migration/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@
<include file="changelog-202404091617.xml" relativeToChangelogFile="true"/>
<include file="changelog-202405071617.xml" relativeToChangelogFile="true"/>
<include file="changelog-202410040829.xml" relativeToChangelogFile="true"/>
<include file="changelog-202410240839.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>

Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,11 @@ public void getAttributesForUserWhenAttributesFound() throws Exception {
.build();
AttributeDTO attributeDTO = AttributeDTO.builder().phrase(phraseDTO).build();
List<AttributeDTO> expectedAttributes = List.of(attributeDTO);
Optional<List<AttributeDTO>> opt = Optional.of(expectedAttributes);
Map<String, List<AttributeDTO>> expectedAttributesMap = Map.of("attributes", expectedAttributes);

Optional<Map<String, List<AttributeDTO>>> opt = Optional.of(expectedAttributesMap);
when(attributesService.getAttributesByUserId(anyLong())).thenReturn(opt);

MvcResult result =
this.mockMvc
.perform(
Expand All @@ -117,12 +120,11 @@ public void getAttributesForUserWhenAttributesFound() throws Exception {
.andExpect(status().isOk())
.andReturn();

Type attributeDTOListType = new TypeToken<List<AttributeDTO>>() {
}.getType();
Type attributeDTOMapType = new TypeToken<Map<String, List<AttributeDTO>>>() {}.getType();
Map<String, List<AttributeDTO>> actualAttributesMap = gson.fromJson(
result.getResponse().getContentAsString(), attributeDTOMapType);

List<AttributeDTO> actualAttributes =
gson.fromJson(result.getResponse().getContentAsString(), attributeDTOListType);
assertThat(actualAttributes).usingRecursiveComparison().isEqualTo(expectedAttributes);
assertThat(actualAttributesMap).usingRecursiveComparison().isEqualTo(expectedAttributesMap);
}

@Test
Expand Down
Loading

0 comments on commit 67cc37e

Please sign in to comment.