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

search by specification #69

Merged
merged 7 commits into from
Sep 13, 2024
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
Expand Up @@ -5,6 +5,7 @@

import com.fjb.sunrise.dtos.base.DataTableInputDTO;
import com.fjb.sunrise.dtos.requests.CreateOrUpdateTransactionRequest;
import com.fjb.sunrise.dtos.responses.StatisticResponse;
import com.fjb.sunrise.dtos.responses.TransactionFullPageResponse;
import com.fjb.sunrise.mappers.TransactionMapper;
import com.fjb.sunrise.models.Transaction;
Expand Down Expand Up @@ -74,6 +75,12 @@ public String postUpdate(@PathVariable Long id,
throws ParseException {
request.setId(id);
transactionService.update(request);
return "redirect:transnsaction/create";
return "redirect:/transaction/create";
}

@GetMapping("/statistic")
@ResponseBody
public StatisticResponse getStatistic() {
return transactionService.statistic();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fjb.sunrise.dtos.base;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategoryAndTotalAmountPerCategory implements Serializable {
private String category;
private Double totalAmountPerCategory;

public CategoryAndTotalAmountPerCategory() {
}

public CategoryAndTotalAmountPerCategory(String category, Double totalAmountPerCategory) {
this.category = category;
this.totalAmountPerCategory = totalAmountPerCategory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.fjb.sunrise.dtos.base;

import java.io.Serializable;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CategoryAndTotalAmountPerCategoryForChart implements Serializable {
private String category;
private String totalAmountPerCategory;

public CategoryAndTotalAmountPerCategoryForChart() {
}

public CategoryAndTotalAmountPerCategoryForChart(String category, String totalAmountPerCategory) {
this.category = category;
this.totalAmountPerCategory = totalAmountPerCategory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.fjb.sunrise.dtos.base;

import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class DayAndTotalAmountPerDay implements Serializable {
private LocalDateTime day;
private Double amountPerDay;

public DayAndTotalAmountPerDay() {
}

public DayAndTotalAmountPerDay(LocalDateTime day, Double amountPerDay) {
this.day = day;
this.amountPerDay = amountPerDay;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fjb.sunrise.dtos.base;

import java.io.Serializable;
import java.time.LocalDateTime;

public class DayAndTotalAmountPerDayForChart implements Serializable {
private String day;
private String amountPerDay;

public DayAndTotalAmountPerDayForChart() {
}

public DayAndTotalAmountPerDayForChart(String day, String amountPerDay) {
this.day = day;
this.amountPerDay = amountPerDay;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.fjb.sunrise.dtos.responses;

import com.fjb.sunrise.dtos.base.DayAndTotalAmountPerDay;
import com.fjb.sunrise.dtos.base.DayAndTotalAmountPerDayForChart;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import lombok.Data;

@Data
public class StatisticResponse implements Serializable {
private List<DayAndTotalAmountPerDayForChart> totalIn3Month;
private String totalThisYear;
private String totalInputThisYear;
private String totalThisMonth;
private List<?> totalThisMonthByCategory;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

public interface CategoryRepository extends JpaRepository<Category, Long> {

public interface CategoryRepository extends JpaRepository<Category, Long>, JpaSpecificationExecutor<Category> {
Page<Category> findAll(Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package com.fjb.sunrise.repositories;

import com.fjb.sunrise.dtos.base.CategoryAndTotalAmountPerCategory;
import com.fjb.sunrise.dtos.base.DayAndTotalAmountPerDay;
import com.fjb.sunrise.enums.ETrans;
import com.fjb.sunrise.models.Transaction;
import java.time.LocalDateTime;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

public interface TransactionRepository extends JpaRepository<Transaction, Long>,
JpaSpecificationExecutor<Transaction> {

public interface TransactionRepository extends JpaRepository<Transaction, Long> {
Page<Transaction> findAll(Pageable pageable);

@Query("select sum(t.amount) from Transaction t "
+ "where t.updatedAt between ?1 and ?2")
Double sumAmountInRange(LocalDateTime start, LocalDateTime end);

@Query("select new com.fjb.sunrise.dtos.base.DayAndTotalAmountPerDay(t.updatedAt,sum(t.amount)) from Transaction t"
+ " where t.updatedAt between ?1 and ?2 group by t.updatedAt")
List<DayAndTotalAmountPerDay> sumAmountPerDayIn3Month(LocalDateTime start, LocalDateTime endOf3Month);

@Query("select sum(t.amount) from Transaction t "
+ "where t.transactionType = ?1 and t.updatedAt between ?2 and ?3 ")
Double sumTransactionTypeINInThisYear(ETrans transactionType, LocalDateTime start, LocalDateTime end);

@Query("select new com.fjb.sunrise.dtos.base.CategoryAndTotalAmountPerCategory(t.category.name,sum(t.amount)) "
+ "from Transaction t "
+ "group by t.category order by sum(t.amount) desc limit 4")
List<CategoryAndTotalAmountPerCategory> sumAmountPerCategory();
}
4 changes: 4 additions & 0 deletions src/main/java/com/fjb/sunrise/services/CategoryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public interface CategoryService {

List<CategoryResponseDto> getAllCategories();


List<Category> findAllByUser();

Page<Category> getCategoryList(DataTableInputDTO payload);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fjb.sunrise.dtos.base.DataTableInputDTO;
import com.fjb.sunrise.dtos.requests.CreateOrUpdateTransactionRequest;
import com.fjb.sunrise.dtos.responses.StatisticResponse;
import com.fjb.sunrise.models.Transaction;
import java.text.ParseException;
import org.springframework.data.domain.Page;
Expand All @@ -16,4 +17,6 @@ public interface TransactionService {
Page<Transaction> getTransactionList(DataTableInputDTO payload);

Transaction update(CreateOrUpdateTransactionRequest request) throws ParseException;

StatisticResponse statistic();
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,17 @@ public List<CategoryResponseDto> getAllCategories() {
.stream().map(categoryMapper::toCategoryResponseDto)
.toList();
}

@Override
public List<Category> findAllByUser() {
// Specification specs = Specification.where(
// ((root, query, builder) -> {
// builder.equal(root.get("createdBy"),
// "ADMIN")
// })
// );
return List.of();
}


}
Loading
Loading