Skip to content

Commit

Permalink
Added queries for passes as well as test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
guptakshitij2308 committed Aug 15, 2024
1 parent cc01dd9 commit bef0c52
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 0 deletions.
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;
}
}

}

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);
}
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;
}
}
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);
}
}
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;

}
13 changes: 13 additions & 0 deletions platform/src/main/resources/graphql/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ type Query {
cities: [City!]!
city(cityId: ID!): City
event(uniqueEventId: ID!): EVENT
passes: [Pass!]!
passesForCity(cityId:ID!): [Pass!]
}

type Mutation {
Expand Down Expand Up @@ -72,6 +74,17 @@ input RazorPayInput {
currency: String!
}

type Pass {
passId: String!
cityId: String!
passType: String!
totalGames: Int!
totalDays: Int!
price: Int!
title: String!
status: String!
}

type User {
email: String!
name: String!
Expand Down
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");
}


}

0 comments on commit bef0c52

Please sign in to comment.