-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added queries for passes as well as test cases.
- Loading branch information
1 parent
cc01dd9
commit bef0c52
Showing
7 changed files
with
380 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
platform/src/main/java/com/flickmatch/platform/dynamodb/model/Pass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package com.flickmatch.platform.dynamodb.model; | ||
|
||
import com.amazonaws.services.dynamodbv2.datamodeling.*; | ||
import com.flickmatch.platform.dynamodb.model.Event.EventId; | ||
|
||
import lombok.*; | ||
import org.springframework.data.annotation.Id; | ||
|
||
|
||
@DynamoDBTable(tableName = "Pass") | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class Pass { | ||
|
||
@DynamoDBHashKey(attributeName = "cityId") | ||
public String getCityId() { | ||
return this.passKey != null ? this.passKey.getCityId() : null; | ||
} | ||
|
||
public void setCityId(String cityId) { | ||
if (this.passKey == null) { | ||
this.passKey = new PassKey(); | ||
} | ||
this.passKey.setCityId(cityId); | ||
} | ||
|
||
@DynamoDBRangeKey(attributeName = "passId") | ||
@DynamoDBAutoGeneratedKey | ||
public String getPassId() { | ||
return this.passKey != null ? this.passKey.getPassId() : null; | ||
} | ||
|
||
public void setPassId(String passId) { | ||
if (this.passKey == null) { | ||
this.passKey = new PassKey(); | ||
} | ||
this.passKey.setPassId(passId); | ||
} | ||
|
||
private String passType; | ||
|
||
private Integer totalGames; | ||
|
||
private Integer totalDays; | ||
|
||
private String title; | ||
|
||
private Double price; | ||
|
||
private String status; | ||
|
||
public String getPassType() { | ||
return passType; | ||
} | ||
|
||
public void setPassType(String passType) { | ||
this.passType = passType; | ||
} | ||
|
||
public Integer getTotalGames() { | ||
return totalGames; | ||
} | ||
|
||
public void setTotalGames(Integer totalGames) { | ||
this.totalGames = totalGames; | ||
} | ||
|
||
public Integer getTotalDays() { | ||
return totalDays; | ||
} | ||
|
||
public void setTotalDays(Integer totalDays) { | ||
this.totalDays = totalDays; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public Double getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(Double price) { | ||
this.price = price; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
// This is a composite key made up of cityId and passId | ||
@Id | ||
private PassKey passKey; | ||
|
||
@Builder | ||
public static class PassKey { | ||
|
||
private String cityId; | ||
private String passId; | ||
|
||
PassKey() { | ||
} | ||
|
||
PassKey(String passId, String cityId) { | ||
this.passId = passId; | ||
this.cityId = cityId; | ||
} | ||
|
||
@DynamoDBHashKey(attributeName = "cityId") | ||
public String getCityId() { | ||
return cityId; | ||
} | ||
|
||
public void setCityId(String cityId) { | ||
this.cityId = cityId; | ||
} | ||
|
||
@DynamoDBRangeKey(attributeName = "passId") | ||
@DynamoDBAutoGeneratedKey | ||
public String getPassId() { | ||
return passId; | ||
} | ||
|
||
public void setPassId(String passId) { | ||
this.passId = passId; | ||
} | ||
} | ||
|
||
} | ||
|
20 changes: 20 additions & 0 deletions
20
platform/src/main/java/com/flickmatch/platform/dynamodb/repository/PassRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.flickmatch.platform.dynamodb.repository; | ||
|
||
import com.flickmatch.platform.dynamodb.model.Pass; | ||
import org.socialsignin.spring.data.dynamodb.repository.EnableScan; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@EnableScan | ||
public interface PassRepository extends CrudRepository<Pass,Pass.PassKey> { | ||
List<Pass> findByStatus(String status); | ||
|
||
// Find all active passes for a given cityId | ||
List<Pass> findByCityIdAndStatus(String cityId, String status); | ||
|
||
List<Pass> findAll(); | ||
|
||
Optional<Pass> findByPassId(String passId); | ||
} |
62 changes: 62 additions & 0 deletions
62
platform/src/main/java/com/flickmatch/platform/graphql/builder/PassBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.flickmatch.platform.graphql.builder; | ||
|
||
import com.flickmatch.platform.graphql.type.Pass; | ||
import com.flickmatch.platform.dynamodb.repository.PassRepository; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@Log4j2 | ||
public class PassBuilder { | ||
private PassRepository passRepository; | ||
|
||
public PassBuilder(PassRepository passRepository) { | ||
this.passRepository = passRepository; | ||
} | ||
|
||
public List<Pass> getAllActivePasses() { | ||
try { | ||
return passRepository.findByStatus("Active") | ||
.stream() | ||
.map(this::mapEventToGQLType) | ||
.collect(Collectors.toList()); | ||
} catch (Exception e) { | ||
log.error("Error while fetching passes", e); | ||
return List.of(); | ||
} | ||
} | ||
|
||
// to be used later with GQL query | ||
public List<Pass> getAllActivePassesForCity(String cityId) { | ||
return | ||
passRepository.findByCityIdAndStatus(cityId, "Active") | ||
.stream() | ||
.map(this::mapEventToGQLType) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
Pass mapEventToGQLType(com.flickmatch.platform.dynamodb.model.Pass ddbPass) { | ||
System.out.println(ddbPass.toString()); | ||
|
||
return Pass.builder() | ||
.passId(ddbPass.getPassId()) | ||
.status(ddbPass.getStatus()) | ||
.cityId(ddbPass.getCityId()) | ||
.passType(ddbPass.getPassType()) | ||
.totalGames(ddbPass.getTotalGames()) | ||
.totalDays(ddbPass.getTotalDays()) | ||
.title(ddbPass.getTitle()) | ||
.price(ddbPass.getPrice()) | ||
.build(); | ||
} | ||
|
||
public double getAmountForPass(final String passId) { | ||
Optional<com.flickmatch.platform.dynamodb.model.Pass> pass = passRepository.findByPassId(passId); | ||
Double amount = pass.get().getPrice(); | ||
return amount; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
platform/src/main/java/com/flickmatch/platform/graphql/controller/PassController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.flickmatch.platform.graphql.controller; | ||
|
||
import com.flickmatch.platform.graphql.type.Pass; | ||
import com.flickmatch.platform.graphql.builder.PassBuilder; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.QueryMapping; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import java.util.List; | ||
|
||
@Controller | ||
@Log4j2 | ||
public class PassController { | ||
@Autowired | ||
PassBuilder passBuilder; | ||
|
||
@QueryMapping(name = "passes") | ||
public List<Pass> getAllActivePasses() { | ||
return passBuilder.getAllActivePasses(); | ||
} | ||
|
||
@QueryMapping(name = "passesForCity") | ||
public List<Pass> getAllActivePassesForCity(@Argument String cityId) { | ||
return passBuilder.getAllActivePassesForCity(cityId); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
platform/src/main/java/com/flickmatch/platform/graphql/type/Pass.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.flickmatch.platform.graphql.type; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Builder | ||
@Setter | ||
@Getter | ||
public class Pass { | ||
private String passId; | ||
private String cityId; | ||
private String passType; | ||
private Integer totalGames; | ||
private Integer totalDays; | ||
private String title; | ||
private Double price; | ||
private String status; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
platform/src/test/java/com/flickmatch/platform/graphql/builder/PassBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.flickmatch.platform.graphql.builder; | ||
|
||
import com.flickmatch.platform.dynamodb.model.Pass; | ||
import com.flickmatch.platform.dynamodb.repository.PassRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class PassBuilderTest { | ||
@Mock | ||
private PassRepository passRepository; | ||
|
||
@InjectMocks | ||
private PassBuilder passBuilder; | ||
|
||
private Pass ddbPass1; | ||
private Pass ddbPass2; | ||
private Pass ddbPass3; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
MockitoAnnotations.initMocks(this); | ||
|
||
// Sample DynamoDB Pass objects | ||
ddbPass1 = new Pass(); | ||
ddbPass1.setPassId("1"); | ||
ddbPass1.setCityId("1"); | ||
ddbPass1.setPassType("LimitedGames"); | ||
ddbPass1.setTotalGames(10); | ||
ddbPass1.setTotalDays(3000); | ||
ddbPass1.setPrice(1000.0); | ||
ddbPass1.setTitle("10 games pass"); | ||
ddbPass1.setStatus("Active"); | ||
|
||
ddbPass2 = new Pass(); | ||
ddbPass2.setPassId("2"); | ||
ddbPass2.setCityId("2"); | ||
ddbPass2.setPassType("LimitedDays"); | ||
ddbPass2.setTotalGames(1000); | ||
ddbPass2.setTotalDays(30); | ||
ddbPass2.setPrice(1000.0); | ||
ddbPass2.setTitle("30 days pass"); | ||
ddbPass2.setStatus("Active"); | ||
|
||
ddbPass2 = new Pass(); | ||
ddbPass2.setPassId("2"); | ||
ddbPass2.setCityId("2"); | ||
ddbPass2.setPassType("LimitedDays"); | ||
ddbPass2.setTotalGames(10000); | ||
ddbPass2.setTotalDays(30); | ||
ddbPass2.setPrice(100.0); | ||
ddbPass2.setTitle("30 days pass"); | ||
ddbPass2.setStatus("Inactive"); | ||
} | ||
|
||
@Test | ||
public void testGetAllActivePasses() { | ||
// Mock data | ||
when(passRepository.findByStatus("Active")).thenReturn(List.of(ddbPass1, ddbPass2)); | ||
|
||
// Call the method under test | ||
List<com.flickmatch.platform.graphql.type.Pass> result = passBuilder.getAllActivePasses(); | ||
|
||
// Verify results | ||
assertThat(result, notNullValue()); | ||
assertThat(result, hasSize(2)); | ||
verify(passRepository, times(1)).findByStatus("Active"); | ||
} | ||
|
||
@Test | ||
public void testGetAllActivePassesForCity() { | ||
// Mock data | ||
String cityId = "1"; | ||
when(passRepository.findByCityIdAndStatus(cityId, "Active")).thenReturn(List.of(ddbPass1)); | ||
|
||
// Call the method under test | ||
List<com.flickmatch.platform.graphql.type.Pass> result = passBuilder.getAllActivePassesForCity(cityId); | ||
|
||
// Verify results | ||
assertThat(result, notNullValue()); | ||
assertThat(result, hasSize(1)); | ||
verify(passRepository, times(1)).findByCityIdAndStatus(cityId, "Active"); | ||
} | ||
|
||
|
||
} |