Skip to content

Commit

Permalink
Merge pull request #64 from f-lab-edu/feature/#34
Browse files Browse the repository at this point in the history
다이어리 조회
  • Loading branch information
koo995 authored Oct 3, 2024
2 parents d59d10c + e7ffa1b commit 07571e5
Show file tree
Hide file tree
Showing 22 changed files with 534 additions and 53 deletions.
9 changes: 9 additions & 0 deletions src/main/java/flab/nutridiary/commom/generic/Nutrition.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ private BigDecimal stripIfNecessary(BigDecimal value) {
return strippedValue.scale() <= 0 ? strippedValue.setScale(0) : strippedValue;
}

public static Nutrition empty() {
return Nutrition.builder()
.calories(BigDecimal.ZERO)
.carbohydrate(BigDecimal.ZERO)
.protein(BigDecimal.ZERO)
.fat(BigDecimal.ZERO)
.build();
}

public static Nutrition of(BigDecimal calories, BigDecimal carbohydrate, BigDecimal protein, BigDecimal fat) {
return Nutrition.builder().
calories(calories).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package flab.nutridiary.diary.controller;

import flab.nutridiary.commom.dto.ApiResponse;
import flab.nutridiary.diary.dto.AddDiaryRecordRequest;
import flab.nutridiary.diary.dto.DiaryRegisterRequest;
import flab.nutridiary.diary.dto.DiarySavedResponse;
import flab.nutridiary.diary.dto.request.AddDiaryRecordRequest;
import flab.nutridiary.diary.dto.request.DiaryRegisterRequest;
import flab.nutridiary.diary.dto.response.DiarySavedResponse;
import flab.nutridiary.diary.dto.response.query.DiaryRecordWithProduct;
import flab.nutridiary.diary.service.AddDiaryRecordService;
import flab.nutridiary.diary.service.DiaryRegisterService;
import flab.nutridiary.diary.service.DiarySearchService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@RequiredArgsConstructor
@RestController
public class DiaryController {
private final DiaryRegisterService diaryRegisterService;
private final AddDiaryRecordService addDiaryRecordService;
private final DiarySearchService diarySearchService;

@PostMapping("/diary/new")
public ApiResponse<DiarySavedResponse> createDiary(@Valid @RequestBody DiaryRegisterRequest diaryRegisterRequest) {
Expand All @@ -29,4 +32,10 @@ public ApiResponse<DiarySavedResponse> addDiaryRecord(@Valid @RequestBody AddDia
@PathVariable Long diaryId) {
return ApiResponse.success(addDiaryRecordService.addDiaryRecord(addDiaryRecordRequest, diaryId));
}

@GetMapping("/diary")
public ApiResponse<List<DiaryRecordWithProduct>> getDiary(@RequestParam(name = "diaryDate") LocalDate diaryDate) {
Long memberId = 1L;
return ApiResponse.success(diarySearchService.getDiary(memberId, diaryDate));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flab.nutridiary.diary.dto;
package flab.nutridiary.diary.dto.request;

import flab.nutridiary.commom.validation.EnumValidator;
import flab.nutridiary.diary.domain.MealType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flab.nutridiary.diary.dto;
package flab.nutridiary.diary.dto.request;

import flab.nutridiary.commom.validation.EnumValidator;
import flab.nutridiary.diary.domain.MealType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flab.nutridiary.diary.dto;
package flab.nutridiary.diary.dto.response;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package flab.nutridiary.diary.dto.response.query;

import flab.nutridiary.commom.generic.Nutrition;
import flab.nutridiary.diary.domain.MealType;
import lombok.*;

import java.math.BigDecimal;
import java.time.LocalDate;

@EqualsAndHashCode(exclude = {"diaryId", "diaryRecordId"})
@Builder
@RequiredArgsConstructor
@ToString
@Getter
public class DiaryRecordWithProduct {
private final Long diaryId;
private final Long memberId;
private final LocalDate diaryDate;
private final Long diaryRecordId;
private final MealType mealType;
private final String productName;
private final String productCorp;
private final BigDecimal quantity;
private final String clientChoiceServingUnitDescription;
private final Nutrition calculatedNutrition;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Repository
@RequiredArgsConstructor
public class DiaryRepositoryImpl implements DiaryRepository{

private final DiaryCrudRepository diaryCrudRepository;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package flab.nutridiary.diary.repository;

import flab.nutridiary.diary.dto.response.query.DiaryRecordWithProduct;

import java.time.LocalDate;
import java.util.List;

public interface DiarySearchRepository {
List<DiaryRecordWithProduct> findDiaryWithProductAllByMemberIdAndDiaryDate(Long memberId, LocalDate diaryDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package flab.nutridiary.diary.repository.query;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import flab.nutridiary.commom.exception.SystemException;
import flab.nutridiary.commom.generic.Nutrition;
import flab.nutridiary.diary.domain.MealType;
import flab.nutridiary.diary.dto.response.query.DiaryRecordWithProduct;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import java.sql.ResultSet;
import java.sql.SQLException;

@RequiredArgsConstructor
@Component
public class DiaryRecordWithProductRowMapper implements RowMapper<DiaryRecordWithProduct> {
private final ObjectMapper objectMapper;

@Override
public DiaryRecordWithProduct mapRow(ResultSet rs, int rowNum) throws SQLException {
return DiaryRecordWithProduct.builder()
.diaryId(rs.getLong("diary_id"))
.memberId(rs.getLong("member_id"))
.diaryDate(rs.getDate("diary_date").toLocalDate())
.diaryRecordId(rs.getLong("diary_record_id"))
.mealType(MealType.valueOf(rs.getString("meal_type")))
.productName(rs.getString("product_name"))
.productCorp(rs.getString("product_corp"))
.quantity(rs.getBigDecimal("quantity"))
.clientChoiceServingUnitDescription(rs.getString("client_choice_serving_unit_description"))
.calculatedNutrition(getNutritionFromJsonString(rs.getString("calculated_nutrition")))
.build();
}

private Nutrition getNutritionFromJsonString(String jsonString) {
try {
return objectMapper.readValue(jsonString, Nutrition.class);
} catch (JsonProcessingException e) {
throw new SystemException("Nutrition 객체 매핑 중 오류가 발생했습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package flab.nutridiary.diary.repository.query;

import flab.nutridiary.diary.dto.response.query.DiaryRecordWithProduct;
import flab.nutridiary.diary.repository.DiarySearchRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

@Repository
@RequiredArgsConstructor
public class JdbcTemplateDiarySearchRepository implements DiarySearchRepository {
private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;
private final DiaryRecordWithProductRowMapper diaryRecordWithProductRowMapper;

@Override
public List<DiaryRecordWithProduct> findDiaryWithProductAllByMemberIdAndDiaryDate(Long memberId, LocalDate diaryDate) {
String sql = "SELECT " +
"d.diary_id, d.member_id, d.diary_date, " +
"dr.diary_record_id, dr.meal_type, dr.quantity, dr.client_choice_serving_unit_description, dr.calculated_nutrition, " +
"p.product_id, p.product_name, p.product_corp " +
"FROM diary d " +
"JOIN diary_record dr ON d.diary_id = dr.diary_id " +
"JOIN product p ON dr.product_id = p.product_id " +
"WHERE d.member_id = :memberId AND d.diary_date = :diaryDate";

MapSqlParameterSource parameters = new MapSqlParameterSource()
.addValue("memberId", memberId)
.addValue("diaryDate", diaryDate);

return namedParameterJdbcTemplate.query(sql, parameters, diaryRecordWithProductRowMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import flab.nutridiary.diary.domain.DiaryRecord;
import flab.nutridiary.diary.domain.NutritionCalculator;
import flab.nutridiary.diary.domain.ProductIntakeInfo;
import flab.nutridiary.diary.dto.AddDiaryRecordRequest;
import flab.nutridiary.diary.dto.DiarySavedResponse;
import flab.nutridiary.diary.dto.request.AddDiaryRecordRequest;
import flab.nutridiary.diary.dto.response.DiarySavedResponse;
import flab.nutridiary.diary.repository.DiaryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package flab.nutridiary.diary.service;

import flab.nutridiary.diary.domain.*;
import flab.nutridiary.diary.dto.DiaryRegisterRequest;
import flab.nutridiary.diary.dto.DiarySavedResponse;
import flab.nutridiary.diary.dto.request.DiaryRegisterRequest;
import flab.nutridiary.diary.dto.response.DiarySavedResponse;
import flab.nutridiary.diary.repository.DiaryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package flab.nutridiary.diary.service;

import flab.nutridiary.diary.dto.response.query.DiaryRecordWithProduct;
import flab.nutridiary.diary.repository.DiarySearchRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Transactional
@Service
@RequiredArgsConstructor
public class DiarySearchService {
private final DiarySearchRepository diarySearchRepository;

@Transactional(readOnly = true)
public List<DiaryRecordWithProduct> getDiary(Long memberId, LocalDate diaryDate) {
return diarySearchRepository.findDiaryWithProductAllByMemberIdAndDiaryDate(memberId, diaryDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package flab.nutridiary.diary.service;

import flab.nutridiary.diary.domain.ProductIntakeInfo;
import flab.nutridiary.diary.dto.DiaryRegisterRequest;
import flab.nutridiary.diary.dto.request.DiaryRegisterRequest;
import org.springframework.stereotype.Component;

@Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package flab.nutridiary.product.repository;

import flab.nutridiary.product.domain.Product;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;

public interface ProductCrudRepository extends CrudRepository<Product, Long> {

@Query("select count(*) from product where product_normalized_name = :normalizedName")
Integer isExistDuplicatedProductByNormalizedName(String normalizedName);
int countByProductNormalizedName(String normalizedName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ public Optional<Product> findById(Long id) {

@Override
public Boolean isExistDuplicatedProductByNormalizedName(String normalizedName) {
return productCrudRepository.isExistDuplicatedProductByNormalizedName(normalizedName) > 0;
return productCrudRepository.countByProductNormalizedName(normalizedName) > 0;
}
}
Loading

0 comments on commit 07571e5

Please sign in to comment.