Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tithe queries for getting sum of tithe amounts #73

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
*
*/
package com.tithe.controller.query;

import java.util.List;

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 com.tithe.model.AnnualTitheTotalInterface;
import com.tithe.service.query.TitheQueryService;

/**
* @author Ashish Sam T George
*
*/
@Controller
public class TitheQueries {

@Autowired
private TitheQueryService titheQueryService;

@QueryMapping(name = "getCurrentYearPersonTitheTotal")
public Double getCurrentYearPersonTitheTotal(@Argument(name = "personId") Long personId){
return titheQueryService.getCurrentYearPersonTitheTotal(personId);
}

@QueryMapping(name = "getAnnualPersonTitheTotal")
public List<AnnualTitheTotalInterface> getAnnualPersonTitheTotal(@Argument(name = "personId") Long personId){
return titheQueryService.getAnnualPersonTitheTotal(personId);
}

@QueryMapping(name = "getCurrentYearFamilyTitheTotal")
public Double getCurrentYearFamilyTitheTotal(@Argument(name = "familyId") Long familyId){
return titheQueryService.getCurrentYearFamilyTitheTotal(familyId);
}

@QueryMapping(name = "getAnnualFamilyTitheTotal")
public List<AnnualTitheTotalInterface> getAnnualFamilyTitheTotal(@Argument(name = "familyId") Long familyId){
return titheQueryService.getAnnualFamilyTitheTotal(familyId);
}

@QueryMapping(name = "getCurrentYearKoottaymaTitheTotal")
public Double getCurrentYearKoottaymaTitheTotal(@Argument(name = "koottaymaId") Long koottaymaId){
return titheQueryService.getCurrentYearKoottaymaTitheTotal(koottaymaId);
}

@QueryMapping(name = "getAnnualKoottaymaTitheTotal")
public List<AnnualTitheTotalInterface> getAnnualKoottaymaTitheTotal(@Argument(name = "koottaymaId") Long koottaymaId){
return titheQueryService.getAnnualKoottaymaTitheTotal(koottaymaId);
}

@QueryMapping(name = "getCurrentYearParishTitheTotal")
public Double getCurrentYearParishTitheTotal(@Argument(name = "parishId") Long parishId){
return titheQueryService.getCurrentYearParishTitheTotal(parishId);
}

@QueryMapping(name = "getAnnualParishTitheTotal")
public List<AnnualTitheTotalInterface> getAnnualParishTitheTotal(@Argument(name = "parishId") Long parishId){
return titheQueryService.getAnnualParishTitheTotal(parishId);
}

@QueryMapping(name = "getCurrentYearForaneTitheTotal")
public Double getCurrentYearForaneTitheTotal(@Argument(name = "foraneId") Long foraneId){
return titheQueryService.getCurrentYearForaneTitheTotal(foraneId);
}

@QueryMapping(name = "getAnnualForaneTitheTotal")
public List<AnnualTitheTotalInterface> getAnnualForaneTitheTotal(@Argument(name = "foraneId") Long foraneId){
return titheQueryService.getAnnualForaneTitheTotal(foraneId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
*
*/
package com.tithe.model;

import java.time.LocalDate;

/**
* @author Ashish Sam T George
*
*/
public interface AnnualTitheTotalInterface {

String getYear();
Double getTitheTotal();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
*/
package com.tithe.repository;


import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.tithe.entity.TitheEntity;
import com.tithe.model.AnnualTitheTotalInterface;


/**
* @author Ashish Sam T George
Expand All @@ -15,4 +22,60 @@
@Repository
public interface TitheRepository extends JpaRepository<TitheEntity, Long> {

@Query(value = "SELECT SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.person_id = :personId AND "
+ "EXTRACT(YEAR FROM tt.time_stamp) = EXTRACT(YEAR FROM NOW())", nativeQuery = true)
Double findCurrentYearPersonTitheTotal(@Param("personId") Long personId);

@Query(value = "SELECT EXTRACT(YEAR FROM time_stamp) AS year, "
+ "SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.person_id = :personId GROUP BY EXTRACT(YEAR FROM time_stamp) "
+ "ORDER BY EXTRACT(YEAR FROM time_stamp)", nativeQuery = true)
List<AnnualTitheTotalInterface> findAnnualPersonTitheTotal(@Param("personId") Long personId);

@Query(value = "SELECT SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.family_id = :familyId AND "
+ "EXTRACT(YEAR FROM tt.time_stamp) = EXTRACT(YEAR FROM NOW())", nativeQuery = true)
Double findCurrentYearFamilyTitheTotal(@Param("familyId") Long familyId);

@Query(value = "SELECT EXTRACT(YEAR FROM time_stamp) AS year, "
+ "SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.family_id = :familyId GROUP BY EXTRACT(YEAR FROM time_stamp) "
+ "ORDER BY EXTRACT(YEAR FROM time_stamp)", nativeQuery = true)
List<AnnualTitheTotalInterface> findAnnualFamilyTitheTotal(@Param("familyId") Long familyId);

@Query(value = "SELECT SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.koottayma_id = :koottaymaId AND "
+ "EXTRACT(YEAR FROM tt.time_stamp) = EXTRACT(YEAR FROM NOW())", nativeQuery = true)
Double findCurrentYearKoottaymaTitheTotal(@Param("koottaymaId") Long koottaymaId);

@Query(value = "SELECT EXTRACT(YEAR FROM time_stamp) AS year, "
+ "SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.koottayma_id = :koottaymaId GROUP BY EXTRACT(YEAR FROM time_stamp) "
+ "ORDER BY EXTRACT(YEAR FROM time_stamp)", nativeQuery = true)
List<AnnualTitheTotalInterface> findAnnualKoottaymaTitheTotal(
@Param("koottaymaId") Long koottaymaId);

@Query(value = "SELECT SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.parish_id = :parishId AND "
+ "EXTRACT(YEAR FROM tt.time_stamp) = EXTRACT(YEAR FROM NOW())", nativeQuery = true)
Double findCurrentYearParishTitheTotal(@Param("parishId") Long parishId);

@Query(value = "SELECT EXTRACT(YEAR FROM time_stamp) AS year, "
+ "SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.parish_id = :parishId GROUP BY EXTRACT(YEAR FROM time_stamp) "
+ "ORDER BY EXTRACT(YEAR FROM time_stamp)", nativeQuery = true)
List<AnnualTitheTotalInterface> findAnnualParishTitheTotal(@Param("parishId") Long parishId);

@Query(value = "SELECT SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.forane_id = :foraneId AND "
+ "EXTRACT(YEAR FROM tt.time_stamp) = EXTRACT(YEAR FROM NOW())", nativeQuery = true)
Double findCurrentYearForaneTitheTotal(@Param("foraneId") Long foraneId);

@Query(value = "SELECT EXTRACT(YEAR FROM time_stamp) AS year, "
+ "SUM(tithe_amount) AS titheTotal FROM tithe_table tt "
+ "WHERE tt.forane_id = :foraneId GROUP BY EXTRACT(YEAR FROM time_stamp) "
+ "ORDER BY EXTRACT(YEAR FROM time_stamp)", nativeQuery = true)
List<AnnualTitheTotalInterface> findAnnualForaneTitheTotal(@Param("foraneId") Long foraneId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@
import com.tithe.repository.TitheRepository;
import com.tithe.service.query.PersonQueryService;
import com.tithe.service.query.TitheQueryService;
import com.tithe.utils.ObjectValidation;

import graphql.GraphQLException;

/**
* @author Ashish Sam T George
*
*/
@Service
public class TitheMutationService {

@Autowired
private ObjectValidation objectValidation;

@Autowired
private PersonQueryService personQueryService;
Expand All @@ -34,9 +40,18 @@ public class TitheMutationService {

public List<TitheEntity> createManyTithes(Long personId,
List<TitheMutationInput> titheMutationInputs) {

objectValidation.validateObjects(titheMutationInputs);

// TODO Try adding @NotNull in the method parameter above - No Use
if(personId==null) {
throw new GraphQLException("Person Id is not valid");
}

PersonEntity person = personQueryService.getOnePerson(personId);
if(person==null) {
throw new GraphQLException("Person does not exist");
}

TitheBuilder titheBuilder = new TitheBuilder();
List<TitheEntity> tithes = titheBuilder.buildTithe(person, titheMutationInputs);
return titheRepository.saveAll(tithes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,116 @@
*/
package com.tithe.service.query;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tithe.model.AnnualTitheTotalInterface;
import com.tithe.repository.TitheRepository;

import graphql.GraphQLException;


/**
* @author Ashish Sam T George
*
*/
@Service
public class TitheQueryService {

@Autowired
private TitheRepository titheRepository;

public Double getCurrentYearPersonTitheTotal(Long personId) {
if (personId == null) {
throw new GraphQLException("Person Id should be valid");
}
Double titheTotal = titheRepository.findCurrentYearPersonTitheTotal(personId);
if (titheTotal != null && !titheTotal.isNaN()) {
return titheTotal;
}
return 0.0;
}

public List<AnnualTitheTotalInterface> getAnnualPersonTitheTotal(Long personId) {
if (personId == null) {
throw new GraphQLException("Person Id should be valid");
}
return titheRepository.findAnnualPersonTitheTotal(personId);
}

public Double getCurrentYearFamilyTitheTotal(Long familyId) {
if (familyId == null) {
throw new GraphQLException("Family Id should be valid");
}
Double titheTotal = titheRepository.findCurrentYearFamilyTitheTotal(familyId);
if (titheTotal != null && !titheTotal.isNaN()) {
return titheTotal;
}
return 0.0;
}

public List<AnnualTitheTotalInterface> getAnnualFamilyTitheTotal(Long familyId) {
if (familyId == null) {
throw new GraphQLException("Family Id should be valid");
}
return titheRepository.findAnnualFamilyTitheTotal(familyId);
}

public Double getCurrentYearKoottaymaTitheTotal(Long koottaymaId) {
if (koottaymaId == null) {
throw new GraphQLException("Koottayma Id should be valid");
}
Double titheTotal = titheRepository.findCurrentYearKoottaymaTitheTotal(koottaymaId);
if (titheTotal != null && !titheTotal.isNaN()) {
return titheTotal;
}
return 0.0;
}

public List<AnnualTitheTotalInterface> getAnnualKoottaymaTitheTotal(Long koottaymaId) {
if (koottaymaId == null) {
throw new GraphQLException("Koottayma Id should be valid");
}
return titheRepository.findAnnualKoottaymaTitheTotal(koottaymaId);
}

public Double getCurrentYearParishTitheTotal(Long parishId) {
if (parishId == null) {
throw new GraphQLException("Parish Id should be valid");
}
Double titheTotal = titheRepository.findCurrentYearParishTitheTotal(parishId);
if (titheTotal != null && !titheTotal.isNaN()) {
return titheTotal;
}
return 0.0;
}

public List<AnnualTitheTotalInterface> getAnnualParishTitheTotal(Long parishId) {
if (parishId == null) {
throw new GraphQLException("Parish Id should be valid");
}
return titheRepository.findAnnualParishTitheTotal(parishId);
}

public Double getCurrentYearForaneTitheTotal(Long foraneId) {
if (foraneId == null) {
throw new GraphQLException("Forane Id should be valid");
}
Double titheTotal = titheRepository.findCurrentYearForaneTitheTotal(foraneId);
if (titheTotal != null && !titheTotal.isNaN()) {
return titheTotal;
}
return 0.0;
}

public List<AnnualTitheTotalInterface> getAnnualForaneTitheTotal(Long foraneId) {
if (foraneId == null) {
throw new GraphQLException("Forane Id should be valid");
}
return titheRepository.findAnnualForaneTitheTotal(foraneId);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
extend type Query{
getCurrentYearPersonTitheTotal(personId: ID!): Float!
getAnnualPersonTitheTotal(personId: ID!): [AnnualTitheTotalSchema]

getCurrentYearFamilyTitheTotal(familyId: ID!): Float!
getAnnualFamilyTitheTotal(familyId: ID!): [AnnualTitheTotalSchema]

getCurrentYearKoottaymaTitheTotal(koottaymaId: ID!): Float!
getAnnualKoottaymaTitheTotal(koottaymaId: ID!): [AnnualTitheTotalSchema]

getCurrentYearParishTitheTotal(parishId: ID!): Float!
getAnnualParishTitheTotal(parishId: ID!): [AnnualTitheTotalSchema]

getCurrentYearForaneTitheTotal(foraneId: ID!): Float!
getAnnualForaneTitheTotal(foraneId: ID!): [AnnualTitheTotalSchema]
}
5 changes: 5 additions & 0 deletions Tithe-Spring/src/main/resources/graphql/types/Tithe.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ input TitheMutationInput{
titheAmount: Float!
timeStamp: LocalDate!
}

type AnnualTitheTotalSchema{
year: String!
titheTotal: Float!
}
Loading